ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.9
Committed: Mon Nov 29 17:32:14 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.8: +38 -1 lines
Log Message:
activatdoc tested basic-level

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.6 # newdoc(file) : Return an new object of the appropriate type
21     # getfile(url) : get a processedfile object given a url
22 williamc 1.9 # activatedoc(url) : Return the object ref for a doc described by the given url
23 williamc 1.6 # 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     #
28     # -- error methods --
29     # error(string) : Report an general error to the user
30     # parseerror(string) : Report an error during parsing a file
31     # line() : Return the current line number of the document
32     # and the ProcessedFileObj it is in
33 williamc 1.2
34     package ActiveDoc::ActiveDoc;
35 williamc 1.6 require 5.004;
36     use ActiveDoc::Parse;
37     use ActiveDoc::ActiveConfig;
38     use ActiveDoc::PreProcessedFile;
39     use ObjectUtilities::ObjectBase;
40     use URL::URLhandler;
41    
42     @ISA = qw(ObjectUtilities::ObjectBase);
43    
44     sub new {
45     my $class=shift;
46     $self={};
47     bless $self, $class;
48     $self->config(shift);
49    
50     # A URL handler per document
51     $self->{urlhandler}=URL::URLhandler->new($self->config()->cache());
52    
53     $self->init(@_);
54     return $self;
55     }
56    
57     # ----- parse related routines --------------
58     sub parse {
59     my $self=shift;
60     $parselabel=shift;
61    
62     my $file=$self->file();
63     if ( $file ) {
64 williamc 1.7 $self->{currentparser}=$self->{parsers}{$parselabel};
65 williamc 1.6 $self->{parsers}{$parselabel}->parse($file,@_);
66 williamc 1.7 delete $self->{currentparser};
67 williamc 1.6 }
68     else {
69     print "Cannot parse - file not known\n";
70     }
71 williamc 1.1 }
72    
73 williamc 1.6 sub newparse {
74     my $self=shift;
75     my $parselabel=shift;
76    
77     $self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
78     $self->{parsers}{$parselabel}->addignoretags();
79     $self->{parsers}{$parselabel}->addgrouptags();
80 williamc 1.2 }
81 williamc 1.6
82     sub addtag {
83     my $self=shift;
84     my $parselabel=shift;
85     if ( $#_ != 6 ) {
86     $self->error("Incorrect addtags specification\n".
87     "called with :\n@_ \n");
88 williamc 1.4 }
89 williamc 1.6 $self->{parsers}{$parselabel}->addtag(@_);
90     }
91 williamc 1.2
92 williamc 1.6 sub addurltags {
93     my $self=shift;
94     my $parselabel=shift;
95    
96     $self->{parsers}{$parselabel}->
97     addtag("Base", \&Base_start, $self, "", $self,
98     \&Base_end, $self);
99 williamc 1.1 }
100    
101 williamc 1.6 sub url {
102 williamc 1.2 my $self=shift;
103 williamc 1.8 # get file & preprocess
104     if ( @_ ) {$self->{File}=$self->getfile(shift)}
105     $self->{File}->url();
106 williamc 1.2 }
107    
108 williamc 1.6 sub copydocconfig {
109 williamc 1.1 my $self=shift;
110 williamc 1.6 my $ActiveDoc=shift;
111    
112     $self->config($ActiveDoc->config());
113    
114 williamc 1.1 }
115    
116 williamc 1.6 sub copydocquery {
117     my $self=shift;
118     my $ActiveDoc=shift;
119    
120     $self->basequery($ActiveDoc->basequery());
121 williamc 1.1 }
122    
123 williamc 1.6 sub config {
124 williamc 1.1 my $self=shift;
125 williamc 1.6 @_?$self->{ActiveConfig}=shift
126     : $self->{ActiveConfig};
127     }
128 williamc 1.1
129 williamc 1.6 sub basequery {
130     my $self=shift;
131     @_ ? $self->{UserQuery}=shift
132     : $self->{UserQuery};
133 williamc 1.2 }
134    
135 williamc 1.6 sub getfile() {
136 williamc 1.3 my $self=shift;
137 williamc 1.6 my $origurl=shift;
138    
139     my $fileref;
140     my ($url, $file)=$self->{urlhandler}->get($origurl);
141     # do we already have an appropriate object?
142 williamc 1.7 ($fileref)=$self->config()->find($url);
143     #undef $fileref;
144 williamc 1.6 if ( defined $fileref ) {
145     print "found $url in database ----\n";
146     $fileref->update();
147     }
148     else {
149     if ( $file eq "" ) {
150     $self->parseerror("Unable to get $origurl");
151     }
152     #-- set up a new preprocess file
153     print "Making a new file $url----\n";
154     $fileref=ActiveDoc::PreProcessedFile->new($self->config());
155     $fileref->url($url);
156     $fileref->update();
157     }
158     return $fileref;
159 williamc 1.3 }
160    
161 williamc 1.9 sub activatedoc {
162     my $self=shift;
163     my $url=shift;
164    
165     # first get a preprocessed copy of the file
166     my $fileob=$self->getfile($url);
167    
168     # now parse it for the <DocType> tag
169     $self->newparse("doctype");
170     $self->addtag("doctype","Doc", \&Doc_Start, $self,
171     "", $self, "", $self);
172     $self->parse("doctype");
173    
174     if ( ! defined $self->{docobject} ) {
175     print "No <Doc type=> Specified in ".$fileob->url()."\n";
176     exit 1;
177     }
178     # Set up a new object of the specified type
179     my $newobj=$self->{docobject}->new($self->config());
180     return $newobj;
181     }
182    
183 williamc 1.6 # -------- Error Handling and Error services --------------
184    
185 williamc 1.3 sub error {
186 williamc 1.6 my $self=shift;
187     my $string=shift;
188    
189     die $string."\n";
190     }
191    
192     sub parseerror {
193     my $self=shift;
194     my $string=shift;
195    
196     ($line, $file)=$self->line();
197     print "Parse Error in ".$file->url().", line ".
198     $line."\n";
199     print $string."\n";
200     die;
201     }
202    
203     sub checktag {
204     my $self=shift;
205     my $tagname=shift;
206     my $hashref=shift;
207     my $param=shift;
208 williamc 1.3
209 williamc 1.9 print keys %$hashref;
210     print "-----------------------------------\n";
211 williamc 1.6 if ( ! exists $$hashref{$param} ) {
212     $self->parseerror("Incomplete Tag <$tagname> : $param required");
213     }
214     }
215 williamc 1.3
216 williamc 1.6 sub line {
217 williamc 1.7 my $self=shift;
218 williamc 1.9
219 williamc 1.6 my ($line, $fileobj)=
220 williamc 1.9 $self->{File}->realline($self->{currentparser}->line());
221 williamc 1.6 return ($line, $fileobj);
222 williamc 1.7 }
223    
224     sub tagstartline {
225     my $self=shift;
226 williamc 1.8 my ($line, $fileobj)=$self->{File}->line(
227 williamc 1.7 $self->{currentparser}->tagstartline());
228     return ($line, $fileobj);
229 williamc 1.3 }
230 williamc 1.6
231     sub file {
232 williamc 1.2 my $self=shift;
233    
234 williamc 1.8 $self->{File}->file();
235 williamc 1.2 }
236    
237 williamc 1.6 # --------------- Initialisation Methods ---------------------------
238 williamc 1.2
239 williamc 1.6 sub init {
240     # Dummy Routine - override for derived classes
241 williamc 1.1 }
242    
243 williamc 1.6 # ------------------- Tag Routines -----------------------------------
244 williamc 1.1 #
245 williamc 1.6 # Base - for setting url bases
246 williamc 1.1 #
247 williamc 1.6 sub Base_start {
248     my $self=shift;
249     my $name=shift;
250     my $hashref=shift;
251 williamc 1.2
252 williamc 1.6 $self->checktag($name, $hashref, 'type' );
253     $self->checktag($name, $hashref, 'base' );
254    
255     # Keep track of base tags
256     push @{$self->{basestack}}, $$hashref{"type"};
257     # Set the base
258     $self->{urlhandler}->setbase($$hashref{"type"},$hashref);
259 williamc 1.2
260 williamc 1.1 }
261    
262 williamc 1.6 sub Base_end {
263     my $self=shift;
264 williamc 1.1 my $name=shift;
265 williamc 1.6 my $type;
266 williamc 1.1
267 williamc 1.6 if ( $#{$self->{basestack}} == -1 ) {
268     print "Parse Error : unmatched </".$name."> on line ".
269     $self->line()."\n";
270     die;
271     }
272     else {
273     $type = pop @{$self->{basestack}};
274     $self->{urlhandler}->unsetbase($type);
275     }
276 williamc 1.9 }
277    
278     sub Doc_Start {
279     my $self=shift;
280     my $name=shift;
281     my $hashref=shift;
282    
283     $self->checktag($name, $hashref, "type");
284     $self->{docobject}=$$hashref{'type'};
285 williamc 1.1 }