ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.14
Committed: Tue Jan 18 18:30:10 2000 UTC (25 years, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.13: +65 -19 lines
Log Message:
Debug activatedoc method - change inti slightly

File Contents

# User Rev Content
1 williamc 1.1 #
2 williamc 1.6 # ActiveDoc.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7 williamc 1.1 #
8     # Interface
9     # ---------
10 williamc 1.6 # 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 williamc 1.9 # checktag(tagname, hashref, param) : check for existence of param in
19     # hashref from a tag call
20 williamc 1.12 # includeparse(local_parsename, objparsename, activedoc) : copy the parse from
21     # one object to another
22     # currentparsename([name]) : get/set current parse name
23 williamc 1.6 # newdoc(file) : Return an new object of the appropriate type
24     # getfile(url) : get a processedfile object given a url
25 williamc 1.9 # activatedoc(url) : Return the object ref for a doc described by the given url
26 williamc 1.14 # -- any parse called "init" will also be run
27 williamc 1.6 # config([ActiveConfig]) : Set up/return Configuration for the document
28     # basequery([ActiveConfig]) : Set up/return UserQuery for the doc
29     # copydocconfig(ActiveDoc) : Copy the basic configuration from the ActiveDoc
30     # copydocquery(ActiveDoc) : Copy the basicquery from the ActiveDoc
31 williamc 1.10 # userinterface() : Return the defaullt userinterface
32 williamc 1.14 # option(var) : return the value of the option var
33     # requestoption("message") : Ask the user to supply a value for an option
34     # if it dosnt already exist
35 williamc 1.6 #
36     # -- error methods --
37     # error(string) : Report an general error to the user
38     # parseerror(string) : Report an error during parsing a file
39     # line() : Return the current line number of the document
40     # and the ProcessedFileObj it is in
41 williamc 1.2
42     package ActiveDoc::ActiveDoc;
43 williamc 1.6 require 5.004;
44     use ActiveDoc::Parse;
45     use ActiveDoc::ActiveConfig;
46     use ActiveDoc::PreProcessedFile;
47 williamc 1.10 use ObjectUtilities::StorableObject;
48 williamc 1.6 use URL::URLhandler;
49    
50 williamc 1.10 @ISA = qw(ObjectUtilities::StorableObject);
51 williamc 1.6
52     sub new {
53     my $class=shift;
54     $self={};
55     bless $self, $class;
56     $self->config(shift);
57 williamc 1.14
58     # --- is there a starter document?
59     my $basedoc=$self->config()->basedoc();
60     if ( defined $basedoc ) {
61     $self->copydocquery($basedoc);
62     }
63     else {
64     $self->error("Error : No base doc found");
65     }
66     $self->_init2();
67     }
68    
69     sub _init2 {
70    
71     my $self=shift;
72 williamc 1.6 # A URL handler per document
73     $self->{urlhandler}=URL::URLhandler->new($self->config()->cache());
74    
75 williamc 1.10 # A default UserInterface
76 williamc 1.11 $self->{userinterface}=ActiveDoc::SimpleUserInterface->new();
77 williamc 1.6 $self->init(@_);
78     return $self;
79 williamc 1.14
80 williamc 1.6 }
81    
82     # ----- parse related routines --------------
83     sub parse {
84     my $self=shift;
85     $parselabel=shift;
86    
87     my $file=$self->file();
88     if ( $file ) {
89 williamc 1.14 if ( exists $self->{parsers}{$parselabel} ) {
90     $self->{currentparsename}=$parselabel;
91     $self->{currentparser}=$self->{parsers}{$parselabel};
92     $self->{parsers}{$parselabel}->parse($file,@_);
93     delete $self->{currentparser};
94     $self->{currentparsename}="";
95     }
96 williamc 1.6 }
97     else {
98     print "Cannot parse - file not known\n";
99     }
100 williamc 1.1 }
101    
102 williamc 1.12 sub currentparsename {
103     my $self=shift;
104     @_?$self->{currentparsename}=shift
105     :$self->{currentparsename};
106     }
107    
108 williamc 1.6 sub newparse {
109     my $self=shift;
110     my $parselabel=shift;
111    
112     $self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
113     $self->{parsers}{$parselabel}->addignoretags();
114     $self->{parsers}{$parselabel}->addgrouptags();
115 williamc 1.12 }
116    
117 williamc 1.13 sub cleartags {
118     my $self=shift;
119     my $parselabel=shift;
120    
121     $self->{parsers}{$parselabel}->cleartags();
122     }
123    
124    
125 williamc 1.12 sub includeparse {
126     my $self=shift;
127     my $parselabel=shift;
128     my $remoteparselabel=shift;
129     my $activedoc=shift;
130    
131     # Some error trapping
132     if ( ! exists $self->{parsers}{$parselabel} ) {
133     $self->error("Unknown local parse name specified");
134     }
135     if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) {
136     $self->error("Unknown parse name specified in remote obj $activedoc");
137     }
138    
139     #
140     my $rp=$activedoc->{parsers}{$remoteparselabel};
141     $self->{parsers}{$parselabel}->includeparse($rp);
142 williamc 1.2 }
143 williamc 1.6
144     sub addtag {
145     my $self=shift;
146     my $parselabel=shift;
147     if ( $#_ != 6 ) {
148     $self->error("Incorrect addtags specification\n".
149     "called with :\n@_ \n");
150 williamc 1.4 }
151 williamc 1.6 $self->{parsers}{$parselabel}->addtag(@_);
152     }
153 williamc 1.2
154 williamc 1.6 sub addurltags {
155     my $self=shift;
156     my $parselabel=shift;
157    
158     $self->{parsers}{$parselabel}->
159     addtag("Base", \&Base_start, $self, "", $self,
160     \&Base_end, $self);
161 williamc 1.1 }
162    
163 williamc 1.6 sub url {
164 williamc 1.2 my $self=shift;
165 williamc 1.8 # get file & preprocess
166     if ( @_ ) {$self->{File}=$self->getfile(shift)}
167     $self->{File}->url();
168 williamc 1.2 }
169    
170 williamc 1.6 sub copydocconfig {
171 williamc 1.1 my $self=shift;
172 williamc 1.6 my $ActiveDoc=shift;
173    
174     $self->config($ActiveDoc->config());
175    
176 williamc 1.1 }
177    
178 williamc 1.6 sub copydocquery {
179     my $self=shift;
180     my $ActiveDoc=shift;
181    
182 williamc 1.14 $self->basequery($ActiveDoc->basequery());
183 williamc 1.1 }
184    
185 williamc 1.6 sub config {
186 williamc 1.1 my $self=shift;
187 williamc 1.6 @_?$self->{ActiveConfig}=shift
188     : $self->{ActiveConfig};
189     }
190 williamc 1.1
191 williamc 1.6 sub basequery {
192     my $self=shift;
193 williamc 1.10 @_ ? $self->{Query}=shift
194     : $self->{Query};
195     }
196    
197 williamc 1.14 sub option {
198 williamc 1.10 my $self=shift;
199     my $param=shift;
200 williamc 1.14 $self->basequery()->getparam($param);
201     }
202    
203     sub requestoption {
204     my $self=shift;
205     my $param=shift;
206     my $string=shift;
207    
208     my $par=$self->basequery()->getparam($param);
209     while ( ! defined $par ) {
210     $self->basequery()->querytype( $param, "basic");
211     $self->basequery()->querymessage( $param, $string);
212     $self->userinterface()->askuser($self->basequery());
213     $par=$self->basequery()->getparam('ActiveConfigdir');
214     }
215     return $par;
216 williamc 1.2 }
217    
218 williamc 1.6 sub getfile() {
219 williamc 1.3 my $self=shift;
220 williamc 1.6 my $origurl=shift;
221    
222     my $fileref;
223     my ($url, $file)=$self->{urlhandler}->get($origurl);
224     # do we already have an appropriate object?
225 williamc 1.7 ($fileref)=$self->config()->find($url);
226     #undef $fileref;
227 williamc 1.6 if ( defined $fileref ) {
228     print "found $url in database ----\n";
229     $fileref->update();
230     }
231     else {
232     if ( $file eq "" ) {
233     $self->parseerror("Unable to get $origurl");
234     }
235     #-- set up a new preprocess file
236     print "Making a new file $url----\n";
237     $fileref=ActiveDoc::PreProcessedFile->new($self->config());
238     $fileref->url($url);
239     $fileref->update();
240     }
241     return $fileref;
242 williamc 1.3 }
243    
244 williamc 1.9 sub activatedoc {
245     my $self=shift;
246     my $url=shift;
247    
248     # first get a preprocessed copy of the file
249 williamc 1.14 # my $fileob=$self->getfile($url);
250 williamc 1.9
251     # now parse it for the <DocType> tag
252 williamc 1.14 my $tempdoc=ActiveDoc::ActiveDoc->new($self->config());
253     $tempdoc->url($url);
254     $tempdoc->{doctypefound}=0;
255     $tempdoc->newparse("doctype");
256     $tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc,
257     "", $tempdoc, "", $tempdoc);
258     $tempdoc->parse("doctype");
259 williamc 1.9
260 williamc 1.14 if ( ! defined $tempdoc->{docobject} ) {
261     print "No <Doc type=> Specified in ".$url."\n";
262 williamc 1.9 exit 1;
263     }
264     # Set up a new object of the specified type
265 williamc 1.14 eval "require $tempdoc->{docobject}";
266     die $@ if $@;
267     my $newobj=$tempdoc->{docobject}->new($self->config());
268     undef $tempdoc;
269 williamc 1.10 $newobj->url($url);
270 williamc 1.14 $newobj->_initparse();
271 williamc 1.9 return $newobj;
272     }
273    
274 williamc 1.14 sub _initparse {
275     my $self=shift;
276    
277     $self->parse("init");
278     }
279 williamc 1.6 # -------- Error Handling and Error services --------------
280    
281 williamc 1.3 sub error {
282 williamc 1.6 my $self=shift;
283     my $string=shift;
284    
285     die $string."\n";
286     }
287    
288     sub parseerror {
289     my $self=shift;
290     my $string=shift;
291    
292     ($line, $file)=$self->line();
293     print "Parse Error in ".$file->url().", line ".
294     $line."\n";
295     print $string."\n";
296     die;
297     }
298    
299     sub checktag {
300     my $self=shift;
301     my $tagname=shift;
302     my $hashref=shift;
303     my $param=shift;
304 williamc 1.3
305 williamc 1.6 if ( ! exists $$hashref{$param} ) {
306     $self->parseerror("Incomplete Tag <$tagname> : $param required");
307     }
308     }
309 williamc 1.3
310 williamc 1.6 sub line {
311 williamc 1.7 my $self=shift;
312 williamc 1.9
313 williamc 1.6 my ($line, $fileobj)=
314 williamc 1.9 $self->{File}->realline($self->{currentparser}->line());
315 williamc 1.6 return ($line, $fileobj);
316 williamc 1.7 }
317    
318     sub tagstartline {
319     my $self=shift;
320 williamc 1.8 my ($line, $fileobj)=$self->{File}->line(
321 williamc 1.7 $self->{currentparser}->tagstartline());
322     return ($line, $fileobj);
323 williamc 1.3 }
324 williamc 1.6
325     sub file {
326 williamc 1.2 my $self=shift;
327    
328 williamc 1.8 $self->{File}->file();
329 williamc 1.2 }
330    
331 williamc 1.6 # --------------- Initialisation Methods ---------------------------
332 williamc 1.2
333 williamc 1.6 sub init {
334     # Dummy Routine - override for derived classes
335 williamc 1.1 }
336    
337 williamc 1.6 # ------------------- Tag Routines -----------------------------------
338 williamc 1.1 #
339 williamc 1.6 # Base - for setting url bases
340 williamc 1.1 #
341 williamc 1.6 sub Base_start {
342     my $self=shift;
343     my $name=shift;
344     my $hashref=shift;
345 williamc 1.2
346 williamc 1.6 $self->checktag($name, $hashref, 'type' );
347     $self->checktag($name, $hashref, 'base' );
348    
349     # Keep track of base tags
350     push @{$self->{basestack}}, $$hashref{"type"};
351     # Set the base
352     $self->{urlhandler}->setbase($$hashref{"type"},$hashref);
353 williamc 1.2
354 williamc 1.1 }
355    
356 williamc 1.6 sub Base_end {
357     my $self=shift;
358 williamc 1.1 my $name=shift;
359 williamc 1.6 my $type;
360 williamc 1.1
361 williamc 1.6 if ( $#{$self->{basestack}} == -1 ) {
362     print "Parse Error : unmatched </".$name."> on line ".
363     $self->line()."\n";
364     die;
365     }
366     else {
367     $type = pop @{$self->{basestack}};
368     $self->{urlhandler}->unsetbase($type);
369     }
370 williamc 1.9 }
371    
372     sub Doc_Start {
373     my $self=shift;
374     my $name=shift;
375     my $hashref=shift;
376    
377     $self->checktag($name, $hashref, "type");
378 williamc 1.10 $self->{doctypefound}++;
379     if ( $self->{doctypefound} == 1 ) { # only take first doctype
380     $self->{docobject}=$$hashref{'type'};
381     }
382     }
383    
384     sub userinterface {
385     my $self=shift;
386     @_?$self->{userinterface}=shift
387     :$self->{userinterface}
388 williamc 1.1 }