1 |
#
|
2 |
# ActiveDoc.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
#
|
8 |
# Interface
|
9 |
# ---------
|
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.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::UserInterface_basic->new();
|
57 |
$self->init(@_);
|
58 |
return $self;
|
59 |
}
|
60 |
|
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 |
print "Cannot parse - file not known\n";
|
74 |
}
|
75 |
}
|
76 |
|
77 |
sub newparse {
|
78 |
my $self=shift;
|
79 |
my $parselabel=shift;
|
80 |
|
81 |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
|
82 |
$self->{parsers}{$parselabel}->addignoretags();
|
83 |
$self->{parsers}{$parselabel}->addgrouptags();
|
84 |
}
|
85 |
|
86 |
sub addtag {
|
87 |
my $self=shift;
|
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 addurltags {
|
97 |
my $self=shift;
|
98 |
my $parselabel=shift;
|
99 |
|
100 |
$self->{parsers}{$parselabel}->
|
101 |
addtag("Base", \&Base_start, $self, "", $self,
|
102 |
\&Base_end, $self);
|
103 |
}
|
104 |
|
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 copydocconfig {
|
113 |
my $self=shift;
|
114 |
my $ActiveDoc=shift;
|
115 |
|
116 |
$self->config($ActiveDoc->config());
|
117 |
|
118 |
}
|
119 |
|
120 |
sub copydocquery {
|
121 |
my $self=shift;
|
122 |
my $ActiveDoc=shift;
|
123 |
|
124 |
$self->basequery($ActiveDoc->basequery());
|
125 |
}
|
126 |
|
127 |
sub config {
|
128 |
my $self=shift;
|
129 |
@_?$self->{ActiveConfig}=shift
|
130 |
: $self->{ActiveConfig};
|
131 |
}
|
132 |
|
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 |
|
145 |
sub getfile() {
|
146 |
my $self=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 |
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;
|
228 |
|
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 |
# --------------- Initialisation Methods ---------------------------
|
248 |
|
249 |
sub init {
|
250 |
# Dummy Routine - override for derived classes
|
251 |
}
|
252 |
|
253 |
# ------------------- Tag Routines -----------------------------------
|
254 |
#
|
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 |
}
|
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->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 userinterface {
|
301 |
my $self=shift;
|
302 |
@_?$self->{userinterface}=shift
|
303 |
:$self->{userinterface}
|
304 |
}
|