1 |
williamc |
1.1 |
# Switcher Module
|
2 |
|
|
#
|
3 |
|
|
# Look for elements given in input has in a string
|
4 |
|
|
# If found then call a routine name with the same name
|
5 |
|
|
# Implemented as an object to maintain state info between each line
|
6 |
|
|
# processed.
|
7 |
|
|
# Interface
|
8 |
|
|
# ---------
|
9 |
|
|
# new(file,objectref) : A new object - filename of file to parse
|
10 |
|
|
# objectref->of the methods
|
11 |
|
|
# usetags(tagobjref) : Specify a tagcontainer set to direct to
|
12 |
|
|
# to the desired routines
|
13 |
|
|
# usegroupchecker(groupchecker) : Set a groupchecker
|
14 |
|
|
# parse() : Parse the file
|
15 |
|
|
# line() : return the current line number of the parse
|
16 |
williamc |
1.12 |
# tagstartline() : return the line number on which the current
|
17 |
|
|
# tag was opened
|
18 |
williamc |
1.6 |
# stream(filehandle) : stream output to the filehandle if not handled
|
19 |
|
|
# in any other way
|
20 |
williamc |
1.3 |
package ActiveDoc::Switcher;
|
21 |
williamc |
1.1 |
require 5.001;
|
22 |
|
|
use Carp;
|
23 |
|
|
|
24 |
|
|
sub new {
|
25 |
|
|
my $class=shift;
|
26 |
|
|
my $file=shift;
|
27 |
|
|
my $objectname=shift;
|
28 |
|
|
my $groupchecker=shift;
|
29 |
|
|
|
30 |
williamc |
1.4 |
$self = {};
|
31 |
williamc |
1.1 |
$self->{allw}=$objectname;
|
32 |
|
|
bless $self, $class;
|
33 |
|
|
$self->_initialise($file);
|
34 |
|
|
return $self;
|
35 |
|
|
}
|
36 |
|
|
|
37 |
williamc |
1.6 |
sub stream {
|
38 |
|
|
my $self=shift;
|
39 |
|
|
|
40 |
|
|
$self->{stream}=shift;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
williamc |
1.8 |
sub streamexclude {
|
44 |
|
|
my $self=shift;
|
45 |
|
|
my $tag=shift;
|
46 |
|
|
|
47 |
|
|
$tag=~tr/A-Z/a-z/;
|
48 |
|
|
$self->{streamexclude}{$tag}=1;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
williamc |
1.1 |
sub _initialise (hash1) {
|
52 |
|
|
my $self=shift;
|
53 |
|
|
$self->{filename}=shift;
|
54 |
|
|
|
55 |
|
|
# add a default groupchecker
|
56 |
|
|
use ActiveDoc::GroupChecker;
|
57 |
|
|
$self->{groupchecker}=GroupChecker->new();
|
58 |
|
|
$self->{groupchecker}->include("all");
|
59 |
|
|
|
60 |
|
|
# Add a default TagContainer
|
61 |
|
|
use ActiveDoc::TagContainer;
|
62 |
williamc |
1.3 |
$self->{tagcontainer}=ActiveDoc::TagContainer->new();
|
63 |
williamc |
1.1 |
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
sub usetags {
|
67 |
|
|
my $self=shift;
|
68 |
|
|
my $tagcontainer=shift;
|
69 |
|
|
|
70 |
|
|
$self->{tagcontainer}=$tagcontainer;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
sub usegroupchecker {
|
74 |
|
|
my $self=shift;
|
75 |
|
|
my $ref=shift;
|
76 |
|
|
|
77 |
|
|
$self->{groupchecker}=$ref;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
sub parse {
|
81 |
|
|
my $self=shift;
|
82 |
|
|
my $char;
|
83 |
williamc |
1.4 |
my $buf;
|
84 |
williamc |
1.1 |
$self->{linecount}=0;
|
85 |
|
|
$self->_resetvars();
|
86 |
williamc |
1.6 |
$self->{streamstore}="";
|
87 |
williamc |
1.8 |
$self->{streamtmp}="";
|
88 |
williamc |
1.1 |
|
89 |
|
|
# Open the file
|
90 |
|
|
use FileHandle;
|
91 |
williamc |
1.6 |
local $filehandle;
|
92 |
williamc |
1.4 |
$filehandle=FileHandle->new();
|
93 |
williamc |
1.6 |
$filehandle->open("<".$self->{filename})
|
94 |
williamc |
1.2 |
or return 1;
|
95 |
williamc |
1.4 |
# The buffering seems all messed up - best not to use it
|
96 |
williamc |
1.5 |
$filehandle->setvbuf($buf, _IONBF, 3000);
|
97 |
williamc |
1.1 |
|
98 |
|
|
# Start file processing
|
99 |
williamc |
1.4 |
while ( ($_=<$filehandle>) ) {
|
100 |
williamc |
1.1 |
$self->{linecount}++;
|
101 |
|
|
$self->{currentline}=$_;
|
102 |
|
|
$self->{stringpos}=0;
|
103 |
|
|
while ( ($char=$self->_nextchar()) ne "" ) {
|
104 |
|
|
$self->_checkchar($char);
|
105 |
|
|
} # end char while
|
106 |
|
|
} # End String while loop
|
107 |
williamc |
1.5 |
undef $filehandle;
|
108 |
williamc |
1.6 |
$self->_printstream();
|
109 |
williamc |
1.1 |
}
|
110 |
|
|
|
111 |
|
|
#
|
112 |
|
|
# return the current line number
|
113 |
|
|
#
|
114 |
|
|
sub line {
|
115 |
|
|
my $self=shift;
|
116 |
|
|
return $self->{linecount};
|
117 |
|
|
}
|
118 |
williamc |
1.12 |
|
119 |
|
|
# return the line the current tag was opened
|
120 |
|
|
sub tagstartline {
|
121 |
|
|
my $self=shift;
|
122 |
|
|
$self->{tagstart};
|
123 |
|
|
}
|
124 |
williamc |
1.1 |
# --------------- Utility routines ----------------------------
|
125 |
|
|
|
126 |
|
|
#
|
127 |
|
|
# Some initialisation of test suites
|
128 |
|
|
#
|
129 |
|
|
sub _resetvars {
|
130 |
|
|
my $self=shift;
|
131 |
|
|
$self->{quotes}=0;
|
132 |
|
|
$self->{lastlabel}="";
|
133 |
|
|
$self->{textcontext}='none';
|
134 |
|
|
$self->{tagcontext}="text";
|
135 |
|
|
$self->_resetstore();
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
#
|
139 |
|
|
# Check for control characters
|
140 |
|
|
#
|
141 |
|
|
sub _checkchar {
|
142 |
|
|
my $self=shift;
|
143 |
|
|
my $char=shift;
|
144 |
|
|
my $string;
|
145 |
|
|
|
146 |
williamc |
1.6 |
|
147 |
williamc |
1.1 |
# ---- In a tag
|
148 |
|
|
if ( $self->{tagcontext}=~/tag/ ) {
|
149 |
|
|
if ( ! $self->_quotetest($char) ) {
|
150 |
|
|
if ( ! $self->_labeltest($char) ) {
|
151 |
|
|
if ( $char eq ">") { $self->_closetag(); }
|
152 |
|
|
else { $self->_putstore($char); }
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
}
|
156 |
|
|
# ------ Outside a tag
|
157 |
|
|
else {
|
158 |
|
|
if ( $char eq "<") { $self->_opentag() }
|
159 |
|
|
else { $self->_putstore($char) }
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
#
|
165 |
|
|
# Return the next character from the current string buffer
|
166 |
|
|
#
|
167 |
|
|
sub _nextchar() {
|
168 |
|
|
my $self=shift;
|
169 |
|
|
my $char;
|
170 |
|
|
$char=substr($self->{currentline},$self->{stringpos}++,1);
|
171 |
|
|
# print "Debug : Fetching character $char\n";
|
172 |
williamc |
1.6 |
|
173 |
|
|
# Keep a record for any stream processes
|
174 |
|
|
$self->{streamstore}=$self->{streamstore}.$char;
|
175 |
|
|
|
176 |
williamc |
1.1 |
return $char;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
sub _opentag {
|
180 |
|
|
my $self=shift;
|
181 |
|
|
my $char;
|
182 |
williamc |
1.12 |
|
183 |
|
|
# Keep a record of where the tag started
|
184 |
|
|
$self->{tagstart}=$self->line();
|
185 |
williamc |
1.1 |
|
186 |
|
|
# Close the last text segment
|
187 |
williamc |
1.8 |
$self->{streamtmp}=$self->_popstream();
|
188 |
williamc |
1.1 |
$self->_calltag($self->{textcontext}, $self->{textcontext},
|
189 |
|
|
$self->_getstore());
|
190 |
|
|
$self->_resetstore();
|
191 |
|
|
$self->_resetlabels();
|
192 |
|
|
|
193 |
|
|
# Do we have an opening or closing tag?
|
194 |
|
|
if ( ($char=$self->_nextchar()) eq "/" ) { #we have a closing tag
|
195 |
|
|
$self->{tagcontext}="endtag";
|
196 |
|
|
}
|
197 |
|
|
else { # an opening tag
|
198 |
|
|
$self->{tagcontext}="starttag";
|
199 |
|
|
$self->_checkchar($char);
|
200 |
|
|
}
|
201 |
|
|
#print "\nDebug : Opening $self->{tagcontext}\n";
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
#
|
205 |
|
|
# Close a tag
|
206 |
|
|
#
|
207 |
|
|
sub _closetag {
|
208 |
|
|
my $self=shift;
|
209 |
|
|
my $tagroutine;
|
210 |
|
|
|
211 |
|
|
# -- Finish off any labels/get tagname
|
212 |
|
|
$self->_closelabel();
|
213 |
|
|
|
214 |
|
|
# -- Call the associated tag function if appropriate
|
215 |
|
|
$tagroutine=$self->{tagname}."_".$self->{tagcontext};
|
216 |
|
|
$self->_calltag($tagroutine, $self->{tagname},
|
217 |
|
|
$self->{tagvar});
|
218 |
|
|
#print "\nDebug : Closing Tag $tagroutine\n";
|
219 |
|
|
|
220 |
|
|
# -- Now make sure the text context is set for calling routines to
|
221 |
|
|
# -- deal with text portions outside of tags
|
222 |
|
|
if ( $self->{tagcontext} eq "starttag" ) {
|
223 |
|
|
push @{$self->{textstack}} , $self->{textcontext};
|
224 |
|
|
$self->{textcontext}=$self->{tagname};
|
225 |
|
|
}
|
226 |
|
|
else {
|
227 |
|
|
if ( $#{$self->{textstack}} > -1 ) {
|
228 |
|
|
if ( $self->{textcontext} eq $self->{tagname} ) {
|
229 |
|
|
$self->{textcontext}=pop @{$self->{textstack}};
|
230 |
|
|
}
|
231 |
|
|
else { #The tag we are closing is not the last one so
|
232 |
|
|
# we keep our current context.
|
233 |
|
|
$self->_removefromstack($self->{tagname},$self->{textstack});
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
}
|
237 |
|
|
else { # more close tags than open ones
|
238 |
|
|
print "Warning : Unmatched </...> tag on line ".
|
239 |
|
|
$self->line()."\n";
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
# Reset context back to text
|
243 |
|
|
$self->{tagcontext}="text";
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
sub _calltag {
|
247 |
|
|
my $self=shift;
|
248 |
|
|
my $tagroutine=shift;
|
249 |
|
|
my @args=@_;
|
250 |
|
|
my $rt;
|
251 |
williamc |
1.6 |
my $found=0;
|
252 |
williamc |
1.1 |
|
253 |
|
|
if ( $self->{groupchecker}->status() ||
|
254 |
|
|
( $self->{tagcontainer}->inquiregroup($tagroutine)) ) {
|
255 |
williamc |
1.7 |
($rt,$obj)=$self->{tagcontainer}->getroutine($tagroutine);
|
256 |
williamc |
1.1 |
if ( $rt ne "" ) {
|
257 |
williamc |
1.7 |
if ( ! defined $obj ) {
|
258 |
williamc |
1.1 |
&{$rt}( $self->{allw},@_);
|
259 |
williamc |
1.7 |
}
|
260 |
|
|
else {
|
261 |
|
|
&{$rt}( $obj,@_);
|
262 |
|
|
}
|
263 |
|
|
$found=1;
|
264 |
williamc |
1.1 |
}
|
265 |
|
|
}
|
266 |
williamc |
1.6 |
|
267 |
williamc |
1.8 |
# stream function
|
268 |
|
|
if ( ! exists $self->{streamexclude}{$tagroutine} ) {
|
269 |
williamc |
1.6 |
$self->_printstream();
|
270 |
|
|
}
|
271 |
|
|
$self->_clearstream();
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
sub _clearstream {
|
275 |
|
|
my $self=shift;
|
276 |
|
|
$self->{streamstore}=(($self->{streamtmp} ne "")?$self->{streamtmp}:"");
|
277 |
|
|
$self->{streamtmp}="";
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
sub _popstream {
|
281 |
|
|
my $self=shift;
|
282 |
|
|
$self->{streamstore}=~s/(.*)(.)$/$1/;
|
283 |
|
|
return $2;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
sub _printstream {
|
287 |
|
|
|
288 |
|
|
my $self=shift;
|
289 |
|
|
|
290 |
|
|
# Stream output functionality
|
291 |
|
|
if ( defined $self->{stream} ) {
|
292 |
|
|
print {$self->{stream}} "$self->{streamstore}";
|
293 |
|
|
}
|
294 |
williamc |
1.1 |
}
|
295 |
|
|
|
296 |
|
|
sub _removefromstack {
|
297 |
|
|
my $self=shift;
|
298 |
|
|
my $name=shift;
|
299 |
|
|
my $stack=shift;
|
300 |
|
|
my $this;
|
301 |
|
|
|
302 |
|
|
undef @tempstack;
|
303 |
|
|
#print "In ----".$#{$stack};
|
304 |
|
|
# Keep popping until we find our string
|
305 |
|
|
while ( ($this=(pop @{$stack})) ne "$name") {
|
306 |
|
|
push @tempstack, $this;
|
307 |
|
|
if ( $#{$stack} < 0 ) { last; }
|
308 |
|
|
}
|
309 |
|
|
# Now put them back
|
310 |
|
|
while ( $#tempstack>-1) {
|
311 |
|
|
$this=pop @tempstack;
|
312 |
|
|
push @{$stack}, $this;
|
313 |
|
|
}
|
314 |
|
|
#print " Out ----".$#{$stack};
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
#
|
318 |
|
|
# Quote handling
|
319 |
|
|
#
|
320 |
|
|
|
321 |
|
|
sub _quotetest {
|
322 |
|
|
my $self=shift;
|
323 |
|
|
my $char=shift;
|
324 |
|
|
|
325 |
|
|
# --- Are we already in a quote context?
|
326 |
|
|
if ( $self->{quotes} ) {
|
327 |
|
|
if ( $char eq $self->{openquote} ) {
|
328 |
|
|
$self->{quotes}=0;
|
329 |
|
|
}
|
330 |
|
|
else {
|
331 |
|
|
$self->_putstore($char);
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
# --- Unquoted Context
|
335 |
|
|
elsif ( (($char eq "\"") || ($char eq "\'") || ($char eq "\`")) ) {
|
336 |
|
|
$self->{quotes}=1;
|
337 |
|
|
$self->{openquote}=$char;
|
338 |
|
|
}
|
339 |
|
|
else { return 0; } # Return zero if not quoted
|
340 |
|
|
return 1; # 1 otherwise
|
341 |
|
|
}
|
342 |
|
|
|
343 |
|
|
#
|
344 |
|
|
# Label handling
|
345 |
|
|
#
|
346 |
|
|
sub _labeltest {
|
347 |
|
|
my $self=shift;
|
348 |
|
|
my $char=shift;
|
349 |
|
|
|
350 |
|
|
# Spaces are markers between tags
|
351 |
|
|
if ( ($char eq " ") || ($char eq "\n") || ($char eq "\t")) {
|
352 |
|
|
$self->_closelabel();
|
353 |
|
|
}
|
354 |
|
|
# Check for a change in label status
|
355 |
|
|
elsif ( $char eq "=" ) {
|
356 |
|
|
$self->{lastlabel}=$self->_getstore();
|
357 |
|
|
$self->_resetstore();
|
358 |
|
|
}
|
359 |
|
|
else {
|
360 |
|
|
return 0;
|
361 |
|
|
}
|
362 |
|
|
return 1;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
sub _resetlabels {
|
366 |
|
|
my $self=shift;
|
367 |
|
|
undef $self->{tagvar};
|
368 |
williamc |
1.9 |
undef $self->{tagname};
|
369 |
williamc |
1.1 |
}
|
370 |
|
|
|
371 |
|
|
sub _closelabel {
|
372 |
|
|
my $self=shift;
|
373 |
|
|
|
374 |
|
|
# Do we have a label name?
|
375 |
|
|
if ( $self->{lastlabel} ne "" ) {
|
376 |
|
|
$self->{tagvar}{$self->{lastlabel}}=$self->_getstore();
|
377 |
|
|
$self->{lastlabel}="";
|
378 |
|
|
}
|
379 |
|
|
elsif ( $self->_getstore() ne "") {
|
380 |
williamc |
1.9 |
# Then it must be the tag name
|
381 |
|
|
if ( ! defined $self->{tagname} ) {
|
382 |
|
|
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/;
|
383 |
|
|
}
|
384 |
|
|
else {
|
385 |
williamc |
1.10 |
die ">Tag syntax error in $self->{tagname} on line ".
|
386 |
williamc |
1.11 |
$self->line()." of file \n$self->{filename}";
|
387 |
williamc |
1.9 |
}
|
388 |
williamc |
1.1 |
}
|
389 |
|
|
$self->_resetstore();
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
#
|
393 |
|
|
# Character Store management interface
|
394 |
|
|
#
|
395 |
|
|
sub _putstore() {
|
396 |
|
|
my $self=shift;
|
397 |
|
|
my $char=shift;
|
398 |
|
|
|
399 |
|
|
$self->{stringbuff}=$self->{stringbuff}.$char;
|
400 |
|
|
}
|
401 |
|
|
|
402 |
|
|
sub _getstore() {
|
403 |
|
|
my $self=shift;
|
404 |
|
|
|
405 |
|
|
return $self->{stringbuff};
|
406 |
|
|
}
|
407 |
|
|
|
408 |
|
|
sub _resetstore {
|
409 |
|
|
my $self=shift;
|
410 |
|
|
$self->{stringbuff}="";
|
411 |
|
|
}
|