7 |
|
# |
8 |
|
# Interface |
9 |
|
# --------- |
10 |
< |
# new(ActiveConfig[,options]) : A new ActiveDoc object |
10 |
> |
# new(cache,dbstore) : 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 |
15 |
|
# 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 |
20 |
– |
# checktag(tagname, hashref, param) : check for existence of param in |
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 |
16 |
|
# getfile(url) : get a processedfile object given a url |
17 |
|
# activatedoc(url) : Return the object ref for a doc described by the given url |
18 |
|
# -- any parse called "init" will also be run |
29 |
– |
# config([ActiveConfig]) : Set up/return Configuration for the document |
30 |
– |
# basequery([ActiveConfig]) : Set up/return UserQuery for the doc |
31 |
– |
# copydocconfig(ActiveDoc) : Copy the basic configuration from the ActiveDoc |
32 |
– |
# copydocquery(ActiveDoc) : Copy the basicquery from the ActiveDoc |
19 |
|
# userinterface() : Return the defaullt userinterface |
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 |
20 |
|
# |
21 |
|
# -- error methods -- |
22 |
|
# error(string) : Report an general error to the user |
24 |
|
# line() : Return the current line number of the document |
25 |
|
# and the ProcessedFileObj it is in |
26 |
|
# |
46 |
– |
# -- support for inheriting classes |
47 |
– |
# _saveactivedoc(filehandle) |
48 |
– |
# _restoreactivedoc(filehandle) |
27 |
|
|
28 |
|
package ActiveDoc::ActiveDoc; |
29 |
|
require 5.004; |
30 |
< |
use ActiveDoc::Parse; |
31 |
< |
use ActiveDoc::ActiveConfig; |
54 |
< |
use ActiveDoc::PreProcessedFile; |
55 |
< |
use ObjectUtilities::StorableObject; |
56 |
< |
use URL::URLhandler; |
30 |
> |
use ActiveDoc::SimpleURLDoc; |
31 |
> |
use Utilities::Verbose; |
32 |
|
|
33 |
< |
@ISA = qw(ObjectUtilities::StorableObject); |
33 |
> |
@ISA = qw(Utilities::Verbose); |
34 |
|
|
35 |
|
sub new { |
36 |
|
my $class=shift; |
37 |
< |
$self={}; |
37 |
> |
my $self={}; |
38 |
|
bless $self, $class; |
39 |
< |
$self->config(shift); |
40 |
< |
|
41 |
< |
# have some override options been passed |
67 |
< |
if ( @_ ) { |
68 |
< |
$self->basequery(shift); |
69 |
< |
} |
70 |
< |
else { |
71 |
< |
# --- is there a starter document? |
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("ActiveDoc Error : No base doc found"); |
79 |
< |
} |
80 |
< |
} |
81 |
< |
$self->verbose("New ActiveDoc (".ref($self).") Created"); |
82 |
< |
$self->_init2(); |
83 |
< |
} |
84 |
< |
|
85 |
< |
sub _init2 { |
86 |
< |
|
87 |
< |
my $self=shift; |
88 |
< |
# A URL handler per document |
89 |
< |
$self->{urlhandler}=URL::URLhandler->new($self->config()->cache()); |
90 |
< |
|
91 |
< |
# A default UserInterface |
92 |
< |
$self->{userinterface}=ActiveDoc::SimpleUserInterface->new(); |
93 |
< |
$self->init(@_); |
39 |
> |
$self->{cache}=shift; |
40 |
> |
$self->{dbstore}=shift; |
41 |
> |
$self->{switch}=ActiveDoc::SimpleURLDoc->new($self->{cache}); |
42 |
|
return $self; |
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->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 { |
126 |
– |
$self->error("Cannot parse $parselabel - file not known"); |
127 |
– |
} |
128 |
– |
} |
129 |
– |
|
130 |
– |
sub currentparsename { |
131 |
– |
my $self=shift; |
132 |
– |
@_?$self->{currentparsename}=shift |
133 |
– |
:(defined $self->{currentparsename}?$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; |
145 |
– |
|
146 |
– |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new(); |
147 |
– |
$self->{parsers}{$parselabel}->addignoretags(); |
148 |
– |
$self->{parsers}{$parselabel}->addgrouptags(); |
149 |
– |
} |
150 |
– |
|
151 |
– |
sub cleartags { |
152 |
– |
my $self=shift; |
153 |
– |
my $parselabel=shift; |
154 |
– |
|
155 |
– |
$self->{parsers}{$parselabel}->cleartags(); |
156 |
– |
} |
157 |
– |
|
158 |
– |
|
159 |
– |
sub includeparse { |
160 |
– |
my $self=shift; |
161 |
– |
my $parselabel=shift; |
162 |
– |
my $remoteparselabel=shift; |
163 |
– |
my $activedoc=shift; |
164 |
– |
|
165 |
– |
# Some error trapping |
166 |
– |
if ( ! exists $self->{parsers}{$parselabel} ) { |
167 |
– |
$self->error("Unknown local parse name specified"); |
168 |
– |
} |
169 |
– |
if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) { |
170 |
– |
$self->error("Unknown parse name specified in remote obj $activedoc"); |
171 |
– |
} |
172 |
– |
|
173 |
– |
# |
174 |
– |
my $rp=$activedoc->{parsers}{$remoteparselabel}; |
175 |
– |
$self->{parsers}{$parselabel}->includeparse($rp); |
176 |
– |
} |
177 |
– |
|
178 |
– |
sub addtag { |
179 |
– |
my $self=shift; |
180 |
– |
my $parselabel=shift; |
181 |
– |
if ( $#_ != 6 ) { |
182 |
– |
$self->error("Incorrect addtags specification\n". |
183 |
– |
"called with :\n@_ \n"); |
184 |
– |
} |
185 |
– |
$self->{parsers}{$parselabel}->addtag(@_); |
186 |
– |
} |
187 |
– |
|
188 |
– |
sub addurltags { |
189 |
– |
my $self=shift; |
190 |
– |
my $parselabel=shift; |
191 |
– |
|
192 |
– |
$self->{parsers}{$parselabel}-> |
193 |
– |
addtag("Base", \&Base_start, $self, "", $self, |
194 |
– |
\&Base_end, $self); |
43 |
|
} |
44 |
|
|
45 |
|
sub url { |
55 |
|
else { return "undefined"; } |
56 |
|
} |
57 |
|
|
210 |
– |
sub copydocconfig { |
211 |
– |
my $self=shift; |
212 |
– |
my $ActiveDoc=shift; |
213 |
– |
|
214 |
– |
$self->config($ActiveDoc->config()); |
215 |
– |
|
216 |
– |
} |
217 |
– |
|
218 |
– |
sub copydocquery { |
219 |
– |
my $self=shift; |
220 |
– |
my $ActiveDoc=shift; |
221 |
– |
|
222 |
– |
if ( defined $ActiveDoc->basequery() ) { |
223 |
– |
$self->basequery($ActiveDoc->basequery()); |
224 |
– |
} |
225 |
– |
else { |
226 |
– |
$self->error("Cannot copy basequery - undefined"); |
227 |
– |
} |
228 |
– |
} |
229 |
– |
|
230 |
– |
sub config { |
231 |
– |
my $self=shift; |
232 |
– |
@_?$self->{ActiveConfig}=shift |
233 |
– |
: $self->{ActiveConfig}; |
234 |
– |
} |
235 |
– |
|
236 |
– |
sub basequery { |
237 |
– |
my $self=shift; |
238 |
– |
@_?$self->{Query}=shift |
239 |
– |
:$self->{Query}; |
240 |
– |
} |
241 |
– |
|
242 |
– |
sub option { |
243 |
– |
my $self=shift; |
244 |
– |
my $param=shift; |
245 |
– |
if ( defined $self->basequery()) { |
246 |
– |
return $self->basequery()->getparam($param); |
247 |
– |
} |
248 |
– |
else { |
249 |
– |
return $undef; |
250 |
– |
} |
251 |
– |
} |
252 |
– |
|
253 |
– |
sub requestoption { |
254 |
– |
my $self=shift; |
255 |
– |
my $param=shift; |
256 |
– |
my $string=shift; |
257 |
– |
|
258 |
– |
my $par=undef; |
259 |
– |
if ( defined $self->basequery()) { |
260 |
– |
$par=$self->basequery()->getparam($param); |
261 |
– |
while ( ! defined $par ) { |
262 |
– |
$self->basequery()->querytype( $param, "basic"); |
263 |
– |
$self->basequery()->querymessage( $param, $string); |
264 |
– |
$self->userinterface()->askuser($self->basequery()); |
265 |
– |
$par=$self->basequery()->getparam($param); |
266 |
– |
} |
267 |
– |
} |
268 |
– |
return $par; |
269 |
– |
} |
270 |
– |
|
271 |
– |
sub askuser { |
272 |
– |
my $self=shift; |
273 |
– |
return $self->userinterface()->askuser(@_); |
274 |
– |
} |
275 |
– |
|
58 |
|
sub getfile { |
59 |
|
my $self=shift; |
60 |
|
my $origurl=shift; |
61 |
|
|
62 |
|
my $fileref; |
63 |
|
my ($url, $file); |
64 |
< |
if ( (defined ($it=$self->option('url_update'))) && |
283 |
< |
( $it eq "1" || $origurl=~/^$it/ )) { |
64 |
> |
if ( 0 ) { |
65 |
|
$self->verbose("Forced download of $origurl"); |
66 |
< |
($url, $file)=$self->{urlhandler}->download($origurl); |
66 |
> |
($url, $file)=$self->{switch}->urldownload($origurl); |
67 |
|
} |
68 |
|
else { |
69 |
|
$self->verbose("Attempting to get $origurl"); |
70 |
< |
($url, $file)=$self->{urlhandler}->get($origurl); |
70 |
> |
($url, $file)=$self->{switch}->urlget($origurl); |
71 |
|
} |
72 |
|
# do we already have an appropriate object? |
73 |
< |
($fileref)=$self->config()->find($url); |
293 |
< |
#undef $fileref; |
73 |
> |
($fileref)=$self->{dbstore}->find($url); |
74 |
|
if ( defined $fileref ) { |
75 |
|
$self->verbose("Found $url in database"); |
76 |
|
$fileref->update(); |
79 |
|
if ( $file eq "" ) { |
80 |
|
$self->parseerror("Unable to get $origurl"); |
81 |
|
} |
82 |
< |
#-- set up a new preprocess file |
82 |
> |
# -- set up a new preprocess file |
83 |
|
$self->verbose("Making a new preprocessed file $url"); |
84 |
< |
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
84 |
> |
$fileref=ActiveDoc::PreProcessedFile->new($self->{cache}, |
85 |
> |
$self->{dbstore}); |
86 |
|
$fileref->url($url); |
87 |
|
$fileref->update(); |
88 |
|
} |
94 |
|
my $url=shift; |
95 |
|
|
96 |
|
# first get a preprocessed copy of the file |
97 |
< |
# my $fileob=$self->getfile($url); |
97 |
> |
#my $fileob=$self->getfile($url); |
98 |
|
|
99 |
< |
# now parse it for the <DocType> tag |
100 |
< |
my $tempdoc=ActiveDoc::ActiveDoc->new($self->config()); |
101 |
< |
$tempdoc->{urlhandler}=$self->{urlhandler}; |
102 |
< |
my $fullurl=$tempdoc->url($url); |
103 |
< |
$url=$fullurl; |
104 |
< |
$tempdoc->{doctypefound}=0; |
105 |
< |
$tempdoc->newparse("doctype"); |
325 |
< |
$tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc, |
326 |
< |
"", $tempdoc, "", $tempdoc); |
327 |
< |
$tempdoc->parse("doctype"); |
328 |
< |
|
329 |
< |
if ( ! defined $tempdoc->{docobject} ) { |
330 |
< |
print "No <Doc type=> Specified in ".$url."\n"; |
331 |
< |
exit 1; |
99 |
> |
# now parse it for the <Doc> tag |
100 |
> |
my $tempdoc=ActiveDoc::SimpleURLDoc->new($self->{cache}); |
101 |
> |
my ($doctype,$docversion)=$tempdoc->doctype(); |
102 |
> |
undef $tempdoc; |
103 |
> |
|
104 |
> |
if ( ! defined $doctype ) { |
105 |
> |
$self->parseerror("No <Doc type=> Specified in ".$url); |
106 |
|
} |
107 |
+ |
|
108 |
|
# Set up a new object of the specified type |
109 |
< |
eval "require $tempdoc->{docobject}"; |
109 |
> |
eval "require $doctype"; |
110 |
|
die $@ if $@; |
111 |
< |
my $newobj=$tempdoc->{docobject}->new($self->config()); |
337 |
< |
undef $tempdoc; |
111 |
> |
my $newobj=$doctype->new($self->{cache},$self->{dbstore}); |
112 |
|
$newobj->url($url); |
113 |
|
$newobj->parent($self); |
340 |
– |
$newobj->_initparse(); |
114 |
|
return $newobj; |
115 |
|
} |
116 |
|
|
121 |
|
:$self->{parent}; |
122 |
|
} |
123 |
|
|
351 |
– |
sub _initparse { |
352 |
– |
my $self=shift; |
353 |
– |
|
354 |
– |
$self->parse("init"); |
355 |
– |
} |
124 |
|
# -------- Error Handling and Error services -------------- |
125 |
|
|
358 |
– |
sub error { |
359 |
– |
my $self=shift; |
360 |
– |
my $string=shift; |
361 |
– |
|
362 |
– |
die $string."\n"; |
363 |
– |
} |
364 |
– |
|
126 |
|
sub parseerror { |
127 |
|
my $self=shift; |
128 |
|
my $string=shift; |
139 |
|
} |
140 |
|
} |
141 |
|
|
381 |
– |
sub checktag { |
382 |
– |
my $self=shift; |
383 |
– |
my $tagname=shift; |
384 |
– |
my $hashref=shift; |
385 |
– |
my $param=shift; |
386 |
– |
|
387 |
– |
if ( ! exists $$hashref{$param} ) { |
388 |
– |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
389 |
– |
} |
390 |
– |
} |
391 |
– |
|
142 |
|
sub line { |
143 |
|
my $self=shift; |
144 |
|
|
166 |
|
return $self->{File}->ProcessedFile(); |
167 |
|
} |
168 |
|
|
419 |
– |
# --------------- Initialisation Methods --------------------------- |
420 |
– |
|
421 |
– |
sub init { |
422 |
– |
# Dummy Routine - override for derived classes |
423 |
– |
} |
424 |
– |
|
425 |
– |
# ------------------- Tag Routines ----------------------------------- |
169 |
|
# |
170 |
< |
# Base - for setting url bases |
170 |
> |
# Delegate all else to the switch |
171 |
|
# |
172 |
< |
sub Base_start { |
172 |
> |
sub AUTOLOAD { |
173 |
|
my $self=shift; |
431 |
– |
my $name=shift; |
432 |
– |
my $hashref=shift; |
174 |
|
|
175 |
< |
$self->checktag($name, $hashref, 'type' ); |
176 |
< |
$self->checktag($name, $hashref, 'base' ); |
436 |
< |
|
437 |
< |
# Keep track of base tags |
438 |
< |
push @{$self->{basestack}}, $$hashref{"type"}; |
439 |
< |
# Set the base |
440 |
< |
$self->{urlhandler}->setbase($$hashref{"type"},$hashref); |
441 |
< |
} |
175 |
> |
# dont propogate destroy methods |
176 |
> |
return if $AUTOLOAD=~/::DESTROY/; |
177 |
|
|
178 |
< |
sub Base_end { |
179 |
< |
my $self=shift; |
445 |
< |
my $name=shift; |
446 |
< |
my $type; |
178 |
> |
# remove this package name |
179 |
> |
($name=$AUTOLOAD)=~s/ActiveDoc::ActiveDoc:://; |
180 |
|
|
181 |
< |
if ( $#{$self->{basestack}} == -1 ) { |
182 |
< |
$self->parseerror("Parse Error : unmatched </$name>"); |
450 |
< |
} |
451 |
< |
else { |
452 |
< |
$type = pop @{$self->{basestack}}; |
453 |
< |
$self->{urlhandler}->unsetbase($type); |
454 |
< |
} |
181 |
> |
# pass the message to SimpleDoc |
182 |
> |
$self->{switch}->$name(@_); |
183 |
|
} |
184 |
|
|
185 |
+ |
|
186 |
+ |
# ------------------- Tag Routines ----------------------------------- |
187 |
|
sub Doc_Start { |
188 |
|
my $self=shift; |
189 |
|
my $name=shift; |
195 |
|
$self->{docobject}=$$hashref{'type'}; |
196 |
|
} |
197 |
|
} |
468 |
– |
|
469 |
– |
sub userinterface { |
470 |
– |
my $self=shift; |
471 |
– |
@_?$self->{userinterface}=shift |
472 |
– |
:$self->{userinterface} |
473 |
– |
} |
474 |
– |
|
475 |
– |
sub _saveactivedoc { |
476 |
– |
my $self=shift; |
477 |
– |
my $fh=shift; |
478 |
– |
print "Storing $self\n"; |
479 |
– |
print $fh $self->url()."\n"; |
480 |
– |
} |
481 |
– |
|
482 |
– |
sub _restoreactivedoc { |
483 |
– |
my $self=shift; |
484 |
– |
my $fh=shift; |
485 |
– |
|
486 |
– |
my $url=<$fh>; |
487 |
– |
chomp $url; |
488 |
– |
$self->url($url); |
489 |
– |
} |