1 |
#
|
2 |
# SimpleDoc.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
# -----------
|
8 |
# Simple multi parsing functionality and group manipulation
|
9 |
#
|
10 |
# Interface
|
11 |
# ---------
|
12 |
# new([DocVersionTag]) : A new ActiveDoc object. You can also
|
13 |
# specify an alternative doc version tag
|
14 |
# filetoparse([filename]) : Set/Return the filename of document
|
15 |
# newparse(parselabel) : Create a new parse type
|
16 |
# parse(parselabel) : Parse the document file for the given parse level
|
17 |
# addtag(parselabel,tagname,start,obj,[text,obj,end,obj]) :
|
18 |
# Add tags to the parse given by label
|
19 |
# grouptag(tagname, parselabel) : Allow a tag to switch context
|
20 |
# - if not you can never turn a context off!
|
21 |
# checktag(tagname, hashref, param) : check for existence of param in
|
22 |
# hashref from a tag call
|
23 |
# includeparse(local_parsename, objparsename, activedoc) : copy the parse from
|
24 |
# one object to another
|
25 |
# currentparser() : return the current parser object
|
26 |
# currentparsename([name]) : get/set current parse name
|
27 |
#
|
28 |
# addignoretags(parsename) : add <ignore> </igonore> tags funtionality to the
|
29 |
# specified parse
|
30 |
# opengroup(name) : declare a group to be open
|
31 |
# closegroup(name) : declare a group to be closed
|
32 |
# allowgroup(name,parse) : allow a group so named
|
33 |
# disallowgroup(name,parse) : disallow the named group
|
34 |
# restoregroup(name,parse) : restore group access setting (that before last change)
|
35 |
# doctype() : return the (type,version) of the document
|
36 |
# as specified by the DocVersionTag
|
37 |
# filenameref(string) : A string to refer to the file in parse error messages
|
38 |
# etc. Default is filetoparse
|
39 |
# --------------- Error handling routines ---------------
|
40 |
# verbose(string) : Print string in verbosity mode
|
41 |
# verbosity(0|1) : verbosity off|on
|
42 |
# line() : return the current line number in the current parse
|
43 |
# tagstartline() : return the line number where the current tag was
|
44 |
# opened
|
45 |
# parseerror(string) : print error and associate with line number etc.
|
46 |
# error(string) : handle an error
|
47 |
|
48 |
package ActiveDoc::SimpleDoc;
|
49 |
require 5.004;
|
50 |
use ActiveDoc::Parse;
|
51 |
|
52 |
sub new {
|
53 |
my $class=shift;
|
54 |
$self={};
|
55 |
bless $self, $class;
|
56 |
$self->_initdoc("doc",@_);
|
57 |
return $self;
|
58 |
}
|
59 |
|
60 |
sub doctype {
|
61 |
my $self=shift;
|
62 |
my $rv=1;
|
63 |
|
64 |
undef $self->{docversion};
|
65 |
undef $self->{doctype};
|
66 |
$self->parse("doc");
|
67 |
return ($self->{doctype},$self->{docversion});
|
68 |
}
|
69 |
|
70 |
sub filenameref {
|
71 |
my $self=shift;
|
72 |
if ( @_ ) {
|
73 |
$self->{filenameref}=shift;
|
74 |
}
|
75 |
return (defined $self->{filenameref})?$self->{filenameref}
|
76 |
:$self->filetoparse();
|
77 |
}
|
78 |
|
79 |
sub _initdoc {
|
80 |
my $self=shift;
|
81 |
my $parsename=shift;
|
82 |
|
83 |
$self->{doctag}="DOC";
|
84 |
if ( @_ ) {
|
85 |
$self->{doctag}=shift;
|
86 |
}
|
87 |
$self->newparse($parsename);
|
88 |
$self->addtag($parsename,$self->{doctag},\&Doc_Start, $self);
|
89 |
}
|
90 |
|
91 |
sub verbosity {
|
92 |
my $self=shift;
|
93 |
$self->{verbose}=shift;
|
94 |
}
|
95 |
|
96 |
sub verbose {
|
97 |
my $self=shift;
|
98 |
my $string=shift;
|
99 |
|
100 |
if ( $self->{verbose} ) {
|
101 |
print ">".ref($self)."($self) : \n->".$string."\n";
|
102 |
}
|
103 |
}
|
104 |
|
105 |
# ----- parse related routines --------------
|
106 |
sub parse {
|
107 |
my $self=shift;
|
108 |
$parselabel=shift;
|
109 |
my $file=$self->filetoparse();
|
110 |
|
111 |
if ( -f $file ) {
|
112 |
if ( exists $self->{parsers}{$parselabel} ) {
|
113 |
$self->verbose("Parsing $parselabel in file $file");
|
114 |
$self->{currentparsename}=$parselabel;
|
115 |
$self->{currentparser}=$self->{parsers}{$parselabel};
|
116 |
$self->{parsers}{$parselabel}->parse($file,@_);
|
117 |
delete $self->{currentparser};
|
118 |
$self->{currentparsename}="";
|
119 |
$self->verbose("Parse $parselabel Complete");
|
120 |
}
|
121 |
}
|
122 |
else {
|
123 |
$self->error("Cannot parse \"$parselabel\" - file $file not known");
|
124 |
}
|
125 |
}
|
126 |
|
127 |
sub parsefilelist
|
128 |
{
|
129 |
my $self=shift;
|
130 |
my $parselabel=shift;
|
131 |
my ($filenames)=@_;
|
132 |
|
133 |
if ( exists $self->{parsers}{$parselabel} )
|
134 |
{
|
135 |
$self->verbose("ParsingFileList: Label = $parselabel (files = ".join(",",@$filenames)." ");
|
136 |
$self->{currentparsename}=$parselabel;
|
137 |
$self->{currentparser}=$self->{parsers}{$parselabel};
|
138 |
$self->{parsers}{$parselabel}->parsefilelist($filenames);
|
139 |
delete $self->{currentparser};
|
140 |
$self->{currentparsename}="";
|
141 |
$self->verbose("ParseFileList $parselabel Complete");
|
142 |
}
|
143 |
else
|
144 |
{
|
145 |
$self->error("Cannot parse \"$parselabel\" - Unknown parser!!");
|
146 |
}
|
147 |
}
|
148 |
|
149 |
sub currentparsename {
|
150 |
my $self=shift;
|
151 |
@_?$self->{currentparsename}=shift
|
152 |
:(defined $self->{currentparsename}?$self->{currentparsename}:"");
|
153 |
}
|
154 |
|
155 |
sub currentparser {
|
156 |
my $self=shift;
|
157 |
return $self->{currentparser};
|
158 |
}
|
159 |
|
160 |
|
161 |
sub newparse {
|
162 |
my $self=shift;
|
163 |
my $parselabel=shift;
|
164 |
|
165 |
$self->{parsers}{$parselabel}=ActiveDoc::Parse->new();
|
166 |
}
|
167 |
|
168 |
sub addignoretags {
|
169 |
my $self=shift;
|
170 |
my $parselabel=shift;
|
171 |
$self->{parsers}{$parselabel}->addignoretags();
|
172 |
}
|
173 |
|
174 |
sub cleartags {
|
175 |
my $self=shift;
|
176 |
my $parselabel=shift;
|
177 |
|
178 |
$self->{parsers}{$parselabel}->cleartags();
|
179 |
}
|
180 |
|
181 |
|
182 |
sub includeparse {
|
183 |
my $self=shift;
|
184 |
my $parselabel=shift;
|
185 |
my $remoteparselabel=shift;
|
186 |
my $activedoc=shift;
|
187 |
|
188 |
# Some error trapping
|
189 |
if ( ! exists $self->{parsers}{$parselabel} ) {
|
190 |
$self->error("Unknown local parse name specified");
|
191 |
}
|
192 |
if ( ! exists $activedoc->{parsers}{$remoteparselabel} ) {
|
193 |
$self->error("Unknown parse name specified in remote obj $activedoc");
|
194 |
}
|
195 |
|
196 |
#
|
197 |
my $rp=$activedoc->{parsers}{$remoteparselabel};
|
198 |
$self->{parsers}{$parselabel}->includeparse($rp);
|
199 |
}
|
200 |
|
201 |
sub addtag {
|
202 |
my $self=shift;
|
203 |
my $parselabel=shift;
|
204 |
if ( ( $#_ != 6 ) && ( $#_ != 2) ) {
|
205 |
$self->error("Incorrect addtags specification\n".
|
206 |
"called with :\n@_ \n");
|
207 |
}
|
208 |
$self->{parsers}{$parselabel}->addtag(@_);
|
209 |
}
|
210 |
|
211 |
sub filetoparse {
|
212 |
my $self=shift;
|
213 |
|
214 |
if ( @_ ) {
|
215 |
$self->{filename}=shift;
|
216 |
}
|
217 |
return $self->{filename};
|
218 |
}
|
219 |
# --------- Group services
|
220 |
sub grouptag {
|
221 |
my $self=shift;
|
222 |
my $name=shift;
|
223 |
my $parselabel=shift;
|
224 |
|
225 |
$self->{parsers}{$parselabel}->contexttag($name);
|
226 |
}
|
227 |
|
228 |
sub opengroup {
|
229 |
my $self=shift;
|
230 |
my $name=shift;
|
231 |
|
232 |
if ( defined $self->currentparser ) {
|
233 |
$self->currentparser()->opencontext($name);
|
234 |
}
|
235 |
else {
|
236 |
$self->error("Cannot Call opengroup outside of a parse (".
|
237 |
caller().")");
|
238 |
}
|
239 |
}
|
240 |
|
241 |
sub closegroup {
|
242 |
my $self=shift;
|
243 |
my $name=shift;
|
244 |
|
245 |
if ( defined $self->currentparser ) {
|
246 |
$self->currentparser()->closecontext($name);
|
247 |
}
|
248 |
else {
|
249 |
$self->error("Cannot Call closegroup outside of a parse (".
|
250 |
caller().")");
|
251 |
}
|
252 |
}
|
253 |
|
254 |
sub allowgroup {
|
255 |
my $self=shift;
|
256 |
my $name=shift;
|
257 |
my $parselabel=shift;
|
258 |
|
259 |
$self->{parsers}{$parselabel}->includecontext($name);
|
260 |
}
|
261 |
|
262 |
sub disallowgroup {
|
263 |
my $self=shift;
|
264 |
my $name=shift;
|
265 |
my $parselabel=shift;
|
266 |
|
267 |
$self->{parsers}{$parselabel}->excludecontext($name);
|
268 |
}
|
269 |
|
270 |
# -------- Error Handling and Error services --------------
|
271 |
|
272 |
sub error {
|
273 |
my $self=shift;
|
274 |
my $string=shift;
|
275 |
|
276 |
die $string."\n";
|
277 |
}
|
278 |
|
279 |
sub parseerror {
|
280 |
my $self=shift;
|
281 |
my $string=shift;
|
282 |
|
283 |
if ( $self->currentparsename() eq "" ) {
|
284 |
$self->error("Error In file ".$self->filenameref."\n".$string);
|
285 |
}
|
286 |
else {
|
287 |
$line=$self->line();
|
288 |
print "Parse Error in ".$self->filenameref().", line ".
|
289 |
$line."\n";
|
290 |
print $string."\n";
|
291 |
exit;
|
292 |
}
|
293 |
}
|
294 |
|
295 |
sub checktag {
|
296 |
my $self=shift;
|
297 |
my $tagname=shift;
|
298 |
my $hashref=shift;
|
299 |
my $param=shift;
|
300 |
|
301 |
if ( ! exists $$hashref{$param} ) {
|
302 |
$self->parseerror("Incomplete Tag <$tagname> : $param required");
|
303 |
}
|
304 |
}
|
305 |
|
306 |
sub line {
|
307 |
my $self=shift;
|
308 |
return $self->{currentparser}->line();
|
309 |
}
|
310 |
|
311 |
sub tagstartline {
|
312 |
my $self=shift;
|
313 |
return $self->{currentparser}->tagstartline();
|
314 |
}
|
315 |
|
316 |
# -- tag routines
|
317 |
sub Doc_Start {
|
318 |
my $self=shift;
|
319 |
my $name=shift;
|
320 |
my $hashref=shift;
|
321 |
|
322 |
$self->checktag($name, $hashref, "type");
|
323 |
$self->checktag($name, $hashref, "version");
|
324 |
|
325 |
$self->{doctype}=$$hashref{'type'};
|
326 |
$self->{docversion}=$$hashref{'version'};
|
327 |
}
|