12 |
|
# to the desired routines |
13 |
|
# usegroupchecker(groupchecker) : Set a groupchecker |
14 |
|
# parse() : Parse the file |
15 |
– |
# checkparam($name,$par) : Exit with an error message if parameter |
16 |
– |
# is undefined in tag $name |
15 |
|
# line() : return the current line number of the parse |
16 |
< |
|
17 |
< |
package Switcher; |
16 |
> |
# tagstartline() : return the line number on which the current |
17 |
> |
# tag was opened |
18 |
> |
# stream(filehandle) : stream output to the filehandle if not handled |
19 |
> |
# in any other way |
20 |
> |
package ActiveDoc::Switcher; |
21 |
|
require 5.001; |
22 |
|
use Carp; |
23 |
|
|
27 |
|
my $objectname=shift; |
28 |
|
my $groupchecker=shift; |
29 |
|
|
30 |
< |
my $self = {}; |
30 |
> |
$self = {}; |
31 |
|
$self->{allw}=$objectname; |
32 |
|
bless $self, $class; |
33 |
|
$self->_initialise($file); |
34 |
|
return $self; |
35 |
|
} |
36 |
|
|
37 |
+ |
sub stream { |
38 |
+ |
my $self=shift; |
39 |
+ |
|
40 |
+ |
$self->{stream}=shift; |
41 |
+ |
} |
42 |
+ |
|
43 |
+ |
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 |
|
sub _initialise (hash1) { |
52 |
|
my $self=shift; |
53 |
|
$self->{filename}=shift; |
59 |
|
|
60 |
|
# Add a default TagContainer |
61 |
|
use ActiveDoc::TagContainer; |
62 |
< |
$self->{tagcontainer}=TagContainer->new(); |
62 |
> |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
63 |
|
|
64 |
|
} |
65 |
|
|
80 |
|
sub parse { |
81 |
|
my $self=shift; |
82 |
|
my $char; |
83 |
+ |
my $buf; |
84 |
|
$self->{linecount}=0; |
85 |
|
$self->_resetvars(); |
86 |
+ |
$self->{streamstore}=""; |
87 |
+ |
$self->{streamtmp}=""; |
88 |
|
|
89 |
|
# Open the file |
90 |
|
use FileHandle; |
91 |
< |
my $filehandle=FileHandle->new(); |
92 |
< |
open( $filehandle , "$self->{filename}" ) |
93 |
< |
or carp "Switcher: Cannot open $self->{filename} $! \n"; |
91 |
> |
local $filehandle; |
92 |
> |
$filehandle=FileHandle->new(); |
93 |
> |
$filehandle->open("<".$self->{filename}) |
94 |
> |
or return 1; |
95 |
> |
# The buffering seems all messed up - best not to use it |
96 |
> |
$filehandle->setvbuf($buf, _IONBF, 3000); |
97 |
|
|
98 |
|
# Start file processing |
99 |
< |
while ( <$filehandle> ) { |
99 |
> |
while ( ($_=<$filehandle>) ) { |
100 |
|
$self->{linecount}++; |
101 |
|
$self->{currentline}=$_; |
102 |
|
$self->{stringpos}=0; |
104 |
|
$self->_checkchar($char); |
105 |
|
} # end char while |
106 |
|
} # End String while loop |
107 |
< |
close $filehandle; |
108 |
< |
} |
109 |
< |
|
110 |
< |
sub checkparam($name, $key) { |
111 |
< |
my $self=shift; |
91 |
< |
my $name=shift; |
92 |
< |
my $key=shift; |
93 |
< |
|
94 |
< |
if ( ! defined $self->{tagvar}{$key} ) { |
95 |
< |
print "Switcher: Badly formed $name tag -". |
96 |
< |
" undefined $key parameter\n"; |
97 |
< |
exit 1; |
98 |
< |
} |
107 |
> |
undef $filehandle; |
108 |
> |
# make sure we close the last buffer |
109 |
> |
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
110 |
> |
$self->_getstore()); |
111 |
> |
#$self->_printstream(); |
112 |
|
} |
113 |
|
|
114 |
|
# |
118 |
|
my $self=shift; |
119 |
|
return $self->{linecount}; |
120 |
|
} |
121 |
+ |
|
122 |
+ |
# return the line the current tag was opened |
123 |
+ |
sub tagstartline { |
124 |
+ |
my $self=shift; |
125 |
+ |
$self->{tagstart}; |
126 |
+ |
} |
127 |
|
# --------------- Utility routines ---------------------------- |
128 |
|
|
129 |
|
# |
146 |
|
my $char=shift; |
147 |
|
my $string; |
148 |
|
|
149 |
+ |
|
150 |
|
# ---- In a tag |
151 |
|
if ( $self->{tagcontext}=~/tag/ ) { |
152 |
|
if ( ! $self->_quotetest($char) ) { |
171 |
|
my $self=shift; |
172 |
|
my $char; |
173 |
|
$char=substr($self->{currentline},$self->{stringpos}++,1); |
174 |
< |
# print "Debug : Fetching character $char\n"; |
174 |
> |
#print "Debug : Fetching character $char\n"; |
175 |
> |
|
176 |
> |
# Keep a record for any stream processes |
177 |
> |
$self->{streamstore}=$self->{streamstore}.$char; |
178 |
> |
|
179 |
|
return $char; |
180 |
|
} |
181 |
|
|
183 |
|
my $self=shift; |
184 |
|
my $char; |
185 |
|
|
186 |
+ |
# Keep a record of where the tag started |
187 |
+ |
$self->{tagstart}=$self->line(); |
188 |
+ |
|
189 |
|
# Close the last text segment |
190 |
+ |
$self->{streamtmp}=$self->_popstream(); |
191 |
|
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
192 |
|
$self->_getstore()); |
193 |
|
$self->_resetstore(); |
215 |
|
$self->_closelabel(); |
216 |
|
|
217 |
|
# -- Call the associated tag function if appropriate |
218 |
< |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
219 |
< |
$self->_calltag($tagroutine, $self->{tagname}, |
218 |
> |
if ( defined $self->{tagname} ) { |
219 |
> |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
220 |
> |
$self->_calltag($tagroutine, $self->{tagname}, |
221 |
|
$self->{tagvar}); |
222 |
< |
#print "\nDebug : Closing Tag $tagroutine\n"; |
222 |
> |
#print "\nDebug : Closing Tag $tagroutine\n"; |
223 |
|
|
224 |
< |
# -- Now make sure the text context is set for calling routines to |
225 |
< |
# -- deal with text portions outside of tags |
226 |
< |
if ( $self->{tagcontext} eq "starttag" ) { |
227 |
< |
push @{$self->{textstack}} , $self->{textcontext}; |
228 |
< |
$self->{textcontext}=$self->{tagname}; |
229 |
< |
} |
230 |
< |
else { |
224 |
> |
# -- Now make sure the text context is set for calling routines to |
225 |
> |
# -- deal with text portions outside of tags |
226 |
> |
if ( ($self->{tagcontext} eq "starttag") ) { |
227 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
228 |
> |
push @{$self->{textstack}} , $self->{textcontext}; |
229 |
> |
$self->{textcontext}=$self->{tagname}; |
230 |
> |
} |
231 |
> |
} |
232 |
> |
else { |
233 |
|
if ( $#{$self->{textstack}} > -1 ) { |
234 |
< |
if ( $self->{textcontext} eq $self->{tagname} ) { |
235 |
< |
$self->{textcontext}=pop @{$self->{textstack}}; |
236 |
< |
} |
234 |
> |
if ( $self->{textcontext} eq $self->{tagname} ) { |
235 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
236 |
> |
# -- watch out for valid tags we ignore in this parse |
237 |
> |
$self->{textcontext}=pop @{$self->{textstack}}; |
238 |
> |
} |
239 |
> |
} |
240 |
|
else { #The tag we are closing is not the last one so |
241 |
|
# we keep our current context. |
242 |
|
$self->_removefromstack($self->{tagname},$self->{textstack}); |
244 |
|
|
245 |
|
} |
246 |
|
else { # more close tags than open ones |
247 |
+ |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
248 |
|
print "Warning : Unmatched </...> tag on line ". |
249 |
|
$self->line()."\n"; |
250 |
+ |
} |
251 |
|
} |
252 |
+ |
} |
253 |
|
} |
254 |
|
# Reset context back to text |
255 |
|
$self->{tagcontext}="text"; |
260 |
|
my $tagroutine=shift; |
261 |
|
my @args=@_; |
262 |
|
my $rt; |
263 |
+ |
my $found=0; |
264 |
|
|
265 |
|
if ( $self->{groupchecker}->status() || |
266 |
|
( $self->{tagcontainer}->inquiregroup($tagroutine)) ) { |
267 |
< |
$rt=$self->{tagcontainer}->getroutine($tagroutine); |
267 |
> |
($rt,$obj)=$self->{tagcontainer}->getroutine($tagroutine); |
268 |
|
if ( $rt ne "" ) { |
269 |
+ |
if ( ! defined $obj ) { |
270 |
|
&{$rt}( $self->{allw},@_); |
271 |
+ |
} |
272 |
+ |
else { |
273 |
+ |
&{$rt}( $obj,@_); |
274 |
+ |
} |
275 |
+ |
$found=1; |
276 |
|
} |
277 |
|
} |
278 |
+ |
|
279 |
+ |
# stream function |
280 |
+ |
if ( ! exists $self->{streamexclude}{$tagroutine} ) { |
281 |
+ |
$self->_printstream(); |
282 |
+ |
} |
283 |
+ |
$self->_clearstream(); |
284 |
+ |
} |
285 |
+ |
|
286 |
+ |
sub _clearstream { |
287 |
+ |
my $self=shift; |
288 |
+ |
$self->{streamstore}=(($self->{streamtmp} ne "")?$self->{streamtmp}:""); |
289 |
+ |
$self->{streamtmp}=""; |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
sub _popstream { |
293 |
+ |
my $self=shift; |
294 |
+ |
$self->{streamstore}=~s/(.*)(.)$/$1/; |
295 |
+ |
return $2; |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
sub _printstream { |
299 |
+ |
|
300 |
+ |
my $self=shift; |
301 |
+ |
|
302 |
+ |
# Stream output functionality |
303 |
+ |
if ( defined $self->{stream} ) { |
304 |
+ |
print {$self->{stream}} "$self->{streamstore}"; |
305 |
+ |
} |
306 |
|
} |
307 |
|
|
308 |
|
sub _removefromstack { |
377 |
|
sub _resetlabels { |
378 |
|
my $self=shift; |
379 |
|
undef $self->{tagvar}; |
380 |
+ |
undef $self->{tagname}; |
381 |
|
} |
382 |
|
|
383 |
|
sub _closelabel { |
385 |
|
|
386 |
|
# Do we have a label name? |
387 |
|
if ( $self->{lastlabel} ne "" ) { |
388 |
< |
$self->{tagvar}{$self->{lastlabel}}=$self->_getstore(); |
388 |
> |
(my $label=$self->{lastlabel})=~tr[A-Z][a-z]; |
389 |
> |
$self->{tagvar}{$label}=$self->_getstore(); |
390 |
|
$self->{lastlabel}=""; |
391 |
|
} |
392 |
|
elsif ( $self->_getstore() ne "") { |
393 |
< |
#Then it must be the tag name |
394 |
< |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
393 |
> |
# Then it must be the tag name |
394 |
> |
if ( ! defined $self->{tagname} ) { |
395 |
> |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
396 |
> |
} |
397 |
> |
else { |
398 |
> |
die ">Tag syntax error in $self->{tagname} on line ". |
399 |
> |
$self->line()." of file \n$self->{filename}"; |
400 |
> |
} |
401 |
|
} |
402 |
|
$self->_resetstore(); |
403 |
|
} |