blib/lib/Data/Stag/BaseHandler.pm

filter: Statementstmt 64.7% 184 / 284 Branchbran 50.9% 55 / 108 Conditioncond 31.4% 11 / 35 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 56.8% 29 / 51 Podpod 22.7% 10 / 44 totaltotal 53.6% 291 / 542 CC 73 SCAR 22.5
CC
73
Cov
59%
end_event
27.4
start_element
15.4
err_event
12.0
CRAP 9.5
« blib/script/stag-splitter.pl ↑ Summary blib/lib/Data/Stag/ITextParser.pm »
1 # $Id: BaseHandler.pm,v 1.34 2008/06/03 17:31:15 cmungall Exp $
2 #
3 # This module is maintained by Chris Mungall <cjm@fruitfly.org>
4  
5 =head1 NAME
6  
7 Data::Stag::BaseHandler - Base class for writing tag stream handlers
8  
9 =head1 SYNOPSIS
10  
11 # EXAMPLE 1
12 package MyPersonHandler;
13 use base qw(Data::Stag::BaseHandler);
14  
15 # handler that prints <person> nodes as they are parsed;
16 # after each <person> node is intercepted, it is discarded
17 # (it does not go to form the final tree)
18 sub e_person {
19 my $self = shift;
20 my $node = shift;
21 printf "Person name:%s address:%s\n",
22 $node->sget('name'), $node->sget('address');
23 return; # prune this from tree
24 }
25 1;
26
27 # EXAMPLE 2
28 package MyStatsHandler;
29 use base qw(Data::Stag::BaseHandler);
30  
31 # handler that modifies tree as it goes
32 # changes <measurement><unit>inch</unit><quantity>10</quantity></measurement>
33 # to <measurement><unit>cm</unit><quantity>25</quantity></measurement>
34 sub e_measurement {
35 my $self = shift;
36 my $node = shift;
37 if ($node->sget('unit') eq 'inch') {
38 $node->set('unit', 'cm');
39 $node->set('quantity', $node->get('quantity') * 2.5);
40 }
41 return $node; # replace <measurement> with new data in result tree
42 }
43 1;
44
45 # Using the handlers
46 my $handler = MyHandler->new;
47 my $stag = Data::Stag->parse(-fh=>$fh, -handler=>$handler);
48  
49 # Using a handler from the command line:
50 unix> stag-handle.pl -m MyHandler input.xml > post-processed.xml
51  
52 =cut
53  
54 =head1 DESCRIPTION
55  
56 Default Simple Event Handler, other handlers inherit from this class
57  
58 See also L<Data::Stag> and L<Data::Stag::BaseGenerator>
59  
60 Stag has an event-handling architecture; parsers or generators
61 B<generate> or B<fire> events. Events can be hierarchical/nested, just
62 like stag nodes. These events are caught by handlers. By default,
63 uncaught events stack to form stag trees.
64  
65 Stag has built in parsers for parsing xml, sxpr and itext data. You
66 can construct your own parsers for dealing with your own formats
67 specific to your own data; these should inherit from
68 L<Data::Stag::BaseGenerator>
69  
70 Stag also has built in handlers for these formats. You can construct
71 your own - either as modules that inherit from this one, or as hashes
72 of anonymous subroutines.
73  
74 If you wish to write your own handler that writes out to another
75 format, you may wish to inherit from L<Data::Stag::Writer>
76  
77 =head2 CATCHING EVENTS
78  
79 This class catches Data::Stag node events (start, end and body) and allows the
80 subclassing module to intercept these. Unintercepted events get pushed
81 into a tree. The final tree is returned at the end of a parse() call
82  
83 This class can take SAX events and turn them into simple
84 Data::Stag events
85  
86 the events recognised are
87  
88 start_event(node-name)
89 evbody(node-data)
90 end_event(node-name)
91  
92 and also
93  
94 event(node-name, node-data|[nodes])
95  
96 which is just a wrapper for the other events
97  
98 you can either intercept these methods; or you can define methods
99  
100  
101 s_<element_name>
102 e_<element_name>
103  
104 that get called on the start/end of an event; you can dynamically
105 change the structure of the tree by returning nodes from these methods.
106  
107 # the follow handler prunes <foo> nodes from the tree, and writes
108 # out data from the <person> node
109 # when parsing large datasets, it can be a good idea to prune nodes
110 # from the tree, so the result tree of the parse is not too big
111 my $h = Data::Stag->makehandler( foo => 0,
112 person => sub {
113 my $self = shift;
114 my $node = shift;
115 printf "Person name:%s address:%s\n",
116 $node->sget('name'), $node->sget('address');
117 return;
118 });
119 my $parser = MyParser->new;
120 $parser->handler($h);
121 $parser->parse(-fh=>$fh);
122 my $result_tree = $h->stag;
123  
124  
125 =head1 PUBLIC METHODS -
126  
127 =head3 new
128  
129 Title: new
130  
131 Args:
132 Return: L<Data::Stag::BaseHandler>
133 Example:
134  
135 returns the tree that was built from all uncaught events
136  
137 =head3 tree (stag)
138  
139 Title: tree
140 Synonym: stag
141  
142 Args:
143 Return: L<Data::Stag>
144 Example: print $parser->handler->tree->xml;
145  
146 returns the tree that was built from all uncaught events
147  
148 =head1 CAUGHT EVENTS
149  
150 A L<Data::Stag::BaseGenerator> class will generate events by calling the following methods on this class:
151  
152 =over
153  
154 =item start_event NODENAME
155  
156 =item evbody DATA
157  
158 =item end_event NODENAME {optional}
159  
160 =item event NODENAME DATA
161  
162 =back
163  
164 These events can be nested/hierarchical
165  
166 If uncaught, these events are stacked into a stag tree, which can be
167 written as xml or one of the other stag formats
168  
169  
170 =head1 PROTECTED METHODS -
171  
172 =head3 s_*
173  
174 Args: handler L<Data::Stag::BaseHandler>
175 Return:
176 Example:
177  
178 autogenerated method - called by the parser when ever it starts a
179 node; * matches the node name
180  
181 override this class providing the name of the node you wish to intercept
182  
183 =head3 e_*
184  
185 Args: handler L<Data::Stag::BaseHandler>, node L<Data::Stag>
186 Return: node L<Data::Stag>
187 Example:
188  
189 autogenerated method - called by the parser when ever it ends a
190 node; * matches the node name
191  
192 override this class providing the name of the node you wish to intercept
193  
194 =head3 CONSUMES
195  
196 define this in your handler class to make explicit the list of node
197 names that your parser consumes; this is then used if your handler is
198 placed in a chain
199  
200 package MyHandler;
201 use base qw(Data::Stag::BaseHandler);
202 sub CONSUMES {qw(person city)}
203 sub e_person {....}
204 sub e_city {....}
205  
206 =head3 depth
207  
208 Title: depth
209  
210 Args:
211 Return: depth int
212 Example:
213  
214 depth of the nested event tree
215  
216 =head3 up
217  
218 Title: up
219  
220 Args: dist int
221 Return: node stag
222 Example: $stag->up(-2);
223  
224 when called when intercepting a node <foo>, this will look B<dist> up
225 the tree to find the container node
226  
227 For example, if our data contains the node <blah> below:
228  
229 <blah>
230 <foo>
231 <a>1</b>
232 </foo>
233 <foo>
234 <a>2</b>
235 </foo>
236 </blah>
237  
238 # and we have the following code:
239 $h = Data::Stag->makehandler(foo=>sub {
240 my ($self, $foo) = @_;
241 print $foo->up(1)->xml;
242 return});
243  
244 The <foo> handler will be called twice; it will print the structure of
245 the containing <blah> node, but the first time round, the <blah> node
246 will not be complete
247  
248 =head3 up_to
249  
250 Title: up_to
251  
252 Args: nodename str
253 Return: node stag
254 Example: $stag->up_to('blah');
255  
256 Similar to up(), but it will go up the container event nodes until it
257 finds one with the matching name
258  
259 =cut
260  
261 package Data::Stag::BaseHandler;
262  
263 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 20 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 #use XML::Filter::Base;
266 20 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
267 20 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
268 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 20 ▶use Data::Stag;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 20 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
272 20 $VERSION="0.14";
273  
274 0 ▶sub EMITS { () }
Subroutine EMITS: not called CC=1 SCAR=6.9
Pod undocumented
275 0 ▶sub CONSUMES { () }
Subroutine CONSUMES: not called CC=1 SCAR=6.9
Pod documented
276 1029 ▶sub REPLACE { () }
Subroutine REPLACE: called CC=1 SCAR=0.0
Pod undocumented
277 1029 ▶sub SKIP { () }
Subroutine SKIP: called CC=1 SCAR=0.0
Pod undocumented
278  
279 535 ▶sub tree {
Subroutine tree: called CC=3 SCAR=11.0
Pod documented
280 535 my $self = shift;
281 535 ▶ $self->{_tree} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 512 23 535
282 535 ▶ return Data::Stag::stag_nodify($self->{_tree} || []);
Condition100%2/2
$self->{'_tree'} || []
l!l
5332
MC/DC100%1/1
$self->{'_tree'} || []
$self->{'_tree'}
Truth table100%2/2
A
A= $self->{'_tree'}
Aresult
11
00
283 }
284 20 *stag = \&tree;
285  
286 # deprecated
287 93 ▶sub messages {
Subroutine messages: called CC=2 SCAR=7.1
Pod undocumented
288 93 my $self = shift;
289 93 ▶ $self->{_messages} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 93 0 93
290 93 return $self->{_messages};
291 }
292  
293 20 *error_list = \&messages;
294  
295 0 ▶sub message {
Subroutine message: not called CC=1 SCAR=6.9
Pod undocumented
296 0 my $self = shift;
297 0 push(@{$self->messages},
298 shift);
299 }
300  
301  
302 95 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
303 95 my ($class, @args) = @_;
304 # my $self = XML::Filter::Base::new(@_);
305 95 my $self = {};
306 95 bless $self, $class;
307 95 $self->{node} = [];
308 95 $self->{_stack} = [];
309 95 ▶ $self->init(@args) if $self->can("init");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init") 95 0 95
310 95 $self;
311 }
312  
313 17 ▶sub errhandler {
Subroutine errhandler: called CC=2 SCAR=7.6
Pod undocumented
314 17 my $self = shift;
315 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 17 17
316 0 $self->{errhandler} = shift;
317 }
318 17 return $self->{errhandler};
319 }
320  
321 0 ▶sub err_event {
Subroutine err_event: not called CC=3 SCAR=24.8
Pod undocumented
322 0 my $self = shift;
323 0 ▶ if (!$self->errhandler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errhandler) 0 0 0
324 0 $self->errhandler(Data::Stag->getformathandler('xml'));
325 0 $self->errhandler->fh(\*STDERR);
326
327 # my $estag = Data::Stag->new(@_);
328 # eval {
329 # confess;
330 # };
331 # $estag->set_stacktrace($@);
332 # print STDERR $estag->xml;
333 # print STDERR "NO ERRHANDLER SET\n";
334 # exit 1;
335 }
336 0 ▶ if (!$self->errhandler->depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errhandler->depth) 0 0 0
337 0 $self->errhandler->start_event("error_eventset");
338 }
339 0 $self->errhandler->event(@_);
340 0 return;
341 }
342  
343 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
344 0 my $self = shift;
345 0 confess("@_");
346 }
347  
348 0 ▶sub err {
Subroutine err: not called CC=2 SCAR=17.9
Pod undocumented
349 0 my $self = shift;
350 0 my $err = shift;
351 0 ▶ if (ref($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err) 0 0 0
352 0 $self->throw("Bad error msg $err - must not by ref");
353 }
354 0 $self->err_event(message=>$err);
355 0 return;
356 }
357  
358 5 ▶sub trap_h {
Subroutine trap_h: called CC=2 SCAR=7.1
Pod undocumented
359 5 my $self = shift;
360 5 ▶ $self->{_trap_h} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 0 5
361 5 return $self->{_trap_h};
362 }
363  
364 0 ▶sub catch_end_sub {
Subroutine catch_end_sub: not called CC=2 SCAR=17.9
Pod undocumented
365 0 my $self = shift;
366 0 ▶ $self->{_catch_end_sub} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
367 0 return $self->{_catch_end_sub};
368 }
369  
370  
371 3407 ▶sub stack {
Subroutine stack: called CC=2 SCAR=7.1
Pod undocumented
372 3407 my $self = shift;
373 3407 ▶ $self->{_stack} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3407 3407
374 3407 return $self->{_stack};
375 }
376 20 *elt_stack = \&stack;
377  
378 0 ▶sub in {
Subroutine in: not called CC=2 SCAR=17.9
Pod undocumented
379 0 my $self = shift;
380 0 my $in = shift;
381 0 ▶ return 1 if grep {$in eq $_} @{$self->stack};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$in eq $_;} @{$self->stack;} 0 0 0
382 }
383  
384 59 ▶sub depth {
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
385 59 my $self = shift;
386 59 return scalar(@{$self->stack});
387 }
388  
389  
390 2 ▶sub node {
Subroutine node: called CC=2 SCAR=7.1
Pod undocumented
391 2 my $self = shift;
392 2 ▶ $self->{node} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
393 2 return $self->{node};
394 }
395  
396 0 ▶sub remove_elts {
Subroutine remove_elts: not called CC=3 SCAR=24.8
Pod undocumented
397 0 my $self = shift;
398 0 ▶ $self->{_remove_elts} = [@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
399 0 ▶ return @{$self->{_remove_elts} || []};
Condition0%0/2
$self->{'_remove_elts'} || []
l!l
00
MC/DC0%0/1
$self->{'_remove_elts'} || []
$self->{'_remove_elts'}
Truth table0%0/2
A
A= $self->{'_remove_elts'}
Aresult
11
00
400 }
401 20 *kill_elts = \&remove_elts;
402  
403 0 ▶sub flatten_elts {
Subroutine flatten_elts: not called CC=3 SCAR=24.8
Pod undocumented
404 0 my $self = shift;
405 0 ▶ $self->{_flatten_elts} = [@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
406 0 ▶ return @{$self->{_flatten_elts} || []};
Condition0%0/2
$self->{'_flatten_elts'} || []
l!l
00
MC/DC0%0/1
$self->{'_flatten_elts'} || []
$self->{'_flatten_elts'}
Truth table0%0/2
A
A= $self->{'_flatten_elts'}
Aresult
11
00
407 }
408  
409 0 ▶sub skip_elts {
Subroutine skip_elts: not called CC=3 SCAR=24.8
Pod undocumented
410 0 my $self = shift;
411 0 ▶ $self->{_skip_elts} = [@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
412 0 ▶ return @{$self->{_skip_elts} || []};
Condition0%0/2
$self->{'_skip_elts'} || []
l!l
00
MC/DC0%0/1
$self->{'_skip_elts'} || []
$self->{'_skip_elts'}
Truth table0%0/2
A
A= $self->{'_skip_elts'}
Aresult
11
00
413 }
414 20 *raise_elts = \&skip_elts;
415  
416 0 ▶sub rename_elts {
Subroutine rename_elts: not called CC=3 SCAR=24.8
Pod undocumented
417 0 my $self = shift;
418 0 confess "experimental feature - deprecated";
419 0 ▶ $self->{_rename_elts} = {@_} if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
420 0 ▶ return %{$self->{_rename_elts} || {}};
Condition0%0/2
$self->{'_rename_elts'} || {}
l!l
00
MC/DC0%0/1
$self->{'_rename_elts'} || {}
$self->{'_rename_elts'}
Truth table0%0/2
A
A= $self->{'_rename_elts'}
Aresult
11
00
421 }
422  
423 0 ▶sub lookup {
Subroutine lookup: not called CC=2 SCAR=17.9
Pod undocumented
424 0 my $tree = shift;
425 0 my $k = shift;
426 0 my @v = map {$_->[1]} grep {$_->[0] eq $k} @$tree;
427 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
428 0 return @v;
429 }
430 0 $v[0];
431 }
432  
433 93 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
434 93 my $self = shift;
435 93 $self->messages([]);
436 93 $self->{node} = [];
437 }
438  
439 0 ▶sub perlify {
Subroutine perlify: not called CC=1 SCAR=6.9
Pod undocumented
440 0 my $word = shift;
441 0 $word =~ s/\-/_/g;
442 0 $word =~ s/\:/_/g;
443 0 return $word;
444 }
445  
446 # start_event is called at the beginning of any event;
447 # equivalent to the event fired at the opening of any
448 # xml <tag> in a SAX parser
449  
450 # action: checks for method of name s_EVENTNAME()
451 # calls it if it is present
452 517 ▶sub start_event {
Subroutine start_event: called CC=6 SCAR=21.2
Pod documented
453 517 my $self = shift;
454 517 my $ev = shift;
455 0 ▶ if (grep {$ev eq $_} $self->SKIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$ev eq $_;} $self->SKIP) 0 517 517
456 0 return;
457 }
458 517 my %REPLACE = $self->REPLACE;
459 517 ▶ if (%REPLACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (%REPLACE) 0 517 517
460 0 ▶ $ev = $REPLACE{$ev} || $ev;
Condition0%0/3
$REPLACE{$ev} || $ev
l!l&&r!l&&!r
000
MC/DC0%0/2
$REPLACE{$ev} || $ev
$REPLACE{$ev} $ev
Truth table0%0/3
A || B
A= $REPLACE{$ev}B= $ev
ABresult
1X1
011
000
461 }
462 517 my $m = 's_'.$ev;
463 517 $m =~ tr/\-\:/_/;
464  
465 517 ▶ if ($self->can($m)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can($m)) { } 0 517 517
elsif ($self->can('catch_start')) { } 0 517 517
466 0 $self->$m($ev);
467 }
468 elsif ($self->can("catch_start")) {
469 0 $self->catch_start($ev);
470 }
471 else {
472 }
473  
474 517 push(@{$self->{_stack}}, $ev);
475  
476 517 my $el = [$ev];
477 517 push(@{$self->{node}}, $el);
478 517 $el;
479 }
480  
481 # deprecated
482 0 ▶sub S {shift->start_event(@_)}
Subroutine S: not called CC=1 SCAR=6.9
Pod undocumented
483  
484  
485 330 ▶sub evbody {
Subroutine evbody: called CC=4 SCAR=14.2
Pod documented
486 330 my $self = shift;
487 330 foreach my $arg (@_) {
488 330 ▶ if (ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 0 330 330
489 0 $self->event(@$arg);
490 }
491 else {
492 330 my $node = $self->{node};
493 330 my $el = $node->[$#{$node}];
494 330 ▶ confess unless $el;
Branch: 1 branch
BranchTrueFalseTotal
unless $el 0 330 330
495 330 $el->[1] = $arg;
496 }
497 }
498 330 return;
499 }
500  
501 # deprecated
502 0 ▶sub B {shift->evbody(@_)}
Subroutine B: not called CC=1 SCAR=6.9
Pod undocumented
503 0 ▶sub b {shift->evbody(@_)}
Subroutine b: not called CC=1 SCAR=6.9
Pod undocumented
504  
505 0 ▶sub up {
Subroutine up: not called CC=2 SCAR=17.9
Pod documented
506 0 my $self = shift;
507 0 ▶ my $dist = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
508 0 my $node = $self->node->[-$dist];
509 0 return Data::Stag::stag_nodify($node);
510 }
511  
512 2 ▶sub up_to {
Subroutine up_to: called CC=4 SCAR=14.8
Pod documented
513 2 my $self = shift;
514 2 ▶ my $n = shift || confess "must specify node name";
Condition50%1/2
shift() || confess('must specify node name')
l!l
20
MC/DC0%0/1
shift() || confess('must specify node name')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
515 2 ▶ my $nodes = $self->node || [];
Condition50%1/2
$self->node || []
l!l
20
MC/DC0%0/1
$self->node || []
$self->node
Truth table50%1/2
A
A= $self->node
Aresult
11
00
516 2 my ($node) = grep {$_->[0] eq $n} @$nodes;
517 confess " no such node name as $n; valid names are:".
518 0 ▶ join(", ", map {$_->[0]} @$nodes)
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 2 2
519 unless $node;
520 2 return Data::Stag::stag_nodify($node);
521 }
522  
523 # end_event is called at the end of any event;
524 # equivalent to the event fired at the closing of any
525 # xml </tag> in a SAX parser
526  
527 # action: checks for method of name e_EVENTNAME()
528 # calls it if it is present
529 512 ▶sub end_event {
Subroutine end_event: called CC=21 SCAR=33.1
Pod documented
530 512 my $self = shift;
531 512 ▶ my $ev = shift || '';
Condition50%1/2
shift() || ''
l!l
5120
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
532  
533 0 ▶ if (grep {$ev eq $_} $self->SKIP) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$ev eq $_;} $self->SKIP) 0 512 512
534 0 return;
535 }
536 512 my %REPLACE = $self->REPLACE;
537 512 ▶ if (%REPLACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (%REPLACE) 0 512 512
538 0 ▶ $ev = $REPLACE{$ev} || $ev;
Condition0%0/3
$REPLACE{$ev} || $ev
l!l&&r!l&&!r
000
MC/DC0%0/2
$REPLACE{$ev} || $ev
$REPLACE{$ev} $ev
Truth table0%0/3
A || B
A= $REPLACE{$ev}B= $ev
ABresult
1X1
011
000
539 }
540  
541 512 my $stack = $self->{_stack};
542 512 pop(@$stack);
543  
544 512 my $node = $self->{node}; # array of (0..$indent)
545 512 my $topnode = pop @$node; # node we are closing now
546  
547 # my %rename = $self->rename_elts;
548 # if ($rename{$ev}) {
549 # $ev = $rename{$ev};
550 # $topnode->[0] = $ev;
551 # }
552
553 512 ▶ if (!ref($topnode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $topnode) 0 512 512
554 0 confess("ASSERTION ERROR: $topnode not an array");
555 }
556 512 ▶ if (scalar(@$topnode) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$topnode < 2) 3 509 512
557 # NULLs are treated the same as
558 # empty strings
559 # [if we have empty tags <abcde></abcde>
560 # then no evbody will be called - we have to
561 # fill in the equivalent of a null evbody here]
562 # push(@$topnode, '');
563 3 push(@$topnode, '');
564 }
565 512 my $topnodeval = $topnode->[1];
566  
567 512 my @R = ($topnode); # return
568  
569 # check for trapped events; trap_h is a hash keyed by node name
570 # the value is a subroutine to be called at the end of that node
571 512 my $trap_h = $self->{_trap_h};
572 512 ▶ if ($trap_h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trap_h) 126 386 512
573 126 my $trapped_ev = $ev;
574 126 my @P = @$stack;
575 126 ▶ while (!defined($trap_h->{$trapped_ev}) && scalar(@P)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $trap_h->{$trapped_ev} and scalar @P) 296 126 422
Condition100%2/2
not defined $trap_h->{$trapped_ev} and scalar @P
!ll
19403
MC/DC100%1/1
not defined $trap_h->{$trapped_ev} and scalar @P
not defined $trap_h->{$trapped_ev}
Truth table100%2/2
not(A)
A= not defined $trap_h->{$trapped_ev}
Aresult
00
11
576 296 my $next = pop @P;
577 296 $trapped_ev = "$next/$trapped_ev";
578 }
579  
580 126 ▶ if (defined($trap_h->{$trapped_ev})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trap_h->{$trapped_ev}) 19 107 126
581 19 ▶ if ($trap_h->{$trapped_ev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trap_h->{$trapped_ev}) { } 18 1 19
582 # call anonymous subroutine supplied in hash
583 18 @R = $trap_h->{$trapped_ev}->($self, Data::Stag::stag_nodify($topnode));
584 }
585 else {
586 1 @R = ();
587 }
588 }
589 }
590  
591 512 my $m = 'e_'.$ev;
592 512 $m =~ tr/\-\:/_/;
593  
594 512 ▶ if ($self->can($m)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->can($m)) { } 0 512 512
elsif ($self->can('catch_end')) { } 0 512 512
elsif ($self->{'_catch_end_sub'}) { } 0 512 512
595 0 @R = $self->$m(Data::Stag::stag_nodify($topnode));
596 }
597 elsif ($self->can("catch_end")) {
598 0 @R = $self->catch_end($ev, Data::Stag::stag_nodify($topnode));
599 }
600 elsif ($self->{_catch_end_sub}) {
601 0 @R = $self->{_catch_end_sub}->($self, Data::Stag::stag_nodify($topnode));
602 }
603 else {
604 # do nothing
605 }
606  
607 512 ▶ if (@$node) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$node) 494 18 512
608 494 my $el = $node->[-1]; # next node up
609 494 ▶ if (!$el->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($el->[1]) 184 310 494
610 184 $el->[1] = [];
611 }
612
613 494 ▶ if (scalar(@R) && !$R[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @R and not $R[0]) 0 494 494
Condition66%2/3
scalar @R and not $R[0]
!ll&&!rl&&r
64880
MC/DC0%0/2
scalar @R and not $R[0]
scalar @R not $R[0]
Truth table66%2/3
A and not(B)
A= scalar @RB= not $R[0]
ABresult
0X0
100
111
614 0 @R = ();
615 }
616 494 push(@{$el->[1]}, @R);
617 }
618  
619 512 $self->tree(Data::Stag::stag_nodify($topnode));
620 512 ▶ if (!@$stack) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$stack) 16 496 512
621 # final event; call end_stag if required
622 16 ▶ if ($self->can("end_stag")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("end_stag")) 0 16 16
623 0 $self->end_stag($self->tree);
624 }
625 }
626  
627 512 return @R;
628 }
629 0 ▶sub E {shift->end_event(@_)}
Subroutine E: not called CC=1 SCAR=6.9
Pod undocumented
630 0 ▶sub e {shift->end_event(@_)}
Subroutine e: not called CC=1 SCAR=6.9
Pod undocumented
631  
632  
633 5 ▶sub popnode {
Subroutine popnode: called CC=1 SCAR=0.0
Pod undocumented
634 5 my $self = shift;
635 5 my $node = $self->{node};
636 5 my $topnode = pop @$node;
637 5 return $topnode;
638 }
639  
640 925 ▶sub event {
Subroutine event: called CC=5 SCAR=16.3
Pod documented
641 925 my $self = shift;
642 925 my $ev = shift;
643 925 my $st = shift;
644 925 $self->start_event($ev);
645 925 ▶ if (ref($st)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $st) { } 306 619 925
646 0 ▶ if (ref($st) ne "ARRAY") {confess($st)}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $st ne "ARRAY") 0 306 306
647 306 foreach (@$st) {
648 884 ▶ confess("$ev $st $_") unless ref($_);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 884 884
649 884 $self->event(@$_)
650 }
651 }
652 else {
653 619 $self->evbody($st);
654 }
655 925 $self->end_event($ev);
656 }
657 20 *ev = \&event;
658  
659  
660 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod undocumented
661 0 my $self = shift;
662 0 print "@_";
663 }
664  
665 0 ▶sub printf {
Subroutine printf: not called CC=1 SCAR=6.9
Pod undocumented
666 0 my $self = shift;
667 0 printf @_;
668 }
669  
670  
671 37 ▶sub start_element {
Subroutine start_element: called CC=9 SCAR=27.3
Pod undocumented
672 37 my ($self, $element) = @_;
673  
674 37 my $name = $element->{Name};
675 37 my $atts = $element->{Attributes};
676  
677 37 ▶ if (!$self->{sax_stack}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sax_stack"}) 1 36 37
678 1 $self->{sax_stack} = [];
679 }
680 37 push(@{$self->{sax_stack}}, $name);
681 37 push(@{$self->{is_nonterminal_stack}}, 0);
682 37 ▶ if (@{$self->{is_nonterminal_stack}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"is_nonterminal_stack"};} > 1) 36 1 37
683 36 $self->{is_nonterminal_stack}->[-2] = 1;
684 }
685  
686 # check if we need an event
687 # for any preceeding pcdata
688 37 my $str = $self->{__str};
689 37 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 0 37 37
690 # mixed attribute text - use element '.'
691 0 $str =~ s/^\s*//;
692 0 $str =~ s/\s*$//;
693 0 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 0 0
694 0 ▶ $self->event(".", $str) if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 0 0 0
695 }
696 0 $self->{__str} = undef;
697 }
698  
699 37 $self->start_event($name);
700 37 ▶ if ($atts && %$atts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts and %$atts) 0 37 37
Condition33%1/3
$atts and %$atts
!ll&&!rl&&r
3700
MC/DC0%0/2
$atts and %$atts
$atts %$atts
Truth table33%1/3
A and B
A= $attsB= %$atts
ABresult
0X0
100
111
701 # treat atts same way as SXML
702 0 $self->start_event('@');
703 0 foreach my $k (keys %$atts) {
704 0 $self->event("$k", $atts->{$k});
705 0 $self->{is_nonterminal_stack}->[-1] = 1;
706 }
707 0 $self->end_event('@');
708 }
709 37 return $element;
710 }
711  
712 24 ▶sub characters {
Subroutine characters: called CC=3 SCAR=11.2
Pod undocumented
713 24 my ($self, $characters) = @_;
714 24 my $char = $characters->{Data};
715 24 ▶ if (defined $char) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char) 24 0 24
716 24 ▶ $self->{__str} = "" unless defined $self->{__str};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__str"} 24 0 24
717 24 $self->{__str} .= $char;
718 }
719 24 return;
720 }
721  
722 37 ▶sub end_element {
Subroutine end_element: called CC=5 SCAR=16.5
Pod undocumented
723 37 my ($self, $element) = @_;
724 37 my $name = $element->{Name};
725 37 my $str = $self->{__str};
726 37 my $parent = pop(@{$self->{sax_stack}});
727 37 my $is_nt = pop(@{$self->{is_nonterminal_stack}});
728 37 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 24 13 37
729 24 $str =~ s/^\s*//;
730 24 $str =~ s/\s*$//;
731 24 ▶ if ($str || $str eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($str or $str eq 0) 24 0 24
Condition33%1/3
$str or $str eq 0
l!l&&r!l&&!r
2400
MC/DC0%0/2
$str or $str eq 0
$str $str eq 0
Truth table33%1/3
A or B
A= $strB= $str eq 0
ABresult
1X1
011
000
732 24 ▶ if ($is_nt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nt) { } 0 24 24
733 0 $self->event('.' =>
734 $str);
735
736 }
737 else {
738 24 $self->evbody($str);
739 }
740 }
741 }
742 37 $self->end_event($name);
743 37 $self->{__str} = undef;
744 # $self->{Handler}->end_element($element);
745 }
746  
747 1 ▶sub start_document {
Subroutine start_document: called CC=1 SCAR=0.0
Pod undocumented
748 }
749  
750 1 ▶sub end_document {
Subroutine end_document: called CC=1 SCAR=0.0
Pod undocumented
751 }
752  
753  
754 20 1
« blib/script/stag-splitter.pl ↑ Summary blib/lib/Data/Stag/ITextParser.pm »