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 |
print "Parse called on file $file\n";
|
61 |
if ( $file ) {
|
62 |
$self->{parsers}{$parselabel}->parse($file,@_);
|
63 |
}
|
64 |
else {
|
65 |
print "Cannot parse - file not known\n";
|
66 |
}
|
67 |
}
|
68 |
|
69 |
sub newparse {
|
70 |
my $self=shift;
|
71 |
my $parselabel=shift;
|
72 |
|
73 |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
|
74 |
$self->{parsers}{$parselabel}->addignoretags();
|
75 |
$self->{parsers}{$parselabel}->addgrouptags();
|
76 |
}
|
77 |
|
78 |
sub addtag {
|
79 |
my $self=shift;
|
80 |
my $parselabel=shift;
|
81 |
if ( $#_ != 6 ) {
|
82 |
$self->error("Incorrect addtags specification\n".
|
83 |
"called with :\n@_ \n");
|
84 |
}
|
85 |
$self->{parsers}{$parselabel}->addtag(@_);
|
86 |
}
|
87 |
|
88 |
sub addurltags {
|
89 |
my $self=shift;
|
90 |
my $parselabel=shift;
|
91 |
|
92 |
$self->{parsers}{$parselabel}->
|
93 |
addtag("Base", \&Base_start, $self, "", $self,
|
94 |
\&Base_end, $self);
|
95 |
}
|
96 |
|
97 |
sub url {
|
98 |
my $self=shift;
|
99 |
@_ ?$self->{File}=$self->getfile(shift)
|
100 |
: $self->{File};
|
101 |
}
|
102 |
|
103 |
sub copydocconfig {
|
104 |
my $self=shift;
|
105 |
my $ActiveDoc=shift;
|
106 |
|
107 |
$self->config($ActiveDoc->config());
|
108 |
|
109 |
}
|
110 |
|
111 |
sub copydocquery {
|
112 |
my $self=shift;
|
113 |
my $ActiveDoc=shift;
|
114 |
|
115 |
$self->basequery($ActiveDoc->basequery());
|
116 |
}
|
117 |
|
118 |
sub config {
|
119 |
my $self=shift;
|
120 |
@_?$self->{ActiveConfig}=shift
|
121 |
: $self->{ActiveConfig};
|
122 |
}
|
123 |
|
124 |
sub basequery {
|
125 |
my $self=shift;
|
126 |
@_ ? $self->{UserQuery}=shift
|
127 |
: $self->{UserQuery};
|
128 |
}
|
129 |
|
130 |
sub getfile() {
|
131 |
my $self=shift;
|
132 |
my $origurl=shift;
|
133 |
|
134 |
my $fileref;
|
135 |
print "GETFILE called\n";
|
136 |
my ($url, $file)=$self->{urlhandler}->get($origurl);
|
137 |
# do we already have an appropriate object?
|
138 |
#my ($fileref)=$self->config()->find("__preprocessed",$url);
|
139 |
undef $fileref;
|
140 |
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 |
$self->config()->store($fileref,"__preprocessed",$url);
|
154 |
}
|
155 |
print "---------- returning".$fileref."\n";
|
156 |
return $fileref;
|
157 |
}
|
158 |
|
159 |
# -------- Error Handling and Error services --------------
|
160 |
|
161 |
sub error {
|
162 |
my $self=shift;
|
163 |
my $string=shift;
|
164 |
|
165 |
die $string."\n";
|
166 |
}
|
167 |
|
168 |
sub parseerror {
|
169 |
my $self=shift;
|
170 |
my $string=shift;
|
171 |
|
172 |
($line, $file)=$self->line();
|
173 |
print "Parse Error in ".$file->url().", line ".
|
174 |
$line."\n";
|
175 |
print $string."\n";
|
176 |
die;
|
177 |
}
|
178 |
|
179 |
sub checktag {
|
180 |
my $self=shift;
|
181 |
my $tagname=shift;
|
182 |
my $hashref=shift;
|
183 |
my $param=shift;
|
184 |
|
185 |
if ( ! exists $$hashref{$param} ) {
|
186 |
$self->parseerror("Incomplete Tag <$tagname> : $param required");
|
187 |
}
|
188 |
}
|
189 |
|
190 |
sub line {
|
191 |
$self=shift;
|
192 |
my ($line, $fileobj)=
|
193 |
$self->{Processedfile}->line($self->{switch}->line());
|
194 |
return ($line, $fileobj);
|
195 |
}
|
196 |
|
197 |
sub file {
|
198 |
my $self=shift;
|
199 |
|
200 |
$self->{PPf}->file();
|
201 |
}
|
202 |
|
203 |
# --------------- Initialisation Methods ---------------------------
|
204 |
|
205 |
sub preprocess_init {
|
206 |
my $self=shift;
|
207 |
$self->{PPfile}=PreProcessedFile->new($self->config());
|
208 |
}
|
209 |
|
210 |
sub init {
|
211 |
# Dummy Routine - override for derived classes
|
212 |
}
|
213 |
|
214 |
# ------------------- Tag Routines -----------------------------------
|
215 |
#
|
216 |
# Base - for setting url bases
|
217 |
#
|
218 |
sub Base_start {
|
219 |
my $self=shift;
|
220 |
my $name=shift;
|
221 |
my $hashref=shift;
|
222 |
|
223 |
$self->checktag($name, $hashref, 'type' );
|
224 |
$self->checktag($name, $hashref, 'base' );
|
225 |
|
226 |
# Keep track of base tags
|
227 |
push @{$self->{basestack}}, $$hashref{"type"};
|
228 |
# Set the base
|
229 |
$self->{urlhandler}->setbase($$hashref{"type"},$hashref);
|
230 |
|
231 |
}
|
232 |
|
233 |
sub Base_end {
|
234 |
my $self=shift;
|
235 |
my $name=shift;
|
236 |
my $type;
|
237 |
|
238 |
if ( $#{$self->{basestack}} == -1 ) {
|
239 |
print "Parse Error : unmatched </".$name."> on line ".
|
240 |
$self->line()."\n";
|
241 |
die;
|
242 |
}
|
243 |
else {
|
244 |
$type = pop @{$self->{basestack}};
|
245 |
$self->{urlhandler}->unsetbase($type);
|
246 |
}
|
247 |
}
|