ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.7
Committed: Tue Nov 23 17:20:40 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.6: +13 -8 lines
Log Message:
Changes to get PreProcessor Working

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