1 |
|
# |
2 |
< |
# The base functionality for the ActiveDocument - inherits from Basetags |
2 |
> |
# ActiveDoc.pm |
3 |
> |
# |
4 |
> |
# Originally Written by Christopher Williams |
5 |
> |
# |
6 |
> |
# Description |
7 |
|
# |
4 |
– |
# Inherits from BaseTags |
5 |
– |
# -------- |
8 |
|
# Interface |
9 |
|
# --------- |
10 |
< |
# new(filename, DOChandler): create a new object based on a file and |
11 |
< |
# associate with a base DOChandler |
12 |
< |
# parse() : parse the input file |
13 |
< |
# include(url) : Activate include file mechanism, returns the object ref if OK |
14 |
< |
# treenode() : return the associated TreeNode object reference |
15 |
< |
# getincludeObjectStore : Return a pointer to the ObectStore that contains all |
16 |
< |
# included objects |
17 |
< |
# find(string) : find the object reference related to string in the associated |
18 |
< |
# tree. Mechanism for getting object references |
19 |
< |
# _addgroup() : Add group functionality to document |
20 |
< |
# parseerror(String) : Report an error to the user |
21 |
< |
# userinterface() : return the default User Interface object |
22 |
< |
# checktag($hashref, param , tagname) : Check a hash returned from switcher |
23 |
< |
# for a given parameter |
10 |
> |
# new() : A new ActiveDoc object |
11 |
> |
# config([ActiveConfig]) : Set up/return Configuration for the document |
12 |
> |
# url() : Return/set the docs url - essential |
13 |
> |
# file() : Return the local filename of document |
14 |
> |
# ProcessFile() : Return the filename of PreProcessed document |
15 |
> |
# |
16 |
> |
# parent() : return the object ref of the calling parent |
17 |
> |
# getfile(url) : get a processedfile object given a url |
18 |
> |
# activatedoc(url) : Return the object ref for a doc described by the given url |
19 |
> |
# -- any parse called "init" will also be run |
20 |
> |
# userinterface() : Return the defaullt userinterface |
21 |
> |
# option(var) : return the value of the option var ( or undef ) |
22 |
> |
# |
23 |
> |
# addurltags(parse) : add the <base> tags to manage urls to parse |
24 |
> |
# -- error methods -- |
25 |
> |
# error(string) : Report an general error to the user |
26 |
> |
# parseerror(string) : Report an error during parsing a file |
27 |
> |
# line() : Return the current line number of the document |
28 |
> |
# and the ProcessedFileObj it is in |
29 |
> |
# |
30 |
|
|
31 |
|
package ActiveDoc::ActiveDoc; |
32 |
< |
require 5.001; |
33 |
< |
use ActiveDoc::DOChandler; |
34 |
< |
use ActiveDoc::TreeNode; |
35 |
< |
use ActiveDoc::UserQuery; |
36 |
< |
use ObjectStoreCont; |
37 |
< |
|
38 |
< |
@ISA = qw(ActiveDoc::BaseTags); |
39 |
< |
|
40 |
< |
# Initialise |
41 |
< |
sub _init { |
42 |
< |
my $self=shift; |
43 |
< |
my $DOChandler=shift; |
44 |
< |
my $OC=shift; |
45 |
< |
|
46 |
< |
$self->_addurl(); |
47 |
< |
$self->{urlhandler}->setcache($DOChandler->defaultcache()); |
48 |
< |
$self->{treenode}=ActiveDoc::TreeNode->new(); |
49 |
< |
$self->{dochandler}=$DOChandler; |
50 |
< |
$self->{UserQuery}=$DOChandler->{UserQuery}; |
51 |
< |
$self->{tags}->addtag("Use", \&Use_Start, "", ""); |
52 |
< |
# Add the minimal functionality tag - feel free to override |
53 |
< |
$self->{tags}->addtag("Include", \&Include_Start, "", ""); |
54 |
< |
$self->init(); |
32 |
> |
require 5.004; |
33 |
> |
use ActiveDoc::SimpleDoc; |
34 |
> |
use URL::URLhandler; |
35 |
> |
|
36 |
> |
@ISA = qw(ActiveDoc::SimpleDoc); |
37 |
> |
|
38 |
> |
sub addurltags { |
39 |
> |
my $self=shift; |
40 |
> |
my $parselabel=shift; |
41 |
> |
|
42 |
> |
$self->{parsers}{$parselabel}-> |
43 |
> |
addtag("Base", \&Base_start, $self, "", $self, |
44 |
> |
\&Base_end, $self); |
45 |
> |
} |
46 |
> |
|
47 |
> |
sub url { |
48 |
> |
my $self=shift; |
49 |
> |
# get file & preprocess |
50 |
> |
if ( @_ ) { |
51 |
> |
$self->{File}=$self->getfile(shift); |
52 |
> |
$self->verbose("url downloaded to $self->{File}"); |
53 |
> |
} |
54 |
> |
if ( defined $self->{File} ) { |
55 |
> |
return $self->{File}->url(); |
56 |
> |
} |
57 |
> |
else { return "undefined"; } |
58 |
|
} |
59 |
|
|
60 |
< |
sub init { |
61 |
< |
# Dummy Routine - override for derrived classes |
60 |
> |
sub config { |
61 |
> |
my $self=shift; |
62 |
> |
@_?$self->{ActiveConfig}=shift |
63 |
> |
: $self->{ActiveConfig}; |
64 |
|
} |
65 |
< |
# |
66 |
< |
# use mechanism |
67 |
< |
# |
68 |
< |
sub include { |
69 |
< |
my $self=shift; |
70 |
< |
my $url=shift; |
71 |
< |
my $linkfile=shift; |
72 |
< |
my $filename; |
73 |
< |
my $obj; |
74 |
< |
|
75 |
< |
$file=$self->{urlhandler}->get($url); |
63 |
< |
if ( ( defined $linkfile) && ( $linkfile ne "" ) ) { |
64 |
< |
$filename=$file."/".$linkfile; |
65 |
> |
|
66 |
> |
sub getfile { |
67 |
> |
my $self=shift; |
68 |
> |
my $origurl=shift; |
69 |
> |
|
70 |
> |
my $fileref; |
71 |
> |
my ($url, $file); |
72 |
> |
if ( (defined ($it=$self->option('url_update'))) && |
73 |
> |
( $it eq "1" || $origurl=~/^$it/ )) { |
74 |
> |
$self->verbose("Forced download of $origurl"); |
75 |
> |
($url, $file)=$self->{urlhandler}->download($origurl); |
76 |
|
} |
77 |
|
else { |
78 |
< |
$filename=$file; |
78 |
> |
$self->verbose("Attempting to get $origurl"); |
79 |
> |
($url, $file)=$self->{urlhandler}->get($origurl); |
80 |
|
} |
81 |
< |
$obj=$self->{dochandler}->newdoc($filename); |
82 |
< |
|
83 |
< |
# Now Extend our tree |
84 |
< |
$self->{treenode}->grow($obj->treenode()); |
85 |
< |
return $obj; |
81 |
> |
# do we already have an appropriate object? |
82 |
> |
($fileref)=$self->config()->find($url); |
83 |
> |
#undef $fileref; |
84 |
> |
if ( defined $fileref ) { |
85 |
> |
$self->verbose("Found $url in database"); |
86 |
> |
$fileref->update(); |
87 |
> |
} |
88 |
> |
else { |
89 |
> |
if ( $file eq "" ) { |
90 |
> |
$self->parseerror("Unable to get $origurl"); |
91 |
> |
} |
92 |
> |
#-- set up a new preprocess file |
93 |
> |
$self->verbose("Making a new preprocessed file $url"); |
94 |
> |
$fileref=ActiveDoc::PreProcessedFile->new($self->config()); |
95 |
> |
$fileref->url($url); |
96 |
> |
$fileref->update(); |
97 |
> |
} |
98 |
> |
return $fileref; |
99 |
|
} |
100 |
|
|
101 |
< |
sub userinterface { |
101 |
> |
sub activatedoc { |
102 |
|
my $self=shift; |
103 |
< |
return $self->{dochandler}->{UserInterface}; |
103 |
> |
my $url=shift; |
104 |
> |
|
105 |
> |
# first get a preprocessed copy of the file |
106 |
> |
# my $fileob=$self->getfile($url); |
107 |
> |
|
108 |
> |
# now parse it for the <DocType> tag |
109 |
> |
my $tempdoc=ActiveDoc::ActiveDoc->new($self->config()); |
110 |
> |
$tempdoc->{urlhandler}=$self->{urlhandler}; |
111 |
> |
my $fullurl=$tempdoc->url($url); |
112 |
> |
$url=$fullurl; |
113 |
> |
$tempdoc->{doctypefound}=0; |
114 |
> |
$tempdoc->newparse("doctype"); |
115 |
> |
$tempdoc->addtag("doctype","Doc", \&Doc_Start, $tempdoc, |
116 |
> |
"", $tempdoc, "", $tempdoc); |
117 |
> |
$tempdoc->parse("doctype"); |
118 |
> |
|
119 |
> |
if ( ! defined $tempdoc->{docobject} ) { |
120 |
> |
print "No <Doc type=> Specified in ".$url."\n"; |
121 |
> |
exit 1; |
122 |
> |
} |
123 |
> |
# Set up a new object of the specified type |
124 |
> |
eval "require $tempdoc->{docobject}"; |
125 |
> |
die $@ if $@; |
126 |
> |
my $newobj=$tempdoc->{docobject}->new($self->config()); |
127 |
> |
undef $tempdoc; |
128 |
> |
$newobj->url($url); |
129 |
> |
$newobj->parent($self); |
130 |
> |
return $newobj; |
131 |
|
} |
132 |
|
|
133 |
< |
sub treenode { |
133 |
> |
sub parent { |
134 |
|
my $self=shift; |
135 |
< |
return $self->{treenode}; |
135 |
> |
|
136 |
> |
@_?$self->{parent}=shift |
137 |
> |
:$self->{parent}; |
138 |
|
} |
139 |
|
|
140 |
< |
sub getincludeObjectStore { |
140 |
> |
# -------- Error Handling and Error services -------------- |
141 |
> |
|
142 |
> |
sub error { |
143 |
|
my $self=shift; |
144 |
< |
return $self->{includeOS}; |
144 |
> |
my $string=shift; |
145 |
> |
|
146 |
> |
die $string."\n"; |
147 |
|
} |
148 |
|
|
149 |
< |
sub find($) { |
150 |
< |
my $self=shift; |
151 |
< |
my $string=shift; |
94 |
< |
my $tn; |
149 |
> |
sub parseerror { |
150 |
> |
my $self=shift; |
151 |
> |
my $string=shift; |
152 |
|
|
153 |
< |
$tn=$self->{treenode}->find($string); |
154 |
< |
if ( $tn eq "" ) { |
155 |
< |
$self->parseerror("Unable to find $string"); |
153 |
> |
if ( $self->currentparsename() eq "" ) { |
154 |
> |
$self->error($string); |
155 |
> |
} |
156 |
> |
else { |
157 |
> |
($line, $file)=$self->line(); |
158 |
> |
print "Parse Error in ".$file->url().", line ". |
159 |
> |
$line."\n"; |
160 |
> |
print $string."\n"; |
161 |
> |
exit; |
162 |
|
} |
100 |
– |
return $tn->associate(); |
163 |
|
} |
164 |
|
|
165 |
|
sub line { |
166 |
|
my $self=shift; |
167 |
< |
return $self->{switch}->line(); |
167 |
> |
|
168 |
> |
my ($line, $fileobj)= |
169 |
> |
$self->{File}->realline($self->{currentparser}->line()); |
170 |
> |
return ($line, $fileobj); |
171 |
|
} |
172 |
|
|
173 |
< |
sub error { |
173 |
> |
sub tagstartline { |
174 |
|
my $self=shift; |
175 |
< |
my $string=shift; |
175 |
> |
my ($line, $fileobj)=$self->{File}->line( |
176 |
> |
$self->{currentparser}->tagstartline()); |
177 |
> |
return ($line, $fileobj); |
178 |
> |
} |
179 |
|
|
180 |
< |
die $string."\n"; |
180 |
> |
sub file { |
181 |
> |
my $self=shift; |
182 |
|
|
183 |
+ |
$self->{File}->file(); |
184 |
|
} |
185 |
< |
sub parseerror { |
185 |
> |
|
186 |
> |
sub ProcessFile { |
187 |
|
my $self=shift; |
117 |
– |
my $string=shift; |
188 |
|
|
189 |
< |
print "Parse Error in $self->{url}, line ". |
120 |
< |
$self->line()."\n"; |
121 |
< |
print $string."\n"; |
122 |
< |
die; |
189 |
> |
return $self->{File}->ProcessedFile(); |
190 |
|
} |
191 |
|
|
192 |
< |
sub checktag { |
126 |
< |
my $self=shift; |
127 |
< |
my $hashref=shift; |
128 |
< |
my $param=shift; |
129 |
< |
my $tagname=shift; |
192 |
> |
# --------------- Initialisation Methods --------------------------- |
193 |
|
|
194 |
< |
if ( ! exists $$hashref{$param} ) { |
195 |
< |
$self->parseerror("Incomplete Tag <$tagname> : $param required"); |
133 |
< |
} |
194 |
> |
sub init { |
195 |
> |
# Dummy Routine - override for derived classes |
196 |
|
} |
197 |
|
|
198 |
< |
# ------------------------ Tag Routines ------------------------------ |
198 |
> |
# ------------------- Tag Routines ----------------------------------- |
199 |
|
# |
200 |
< |
# The Include tag |
200 |
> |
# Base - for setting url bases |
201 |
|
# |
202 |
+ |
sub Base_start { |
203 |
+ |
my $self=shift; |
204 |
+ |
my $name=shift; |
205 |
+ |
my $hashref=shift; |
206 |
|
|
207 |
< |
sub Include_Start { |
208 |
< |
my $self=shift; |
209 |
< |
my $name=shift; |
210 |
< |
my $hashref=shift; |
211 |
< |
|
212 |
< |
$self->{switch}->checkparam( $name, "ref"); |
213 |
< |
print "<Include> tag not yet implemented\n"; |
148 |
< |
# $self->include($$hashref{'ref'},$$hashref{'linkdoc'}); |
207 |
> |
$self->checktag($name, $hashref, 'type' ); |
208 |
> |
$self->checktag($name, $hashref, 'base' ); |
209 |
> |
|
210 |
> |
# Keep track of base tags |
211 |
> |
push @{$self->{basestack}}, $$hashref{"type"}; |
212 |
> |
# Set the base |
213 |
> |
$self->{urlhandler}->setbase($$hashref{"type"},$hashref); |
214 |
|
} |
215 |
|
|
216 |
< |
sub Use_Start { |
217 |
< |
my $self=shift; |
216 |
> |
sub Base_end { |
217 |
> |
my $self=shift; |
218 |
|
my $name=shift; |
219 |
< |
my $hashref=shift; |
219 |
> |
my $type; |
220 |
|
|
221 |
< |
print "<Use> tag not yet implemented\n"; |
221 |
> |
if ( $#{$self->{basestack}} == -1 ) { |
222 |
> |
$self->parseerror("Parse Error : unmatched </$name>"); |
223 |
> |
} |
224 |
> |
else { |
225 |
> |
$type = pop @{$self->{basestack}}; |
226 |
> |
$self->{urlhandler}->unsetbase($type); |
227 |
> |
} |
228 |
> |
} |
229 |
> |
|
230 |
> |
sub Doc_Start { |
231 |
> |
my $self=shift; |
232 |
> |
my $name=shift; |
233 |
> |
my $hashref=shift; |
234 |
> |
|
235 |
> |
$self->checktag($name, $hashref, "type"); |
236 |
> |
$self->{doctypefound}++; |
237 |
> |
if ( $self->{doctypefound} == 1 ) { # only take first doctype |
238 |
> |
$self->{docobject}=$$hashref{'type'}; |
239 |
> |
} |
240 |
|
} |