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