10 |
|
# new(ActiveConfig[,options]) : A new ActiveDoc object |
11 |
|
# url() : Return/set the docs url - essential |
12 |
|
# file() : Return the local filename of document |
13 |
+ |
# ProcessFile() : Return the filename of PreProcessed document |
14 |
|
# |
15 |
|
# parse(parselabel): Parse the document file for the given parse level |
16 |
+ |
# parent() : return the object ref of the calling parent |
17 |
|
# newparse(parselabel) : Create a new parse type |
18 |
|
# addtag(parselabel,tagname,start,obj,text,obj,end,obj) |
19 |
|
# : Add tags to the parse given by label |
21 |
|
# hashref from a tag call |
22 |
|
# includeparse(local_parsename, objparsename, activedoc) : copy the parse from |
23 |
|
# one object to another |
24 |
+ |
# currentparser() : return the current parser object |
25 |
|
# currentparsename([name]) : get/set current parse name |
23 |
– |
# newdoc(file) : Return an new object of the appropriate type |
26 |
|
# getfile(url) : get a processedfile object given a url |
27 |
|
# activatedoc(url) : Return the object ref for a doc described by the given url |
28 |
|
# -- any parse called "init" will also be run |
31 |
|
# copydocconfig(ActiveDoc) : Copy the basic configuration from the ActiveDoc |
32 |
|
# copydocquery(ActiveDoc) : Copy the basicquery from the ActiveDoc |
33 |
|
# userinterface() : Return the defaullt userinterface |
34 |
< |
# option(var) : return the value of the option var |
34 |
> |
# option(var) : return the value of the option var ( or undef ) |
35 |
|
# requestoption("message") : Ask the user to supply a value for an option |
36 |
|
# if it dosnt already exist |
37 |
+ |
# askuser(Query) : send a query object to the userinterface |
38 |
+ |
# verbose(string) : Print string in verbosity mode |
39 |
|
# |
40 |
|
# -- error methods -- |
41 |
|
# error(string) : Report an general error to the user |
42 |
|
# parseerror(string) : Report an error during parsing a file |
43 |
|
# line() : Return the current line number of the document |
44 |
|
# and the ProcessedFileObj it is in |
45 |
+ |
# |
46 |
+ |
# -- support for inheriting classes |
47 |
+ |
# _saveactivedoc(filehandle) |
48 |
+ |
# _restoreactivedoc(filehandle) |
49 |
|
|
50 |
|
package ActiveDoc::ActiveDoc; |
51 |
|
require 5.004; |
72 |
|
my $basedoc=$self->config()->basedoc(); |
73 |
|
if ( defined $basedoc ) { |
74 |
|
$self->copydocquery($basedoc); |
75 |
+ |
$self->verbose("Initialising from $basedoc"); |
76 |
|
} |
77 |
|
else { |
78 |
< |
$self->error("Error : No base doc found"); |
78 |
> |
$self->error("ActiveDoc Error : No base doc found"); |
79 |
|
} |
80 |
|
} |
81 |
+ |
$self->verbose("New ActiveDoc (".ref($self).") Created"); |
82 |
|
$self->_init2(); |
83 |
|
} |
84 |
|
|
95 |
|
|
96 |
|
} |
97 |
|
|
98 |
+ |
sub verbose { |
99 |
+ |
my $self=shift; |
100 |
+ |
my $string=shift; |
101 |
+ |
|
102 |
+ |
if ( $self->option('verbose_all') || |
103 |
+ |
$self->option('verbose_'.ref($self)) ) { |
104 |
+ |
print ">".ref($self)."($self) : \n->".$string."\n"; |
105 |
+ |
} |
106 |
+ |
} |
107 |
+ |
|
108 |
|
# ----- parse related routines -------------- |
109 |
|
sub parse { |
110 |
|
my $self=shift; |
111 |
|
$parselabel=shift; |
112 |
|
|
113 |
< |
my $file=$self->file(); |
113 |
> |
my $file=$self->ProcessFile(); |
114 |
|
if ( $file ) { |
115 |
|
if ( exists $self->{parsers}{$parselabel} ) { |
116 |
+ |
$self->verbose("Parsing $parselabel in file $file"); |
117 |
|
$self->{currentparsename}=$parselabel; |
118 |
|
$self->{currentparser}=$self->{parsers}{$parselabel}; |
119 |
|
$self->{parsers}{$parselabel}->parse($file,@_); |
120 |
|
delete $self->{currentparser}; |
121 |
|
$self->{currentparsename}=""; |
122 |
+ |
$self->verbose("Parse $parselabel Complete"); |
123 |
|
} |
124 |
|
} |
125 |
|
else { |
133 |
|
:$self->{currentparsename}; |
134 |
|
} |
135 |
|
|
136 |
+ |
sub currentparser { |
137 |
+ |
my $self=shift; |
138 |
+ |
return $self->{currentparser}; |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
|
142 |
|
sub newparse { |
143 |
|
my $self=shift; |
144 |
|
my $parselabel=shift; |
197 |
|
sub url { |
198 |
|
my $self=shift; |
199 |
|
# get file & preprocess |
200 |
< |
if ( @_ ) {$self->{File}=$self->getfile(shift)} |
200 |
> |
if ( @_ ) { |
201 |
> |
$self->{File}=$self->getfile(shift); |
202 |
> |
$self->verbose("url downloaded to $self->{File}"); |
203 |
> |
} |
204 |
|
$self->{File}->url(); |
205 |
|
} |
206 |
|
|
216 |
|
my $self=shift; |
217 |
|
my $ActiveDoc=shift; |
218 |
|
|
219 |
< |
$self->basequery($ActiveDoc->basequery()); |
219 |
> |
if ( defined $ActiveDoc->basequery() ) { |
220 |
> |
$self->basequery($ActiveDoc->basequery()); |
221 |
> |
} |
222 |
> |
else { |
223 |
> |
$self->error("Cannot copy basequery - undefined"); |
224 |
> |
} |
225 |
|
} |
226 |
|
|
227 |
|
sub config { |
232 |
|
|
233 |
|
sub basequery { |
234 |
|
my $self=shift; |
235 |
< |
@_ ? $self->{Query}=shift |
236 |
< |
: $self->{Query}; |
235 |
> |
@_?$self->{Query}=shift |
236 |
> |
:$self->{Query}; |
237 |
|
} |
238 |
|
|
239 |
|
sub option { |
265 |
|
return $par; |
266 |
|
} |
267 |
|
|
268 |
< |
sub getfile() { |
268 |
> |
sub askuser { |
269 |
> |
my $self=shift; |
270 |
> |
return $self->userinterface()->askuser(@_); |
271 |
> |
} |
272 |
> |
|
273 |
> |
sub getfile { |
274 |
|
my $self=shift; |
275 |
|
my $origurl=shift; |
276 |
|
|
277 |
|
my $fileref; |
278 |
< |
my ($url, $file)=$self->{urlhandler}->get($origurl); |
278 |
> |
my ($url, $file); |
279 |
> |
if ( (defined ($it=$self->option('url_update'))) && |
280 |
> |
( $it eq "1" || $origurl=~/^$it/ )) { |
281 |
> |
$self->verbose("Forced download of $origurl"); |
282 |
> |
($url, $file)=$self->{urlhandler}->download($origurl); |
283 |
> |
} |
284 |
> |
else { |
285 |
> |
$self->verbose("Attempting to get $origurl"); |
286 |
> |
($url, $file)=$self->{urlhandler}->get($origurl); |
287 |
> |
} |
288 |
|
# do we already have an appropriate object? |
289 |
|
($fileref)=$self->config()->find($url); |
290 |
|
#undef $fileref; |
291 |
|
if ( defined $fileref ) { |
292 |
< |
print "found $url in database ----\n"; |
292 |
> |
$self->verbose("Found $url in database"); |
293 |
|
$fileref->update(); |
294 |
|
} |
295 |
|
else { |
297 |
|
$self->parseerror("Unable to get $origurl"); |
298 |
|
} |
299 |
|
#-- set up a new preprocess file |
300 |
< |
print "Making a new file $url----\n"; |
300 |
> |
$self->verbose("Making a new preprocessed file $url"); |
301 |
|
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
302 |
|
$fileref->url($url); |
303 |
|
$fileref->update(); |
315 |
|
# now parse it for the <DocType> tag |
316 |
|
my $tempdoc=ActiveDoc::ActiveDoc->new($self->config()); |
317 |
|
$tempdoc->{urlhandler}=$self->{urlhandler}; |
318 |
< |
$tempdoc->url($url); |
318 |
> |
my $fullurl=$tempdoc->url($url); |
319 |
> |
$url=$fullurl; |
320 |
|
$tempdoc->{doctypefound}=0; |
321 |
|
$tempdoc->newparse("doctype"); |
322 |
|
$tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc, |
333 |
|
my $newobj=$tempdoc->{docobject}->new($self->config()); |
334 |
|
undef $tempdoc; |
335 |
|
$newobj->url($url); |
336 |
+ |
$newobj->parent($self); |
337 |
|
$newobj->_initparse(); |
338 |
|
return $newobj; |
339 |
|
} |
340 |
|
|
341 |
+ |
sub parent { |
342 |
+ |
my $self=shift; |
343 |
+ |
|
344 |
+ |
@_?$self->{parent}=shift |
345 |
+ |
:$self->{parent}; |
346 |
+ |
} |
347 |
+ |
|
348 |
|
sub _initparse { |
349 |
|
my $self=shift; |
350 |
|
|
363 |
|
my $self=shift; |
364 |
|
my $string=shift; |
365 |
|
|
366 |
< |
($line, $file)=$self->line(); |
367 |
< |
print "Parse Error in ".$file->url().", line ". |
366 |
> |
if ( $self->currentparsename() eq "" ) { |
367 |
> |
$self->error($string); |
368 |
> |
} |
369 |
> |
else { |
370 |
> |
($line, $file)=$self->line(); |
371 |
> |
print "Parse Error in ".$file->url().", line ". |
372 |
|
$line."\n"; |
373 |
< |
print $string."\n"; |
374 |
< |
die; |
373 |
> |
print $string."\n"; |
374 |
> |
exit; |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
sub checktag { |
407 |
|
$self->{File}->file(); |
408 |
|
} |
409 |
|
|
410 |
+ |
sub ProcessFile { |
411 |
+ |
my $self=shift; |
412 |
+ |
|
413 |
+ |
return $self->{File}->ProcessedFile(); |
414 |
+ |
} |
415 |
+ |
|
416 |
|
# --------------- Initialisation Methods --------------------------- |
417 |
|
|
418 |
|
sub init { |
443 |
|
my $type; |
444 |
|
|
445 |
|
if ( $#{$self->{basestack}} == -1 ) { |
446 |
< |
print "Parse Error : unmatched </".$name."> on line ". |
377 |
< |
$self->line()."\n"; |
378 |
< |
die; |
446 |
> |
$self->parseerror("Parse Error : unmatched </$name>"); |
447 |
|
} |
448 |
|
else { |
449 |
|
$type = pop @{$self->{basestack}}; |
468 |
|
@_?$self->{userinterface}=shift |
469 |
|
:$self->{userinterface} |
470 |
|
} |
471 |
+ |
|
472 |
+ |
sub _saveactivedoc { |
473 |
+ |
my $self=shift; |
474 |
+ |
my $fh=shift; |
475 |
+ |
print "Storing $self\n"; |
476 |
+ |
print $fh $self->url()."\n"; |
477 |
+ |
} |
478 |
+ |
|
479 |
+ |
sub _restoreactivedoc { |
480 |
+ |
my $self=shift; |
481 |
+ |
my $fh=shift; |
482 |
+ |
|
483 |
+ |
my $url=<$fh>; |
484 |
+ |
chomp $url; |
485 |
+ |
$self->url($url); |
486 |
+ |
} |