1 |
|
# |
2 |
< |
# The base functionality for the ActiveDocument - inherits from Basetags |
2 |
> |
# ActiveDoc.pm |
3 |
> |
# |
4 |
> |
# Originally Written by Christopher Williams |
5 |
> |
# |
6 |
> |
# Description |
7 |
|
# |
4 |
– |
# Inherits from BaseTags |
5 |
– |
# -------- |
8 |
|
# Interface |
9 |
|
# --------- |
10 |
< |
# new(filename, DOChandler): create a new object based on a file and |
11 |
< |
# associate with a base DOChandler |
12 |
< |
# parse() : parse the input file |
13 |
< |
# include(url) : Activate include file mechanism, returns the object ref if OK |
14 |
< |
# treenode() : return the associated TreeNode object reference |
15 |
< |
# getincludeObjectStore : Return a pointer to the ObectStore that contains all |
16 |
< |
# included objects |
17 |
< |
# find(string) : find the object reference related to string in the associated |
18 |
< |
# tree. Mechanism for getting object references |
19 |
< |
# _addgroup() : Add group functionality to document |
20 |
< |
# parseerror(String) : Report an error to the user |
21 |
< |
# userinterface() : return the default User Interface object |
22 |
< |
# checktag($hashref, param , tagname) : Check a hash returned from switcher |
23 |
< |
# for a given parameter |
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 |
> |
# |
14 |
> |
# 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 |
> |
# includeparse(local_parsename, objparsename, activedoc) : copy the parse from |
21 |
> |
# one object to another |
22 |
> |
# currentparsename([name]) : get/set current parse name |
23 |
> |
# getfile(url) : get a processedfile object given a url |
24 |
> |
# activatedoc(url) : Return the object ref for a doc described by the given url |
25 |
> |
# -- any parse called "init" will also be run |
26 |
> |
# config([ActiveConfig]) : Set up/return Configuration for the document |
27 |
> |
# basequery([ActiveConfig]) : Set up/return UserQuery for the doc |
28 |
> |
# copydocconfig(ActiveDoc) : Copy the basic configuration from the ActiveDoc |
29 |
> |
# copydocquery(ActiveDoc) : Copy the basicquery from the ActiveDoc |
30 |
> |
# userinterface() : Return the defaullt userinterface |
31 |
> |
# option(var) : return the value of the option var |
32 |
> |
# requestoption("message") : Ask the user to supply a value for an option |
33 |
> |
# if it dosnt already exist |
34 |
> |
# |
35 |
> |
# -- error methods -- |
36 |
> |
# error(string) : Report an general error to the user |
37 |
> |
# parseerror(string) : Report an error during parsing a file |
38 |
> |
# line() : Return the current line number of the document |
39 |
> |
# and the ProcessedFileObj it is in |
40 |
|
|
41 |
|
package ActiveDoc::ActiveDoc; |
42 |
< |
require 5.001; |
43 |
< |
use ActiveDoc::DOChandler; |
44 |
< |
use ActiveDoc::TreeNode; |
45 |
< |
use ActiveDoc::UserQuery; |
46 |
< |
use ObjectStoreCont; |
47 |
< |
|
48 |
< |
@ISA = qw(BaseTags); |
49 |
< |
|
50 |
< |
# Initialise |
51 |
< |
sub _init { |
52 |
< |
my $self=shift; |
53 |
< |
my $DOChandler=shift; |
54 |
< |
my $OC=shift; |
55 |
< |
|
56 |
< |
$self->_addurl(); |
57 |
< |
$self->{urlhandler}->setcache($DOChandler->defaultcache()); |
58 |
< |
$self->{treenode}=ActiveDoc::TreeNode->new(); |
59 |
< |
$self->{dochandler}=$DOChandler; |
60 |
< |
$self->{UserQuery}=$DOChandler->{UserQuery}; |
61 |
< |
$self->{tags}->addtag("Use", \&Use_Start, "", ""); |
62 |
< |
# Add the minimal functionality tag - feel free to override |
63 |
< |
$self->{tags}->addtag("Include", \&Include_Start, "", ""); |
64 |
< |
$self->init(); |
42 |
> |
require 5.004; |
43 |
> |
use ActiveDoc::Parse; |
44 |
> |
use ActiveDoc::ActiveConfig; |
45 |
> |
use ActiveDoc::PreProcessedFile; |
46 |
> |
use ObjectUtilities::StorableObject; |
47 |
> |
use URL::URLhandler; |
48 |
> |
|
49 |
> |
@ISA = qw(ObjectUtilities::StorableObject); |
50 |
> |
|
51 |
> |
sub new { |
52 |
> |
my $class=shift; |
53 |
> |
$self={}; |
54 |
> |
bless $self, $class; |
55 |
> |
$self->config(shift); |
56 |
> |
|
57 |
> |
# have some override options been passed |
58 |
> |
if ( @_ ) { |
59 |
> |
$self->basequery(shift); |
60 |
> |
} |
61 |
> |
else { |
62 |
> |
# --- is there a starter document? |
63 |
> |
my $basedoc=$self->config()->basedoc(); |
64 |
> |
if ( defined $basedoc ) { |
65 |
> |
$self->copydocquery($basedoc); |
66 |
> |
} |
67 |
> |
else { |
68 |
> |
$self->error("ActiveDoc Error : No base doc found"); |
69 |
> |
} |
70 |
> |
} |
71 |
> |
$self->_init2(); |
72 |
|
} |
73 |
|
|
74 |
< |
sub init { |
75 |
< |
# Dummy Routine - override for derrived classes |
74 |
> |
sub _init2 { |
75 |
> |
|
76 |
> |
my $self=shift; |
77 |
> |
# A URL handler per document |
78 |
> |
$self->{urlhandler}=URL::URLhandler->new($self->config()->cache()); |
79 |
> |
|
80 |
> |
# A default UserInterface |
81 |
> |
$self->{userinterface}=ActiveDoc::SimpleUserInterface->new(); |
82 |
> |
$self->init(@_); |
83 |
> |
return $self; |
84 |
> |
|
85 |
|
} |
86 |
< |
# |
87 |
< |
# use mechanism |
88 |
< |
# |
89 |
< |
sub include { |
90 |
< |
my $self=shift; |
91 |
< |
my $url=shift; |
92 |
< |
my $linkfile=shift; |
93 |
< |
my $filename; |
94 |
< |
my $obj; |
95 |
< |
|
96 |
< |
$file=$self->{urlhandler}->get($url); |
97 |
< |
if ( $linkfile ne "" ) { |
98 |
< |
$filename=$file."/".$linkfile; |
99 |
< |
} |
100 |
< |
$obj=$self->{dochandler}->newdoc($filename); |
101 |
< |
|
102 |
< |
# Now Extend our tree |
103 |
< |
$self->{treenode}->grow($obj->treenode()); |
104 |
< |
return $obj; |
86 |
> |
|
87 |
> |
# ----- parse related routines -------------- |
88 |
> |
sub parse { |
89 |
> |
my $self=shift; |
90 |
> |
$parselabel=shift; |
91 |
> |
|
92 |
> |
my $file=$self->file(); |
93 |
> |
if ( $file ) { |
94 |
> |
if ( exists $self->{parsers}{$parselabel} ) { |
95 |
> |
$self->{currentparsename}=$parselabel; |
96 |
> |
$self->{currentparser}=$self->{parsers}{$parselabel}; |
97 |
> |
$self->{parsers}{$parselabel}->parse($file,@_); |
98 |
> |
delete $self->{currentparser}; |
99 |
> |
$self->{currentparsename}=""; |
100 |
> |
} |
101 |
> |
} |
102 |
> |
else { |
103 |
> |
print "Cannot parse - file not known\n"; |
104 |
> |
} |
105 |
|
} |
106 |
|
|
107 |
< |
sub userinterface { |
107 |
> |
sub currentparsename { |
108 |
|
my $self=shift; |
109 |
< |
return $self->{dochandler}->{UserInterface}; |
109 |
> |
@_?$self->{currentparsename}=shift |
110 |
> |
:$self->{currentparsename}; |
111 |
|
} |
112 |
|
|
113 |
< |
sub treenode { |
113 |
> |
sub newparse { |
114 |
|
my $self=shift; |
115 |
< |
return $self->{treenode}; |
115 |
> |
my $parselabel=shift; |
116 |
> |
|
117 |
> |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new(); |
118 |
> |
$self->{parsers}{$parselabel}->addignoretags(); |
119 |
> |
$self->{parsers}{$parselabel}->addgrouptags(); |
120 |
|
} |
121 |
|
|
122 |
< |
sub getincludeObjectStore { |
123 |
< |
my $self=shift; |
124 |
< |
return $self->{includeOS}; |
122 |
> |
sub cleartags { |
123 |
> |
my $self=shift; |
124 |
> |
my $parselabel=shift; |
125 |
> |
|
126 |
> |
$self->{parsers}{$parselabel}->cleartags(); |
127 |
|
} |
128 |
|
|
129 |
< |
sub find($) { |
129 |
> |
|
130 |
> |
sub includeparse { |
131 |
|
my $self=shift; |
132 |
< |
my $string=shift; |
133 |
< |
my $tn; |
132 |
> |
my $parselabel=shift; |
133 |
> |
my $remoteparselabel=shift; |
134 |
> |
my $activedoc=shift; |
135 |
> |
|
136 |
> |
# Some error trapping |
137 |
> |
if ( ! exists $self->{parsers}{$parselabel} ) { |
138 |
> |
$self->error("Unknown local parse name specified"); |
139 |
> |
} |
140 |
> |
if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) { |
141 |
> |
$self->error("Unknown parse name specified in remote obj $activedoc"); |
142 |
> |
} |
143 |
|
|
144 |
< |
$tn=$self->{treenode}->find($string); |
145 |
< |
if ( $tn eq "" ) { |
146 |
< |
$self->parseerror("Unable to find $string"); |
144 |
> |
# |
145 |
> |
my $rp=$activedoc->{parsers}{$remoteparselabel}; |
146 |
> |
$self->{parsers}{$parselabel}->includeparse($rp); |
147 |
> |
} |
148 |
> |
|
149 |
> |
sub addtag { |
150 |
> |
my $self=shift; |
151 |
> |
my $parselabel=shift; |
152 |
> |
if ( $#_ != 6 ) { |
153 |
> |
$self->error("Incorrect addtags specification\n". |
154 |
> |
"called with :\n@_ \n"); |
155 |
|
} |
156 |
< |
return $tn->associate(); |
156 |
> |
$self->{parsers}{$parselabel}->addtag(@_); |
157 |
|
} |
158 |
|
|
159 |
< |
sub parseerror { |
159 |
> |
sub addurltags { |
160 |
|
my $self=shift; |
161 |
< |
my $string=shift; |
161 |
> |
my $parselabel=shift; |
162 |
> |
|
163 |
> |
$self->{parsers}{$parselabel}-> |
164 |
> |
addtag("Base", \&Base_start, $self, "", $self, |
165 |
> |
\&Base_end, $self); |
166 |
> |
} |
167 |
|
|
168 |
< |
print "Parse Error in $self->{url}, line $self-{switch}->line()\n"; |
169 |
< |
print $string."\n"; |
170 |
< |
die; |
168 |
> |
sub url { |
169 |
> |
my $self=shift; |
170 |
> |
# get file & preprocess |
171 |
> |
if ( @_ ) {$self->{File}=$self->getfile(shift)} |
172 |
> |
$self->{File}->url(); |
173 |
|
} |
174 |
|
|
175 |
< |
sub checktag { |
175 |
> |
sub copydocconfig { |
176 |
> |
my $self=shift; |
177 |
> |
my $ActiveDoc=shift; |
178 |
> |
|
179 |
> |
$self->config($ActiveDoc->config()); |
180 |
> |
|
181 |
> |
} |
182 |
> |
|
183 |
> |
sub copydocquery { |
184 |
> |
my $self=shift; |
185 |
> |
my $ActiveDoc=shift; |
186 |
> |
|
187 |
> |
if ( defined $ActiveDoc->basequery() ) { |
188 |
> |
$self->basequery($ActiveDoc->basequery()); |
189 |
> |
} |
190 |
> |
else { |
191 |
> |
$self->error("Cannot copy basequery - undefined"); |
192 |
> |
} |
193 |
> |
} |
194 |
> |
|
195 |
> |
sub config { |
196 |
> |
my $self=shift; |
197 |
> |
@_?$self->{ActiveConfig}=shift |
198 |
> |
: $self->{ActiveConfig}; |
199 |
> |
} |
200 |
> |
|
201 |
> |
sub basequery { |
202 |
> |
my $self=shift; |
203 |
> |
@_ ? $self->{Query}=shift |
204 |
> |
: $self->{Query}; |
205 |
> |
return $self->{Query}; |
206 |
> |
} |
207 |
> |
|
208 |
> |
sub option { |
209 |
|
my $self=shift; |
111 |
– |
my $hashref=shift; |
210 |
|
my $param=shift; |
211 |
< |
my $tagname=shift; |
211 |
> |
if ( defined $self->basequery()) { |
212 |
> |
return $self->basequery()->getparam($param); |
213 |
> |
} |
214 |
> |
else { |
215 |
> |
return $undef; |
216 |
> |
} |
217 |
> |
} |
218 |
> |
|
219 |
> |
sub requestoption { |
220 |
> |
my $self=shift; |
221 |
> |
my $param=shift; |
222 |
> |
my $string=shift; |
223 |
|
|
224 |
< |
if ( ! exists $$hashref{$param} ) { |
225 |
< |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
224 |
> |
my $par=undef; |
225 |
> |
if ( defined $self->basequery()) { |
226 |
> |
$par=$self->basequery()->getparam($param); |
227 |
> |
while ( ! defined $par ) { |
228 |
> |
$self->basequery()->querytype( $param, "basic"); |
229 |
> |
$self->basequery()->querymessage( $param, $string); |
230 |
> |
$self->userinterface()->askuser($self->basequery()); |
231 |
> |
$par=$self->basequery()->getparam($param); |
232 |
> |
} |
233 |
|
} |
234 |
+ |
return $par; |
235 |
|
} |
236 |
|
|
237 |
< |
# ------------------------ Tag Routines ------------------------------ |
238 |
< |
# |
239 |
< |
# The Include tag |
123 |
< |
# |
237 |
> |
sub getfile() { |
238 |
> |
my $self=shift; |
239 |
> |
my $origurl=shift; |
240 |
|
|
241 |
< |
sub Include_Start { |
241 |
> |
my $fileref; |
242 |
> |
my ($url, $file)=$self->{urlhandler}->get($origurl); |
243 |
> |
# do we already have an appropriate object? |
244 |
> |
($fileref)=$self->config()->find($url); |
245 |
> |
#undef $fileref; |
246 |
> |
if ( defined $fileref ) { |
247 |
> |
print "found $url in database ----\n"; |
248 |
> |
$fileref->update(); |
249 |
> |
} |
250 |
> |
else { |
251 |
> |
if ( $file eq "" ) { |
252 |
> |
$self->parseerror("Unable to get $origurl"); |
253 |
> |
} |
254 |
> |
#-- set up a new preprocess file |
255 |
> |
print "Making a new file $url----\n"; |
256 |
> |
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
257 |
> |
$fileref->url($url); |
258 |
> |
$fileref->update(); |
259 |
> |
} |
260 |
> |
return $fileref; |
261 |
> |
} |
262 |
> |
|
263 |
> |
sub activatedoc { |
264 |
|
my $self=shift; |
265 |
< |
my $name=shift; |
266 |
< |
my $hashref=shift; |
265 |
> |
my $url=shift; |
266 |
> |
|
267 |
> |
# first get a preprocessed copy of the file |
268 |
> |
# my $fileob=$self->getfile($url); |
269 |
> |
|
270 |
> |
# now parse it for the <DocType> tag |
271 |
> |
my $tempdoc=ActiveDoc::ActiveDoc->new($self->config()); |
272 |
> |
$tempdoc->{urlhandler}=$self->{urlhandler}; |
273 |
> |
my $fullurl=$tempdoc->url($url); |
274 |
> |
$url=$fullurl; |
275 |
> |
$tempdoc->{doctypefound}=0; |
276 |
> |
$tempdoc->newparse("doctype"); |
277 |
> |
$tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc, |
278 |
> |
"", $tempdoc, "", $tempdoc); |
279 |
> |
$tempdoc->parse("doctype"); |
280 |
> |
|
281 |
> |
if ( ! defined $tempdoc->{docobject} ) { |
282 |
> |
print "No <Doc type=> Specified in ".$url."\n"; |
283 |
> |
exit 1; |
284 |
> |
} |
285 |
> |
# Set up a new object of the specified type |
286 |
> |
eval "require $tempdoc->{docobject}"; |
287 |
> |
die $@ if $@; |
288 |
> |
my $newobj=$tempdoc->{docobject}->new($self->config()); |
289 |
> |
undef $tempdoc; |
290 |
> |
$newobj->url($url); |
291 |
> |
$newobj->_initparse(); |
292 |
> |
return $newobj; |
293 |
> |
} |
294 |
> |
|
295 |
> |
sub _initparse { |
296 |
> |
my $self=shift; |
297 |
> |
|
298 |
> |
$self->parse("init"); |
299 |
> |
} |
300 |
> |
# -------- Error Handling and Error services -------------- |
301 |
> |
|
302 |
> |
sub error { |
303 |
> |
my $self=shift; |
304 |
> |
my $string=shift; |
305 |
> |
|
306 |
> |
die $string."\n"; |
307 |
> |
} |
308 |
> |
|
309 |
> |
sub parseerror { |
310 |
> |
my $self=shift; |
311 |
> |
my $string=shift; |
312 |
> |
|
313 |
> |
if ( ! defined $self->{currentparse} ) { |
314 |
> |
$self->error($string); |
315 |
> |
} |
316 |
> |
else { |
317 |
> |
($line, $file)=$self->line(); |
318 |
> |
print "Parse Error in ".$file->url().", line ". |
319 |
> |
$line."\n"; |
320 |
> |
print $string."\n"; |
321 |
> |
die; |
322 |
> |
} |
323 |
> |
} |
324 |
> |
|
325 |
> |
sub checktag { |
326 |
> |
my $self=shift; |
327 |
> |
my $tagname=shift; |
328 |
> |
my $hashref=shift; |
329 |
> |
my $param=shift; |
330 |
> |
|
331 |
> |
if ( ! exists $$hashref{$param} ) { |
332 |
> |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
333 |
> |
} |
334 |
> |
} |
335 |
|
|
336 |
< |
$self->{switch}->checkparam( $name, "ref"); |
337 |
< |
print "<Include> tag not yet implemented\n"; |
338 |
< |
# $self->include($$hashref{'ref'},$$hashref{'linkdoc'}); |
336 |
> |
sub line { |
337 |
> |
my $self=shift; |
338 |
> |
|
339 |
> |
my ($line, $fileobj)= |
340 |
> |
$self->{File}->realline($self->{currentparser}->line()); |
341 |
> |
return ($line, $fileobj); |
342 |
> |
} |
343 |
> |
|
344 |
> |
sub tagstartline { |
345 |
> |
my $self=shift; |
346 |
> |
my ($line, $fileobj)=$self->{File}->line( |
347 |
> |
$self->{currentparser}->tagstartline()); |
348 |
> |
return ($line, $fileobj); |
349 |
|
} |
350 |
|
|
351 |
< |
sub Use_Start { |
351 |
> |
sub file { |
352 |
|
my $self=shift; |
353 |
+ |
|
354 |
+ |
$self->{File}->file(); |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
# --------------- Initialisation Methods --------------------------- |
358 |
+ |
|
359 |
+ |
sub init { |
360 |
+ |
# Dummy Routine - override for derived classes |
361 |
+ |
} |
362 |
+ |
|
363 |
+ |
# ------------------- Tag Routines ----------------------------------- |
364 |
+ |
# |
365 |
+ |
# Base - for setting url bases |
366 |
+ |
# |
367 |
+ |
sub Base_start { |
368 |
+ |
my $self=shift; |
369 |
|
my $name=shift; |
370 |
|
my $hashref=shift; |
371 |
|
|
372 |
< |
print "<Use> tag not yet implemented\n"; |
372 |
> |
$self->checktag($name, $hashref, 'type' ); |
373 |
> |
$self->checktag($name, $hashref, 'base' ); |
374 |
> |
|
375 |
> |
# Keep track of base tags |
376 |
> |
push @{$self->{basestack}}, $$hashref{"type"}; |
377 |
> |
# Set the base |
378 |
> |
$self->{urlhandler}->setbase($$hashref{"type"},$hashref); |
379 |
> |
} |
380 |
> |
|
381 |
> |
sub Base_end { |
382 |
> |
my $self=shift; |
383 |
> |
my $name=shift; |
384 |
> |
my $type; |
385 |
> |
|
386 |
> |
if ( $#{$self->{basestack}} == -1 ) { |
387 |
> |
$self->parseerror("Parse Error : unmatched </$name>"); |
388 |
> |
} |
389 |
> |
else { |
390 |
> |
$type = pop @{$self->{basestack}}; |
391 |
> |
$self->{urlhandler}->unsetbase($type); |
392 |
> |
} |
393 |
> |
} |
394 |
> |
|
395 |
> |
sub Doc_Start { |
396 |
> |
my $self=shift; |
397 |
> |
my $name=shift; |
398 |
> |
my $hashref=shift; |
399 |
> |
|
400 |
> |
$self->checktag($name, $hashref, "type"); |
401 |
> |
$self->{doctypefound}++; |
402 |
> |
if ( $self->{doctypefound} == 1 ) { # only take first doctype |
403 |
> |
$self->{docobject}=$$hashref{'type'}; |
404 |
> |
} |
405 |
> |
} |
406 |
> |
|
407 |
> |
sub userinterface { |
408 |
> |
my $self=shift; |
409 |
> |
@_?$self->{userinterface}=shift |
410 |
> |
:$self->{userinterface} |
411 |
|
} |