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 |
|
|
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 |
williamc |
1.1 |
}
|
68 |
|
|
|
69 |
williamc |
1.6 |
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 |
williamc |
1.2 |
}
|
77 |
williamc |
1.6 |
|
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 |
williamc |
1.4 |
}
|
85 |
williamc |
1.6 |
$self->{parsers}{$parselabel}->addtag(@_);
|
86 |
|
|
}
|
87 |
williamc |
1.2 |
|
88 |
williamc |
1.6 |
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 |
williamc |
1.1 |
}
|
96 |
|
|
|
97 |
williamc |
1.6 |
sub url {
|
98 |
williamc |
1.2 |
my $self=shift;
|
99 |
williamc |
1.6 |
@_ ?$self->{File}=$self->getfile(shift)
|
100 |
|
|
: $self->{File};
|
101 |
williamc |
1.2 |
}
|
102 |
|
|
|
103 |
williamc |
1.6 |
sub copydocconfig {
|
104 |
williamc |
1.1 |
my $self=shift;
|
105 |
williamc |
1.6 |
my $ActiveDoc=shift;
|
106 |
|
|
|
107 |
|
|
$self->config($ActiveDoc->config());
|
108 |
|
|
|
109 |
williamc |
1.1 |
}
|
110 |
|
|
|
111 |
williamc |
1.6 |
sub copydocquery {
|
112 |
|
|
my $self=shift;
|
113 |
|
|
my $ActiveDoc=shift;
|
114 |
|
|
|
115 |
|
|
$self->basequery($ActiveDoc->basequery());
|
116 |
williamc |
1.1 |
}
|
117 |
|
|
|
118 |
williamc |
1.6 |
sub config {
|
119 |
williamc |
1.1 |
my $self=shift;
|
120 |
williamc |
1.6 |
@_?$self->{ActiveConfig}=shift
|
121 |
|
|
: $self->{ActiveConfig};
|
122 |
|
|
}
|
123 |
williamc |
1.1 |
|
124 |
williamc |
1.6 |
sub basequery {
|
125 |
|
|
my $self=shift;
|
126 |
|
|
@_ ? $self->{UserQuery}=shift
|
127 |
|
|
: $self->{UserQuery};
|
128 |
williamc |
1.2 |
}
|
129 |
|
|
|
130 |
williamc |
1.6 |
sub getfile() {
|
131 |
williamc |
1.3 |
my $self=shift;
|
132 |
williamc |
1.6 |
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 |
williamc |
1.3 |
}
|
158 |
|
|
|
159 |
williamc |
1.6 |
# -------- Error Handling and Error services --------------
|
160 |
|
|
|
161 |
williamc |
1.3 |
sub error {
|
162 |
williamc |
1.6 |
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 |
williamc |
1.3 |
|
185 |
williamc |
1.6 |
if ( ! exists $$hashref{$param} ) {
|
186 |
|
|
$self->parseerror("Incomplete Tag <$tagname> : $param required");
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
williamc |
1.3 |
|
190 |
williamc |
1.6 |
sub line {
|
191 |
|
|
$self=shift;
|
192 |
|
|
my ($line, $fileobj)=
|
193 |
|
|
$self->{Processedfile}->line($self->{switch}->line());
|
194 |
|
|
return ($line, $fileobj);
|
195 |
williamc |
1.3 |
}
|
196 |
williamc |
1.6 |
|
197 |
|
|
sub file {
|
198 |
williamc |
1.2 |
my $self=shift;
|
199 |
|
|
|
200 |
williamc |
1.6 |
$self->{PPf}->file();
|
201 |
williamc |
1.2 |
}
|
202 |
|
|
|
203 |
williamc |
1.6 |
# --------------- Initialisation Methods ---------------------------
|
204 |
|
|
|
205 |
|
|
sub preprocess_init {
|
206 |
williamc |
1.2 |
my $self=shift;
|
207 |
williamc |
1.6 |
$self->{PPfile}=PreProcessedFile->new($self->config());
|
208 |
|
|
}
|
209 |
williamc |
1.2 |
|
210 |
williamc |
1.6 |
sub init {
|
211 |
|
|
# Dummy Routine - override for derived classes
|
212 |
williamc |
1.1 |
}
|
213 |
|
|
|
214 |
williamc |
1.6 |
# ------------------- Tag Routines -----------------------------------
|
215 |
williamc |
1.1 |
#
|
216 |
williamc |
1.6 |
# Base - for setting url bases
|
217 |
williamc |
1.1 |
#
|
218 |
williamc |
1.6 |
sub Base_start {
|
219 |
|
|
my $self=shift;
|
220 |
|
|
my $name=shift;
|
221 |
|
|
my $hashref=shift;
|
222 |
williamc |
1.2 |
|
223 |
williamc |
1.6 |
$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 |
williamc |
1.2 |
|
231 |
williamc |
1.1 |
}
|
232 |
|
|
|
233 |
williamc |
1.6 |
sub Base_end {
|
234 |
|
|
my $self=shift;
|
235 |
williamc |
1.1 |
my $name=shift;
|
236 |
williamc |
1.6 |
my $type;
|
237 |
williamc |
1.1 |
|
238 |
williamc |
1.6 |
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 |
williamc |
1.1 |
}
|