ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/Switcher.pm
Revision: 1.19
Committed: Tue Oct 15 15:56:21 2002 UTC (22 years, 6 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V0_19_7
Branch point for: SCRAM_V1_BRANCH
Changes since 1.18: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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