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