ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/ActiveDoc.pm
Revision: 1.8
Committed: Tue Nov 23 17:31:10 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.7: +6 -10 lines
Log Message:
url -> auto generate Preprocessed file

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.8 # get file & preprocess
101     if ( @_ ) {$self->{File}=$self->getfile(shift)}
102     $self->{File}->url();
103 williamc 1.2 }
104    
105 williamc 1.6 sub copydocconfig {
106 williamc 1.1 my $self=shift;
107 williamc 1.6 my $ActiveDoc=shift;
108    
109     $self->config($ActiveDoc->config());
110    
111 williamc 1.1 }
112    
113 williamc 1.6 sub copydocquery {
114     my $self=shift;
115     my $ActiveDoc=shift;
116    
117     $self->basequery($ActiveDoc->basequery());
118 williamc 1.1 }
119    
120 williamc 1.6 sub config {
121 williamc 1.1 my $self=shift;
122 williamc 1.6 @_?$self->{ActiveConfig}=shift
123     : $self->{ActiveConfig};
124     }
125 williamc 1.1
126 williamc 1.6 sub basequery {
127     my $self=shift;
128     @_ ? $self->{UserQuery}=shift
129     : $self->{UserQuery};
130 williamc 1.2 }
131    
132 williamc 1.6 sub getfile() {
133 williamc 1.3 my $self=shift;
134 williamc 1.6 my $origurl=shift;
135    
136     my $fileref;
137     my ($url, $file)=$self->{urlhandler}->get($origurl);
138     # do we already have an appropriate object?
139 williamc 1.7 ($fileref)=$self->config()->find($url);
140     #undef $fileref;
141 williamc 1.6 if ( defined $fileref ) {
142     print "found $url in database ----\n";
143     $fileref->update();
144     }
145     else {
146     if ( $file eq "" ) {
147     $self->parseerror("Unable to get $origurl");
148     }
149     #-- set up a new preprocess file
150     print "Making a new file $url----\n";
151     $fileref=ActiveDoc::PreProcessedFile->new($self->config());
152     $fileref->url($url);
153     $fileref->update();
154     }
155     return $fileref;
156 williamc 1.3 }
157    
158 williamc 1.6 # -------- Error Handling and Error services --------------
159    
160 williamc 1.3 sub error {
161 williamc 1.6 my $self=shift;
162     my $string=shift;
163    
164     die $string."\n";
165     }
166    
167     sub parseerror {
168     my $self=shift;
169     my $string=shift;
170    
171     ($line, $file)=$self->line();
172     print "Parse Error in ".$file->url().", line ".
173     $line."\n";
174     print $string."\n";
175     die;
176     }
177    
178     sub checktag {
179     my $self=shift;
180     my $tagname=shift;
181     my $hashref=shift;
182     my $param=shift;
183 williamc 1.3
184 williamc 1.6 if ( ! exists $$hashref{$param} ) {
185     $self->parseerror("Incomplete Tag <$tagname> : $param required");
186     }
187     }
188 williamc 1.3
189 williamc 1.6 sub line {
190 williamc 1.7 my $self=shift;
191 williamc 1.6 my ($line, $fileobj)=
192 williamc 1.8 $self->{File}->line($self->{currentparser}->line());
193 williamc 1.6 return ($line, $fileobj);
194 williamc 1.7 }
195    
196     sub tagstartline {
197     my $self=shift;
198 williamc 1.8 my ($line, $fileobj)=$self->{File}->line(
199 williamc 1.7 $self->{currentparser}->tagstartline());
200     return ($line, $fileobj);
201 williamc 1.3 }
202 williamc 1.6
203     sub file {
204 williamc 1.2 my $self=shift;
205    
206 williamc 1.8 $self->{File}->file();
207 williamc 1.2 }
208    
209 williamc 1.6 # --------------- Initialisation Methods ---------------------------
210 williamc 1.2
211 williamc 1.6 sub init {
212     # Dummy Routine - override for derived classes
213 williamc 1.1 }
214    
215 williamc 1.6 # ------------------- Tag Routines -----------------------------------
216 williamc 1.1 #
217 williamc 1.6 # Base - for setting url bases
218 williamc 1.1 #
219 williamc 1.6 sub Base_start {
220     my $self=shift;
221     my $name=shift;
222     my $hashref=shift;
223 williamc 1.2
224 williamc 1.6 $self->checktag($name, $hashref, 'type' );
225     $self->checktag($name, $hashref, 'base' );
226    
227     # Keep track of base tags
228     push @{$self->{basestack}}, $$hashref{"type"};
229     # Set the base
230     $self->{urlhandler}->setbase($$hashref{"type"},$hashref);
231 williamc 1.2
232 williamc 1.1 }
233    
234 williamc 1.6 sub Base_end {
235     my $self=shift;
236 williamc 1.1 my $name=shift;
237 williamc 1.6 my $type;
238 williamc 1.1
239 williamc 1.6 if ( $#{$self->{basestack}} == -1 ) {
240     print "Parse Error : unmatched </".$name."> on line ".
241     $self->line()."\n";
242     die;
243     }
244     else {
245     $type = pop @{$self->{basestack}};
246     $self->{urlhandler}->unsetbase($type);
247     }
248 williamc 1.1 }