8 |
|
# Interface |
9 |
|
# --------- |
10 |
|
# new() : A new ActiveDoc object |
11 |
+ |
# config([ActiveConfig]) : Set up/return Configuration for the document |
12 |
|
# url() : Return/set the docs url - essential |
13 |
|
# file() : Return the local filename of document |
14 |
+ |
# ProcessFile() : Return the filename of PreProcessed document |
15 |
|
# |
16 |
< |
# parse(parselabel): Parse the document file for the given parse level |
15 |
< |
# newparse(parselabel) : Create a new parse type |
16 |
< |
# addtag(parselabel,tagname,start,obj,text,obj,end,obj) |
17 |
< |
# : Add tags to the parse given by label |
18 |
< |
# checktag(tagname, hashref, param) : check for existence of param in |
19 |
< |
# hashref from a tag call |
20 |
< |
# newdoc(file) : Return an new object of the appropriate type |
16 |
> |
# parent() : return the object ref of the calling parent |
17 |
|
# getfile(url) : get a processedfile object given a url |
18 |
|
# activatedoc(url) : Return the object ref for a doc described by the given url |
19 |
< |
# config([ActiveConfig]) : Set up/return Configuration for the document |
20 |
< |
# basequery([ActiveConfig]) : Set up/return UserQuery for the doc |
21 |
< |
# copydocconfig(ActiveDoc) : Copy the basic configuration from the ActiveDoc |
26 |
< |
# copydocquery(ActiveDoc) : Copy the basicquery from the ActiveDoc |
19 |
> |
# -- any parse called "init" will also be run |
20 |
> |
# userinterface() : Return the defaullt userinterface |
21 |
> |
# option(var) : return the value of the option var ( or undef ) |
22 |
|
# |
23 |
+ |
# addurltags(parse) : add the <base> tags to manage urls to parse |
24 |
|
# -- error methods -- |
25 |
|
# error(string) : Report an general error to the user |
26 |
|
# parseerror(string) : Report an error during parsing a file |
27 |
|
# line() : Return the current line number of the document |
28 |
|
# and the ProcessedFileObj it is in |
29 |
+ |
# |
30 |
|
|
31 |
|
package ActiveDoc::ActiveDoc; |
32 |
|
require 5.004; |
33 |
< |
use ActiveDoc::Parse; |
37 |
< |
use ActiveDoc::ActiveConfig; |
38 |
< |
use ActiveDoc::PreProcessedFile; |
39 |
< |
use ObjectUtilities::ObjectBase; |
33 |
> |
use ActiveDoc::SimpleDoc; |
34 |
|
use URL::URLhandler; |
35 |
|
|
36 |
< |
@ISA = qw(ObjectUtilities::ObjectBase); |
43 |
< |
|
44 |
< |
sub new { |
45 |
< |
my $class=shift; |
46 |
< |
$self={}; |
47 |
< |
bless $self, $class; |
48 |
< |
$self->config(shift); |
49 |
< |
|
50 |
< |
# A URL handler per document |
51 |
< |
$self->{urlhandler}=URL::URLhandler->new($self->config()->cache()); |
52 |
< |
|
53 |
< |
$self->init(@_); |
54 |
< |
return $self; |
55 |
< |
} |
56 |
< |
|
57 |
< |
# ----- parse related routines -------------- |
58 |
< |
sub parse { |
59 |
< |
my $self=shift; |
60 |
< |
$parselabel=shift; |
61 |
< |
|
62 |
< |
my $file=$self->file(); |
63 |
< |
if ( $file ) { |
64 |
< |
$self->{currentparser}=$self->{parsers}{$parselabel}; |
65 |
< |
$self->{parsers}{$parselabel}->parse($file,@_); |
66 |
< |
delete $self->{currentparser}; |
67 |
< |
} |
68 |
< |
else { |
69 |
< |
print "Cannot parse - file not known\n"; |
70 |
< |
} |
71 |
< |
} |
72 |
< |
|
73 |
< |
sub newparse { |
74 |
< |
my $self=shift; |
75 |
< |
my $parselabel=shift; |
76 |
< |
|
77 |
< |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new(); |
78 |
< |
$self->{parsers}{$parselabel}->addignoretags(); |
79 |
< |
$self->{parsers}{$parselabel}->addgrouptags(); |
80 |
< |
} |
81 |
< |
|
82 |
< |
sub addtag { |
83 |
< |
my $self=shift; |
84 |
< |
my $parselabel=shift; |
85 |
< |
if ( $#_ != 6 ) { |
86 |
< |
$self->error("Incorrect addtags specification\n". |
87 |
< |
"called with :\n@_ \n"); |
88 |
< |
} |
89 |
< |
$self->{parsers}{$parselabel}->addtag(@_); |
90 |
< |
} |
36 |
> |
@ISA = qw(ActiveDoc::SimpleDoc); |
37 |
|
|
38 |
|
sub addurltags { |
39 |
|
my $self=shift; |
47 |
|
sub url { |
48 |
|
my $self=shift; |
49 |
|
# get file & preprocess |
50 |
< |
if ( @_ ) {$self->{File}=$self->getfile(shift)} |
51 |
< |
$self->{File}->url(); |
52 |
< |
} |
53 |
< |
|
54 |
< |
sub copydocconfig { |
55 |
< |
my $self=shift; |
56 |
< |
my $ActiveDoc=shift; |
57 |
< |
|
112 |
< |
$self->config($ActiveDoc->config()); |
113 |
< |
|
114 |
< |
} |
115 |
< |
|
116 |
< |
sub copydocquery { |
117 |
< |
my $self=shift; |
118 |
< |
my $ActiveDoc=shift; |
119 |
< |
|
120 |
< |
$self->basequery($ActiveDoc->basequery()); |
50 |
> |
if ( @_ ) { |
51 |
> |
$self->{File}=$self->getfile(shift); |
52 |
> |
$self->verbose("url downloaded to $self->{File}"); |
53 |
> |
} |
54 |
> |
if ( defined $self->{File} ) { |
55 |
> |
return $self->{File}->url(); |
56 |
> |
} |
57 |
> |
else { return "undefined"; } |
58 |
|
} |
59 |
|
|
60 |
|
sub config { |
63 |
|
: $self->{ActiveConfig}; |
64 |
|
} |
65 |
|
|
66 |
< |
sub basequery { |
130 |
< |
my $self=shift; |
131 |
< |
@_ ? $self->{UserQuery}=shift |
132 |
< |
: $self->{UserQuery}; |
133 |
< |
} |
134 |
< |
|
135 |
< |
sub getfile() { |
66 |
> |
sub getfile { |
67 |
|
my $self=shift; |
68 |
|
my $origurl=shift; |
69 |
|
|
70 |
|
my $fileref; |
71 |
< |
my ($url, $file)=$self->{urlhandler}->get($origurl); |
71 |
> |
my ($url, $file); |
72 |
> |
if ( (defined ($it=$self->option('url_update'))) && |
73 |
> |
( $it eq "1" || $origurl=~/^$it/ )) { |
74 |
> |
$self->verbose("Forced download of $origurl"); |
75 |
> |
($url, $file)=$self->{urlhandler}->download($origurl); |
76 |
> |
} |
77 |
> |
else { |
78 |
> |
$self->verbose("Attempting to get $origurl"); |
79 |
> |
($url, $file)=$self->{urlhandler}->get($origurl); |
80 |
> |
} |
81 |
|
# do we already have an appropriate object? |
82 |
|
($fileref)=$self->config()->find($url); |
83 |
|
#undef $fileref; |
84 |
|
if ( defined $fileref ) { |
85 |
< |
print "found $url in database ----\n"; |
85 |
> |
$self->verbose("Found $url in database"); |
86 |
|
$fileref->update(); |
87 |
|
} |
88 |
|
else { |
90 |
|
$self->parseerror("Unable to get $origurl"); |
91 |
|
} |
92 |
|
#-- set up a new preprocess file |
93 |
< |
print "Making a new file $url----\n"; |
93 |
> |
$self->verbose("Making a new preprocessed file $url"); |
94 |
|
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
95 |
|
$fileref->url($url); |
96 |
|
$fileref->update(); |
103 |
|
my $url=shift; |
104 |
|
|
105 |
|
# first get a preprocessed copy of the file |
106 |
< |
my $fileob=$self->getfile($url); |
106 |
> |
# my $fileob=$self->getfile($url); |
107 |
|
|
108 |
|
# now parse it for the <DocType> tag |
109 |
< |
$self->newparse("doctype"); |
110 |
< |
$self->addtag("doctype","Doc", \&Doc_Start, $self, |
111 |
< |
"", $self, "", $self); |
112 |
< |
$self->parse("doctype"); |
109 |
> |
my $tempdoc=ActiveDoc::ActiveDoc->new($self->config()); |
110 |
> |
$tempdoc->{urlhandler}=$self->{urlhandler}; |
111 |
> |
my $fullurl=$tempdoc->url($url); |
112 |
> |
$url=$fullurl; |
113 |
> |
$tempdoc->{doctypefound}=0; |
114 |
> |
$tempdoc->newparse("doctype"); |
115 |
> |
$tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc, |
116 |
> |
"", $tempdoc, "", $tempdoc); |
117 |
> |
$tempdoc->parse("doctype"); |
118 |
|
|
119 |
< |
if ( ! defined $self->{docobject} ) { |
120 |
< |
print "No <Doc type=> Specified in ".$fileob->url()."\n"; |
119 |
> |
if ( ! defined $tempdoc->{docobject} ) { |
120 |
> |
print "No <Doc type=> Specified in ".$url."\n"; |
121 |
|
exit 1; |
122 |
|
} |
123 |
|
# Set up a new object of the specified type |
124 |
< |
my $newobj=$self->{docobject}->new($self->config()); |
124 |
> |
eval "require $tempdoc->{docobject}"; |
125 |
> |
die $@ if $@; |
126 |
> |
my $newobj=$tempdoc->{docobject}->new($self->config()); |
127 |
> |
undef $tempdoc; |
128 |
> |
$newobj->url($url); |
129 |
> |
$newobj->parent($self); |
130 |
|
return $newobj; |
131 |
|
} |
132 |
|
|
133 |
+ |
sub parent { |
134 |
+ |
my $self=shift; |
135 |
+ |
|
136 |
+ |
@_?$self->{parent}=shift |
137 |
+ |
:$self->{parent}; |
138 |
+ |
} |
139 |
+ |
|
140 |
|
# -------- Error Handling and Error services -------------- |
141 |
|
|
142 |
|
sub error { |
150 |
|
my $self=shift; |
151 |
|
my $string=shift; |
152 |
|
|
153 |
< |
($line, $file)=$self->line(); |
154 |
< |
print "Parse Error in ".$file->url().", line ". |
153 |
> |
if ( $self->currentparsename() eq "" ) { |
154 |
> |
$self->error($string); |
155 |
> |
} |
156 |
> |
else { |
157 |
> |
($line, $file)=$self->line(); |
158 |
> |
print "Parse Error in ".$file->url().", line ". |
159 |
|
$line."\n"; |
160 |
< |
print $string."\n"; |
161 |
< |
die; |
162 |
< |
} |
202 |
< |
|
203 |
< |
sub checktag { |
204 |
< |
my $self=shift; |
205 |
< |
my $tagname=shift; |
206 |
< |
my $hashref=shift; |
207 |
< |
my $param=shift; |
208 |
< |
|
209 |
< |
print keys %$hashref; |
210 |
< |
print "-----------------------------------\n"; |
211 |
< |
if ( ! exists $$hashref{$param} ) { |
212 |
< |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
213 |
< |
} |
160 |
> |
print $string."\n"; |
161 |
> |
exit; |
162 |
> |
} |
163 |
|
} |
164 |
|
|
165 |
|
sub line { |
183 |
|
$self->{File}->file(); |
184 |
|
} |
185 |
|
|
186 |
+ |
sub ProcessFile { |
187 |
+ |
my $self=shift; |
188 |
+ |
|
189 |
+ |
return $self->{File}->ProcessedFile(); |
190 |
+ |
} |
191 |
+ |
|
192 |
|
# --------------- Initialisation Methods --------------------------- |
193 |
|
|
194 |
|
sub init { |
211 |
|
push @{$self->{basestack}}, $$hashref{"type"}; |
212 |
|
# Set the base |
213 |
|
$self->{urlhandler}->setbase($$hashref{"type"},$hashref); |
259 |
– |
|
214 |
|
} |
215 |
|
|
216 |
|
sub Base_end { |
219 |
|
my $type; |
220 |
|
|
221 |
|
if ( $#{$self->{basestack}} == -1 ) { |
222 |
< |
print "Parse Error : unmatched </".$name."> on line ". |
269 |
< |
$self->line()."\n"; |
270 |
< |
die; |
222 |
> |
$self->parseerror("Parse Error : unmatched </$name>"); |
223 |
|
} |
224 |
|
else { |
225 |
|
$type = pop @{$self->{basestack}}; |
233 |
|
my $hashref=shift; |
234 |
|
|
235 |
|
$self->checktag($name, $hashref, "type"); |
236 |
< |
$self->{docobject}=$$hashref{'type'}; |
236 |
> |
$self->{doctypefound}++; |
237 |
> |
if ( $self->{doctypefound} == 1 ) { # only take first doctype |
238 |
> |
$self->{docobject}=$$hashref{'type'}; |
239 |
> |
} |
240 |
|
} |