1 |
#
|
2 |
# ActiveDoc.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
#
|
8 |
# Interface
|
9 |
# ---------
|
10 |
# 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 |
|
31 |
package ActiveDoc::ActiveDoc;
|
32 |
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 |
$self->{currentparser}=$self->{parsers}{$parselabel};
|
62 |
$self->{parsers}{$parselabel}->parse($file,@_);
|
63 |
delete $self->{currentparser};
|
64 |
}
|
65 |
else {
|
66 |
print "Cannot parse - file not known\n";
|
67 |
}
|
68 |
}
|
69 |
|
70 |
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 |
}
|
78 |
|
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 |
}
|
86 |
$self->{parsers}{$parselabel}->addtag(@_);
|
87 |
}
|
88 |
|
89 |
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 |
}
|
97 |
|
98 |
sub url {
|
99 |
my $self=shift;
|
100 |
# get file & preprocess
|
101 |
if ( @_ ) {$self->{File}=$self->getfile(shift)}
|
102 |
$self->{File}->url();
|
103 |
}
|
104 |
|
105 |
sub copydocconfig {
|
106 |
my $self=shift;
|
107 |
my $ActiveDoc=shift;
|
108 |
|
109 |
$self->config($ActiveDoc->config());
|
110 |
|
111 |
}
|
112 |
|
113 |
sub copydocquery {
|
114 |
my $self=shift;
|
115 |
my $ActiveDoc=shift;
|
116 |
|
117 |
$self->basequery($ActiveDoc->basequery());
|
118 |
}
|
119 |
|
120 |
sub config {
|
121 |
my $self=shift;
|
122 |
@_?$self->{ActiveConfig}=shift
|
123 |
: $self->{ActiveConfig};
|
124 |
}
|
125 |
|
126 |
sub basequery {
|
127 |
my $self=shift;
|
128 |
@_ ? $self->{UserQuery}=shift
|
129 |
: $self->{UserQuery};
|
130 |
}
|
131 |
|
132 |
sub getfile() {
|
133 |
my $self=shift;
|
134 |
my $origurl=shift;
|
135 |
|
136 |
my $fileref;
|
137 |
my ($url, $file)=$self->{urlhandler}->get($origurl);
|
138 |
# do we already have an appropriate object?
|
139 |
($fileref)=$self->config()->find($url);
|
140 |
#undef $fileref;
|
141 |
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 |
}
|
157 |
|
158 |
# -------- Error Handling and Error services --------------
|
159 |
|
160 |
sub error {
|
161 |
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 |
|
184 |
if ( ! exists $$hashref{$param} ) {
|
185 |
$self->parseerror("Incomplete Tag <$tagname> : $param required");
|
186 |
}
|
187 |
}
|
188 |
|
189 |
sub line {
|
190 |
my $self=shift;
|
191 |
my ($line, $fileobj)=
|
192 |
$self->{File}->line($self->{currentparser}->line());
|
193 |
return ($line, $fileobj);
|
194 |
}
|
195 |
|
196 |
sub tagstartline {
|
197 |
my $self=shift;
|
198 |
my ($line, $fileobj)=$self->{File}->line(
|
199 |
$self->{currentparser}->tagstartline());
|
200 |
return ($line, $fileobj);
|
201 |
}
|
202 |
|
203 |
sub file {
|
204 |
my $self=shift;
|
205 |
|
206 |
$self->{File}->file();
|
207 |
}
|
208 |
|
209 |
# --------------- Initialisation Methods ---------------------------
|
210 |
|
211 |
sub init {
|
212 |
# Dummy Routine - override for derived classes
|
213 |
}
|
214 |
|
215 |
# ------------------- Tag Routines -----------------------------------
|
216 |
#
|
217 |
# Base - for setting url bases
|
218 |
#
|
219 |
sub Base_start {
|
220 |
my $self=shift;
|
221 |
my $name=shift;
|
222 |
my $hashref=shift;
|
223 |
|
224 |
$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 |
|
232 |
}
|
233 |
|
234 |
sub Base_end {
|
235 |
my $self=shift;
|
236 |
my $name=shift;
|
237 |
my $type;
|
238 |
|
239 |
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 |
}
|