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() : 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 |
> |
# newdoc(file) : Return an new object of the appropriate type |
24 |
> |
# getfile(url) : get a processedfile object given a url |
25 |
> |
# activatedoc(url) : Return the object ref for a doc described by the given url |
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 |
> |
# options(var) : return the value of the option var |
32 |
> |
# |
33 |
> |
# -- error methods -- |
34 |
> |
# error(string) : Report an general error to the user |
35 |
> |
# parseerror(string) : Report an error during parsing a file |
36 |
> |
# line() : Return the current line number of the document |
37 |
> |
# and the ProcessedFileObj it is in |
38 |
|
|
39 |
|
package ActiveDoc::ActiveDoc; |
40 |
< |
require 5.001; |
41 |
< |
use ActiveDoc::DOChandler; |
42 |
< |
use ActiveDoc::TreeNode; |
43 |
< |
use ActiveDoc::UserQuery; |
44 |
< |
use ObjectStoreCont; |
45 |
< |
|
46 |
< |
@ISA = qw(BaseTags); |
47 |
< |
|
48 |
< |
# Initialise |
49 |
< |
sub _init { |
50 |
< |
my $self=shift; |
51 |
< |
my $DOChandler=shift; |
52 |
< |
my $OC=shift; |
53 |
< |
|
54 |
< |
$self->_addurl(); |
55 |
< |
$self->{urlhandler}->setcache($DOChandler->defaultcache()); |
56 |
< |
$self->{treenode}=ActiveDoc::TreeNode->new(); |
57 |
< |
$self->{dochandler}=$DOChandler; |
58 |
< |
$self->{UserQuery}=$DOChandler->{UserQuery}; |
59 |
< |
$self->{tags}->addtag("Use", \&Use_Start, "", ""); |
60 |
< |
# Add the minimal functionality tag - feel free to override |
61 |
< |
$self->{tags}->addtag("Include", \&Include_Start, "", ""); |
46 |
< |
$self->init(); |
40 |
> |
require 5.004; |
41 |
> |
use ActiveDoc::Parse; |
42 |
> |
use ActiveDoc::ActiveConfig; |
43 |
> |
use ActiveDoc::PreProcessedFile; |
44 |
> |
use ObjectUtilities::StorableObject; |
45 |
> |
use URL::URLhandler; |
46 |
> |
|
47 |
> |
@ISA = qw(ObjectUtilities::StorableObject); |
48 |
> |
|
49 |
> |
sub new { |
50 |
> |
my $class=shift; |
51 |
> |
$self={}; |
52 |
> |
bless $self, $class; |
53 |
> |
$self->config(shift); |
54 |
> |
|
55 |
> |
# A URL handler per document |
56 |
> |
$self->{urlhandler}=URL::URLhandler->new($self->config()->cache()); |
57 |
> |
|
58 |
> |
# A default UserInterface |
59 |
> |
$self->{userinterface}=ActiveDoc::SimpleUserInterface->new(); |
60 |
> |
$self->init(@_); |
61 |
> |
return $self; |
62 |
|
} |
63 |
|
|
64 |
< |
sub init { |
65 |
< |
# Dummy Routine - override for derrived classes |
66 |
< |
} |
67 |
< |
# |
68 |
< |
# use mechanism |
69 |
< |
# |
70 |
< |
sub include { |
71 |
< |
my $self=shift; |
72 |
< |
my $url=shift; |
73 |
< |
my $linkfile=shift; |
74 |
< |
my $filename; |
75 |
< |
my $obj; |
76 |
< |
|
77 |
< |
$file=$self->{urlhandler}->get($url); |
78 |
< |
if ( $linkfile ne "" ) { |
79 |
< |
$filename=$file."/".$linkfile; |
65 |
< |
} |
66 |
< |
$obj=$self->{dochandler}->newdoc($filename); |
67 |
< |
|
68 |
< |
# Now Extend our tree |
69 |
< |
$self->{treenode}->grow($obj->treenode()); |
70 |
< |
return $obj; |
64 |
> |
# ----- parse related routines -------------- |
65 |
> |
sub parse { |
66 |
> |
my $self=shift; |
67 |
> |
$parselabel=shift; |
68 |
> |
|
69 |
> |
my $file=$self->file(); |
70 |
> |
if ( $file ) { |
71 |
> |
$self->{currentparsename}=$parselabel; |
72 |
> |
$self->{currentparser}=$self->{parsers}{$parselabel}; |
73 |
> |
$self->{parsers}{$parselabel}->parse($file,@_); |
74 |
> |
delete $self->{currentparser}; |
75 |
> |
$self->{currentparsename}=""; |
76 |
> |
} |
77 |
> |
else { |
78 |
> |
print "Cannot parse - file not known\n"; |
79 |
> |
} |
80 |
|
} |
81 |
|
|
82 |
< |
sub userinterface { |
82 |
> |
sub currentparsename { |
83 |
|
my $self=shift; |
84 |
< |
return $self->{dochandler}->{UserInterface}; |
84 |
> |
@_?$self->{currentparsename}=shift |
85 |
> |
:$self->{currentparsename}; |
86 |
|
} |
87 |
|
|
88 |
< |
sub treenode { |
88 |
> |
sub newparse { |
89 |
|
my $self=shift; |
90 |
< |
return $self->{treenode}; |
81 |
< |
} |
90 |
> |
my $parselabel=shift; |
91 |
|
|
92 |
< |
sub getincludeObjectStore { |
93 |
< |
my $self=shift; |
94 |
< |
return $self->{includeOS}; |
92 |
> |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new(); |
93 |
> |
$self->{parsers}{$parselabel}->addignoretags(); |
94 |
> |
$self->{parsers}{$parselabel}->addgrouptags(); |
95 |
|
} |
96 |
|
|
97 |
< |
sub find($) { |
97 |
> |
sub includeparse { |
98 |
|
my $self=shift; |
99 |
< |
my $string=shift; |
100 |
< |
my $tn; |
99 |
> |
my $parselabel=shift; |
100 |
> |
my $remoteparselabel=shift; |
101 |
> |
my $activedoc=shift; |
102 |
> |
|
103 |
> |
# Some error trapping |
104 |
> |
if ( ! exists $self->{parsers}{$parselabel} ) { |
105 |
> |
$self->error("Unknown local parse name specified"); |
106 |
> |
} |
107 |
> |
if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) { |
108 |
> |
$self->error("Unknown parse name specified in remote obj $activedoc"); |
109 |
> |
} |
110 |
> |
|
111 |
> |
# |
112 |
> |
my $rp=$activedoc->{parsers}{$remoteparselabel}; |
113 |
> |
$self->{parsers}{$parselabel}->includeparse($rp); |
114 |
> |
} |
115 |
|
|
116 |
< |
$tn=$self->{treenode}->find($string); |
117 |
< |
if ( $tn eq "" ) { |
118 |
< |
$self->parseerror("Unable to find $string"); |
116 |
> |
sub addtag { |
117 |
> |
my $self=shift; |
118 |
> |
my $parselabel=shift; |
119 |
> |
if ( $#_ != 6 ) { |
120 |
> |
$self->error("Incorrect addtags specification\n". |
121 |
> |
"called with :\n@_ \n"); |
122 |
|
} |
123 |
< |
return $tn->associate(); |
123 |
> |
$self->{parsers}{$parselabel}->addtag(@_); |
124 |
|
} |
125 |
|
|
126 |
< |
sub line { |
126 |
> |
sub addurltags { |
127 |
|
my $self=shift; |
128 |
< |
return $self->{switch}->line(); |
128 |
> |
my $parselabel=shift; |
129 |
> |
|
130 |
> |
$self->{parsers}{$parselabel}-> |
131 |
> |
addtag("Base", \&Base_start, $self, "", $self, |
132 |
> |
\&Base_end, $self); |
133 |
|
} |
134 |
|
|
135 |
< |
sub error { |
135 |
> |
sub url { |
136 |
|
my $self=shift; |
137 |
< |
my $string=shift; |
137 |
> |
# get file & preprocess |
138 |
> |
if ( @_ ) {$self->{File}=$self->getfile(shift)} |
139 |
> |
$self->{File}->url(); |
140 |
> |
} |
141 |
|
|
142 |
< |
die $string."\n"; |
142 |
> |
sub copydocconfig { |
143 |
> |
my $self=shift; |
144 |
> |
my $ActiveDoc=shift; |
145 |
> |
|
146 |
> |
$self->config($ActiveDoc->config()); |
147 |
|
|
148 |
|
} |
149 |
< |
sub parseerror { |
149 |
> |
|
150 |
> |
sub copydocquery { |
151 |
|
my $self=shift; |
152 |
< |
my $string=shift; |
152 |
> |
my $ActiveDoc=shift; |
153 |
|
|
154 |
< |
print "Parse Error in $self->{url}, line ". |
117 |
< |
$self->line()."\n"; |
118 |
< |
print $string."\n"; |
119 |
< |
die; |
154 |
> |
$self->basequery($ActiveDoc->basequery()); |
155 |
|
} |
156 |
|
|
157 |
< |
sub checktag { |
157 |
> |
sub config { |
158 |
> |
my $self=shift; |
159 |
> |
@_?$self->{ActiveConfig}=shift |
160 |
> |
: $self->{ActiveConfig}; |
161 |
> |
} |
162 |
> |
|
163 |
> |
sub basequery { |
164 |
> |
my $self=shift; |
165 |
> |
@_ ? $self->{Query}=shift |
166 |
> |
: $self->{Query}; |
167 |
> |
} |
168 |
> |
|
169 |
> |
sub options { |
170 |
|
my $self=shift; |
124 |
– |
my $hashref=shift; |
171 |
|
my $param=shift; |
172 |
< |
my $tagname=shift; |
172 |
> |
$self->basequery()->getparam('option_'.$param); |
173 |
> |
} |
174 |
|
|
175 |
< |
if ( ! exists $$hashref{$param} ) { |
176 |
< |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
175 |
> |
sub getfile() { |
176 |
> |
my $self=shift; |
177 |
> |
my $origurl=shift; |
178 |
> |
|
179 |
> |
my $fileref; |
180 |
> |
my ($url, $file)=$self->{urlhandler}->get($origurl); |
181 |
> |
# do we already have an appropriate object? |
182 |
> |
($fileref)=$self->config()->find($url); |
183 |
> |
#undef $fileref; |
184 |
> |
if ( defined $fileref ) { |
185 |
> |
print "found $url in database ----\n"; |
186 |
> |
$fileref->update(); |
187 |
> |
} |
188 |
> |
else { |
189 |
> |
if ( $file eq "" ) { |
190 |
> |
$self->parseerror("Unable to get $origurl"); |
191 |
> |
} |
192 |
> |
#-- set up a new preprocess file |
193 |
> |
print "Making a new file $url----\n"; |
194 |
> |
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
195 |
> |
$fileref->url($url); |
196 |
> |
$fileref->update(); |
197 |
|
} |
198 |
+ |
return $fileref; |
199 |
|
} |
200 |
|
|
201 |
< |
# ------------------------ Tag Routines ------------------------------ |
202 |
< |
# |
203 |
< |
# The Include tag |
204 |
< |
# |
201 |
> |
sub activatedoc { |
202 |
> |
my $self=shift; |
203 |
> |
my $url=shift; |
204 |
> |
|
205 |
> |
# first get a preprocessed copy of the file |
206 |
> |
my $fileob=$self->getfile($url); |
207 |
|
|
208 |
< |
sub Include_Start { |
208 |
> |
# now parse it for the <DocType> tag |
209 |
> |
$self->{doctypefound}=0; |
210 |
> |
$self->newparse("doctype"); |
211 |
> |
$self->addtag("doctype","Doc", \&Doc_Start, $self, |
212 |
> |
"", $self, "", $self); |
213 |
> |
$self->parse("doctype"); |
214 |
> |
|
215 |
> |
if ( ! defined $self->{docobject} ) { |
216 |
> |
print "No <Doc type=> Specified in ".$fileob->url()."\n"; |
217 |
> |
exit 1; |
218 |
> |
} |
219 |
> |
# Set up a new object of the specified type |
220 |
> |
my $newobj=$self->{docobject}->new($self->config()); |
221 |
> |
$newobj->url($url); |
222 |
> |
return $newobj; |
223 |
> |
} |
224 |
> |
|
225 |
> |
# -------- Error Handling and Error services -------------- |
226 |
> |
|
227 |
> |
sub error { |
228 |
> |
my $self=shift; |
229 |
> |
my $string=shift; |
230 |
> |
|
231 |
> |
die $string."\n"; |
232 |
> |
} |
233 |
> |
|
234 |
> |
sub parseerror { |
235 |
> |
my $self=shift; |
236 |
> |
my $string=shift; |
237 |
> |
|
238 |
> |
($line, $file)=$self->line(); |
239 |
> |
print "Parse Error in ".$file->url().", line ". |
240 |
> |
$line."\n"; |
241 |
> |
print $string."\n"; |
242 |
> |
die; |
243 |
> |
} |
244 |
> |
|
245 |
> |
sub checktag { |
246 |
> |
my $self=shift; |
247 |
> |
my $tagname=shift; |
248 |
> |
my $hashref=shift; |
249 |
> |
my $param=shift; |
250 |
> |
|
251 |
> |
if ( ! exists $$hashref{$param} ) { |
252 |
> |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
253 |
> |
} |
254 |
> |
} |
255 |
> |
|
256 |
> |
sub line { |
257 |
|
my $self=shift; |
140 |
– |
my $name=shift; |
141 |
– |
my $hashref=shift; |
258 |
|
|
259 |
< |
$self->{switch}->checkparam( $name, "ref"); |
260 |
< |
print "<Include> tag not yet implemented\n"; |
261 |
< |
# $self->include($$hashref{'ref'},$$hashref{'linkdoc'}); |
259 |
> |
my ($line, $fileobj)= |
260 |
> |
$self->{File}->realline($self->{currentparser}->line()); |
261 |
> |
return ($line, $fileobj); |
262 |
|
} |
263 |
|
|
264 |
< |
sub Use_Start { |
264 |
> |
sub tagstartline { |
265 |
|
my $self=shift; |
266 |
+ |
my ($line, $fileobj)=$self->{File}->line( |
267 |
+ |
$self->{currentparser}->tagstartline()); |
268 |
+ |
return ($line, $fileobj); |
269 |
+ |
} |
270 |
+ |
|
271 |
+ |
sub file { |
272 |
+ |
my $self=shift; |
273 |
+ |
|
274 |
+ |
$self->{File}->file(); |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
# --------------- Initialisation Methods --------------------------- |
278 |
+ |
|
279 |
+ |
sub init { |
280 |
+ |
# Dummy Routine - override for derived classes |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
# ------------------- Tag Routines ----------------------------------- |
284 |
+ |
# |
285 |
+ |
# Base - for setting url bases |
286 |
+ |
# |
287 |
+ |
sub Base_start { |
288 |
+ |
my $self=shift; |
289 |
|
my $name=shift; |
290 |
|
my $hashref=shift; |
291 |
|
|
292 |
< |
print "<Use> tag not yet implemented\n"; |
292 |
> |
$self->checktag($name, $hashref, 'type' ); |
293 |
> |
$self->checktag($name, $hashref, 'base' ); |
294 |
> |
|
295 |
> |
# Keep track of base tags |
296 |
> |
push @{$self->{basestack}}, $$hashref{"type"}; |
297 |
> |
# Set the base |
298 |
> |
$self->{urlhandler}->setbase($$hashref{"type"},$hashref); |
299 |
> |
|
300 |
> |
} |
301 |
> |
|
302 |
> |
sub Base_end { |
303 |
> |
my $self=shift; |
304 |
> |
my $name=shift; |
305 |
> |
my $type; |
306 |
> |
|
307 |
> |
if ( $#{$self->{basestack}} == -1 ) { |
308 |
> |
print "Parse Error : unmatched </".$name."> on line ". |
309 |
> |
$self->line()."\n"; |
310 |
> |
die; |
311 |
> |
} |
312 |
> |
else { |
313 |
> |
$type = pop @{$self->{basestack}}; |
314 |
> |
$self->{urlhandler}->unsetbase($type); |
315 |
> |
} |
316 |
> |
} |
317 |
> |
|
318 |
> |
sub Doc_Start { |
319 |
> |
my $self=shift; |
320 |
> |
my $name=shift; |
321 |
> |
my $hashref=shift; |
322 |
> |
|
323 |
> |
$self->checktag($name, $hashref, "type"); |
324 |
> |
$self->{doctypefound}++; |
325 |
> |
if ( $self->{doctypefound} == 1 ) { # only take first doctype |
326 |
> |
$self->{docobject}=$$hashref{'type'}; |
327 |
> |
} |
328 |
> |
} |
329 |
> |
|
330 |
> |
sub userinterface { |
331 |
> |
my $self=shift; |
332 |
> |
@_?$self->{userinterface}=shift |
333 |
> |
:$self->{userinterface} |
334 |
|
} |