6 |
|
# processed. |
7 |
|
# Interface |
8 |
|
# --------- |
9 |
+ |
# |
10 |
|
# new(file,objectref) : A new object - filename of file to parse |
11 |
< |
# objectref->of the methods |
11 |
> |
# objectref->of the methods |
12 |
|
# usetags(tagobjref) : Specify a tagcontainer set to direct to |
13 |
|
# to the desired routines |
14 |
|
# usegroupchecker(groupchecker) : Set a groupchecker |
15 |
|
# parse() : Parse the file |
16 |
< |
# line() : return the current line number of the parse |
17 |
< |
# stream(filehandle) : stream output to the filehandle if not handled |
18 |
< |
# in any other way |
16 |
> |
# line() : return the current line number of the parse |
17 |
> |
# tagstartline() : return the line number on which the current |
18 |
> |
# tag was opened |
19 |
> |
# stream(filehandle) : stream output to the filehandle if not handled |
20 |
> |
# in any other way |
21 |
|
package ActiveDoc::Switcher; |
22 |
+ |
use Utilities::Verbose; |
23 |
|
require 5.001; |
24 |
|
use Carp; |
25 |
|
|
26 |
+ |
@ISA=qw(Utilities::Verbose); |
27 |
+ |
|
28 |
|
sub new { |
29 |
|
my $class=shift; |
30 |
|
my $file=shift; |
52 |
|
$self->{streamexclude}{$tag}=1; |
53 |
|
} |
54 |
|
|
55 |
< |
sub _initialise (hash1) { |
56 |
< |
my $self=shift; |
57 |
< |
$self->{filename}=shift; |
58 |
< |
|
59 |
< |
# add a default groupchecker |
60 |
< |
use ActiveDoc::GroupChecker; |
61 |
< |
$self->{groupchecker}=GroupChecker->new(); |
62 |
< |
$self->{groupchecker}->include("all"); |
63 |
< |
|
64 |
< |
# Add a default TagContainer |
65 |
< |
use ActiveDoc::TagContainer; |
66 |
< |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
67 |
< |
|
68 |
< |
} |
55 |
> |
sub _initialise (hash1) |
56 |
> |
{ |
57 |
> |
my $self=shift; |
58 |
> |
$self->{filename}=shift; |
59 |
> |
$self->verbose(">> New ActiveDoc::Switcher created."); |
60 |
> |
# add a default groupchecker |
61 |
> |
use ActiveDoc::GroupChecker; |
62 |
> |
$self->{groupchecker}=GroupChecker->new(); |
63 |
> |
$self->{groupchecker}->include("all"); |
64 |
> |
|
65 |
> |
# Add a default TagContainer |
66 |
> |
use ActiveDoc::TagContainer; |
67 |
> |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
68 |
> |
|
69 |
> |
} |
70 |
|
|
71 |
|
sub usetags { |
72 |
|
my $self=shift; |
95 |
|
use FileHandle; |
96 |
|
local $filehandle; |
97 |
|
$filehandle=FileHandle->new(); |
98 |
+ |
|
99 |
+ |
$self->verbose(">> Reading file: ".$self->{filename}." "); |
100 |
+ |
|
101 |
|
$filehandle->open("<".$self->{filename}) |
102 |
|
or return 1; |
93 |
– |
# The buffering seems all messed up - best not to use it |
94 |
– |
$filehandle->setvbuf($buf, _IONBF, 3000); |
103 |
|
|
104 |
|
# Start file processing |
105 |
|
while ( ($_=<$filehandle>) ) { |
106 |
|
$self->{linecount}++; |
107 |
+ |
# Skip lines that start with a hash. A better way |
108 |
+ |
# of adding comments than ignore tags: |
109 |
+ |
next if (/^#/); |
110 |
|
$self->{currentline}=$_; |
111 |
|
$self->{stringpos}=0; |
112 |
|
while ( ($char=$self->_nextchar()) ne "" ) { |
114 |
|
} # end char while |
115 |
|
} # End String while loop |
116 |
|
undef $filehandle; |
117 |
< |
$self->_printstream(); |
117 |
> |
# make sure we close the last buffer |
118 |
> |
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
119 |
> |
$self->_getstore()); |
120 |
|
} |
121 |
+ |
|
122 |
+ |
sub parsefilelist |
123 |
+ |
{ |
124 |
+ |
my $self=shift; |
125 |
+ |
my ($char,$buf); |
126 |
+ |
|
127 |
+ |
$self->{linecount}=0; |
128 |
+ |
$self->_resetvars(); |
129 |
+ |
$self->{streamstore}=""; |
130 |
+ |
$self->{streamtmp}=""; |
131 |
+ |
|
132 |
+ |
foreach my $buildfile (@{$self->{filename}}) |
133 |
+ |
{ |
134 |
+ |
if ( -f $buildfile) |
135 |
+ |
{ |
136 |
+ |
# Open the file |
137 |
+ |
use FileHandle; |
138 |
+ |
local $filehandle; |
139 |
+ |
$filehandle=FileHandle->new(); |
140 |
+ |
$self->verbose(">> Reading file: ".$buildfile." "); |
141 |
+ |
$filehandle->open("<".$buildfile) or return 1; |
142 |
+ |
|
143 |
+ |
# Start file processing |
144 |
+ |
while ( ($_=<$filehandle>) ) |
145 |
+ |
{ |
146 |
+ |
$self->{linecount}++; |
147 |
+ |
# Skip lines that start with a hash. A better way |
148 |
+ |
# of adding comments than ignore tags: |
149 |
+ |
next if (/^#/); |
150 |
+ |
$self->{currentline}=$_; |
151 |
+ |
$self->{stringpos}=0; |
152 |
+ |
while ( ($char=$self->_nextchar()) ne "" ) |
153 |
+ |
{ |
154 |
+ |
$self->_checkchar($char); |
155 |
+ |
} # end char while |
156 |
+ |
} # End String while loop |
157 |
+ |
undef $filehandle; |
158 |
+ |
# Make sure we close the last buffer: |
159 |
+ |
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
160 |
+ |
$self->_getstore()); |
161 |
+ |
} |
162 |
+ |
} |
163 |
+ |
} |
164 |
|
|
165 |
|
# |
166 |
|
# return the current line number |
169 |
|
my $self=shift; |
170 |
|
return $self->{linecount}; |
171 |
|
} |
172 |
+ |
|
173 |
+ |
# return the line the current tag was opened |
174 |
+ |
sub tagstartline { |
175 |
+ |
my $self=shift; |
176 |
+ |
$self->{tagstart}; |
177 |
+ |
} |
178 |
|
# --------------- Utility routines ---------------------------- |
179 |
|
|
180 |
|
# |
200 |
|
|
201 |
|
# ---- In a tag |
202 |
|
if ( $self->{tagcontext}=~/tag/ ) { |
203 |
+ |
$self->{tagbuff}=$self->{tagbuff}.$char; |
204 |
|
if ( ! $self->_quotetest($char) ) { |
205 |
|
if ( ! $self->_labeltest($char) ) { |
206 |
|
if ( $char eq ">") { $self->_closetag(); } |
223 |
|
my $self=shift; |
224 |
|
my $char; |
225 |
|
$char=substr($self->{currentline},$self->{stringpos}++,1); |
163 |
– |
# print "Debug : Fetching character $char\n"; |
226 |
|
|
227 |
|
# Keep a record for any stream processes |
228 |
|
$self->{streamstore}=$self->{streamstore}.$char; |
234 |
|
my $self=shift; |
235 |
|
my $char; |
236 |
|
|
237 |
+ |
# Keep a record of where the tag started |
238 |
+ |
$self->{tagstart}=$self->line(); |
239 |
+ |
|
240 |
|
# Close the last text segment |
241 |
|
$self->{streamtmp}=$self->_popstream(); |
242 |
|
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
246 |
|
|
247 |
|
# Do we have an opening or closing tag? |
248 |
|
if ( ($char=$self->_nextchar()) eq "/" ) { #we have a closing tag |
249 |
+ |
$self->{tagbuff}="<".$char; |
250 |
|
$self->{tagcontext}="endtag"; |
251 |
|
} |
252 |
|
else { # an opening tag |
253 |
+ |
$self->{tagbuff}="<"; |
254 |
|
$self->{tagcontext}="starttag"; |
255 |
|
$self->_checkchar($char); |
256 |
|
} |
190 |
– |
#print "\nDebug : Opening $self->{tagcontext}\n"; |
257 |
|
} |
258 |
|
|
259 |
|
# |
267 |
|
$self->_closelabel(); |
268 |
|
|
269 |
|
# -- Call the associated tag function if appropriate |
270 |
< |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
271 |
< |
$self->_calltag($tagroutine, $self->{tagname}, |
270 |
> |
if ( defined $self->{tagname} ) { |
271 |
> |
$tagroutine=$self->{tagname}."_".$self->{tagcontext}; |
272 |
> |
$self->_calltag($tagroutine, $self->{tagname}, |
273 |
|
$self->{tagvar}); |
207 |
– |
#print "\nDebug : Closing Tag $tagroutine\n"; |
274 |
|
|
275 |
< |
# -- Now make sure the text context is set for calling routines to |
276 |
< |
# -- deal with text portions outside of tags |
277 |
< |
if ( $self->{tagcontext} eq "starttag" ) { |
278 |
< |
push @{$self->{textstack}} , $self->{textcontext}; |
279 |
< |
$self->{textcontext}=$self->{tagname}; |
280 |
< |
} |
281 |
< |
else { |
275 |
> |
# -- Now make sure the text context is set for calling routines to |
276 |
> |
# -- deal with text portions outside of tags |
277 |
> |
if ( ($self->{tagcontext} eq "starttag") ) { |
278 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
279 |
> |
push @{$self->{textstack}} , $self->{textcontext}; |
280 |
> |
$self->{textcontext}=$self->{tagname}; |
281 |
> |
} |
282 |
> |
} |
283 |
> |
else { |
284 |
|
if ( $#{$self->{textstack}} > -1 ) { |
285 |
< |
if ( $self->{textcontext} eq $self->{tagname} ) { |
286 |
< |
$self->{textcontext}=pop @{$self->{textstack}}; |
287 |
< |
} |
285 |
> |
if ( $self->{textcontext} eq $self->{tagname} ) { |
286 |
> |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
287 |
> |
# -- watch out for valid tags we ignore in this parse |
288 |
> |
$self->{textcontext}=pop @{$self->{textstack}}; |
289 |
> |
} |
290 |
> |
} |
291 |
|
else { #The tag we are closing is not the last one so |
292 |
|
# we keep our current context. |
293 |
|
$self->_removefromstack($self->{tagname},$self->{textstack}); |
295 |
|
|
296 |
|
} |
297 |
|
else { # more close tags than open ones |
298 |
+ |
if ( $self->{tagcontainer}->definescontext($self->{tagname}) ) { |
299 |
|
print "Warning : Unmatched </...> tag on line ". |
300 |
|
$self->line()."\n"; |
301 |
+ |
} |
302 |
|
} |
303 |
+ |
} |
304 |
|
} |
305 |
|
# Reset context back to text |
306 |
|
$self->{tagcontext}="text"; |
313 |
|
my $rt; |
314 |
|
my $found=0; |
315 |
|
|
316 |
+ |
# print "TAGROUTINE: ",$tagroutine,"\n"; |
317 |
+ |
|
318 |
|
if ( $self->{groupchecker}->status() || |
319 |
|
( $self->{tagcontainer}->inquiregroup($tagroutine)) ) { |
320 |
|
($rt,$obj)=$self->{tagcontainer}->getroutine($tagroutine); |
365 |
|
my $this; |
366 |
|
|
367 |
|
undef @tempstack; |
292 |
– |
#print "In ----".$#{$stack}; |
368 |
|
# Keep popping until we find our string |
369 |
|
while ( ($this=(pop @{$stack})) ne "$name") { |
370 |
|
push @tempstack, $this; |
375 |
|
$this=pop @tempstack; |
376 |
|
push @{$stack}, $this; |
377 |
|
} |
303 |
– |
#print " Out ----".$#{$stack}; |
378 |
|
} |
379 |
|
|
380 |
|
# |
428 |
|
sub _resetlabels { |
429 |
|
my $self=shift; |
430 |
|
undef $self->{tagvar}; |
431 |
+ |
undef $self->{tagname}; |
432 |
|
} |
433 |
|
|
434 |
|
sub _closelabel { |
436 |
|
|
437 |
|
# Do we have a label name? |
438 |
|
if ( $self->{lastlabel} ne "" ) { |
439 |
< |
$self->{tagvar}{$self->{lastlabel}}=$self->_getstore(); |
439 |
> |
(my $label=$self->{lastlabel})=~tr[A-Z][a-z]; |
440 |
> |
$self->{tagvar}{$label}=$self->_getstore(); |
441 |
|
$self->{lastlabel}=""; |
442 |
|
} |
443 |
|
elsif ( $self->_getstore() ne "") { |
444 |
< |
#Then it must be the tag name |
445 |
< |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
444 |
> |
# Then it must be the tag name |
445 |
> |
if ( ! defined $self->{tagname} ) { |
446 |
> |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
447 |
> |
} |
448 |
> |
else { |
449 |
> |
# do not die anymore - breaks non tag documents |
450 |
> |
#die ">Tag syntax error in $self->{tagname} on line ". |
451 |
> |
# $self->line()." of file \n$self->{filename}"; |
452 |
> |
# -- assume that this is plain text |
453 |
> |
$self->{tagcontext}="text"; |
454 |
> |
$self->_resetstore(); |
455 |
> |
$self->_unshiftstore($self->{tagbuff}); |
456 |
> |
$self->{tagbuff}=""; |
457 |
> |
return; |
458 |
> |
} |
459 |
|
} |
460 |
|
$self->_resetstore(); |
461 |
|
} |
470 |
|
$self->{stringbuff}=$self->{stringbuff}.$char; |
471 |
|
} |
472 |
|
|
473 |
+ |
sub _unshiftstore() { |
474 |
+ |
my $self=shift; |
475 |
+ |
my $char=shift; |
476 |
+ |
|
477 |
+ |
$self->{stringbuff}=$char.$self->{stringbuff}; |
478 |
+ |
} |
479 |
+ |
|
480 |
|
sub _getstore() { |
481 |
|
my $self=shift; |
482 |
|
|