ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.12
Committed: Fri Jan 14 18:01:03 2000 UTC (25 years, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.11: +30 -0 lines
Log Message:
add includeparse method

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