ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.13
Committed: Fri Jan 14 18:49:57 2000 UTC (25 years, 4 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.12: +8 -0 lines
Log Message:
New interfaces

File Contents

# Content
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 # 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.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 # ----- 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 currentparsename {
83 my $self=shift;
84 @_?$self->{currentparsename}=shift
85 :$self->{currentparsename};
86 }
87
88 sub newparse {
89 my $self=shift;
90 my $parselabel=shift;
91
92 $self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
93 $self->{parsers}{$parselabel}->addignoretags();
94 $self->{parsers}{$parselabel}->addgrouptags();
95 }
96
97 sub cleartags {
98 my $self=shift;
99 my $parselabel=shift;
100
101 $self->{parsers}{$parselabel}->cleartags();
102 }
103
104
105 sub includeparse {
106 my $self=shift;
107 my $parselabel=shift;
108 my $remoteparselabel=shift;
109 my $activedoc=shift;
110
111 # Some error trapping
112 if ( ! exists $self->{parsers}{$parselabel} ) {
113 $self->error("Unknown local parse name specified");
114 }
115 if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) {
116 $self->error("Unknown parse name specified in remote obj $activedoc");
117 }
118
119 #
120 my $rp=$activedoc->{parsers}{$remoteparselabel};
121 $self->{parsers}{$parselabel}->includeparse($rp);
122 }
123
124 sub addtag {
125 my $self=shift;
126 my $parselabel=shift;
127 if ( $#_ != 6 ) {
128 $self->error("Incorrect addtags specification\n".
129 "called with :\n@_ \n");
130 }
131 $self->{parsers}{$parselabel}->addtag(@_);
132 }
133
134 sub addurltags {
135 my $self=shift;
136 my $parselabel=shift;
137
138 $self->{parsers}{$parselabel}->
139 addtag("Base", \&Base_start, $self, "", $self,
140 \&Base_end, $self);
141 }
142
143 sub url {
144 my $self=shift;
145 # get file & preprocess
146 if ( @_ ) {$self->{File}=$self->getfile(shift)}
147 $self->{File}->url();
148 }
149
150 sub copydocconfig {
151 my $self=shift;
152 my $ActiveDoc=shift;
153
154 $self->config($ActiveDoc->config());
155
156 }
157
158 sub copydocquery {
159 my $self=shift;
160 my $ActiveDoc=shift;
161
162 $self->basequery($ActiveDoc->basequery());
163 }
164
165 sub config {
166 my $self=shift;
167 @_?$self->{ActiveConfig}=shift
168 : $self->{ActiveConfig};
169 }
170
171 sub basequery {
172 my $self=shift;
173 @_ ? $self->{Query}=shift
174 : $self->{Query};
175 }
176
177 sub options {
178 my $self=shift;
179 my $param=shift;
180 $self->basequery()->getparam('option_'.$param);
181 }
182
183 sub getfile() {
184 my $self=shift;
185 my $origurl=shift;
186
187 my $fileref;
188 my ($url, $file)=$self->{urlhandler}->get($origurl);
189 # do we already have an appropriate object?
190 ($fileref)=$self->config()->find($url);
191 #undef $fileref;
192 if ( defined $fileref ) {
193 print "found $url in database ----\n";
194 $fileref->update();
195 }
196 else {
197 if ( $file eq "" ) {
198 $self->parseerror("Unable to get $origurl");
199 }
200 #-- set up a new preprocess file
201 print "Making a new file $url----\n";
202 $fileref=ActiveDoc::PreProcessedFile->new($self->config());
203 $fileref->url($url);
204 $fileref->update();
205 }
206 return $fileref;
207 }
208
209 sub activatedoc {
210 my $self=shift;
211 my $url=shift;
212
213 # first get a preprocessed copy of the file
214 my $fileob=$self->getfile($url);
215
216 # now parse it for the <DocType> tag
217 $self->{doctypefound}=0;
218 $self->newparse("doctype");
219 $self->addtag("doctype","Doc", \&Doc_Start, $self,
220 "", $self, "", $self);
221 $self->parse("doctype");
222
223 if ( ! defined $self->{docobject} ) {
224 print "No <Doc type=> Specified in ".$fileob->url()."\n";
225 exit 1;
226 }
227 # Set up a new object of the specified type
228 my $newobj=$self->{docobject}->new($self->config());
229 $newobj->url($url);
230 return $newobj;
231 }
232
233 # -------- Error Handling and Error services --------------
234
235 sub error {
236 my $self=shift;
237 my $string=shift;
238
239 die $string."\n";
240 }
241
242 sub parseerror {
243 my $self=shift;
244 my $string=shift;
245
246 ($line, $file)=$self->line();
247 print "Parse Error in ".$file->url().", line ".
248 $line."\n";
249 print $string."\n";
250 die;
251 }
252
253 sub checktag {
254 my $self=shift;
255 my $tagname=shift;
256 my $hashref=shift;
257 my $param=shift;
258
259 if ( ! exists $$hashref{$param} ) {
260 $self->parseerror("Incomplete Tag <$tagname> : $param required");
261 }
262 }
263
264 sub line {
265 my $self=shift;
266
267 my ($line, $fileobj)=
268 $self->{File}->realline($self->{currentparser}->line());
269 return ($line, $fileobj);
270 }
271
272 sub tagstartline {
273 my $self=shift;
274 my ($line, $fileobj)=$self->{File}->line(
275 $self->{currentparser}->tagstartline());
276 return ($line, $fileobj);
277 }
278
279 sub file {
280 my $self=shift;
281
282 $self->{File}->file();
283 }
284
285 # --------------- Initialisation Methods ---------------------------
286
287 sub init {
288 # Dummy Routine - override for derived classes
289 }
290
291 # ------------------- Tag Routines -----------------------------------
292 #
293 # Base - for setting url bases
294 #
295 sub Base_start {
296 my $self=shift;
297 my $name=shift;
298 my $hashref=shift;
299
300 $self->checktag($name, $hashref, 'type' );
301 $self->checktag($name, $hashref, 'base' );
302
303 # Keep track of base tags
304 push @{$self->{basestack}}, $$hashref{"type"};
305 # Set the base
306 $self->{urlhandler}->setbase($$hashref{"type"},$hashref);
307
308 }
309
310 sub Base_end {
311 my $self=shift;
312 my $name=shift;
313 my $type;
314
315 if ( $#{$self->{basestack}} == -1 ) {
316 print "Parse Error : unmatched </".$name."> on line ".
317 $self->line()."\n";
318 die;
319 }
320 else {
321 $type = pop @{$self->{basestack}};
322 $self->{urlhandler}->unsetbase($type);
323 }
324 }
325
326 sub Doc_Start {
327 my $self=shift;
328 my $name=shift;
329 my $hashref=shift;
330
331 $self->checktag($name, $hashref, "type");
332 $self->{doctypefound}++;
333 if ( $self->{doctypefound} == 1 ) { # only take first doctype
334 $self->{docobject}=$$hashref{'type'};
335 }
336 }
337
338 sub userinterface {
339 my $self=shift;
340 @_?$self->{userinterface}=shift
341 :$self->{userinterface}
342 }