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, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.12: +8 -0 lines
Log Message:
New interfaces

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.6 # 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 williamc 1.10 # userinterface() : Return the defaullt userinterface
31     # options(var) : return the value of the option var
32 williamc 1.6 #
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 williamc 1.2
39     package ActiveDoc::ActiveDoc;
40 williamc 1.6 require 5.004;
41     use ActiveDoc::Parse;
42     use ActiveDoc::ActiveConfig;
43     use ActiveDoc::PreProcessedFile;
44 williamc 1.10 use ObjectUtilities::StorableObject;
45 williamc 1.6 use URL::URLhandler;
46    
47 williamc 1.10 @ISA = qw(ObjectUtilities::StorableObject);
48 williamc 1.6
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 williamc 1.10 # A default UserInterface
59 williamc 1.11 $self->{userinterface}=ActiveDoc::SimpleUserInterface->new();
60 williamc 1.6 $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 williamc 1.12 $self->{currentparsename}=$parselabel;
72 williamc 1.7 $self->{currentparser}=$self->{parsers}{$parselabel};
73 williamc 1.6 $self->{parsers}{$parselabel}->parse($file,@_);
74 williamc 1.7 delete $self->{currentparser};
75 williamc 1.12 $self->{currentparsename}="";
76 williamc 1.6 }
77     else {
78     print "Cannot parse - file not known\n";
79     }
80 williamc 1.1 }
81    
82 williamc 1.12 sub currentparsename {
83     my $self=shift;
84     @_?$self->{currentparsename}=shift
85     :$self->{currentparsename};
86     }
87    
88 williamc 1.6 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 williamc 1.12 }
96    
97 williamc 1.13 sub cleartags {
98     my $self=shift;
99     my $parselabel=shift;
100    
101     $self->{parsers}{$parselabel}->cleartags();
102     }
103    
104    
105 williamc 1.12 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 williamc 1.2 }
123 williamc 1.6
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 williamc 1.4 }
131 williamc 1.6 $self->{parsers}{$parselabel}->addtag(@_);
132     }
133 williamc 1.2
134 williamc 1.6 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 williamc 1.1 }
142    
143 williamc 1.6 sub url {
144 williamc 1.2 my $self=shift;
145 williamc 1.8 # get file & preprocess
146     if ( @_ ) {$self->{File}=$self->getfile(shift)}
147     $self->{File}->url();
148 williamc 1.2 }
149    
150 williamc 1.6 sub copydocconfig {
151 williamc 1.1 my $self=shift;
152 williamc 1.6 my $ActiveDoc=shift;
153    
154     $self->config($ActiveDoc->config());
155    
156 williamc 1.1 }
157    
158 williamc 1.6 sub copydocquery {
159     my $self=shift;
160     my $ActiveDoc=shift;
161    
162     $self->basequery($ActiveDoc->basequery());
163 williamc 1.1 }
164    
165 williamc 1.6 sub config {
166 williamc 1.1 my $self=shift;
167 williamc 1.6 @_?$self->{ActiveConfig}=shift
168     : $self->{ActiveConfig};
169     }
170 williamc 1.1
171 williamc 1.6 sub basequery {
172     my $self=shift;
173 williamc 1.10 @_ ? $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 williamc 1.2 }
182    
183 williamc 1.6 sub getfile() {
184 williamc 1.3 my $self=shift;
185 williamc 1.6 my $origurl=shift;
186    
187     my $fileref;
188     my ($url, $file)=$self->{urlhandler}->get($origurl);
189     # do we already have an appropriate object?
190 williamc 1.7 ($fileref)=$self->config()->find($url);
191     #undef $fileref;
192 williamc 1.6 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 williamc 1.3 }
208    
209 williamc 1.9 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 williamc 1.10 $self->{doctypefound}=0;
218 williamc 1.9 $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 williamc 1.10 $newobj->url($url);
230 williamc 1.9 return $newobj;
231     }
232    
233 williamc 1.6 # -------- Error Handling and Error services --------------
234    
235 williamc 1.3 sub error {
236 williamc 1.6 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 williamc 1.3
259 williamc 1.6 if ( ! exists $$hashref{$param} ) {
260     $self->parseerror("Incomplete Tag <$tagname> : $param required");
261     }
262     }
263 williamc 1.3
264 williamc 1.6 sub line {
265 williamc 1.7 my $self=shift;
266 williamc 1.9
267 williamc 1.6 my ($line, $fileobj)=
268 williamc 1.9 $self->{File}->realline($self->{currentparser}->line());
269 williamc 1.6 return ($line, $fileobj);
270 williamc 1.7 }
271    
272     sub tagstartline {
273     my $self=shift;
274 williamc 1.8 my ($line, $fileobj)=$self->{File}->line(
275 williamc 1.7 $self->{currentparser}->tagstartline());
276     return ($line, $fileobj);
277 williamc 1.3 }
278 williamc 1.6
279     sub file {
280 williamc 1.2 my $self=shift;
281    
282 williamc 1.8 $self->{File}->file();
283 williamc 1.2 }
284    
285 williamc 1.6 # --------------- Initialisation Methods ---------------------------
286 williamc 1.2
287 williamc 1.6 sub init {
288     # Dummy Routine - override for derived classes
289 williamc 1.1 }
290    
291 williamc 1.6 # ------------------- Tag Routines -----------------------------------
292 williamc 1.1 #
293 williamc 1.6 # Base - for setting url bases
294 williamc 1.1 #
295 williamc 1.6 sub Base_start {
296     my $self=shift;
297     my $name=shift;
298     my $hashref=shift;
299 williamc 1.2
300 williamc 1.6 $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 williamc 1.2
308 williamc 1.1 }
309    
310 williamc 1.6 sub Base_end {
311     my $self=shift;
312 williamc 1.1 my $name=shift;
313 williamc 1.6 my $type;
314 williamc 1.1
315 williamc 1.6 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 williamc 1.9 }
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 williamc 1.10 $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 williamc 1.1 }