Essex/Model.pm

filter: Statementstmt 85.1% 269 / 316 Branchbran 53.6% 44 / 82 Conditioncond 63.8% 67 / 105 MC/DCmcdc 26.9% 17 / 63 Subroutinesub 80.6% 75 / 93 Podpod n/a totaltotal 71.6% 472 / 659 CC 79 SCAR 13.2
CC
79
Cov
76%
isa
12.0
new
12.0
new
9.8
CRAP 3.7
« Essex/Constants.pm ↑ Summary Essex/Event.pm »
1 package XML::Essex::Model;
2  
3 9 $VERSION = 0.000_1;
4  
5 =head1 NAME
6  
7 XML::Essex::Model - Essex objects representing SAX events and DOM trees
8  
9 =head1 SYNOPSIS
10  
11 Used internally by Essex, see below for external API examples.
12  
13 =head1 DESCRIPTION
14  
15 A description of all of the events explicitly supported so far.
16 Unsupported events are still handled as anonymous events, see
17 L<XML::Essex::Event|XML::Essex::Event> for details.
18  
19 =head2 A short word on abbreviations
20  
21 A goal of essex is to allow code to be as terse or verbose as is
22 appropriate for the job at hand. So almost every object may be
23 abbreviated. So C<start_element> may be abbreviated as C<start_elt> for
24 both the C<isa()> method/function and for class creation.
25  
26 All objects are actually blessed in to classes using the long name, like
27 C<XML::Essex::start_element> even if you use an abbreviation like
28 C<XML::Essex::start_elt->new> to create them.
29  
30 =head2 Stringification
31  
32 All events are stringifiable for debugging purposes and so that
33 attribute values, character data, comments, and processing instructions
34 may be matched with Perl string operations like regular expressions and
35 C<index()>. It is usually more effective to use EventPath, but you can
36 use stringified values for things like complex regexp matching.
37  
38 It is unwise to match other events with string operators because no XML
39 escaping of data is done, so "<" in an attribute value or character data
40 is stringified as "<", so C<print()>ing out the three events associated
41 with the sequence "<foo>&lt;bar/></foo>" will look like
42 "<foo><bar/></foo>", obviously not what the document intended. Given
43 the rarity of such constructs in real life XML, though, this is
44 sufficient for debugging purposes, and does make it easy to match
45 against strings.
46  
47 Ordinarily, you tell C<get()> what kind of object you want using an
48 EventPath expression:
49  
50 my $start_element = get "start_element::*";
51  
52 You can also just C<get()> whatever's next in the document or use a
53 union expression. In this case, you may need to see what you've gotten.
54 The C<isa()> method (see below) and the C<isa()> functions (see
55 L<XML::Essex|XML::Essex/isa>) should be used to figure out what type of
56 object is being used before relying on the stringification:
57  
58 get until isa "chars" and /Bond, James Bond/;
59 get until type eq "characters" and /Bond, James Bond/;
60 get until isa( "chars" ) && /Bond, James Bond/;
61 get "text()" until /Bond, James Bond/;
62  
63 This makes it easier to match characters data, but other methods
64 should be used to select things like start tags and elements:
65  
66 get "start_element::*" until $_->name eq "address" && $_->{id} eq $id;
67 get "start_element::address" until $_->{id} eq $id;
68  
69 The lack of escaping only affects stringification of objects, for
70 instance:
71  
72 warn $_; ## See what event is being dealt with right now
73 /Bond, James Bond/ ## Match current event
74  
75 . Things are escaped properly when the put operator is used, using
76 C<put()> emits properly escaped XML.
77  
78 Some observervations:
79  
80 =over
81  
82 =item *
83  
84 Stringifying an event does not produce a well formed chunk of
85 XML. Events must be emitted through a downstream filter.
86  
87 =item *
88  
89 Events with no natural XML representation--like
90 start_document--stringify as their name: "start_document()". If it's
91 not listed on this page, it stringifies this way.
92  
93 =item *
94  
95 Whitespace is inserted only where manditory inside XML constructs, and
96 is a single space. It is left unmolested in character data, comments,
97 processing instructions (other than C<< <?xml ...?> >>, which is parsed
98 by all XML parsers).
99  
100 =item *
101  
102 Attributes in start_element events are stringified in alphabetical order
103 according to Perl's C<sort()> function.
104  
105 =item *
106  
107 Processing instructions, including the C<< <?xml...?> >> declaration,
108 often have things that look like attributes but are not, so the items
109 above about whitespace and attribute sort order do not apply. Actually,
110 the C<< <?xml ... ?> >> declaration is well defined and there will
111 be only a single whitespace character, though the pseudo-attributes
112 version, encoding and standalone will not be sorted.
113  
114 =item *
115  
116 No escapes are used. See above.
117  
118 =item *
119  
120 Character data is catenated, including mixed data and CDATA, in to
121 single strings. CDATA sections are tracked and may be analyzed.
122  
123 =item *
124  
125 Namespaces are stringified according to any prefixes that have been
126 registered, otherwise they stringify in james clark notation
127 (C<"{}foo">), except for the empty namespace URI, which alway
128 stringifies as "" (ie no prefix). See L<XML::Essex's Namspaces
129 section|XML::Essex/Namespaces> for details.
130  
131 =back
132  
133 =head1 Common Methods
134  
135 All of the objects in the model provide the following methods. These
136 methods are exported as functions from the L<XML::Essex|XML::Essex>
137 module for convenience (those functions are wrappers around these
138 methods).
139  
140 =over
141  
142 =item isa
143  
144 Returns TRUE if the object is of the type, abbreviated type, or class
145 passed. So, for an object encapsulating a characters event, returns
146 TRUE for any of:
147  
148 XML::Essex::Event ## The base class for all events
149 XML::Essex::start_document ## The actuall class name
150 start_document ## The event type
151 start_doc ## The event type, abbreviated
152  
153 =item class
154  
155 Returns the class name, such as C<XML::Essex::start_document>.
156  
157 =item type
158  
159 Returns the class name, such as C<start_document>.
160  
161 =item types
162  
163 Returns the class name, the type name and any abbreviations. The
164 abbreviations are sorted from longest to shortest.
165  
166 =back
167  
168 =head1 start_document
169  
170 aka: start_doc
171  
172 my $e = start_doc \%values; ## %values is not defined in SAX1/SAX2
173  
174 Stringifies as: C<< start_document($reserved) >>
175  
176 where $reserved is a character string that may sometime include
177 info passed in the start_document event, probably formatted as
178 attributes.
179  
180 =cut
181  
182 9 ▶use XML::Essex::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 {
185 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 9 ▶ use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 9 ▶ sub _jclarkify {
Subroutine _jclarkify: called CC=3 SCAR=11.3
189 9 my ( $name ) = @_;
190  
191 9 ▶ return $name if substr( $name, 0, 1 ) eq "{";
Branch: 1 branch
BranchTrueFalseTotal
if substr($name, 0, 1) eq "{" 1 8 9
192  
193 8 ▶ if ( $name =~ /(.*):(.*)/ ) { ## prefix notation
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(.*):(.*)/) 0 8 8
194 ## "TODO: Namespace prefix access for attrs";
195 0 return "{foo}$name";
196 }
197  
198 ## TODO: default to default ns instead of empty ns
199 8 return "{}$name";
200 }
201  
202  
203 2 ▶ sub _split_name {
Subroutine _split_name: called CC=3 SCAR=11.4
204 2 my ( $name ) = @_;
205  
206 2 ▶ return ( $1, $2 ) if /^\{(.*)\}(.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\{(.*)\}(.*)\z/ 0 2 2
207  
208 ## TODO: prefix => URI Namespace mapping
209 2 ▶ return ( "http://foo/", $2 ) if $name =~ /(.*):(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /(.*):(.*)/ 0 2 2
210  
211 ## TODO: default to default ns instead of empty ns
212 2 return ( "", $name );
213 }
214  
215  
216 3 ▶ sub _render_name {
Subroutine _render_name: called CC=5 SCAR=22.7
217 3 my ( $ns, $local_name ) = @_;
218  
219 3 ▶ $local_name = "*UNDEFINED NAME*" unless defined $local_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local_name 0 3 3
220  
221 3 ▶ return $local_name unless defined $ns && length $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns and length $ns 3 0 3
Condition33%1/3
defined $ns and length $ns
!ll&&!rl&&r
030
MC/DC0%0/2
defined $ns and length $ns
defined $ns length $ns
Truth table33%1/3
A and B
A= defined $nsB= length $ns
ABresult
0X0
100
111
222  
223 ## TODO: ns => prefix mapping
224 0 ▶ return "foo:$local_name" if $ns =~ /foo/;
Branch: 1 branch
BranchTrueFalseTotal
if $ns =~ /foo/ 0 0 0
225  
226 0 return "{$ns}$local_name";
227 }
228  
229 3 ▶ sub _render_event_name {
Subroutine _render_event_name: called CC=1 SCAR=0.0
230 3 _render_name @{$_[0]}{qw( NamespaceURI LocalName )};
231 }
232 }
233  
234 {
235 9 package XML::Essex::Event::start_document;
236  
237 9 @ISA = qw( XML::Essex::Event );
238  
239 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 44 ▶ sub type { "start_document" }
Subroutine type: called CC=1 SCAR=0.0
242  
243 0 ▶ sub types { ( __PACKAGE__, "start_document", "start_doc" ) }
Subroutine types: not called CC=1 SCAR=6.9
244  
245 83 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
246 83 my $self = shift;
247 83 ▶ return $_[0] eq "start_document"
Condition100%6/6
$_[0] eq 'start_document' || $_[0] eq 'start_doc' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
36020
$_[0] eq "start_document" or $_[0] eq "start_doc"
l!l&&r!l&&!r
2180
MC/DC100%3/3
$_[0] eq 'start_document' || $_[0] eq 'start_doc' || $self->SUPER::isa(@_)
$_[0] eq "start_document" $_[0] eq "start_doc" $self->SUPER::isa(@_)
Truth table100%4/4
A or B || C
A= $_[0] eq "start_document"B= $_[0] eq "start_doc"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
248 || $_[0] eq "start_doc"
249 || $self->SUPER::isa( @_ );
250 }
251  
252 9 @XML::Essex::Event::start_doc::ISA = qw( XML::Essex::Event::start_document );
253 1 ▶ sub XML::Essex::Event::start_doc::new {
Subroutine new: called CC=3 SCAR=12.5
254 1 my $proto = shift;
255 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::start_doc" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::start_doc"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::start_doc"
not ref $proto $proto eq "XML:Essex::start_doc"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::start_doc"
ABresult
0X0
100
111
256 if ! ref $proto && $proto eq "XML:Essex::start_doc";
257 1 $proto->XML::Essex::Event::start_document::new( @_ )
258 }
259 }
260  
261 =head1 xml_decl
262  
263 aka: (no abbreviations)
264  
265 my $e = xml_decl;
266  
267 my $e = xml_decl
268 Version => "1",
269 Encoding => "UTF-8",
270 Standalone => "yes";
271  
272 my $e = xml_decl {
273 Version => "1",
274 Encoding => "UTF-8",
275 Standalone => "yes"
276 };
277  
278  
279 Stringifies as: C<< <?xml version="$version" encoding="$enc"
280 standalone="$yes_or_no"?> >>
281  
282 Note that this does not follow the sorted attribute order behavior of
283 start_element, as the seeming attributes here are not attributes, like
284 processing instructions that have pretend attributes.
285  
286 =cut
287  
288 {
289 9 package XML::Essex::Event::xml_decl;
290  
291 9 @ISA = qw( XML::Essex::Event );
292  
293 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 9 ▶ use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
296  
297 1 ▶ sub type { "xml_decl" }
Subroutine type: called CC=1 SCAR=0.0
298  
299 0 ▶ sub types { ( __PACKAGE__, "xml_decl" ) }
Subroutine types: not called CC=1 SCAR=6.9
300  
301 1 ▶ sub _stringify {
Subroutine _stringify: called CC=5 SCAR=20.9
302 1 my $self = shift;
303 return join "",
304 qq[<?xml version="$$self->{Version}"],
305 exists $$self->{Encoding} && $$self->{Encoding}
306 ? qq[ encoding="$$self->{Encoding}"] : (),
307 exists $$self->{Standalone} && defined $$self->{Standalone}
308 1 ▶ ? qq[ standalone="$$self->{Standalone}"] : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $$self->{'Encoding'} && $$self->{'Encoding'} ? : 1 0 1
exists $$self->{'Standalone'} && defined $$self->{'Standalone'} ? : 1 0 1
Condition33%2/6
exists $$self->{"Encoding"} and $$self->{"Encoding"}
!ll&&!rl&&r
001
exists $$self->{"Standalone"} and defined $$self->{"Standalone"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $$self->{"Encoding"} and $$self->{"Encoding"}
exists $$self->{"Encoding"} $$self->{"Encoding"}
MC/DC0%0/2
exists $$self->{"Standalone"} and defined $$self->{"Standalone"}
exists $$self->{"Standalone"} defined $$self->{"Standalone"}
Truth table33%1/3
A and B
A= exists $$self->{"Encoding"}B= $$self->{"Encoding"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= exists $$self->{"Standalone"}B= defined $$self->{"Standalone"}
ABresult
0X0
100
111
309 qq[?>];
310 }
311  
312 2 ▶ sub isa {
Subroutine isa: called CC=2 SCAR=7.1
313 2 my $self = shift;
314 2 ▶ return $_[0] eq "xml_decl"
Condition66%2/3
$_[0] eq 'xml_decl' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[0] eq 'xml_decl' || $self->SUPER::isa(@_)
$_[0] eq 'xml_decl' $self->SUPER::isa(@_)
Truth table66%2/3
A || B
A= $_[0] eq 'xml_decl'B= $self->SUPER::isa(@_)
ABresult
1X1
011
000
315 || $self->SUPER::isa( @_ );
316 }
317  
318 }
319  
320 =head1 end_document
321  
322 aka: end_doc
323  
324 my $e = end_doc \%values; ## %values is not defined in SAX1/SAX2
325  
326 Stringifies as: C<< end_document($reserved) >>
327  
328 where $reserved is a character string that may sometime include
329 info passed in the end_document event, probably formatted as
330 attributes.
331  
332 =cut
333  
334 {
335 9 package XML::Essex::Event::end_document;
336  
337 9 @ISA = qw( XML::Essex::Event );
338  
339 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
340  
341 44 ▶ sub type { "end_document" }
Subroutine type: called CC=1 SCAR=0.0
342  
343 0 ▶ sub types { ( __PACKAGE__, "end_document", "end_doc" ) }
Subroutine types: not called CC=1 SCAR=6.9
344  
345 143 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
346 143 my $self = shift;
347 143 ▶ return $_[0] eq "end_document"
Condition100%6/6
$_[0] eq 'end_document' || $_[0] eq 'end_doc' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
163196
$_[0] eq "end_document" or $_[0] eq "end_doc"
l!l&&r!l&&!r
610127
MC/DC100%3/3
$_[0] eq 'end_document' || $_[0] eq 'end_doc' || $self->SUPER::isa(@_)
$_[0] eq "end_document" $_[0] eq "end_doc" $self->SUPER::isa(@_)
Truth table100%4/4
A or B || C
A= $_[0] eq "end_document"B= $_[0] eq "end_doc"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
348 || $_[0] eq "end_doc"
349 || $self->SUPER::isa( @_ );
350 }
351  
352 9 @XML::Essex::Event::end_doc::ISA = qw( XML::Essex::Event::end_document );
353 1 ▶ sub XML::Essex::Event::end_doc::new {
Subroutine new: called CC=3 SCAR=12.5
354 1 my $proto = shift;
355 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::end_doc" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::end_doc"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::end_doc"
not ref $proto $proto eq "XML:Essex::end_doc"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::end_doc"
ABresult
0X0
100
111
356 if ! ref $proto && $proto eq "XML:Essex::end_doc";
357 1 $proto->XML::Essex::Event::end_document::new( @_ )
358 }
359 }
360  
361 =head1 start_element
362  
363 aka: start_elt
364  
365 my $e = start_elt foo => { attr => "val" };
366 my $e = start_elt $start_elt; ## Copy constructor
367 my $e = start_elt $end_elt; ## end_elt deconstructor
368 my $e = start_elt $elt; ## elt deconstructor
369  
370 Stringifies as: C<< <foo attr1="$val1" attr2="val2"> >>
371  
372 The element name and any attribute names are prefixed according to
373 namespace mappings registered in the Essex processor, the prefixes they
374 had in the source document are ignored. If no prefix has been mapped,
375 jclark notation (C<{http:...}foo>) is used. Then they are sorted
376 according to Perl's C<sort()> function, so jclarked attribute names come
377 last, as it happens.
378  
379 TODO: Support attribute ordering via consecutive {...} sets.
380  
381 Attributes may be accessed using hash dereferences:
382  
383 get "start_element::*" until $_->{id} eq "10"; ## No namespace prefix
384 get "start_element::*" until $_->{"{}id"} eq "10";
385 get "start_element::*" until $_->{"{http://foo/}id"} eq "10";
386 get "start_element::*" until $_->{"foo:id"} eq "10";
387  
388 and the attribute names may be obtained by:
389  
390 keys %$_;
391  
392 . Keys are returned in no predictable order, see
393 L<Namespaces|XML::Essex/Namespaces> for details on the three formats
394 keys may be returned in.
395  
396 =head2 Methods
397  
398 =over
399  
400 =cut
401  
402 {
403 9 package XML::Essex::Event::start_element;
404  
405 9 @ISA = qw( XML::Essex::Event );
406  
407 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
408  
409 9 ▶ use overload(
Subroutine BEGIN: called CC=1 SCAR=0.0
410 9 '""' => \&_stringify,
411 '%{}' => \&_hash_deref,
412 9 );
413  
414 25 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
415 my $self = shift->SUPER::new(
416 ! ref $_[0]
417 25 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 18 7 25
418 18 my $elt_name = shift;
419 18 my $attrs = shift;
420 (
421 Name => $elt_name,
422 LocalName => $elt_name,
423 $attrs
424 ? (
425 Attributes => {
426 18 ▶ map { ( "{}$_" => {
Branch: 1 branch
BranchTrueFalseTotal
$attrs ? : 8 10 18
427 Name => $_,
428 LocalName => $_,
429 8 Value => $attrs->{$_},
430 } ) } keys %$attrs
431 }
432 )
433 : (),
434 )
435 }
436 : @_
437 );
438  
439 25 delete $$self->{StartElement}; ## In case an ::element was passed in
440 25 delete $$self->{EndElement}; ## In case an ::element was passed in
441 25 delete $$self->{Content}; ## In case an ::element was passed in
442 25 return $self;
443 }
444  
445 61 ▶ sub type { "start_element" }
Subroutine type: called CC=1 SCAR=0.0
446  
447 0 ▶ sub types { ( __PACKAGE__, "start_element", "start_elt" ) }
Subroutine types: not called CC=1 SCAR=6.9
448  
449 29 ▶ sub _stringify {
Subroutine _stringify: called CC=6 SCAR=18.8
450 29 my $self = shift;
451  
452 29 my $name = $$self->{LocalName};
453  
454 29 ▶ if ( defined $$self->{NamespaceURI}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}) 0 29 29
Condition66%2/3
defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}
!ll&&!rl&&r
2360
MC/DC0%0/2
defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}
defined $$self->{"NamespaceURI"} length $$self->{"NamespaceURI"}
Truth table66%2/3
A and B
A= defined $$self->{"NamespaceURI"}B= length $$self->{"NamespaceURI"}
ABresult
0X0
100
111
455 && length $$self->{NamespaceURI}
456 ) {
457 ## TODO namespace -> prefix translation
458 0 $name = "foo:$name";
459 }
460  
461 ## Work around some odd thread safety thing.
462 ## TODO: See if this can be removed with perl5.8.1
463 29 my $s = $$self;
464 29 my $a = $s->{Attributes};
465  
466 my $foo = join "",
467 qq[<],
468 $name,
469 keys %$a
470 ? sort map {
471 29 ▶ my $name = $_->{LocalName};
Branch: 1 branch
BranchTrueFalseTotal
keys %$a ? : 14 15 29
472  
473 29 ▶ if ( defined $_->{NamespaceURI}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{"NamespaceURI"} and length $_->{"NamespaceURI"}) 0 29 29
Condition66%2/3
defined $_->{"NamespaceURI"} and length $_->{"NamespaceURI"}
!ll&&!rl&&r
2810
MC/DC0%0/2
defined $_->{"NamespaceURI"} and length $_->{"NamespaceURI"}
defined $_->{"NamespaceURI"} length $_->{"NamespaceURI"}
Truth table66%2/3
A and B
A= defined $_->{"NamespaceURI"}B= length $_->{"NamespaceURI"}
ABresult
0X0
100
111
474 && length $_->{NamespaceURI}
475 ) {
476 ## TODO namespace -> prefix translation
477 0 $name = "foo:$name";
478 }
479  
480 29 join "", qq[ ], $name, qq[="], $_->{Value}, qq["];
481 } values %$a
482 : (),
483 qq[>];
484  
485 29 return $foo;
486 }
487  
488 16 ▶ sub _hash_deref {
Subroutine _hash_deref: called CC=2 SCAR=7.0
489 16 my $self = shift;
490 16 ▶ $$self->{_TiedAttributes} ||= do {
Condition66%2/3
$$self->{"_TiedAttributes"} ||= do { my %h; tie %h, "XML::Essex::Event::_tied_attributes", $$self->{"Attributes"}; \%h }
l!l&&r!l&&!r
1420
MC/DC0%0/2
$$self->{"_TiedAttributes"} ||= do { my %h; tie %h, "XML::Essex::Event::_tied_attributes", $$self->{"Attributes"}; \%h }
$$self->{"_TiedAttributes"} do { my %h; tie %h, "XML::Essex::Event::_tied_attributes", $$self->{"Attributes"}; \%h }
Truth table66%2/3
A ||= B
A= $$self->{"_TiedAttributes"}B= do { my %h; tie %h, "XML::Essex::Event::_tied_attributes", $$self->{"Attributes"}; \%h }
ABresult
1X1
011
000
491 2 my %h;
492 tie
493 %h,
494 "XML::Essex::Event::_tied_attributes",
495 2 $$self->{Attributes};
496 2 \%h;
497 };
498 16 return $$self->{_TiedAttributes};
499 }
500  
501 195 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
502 195 my $self = shift;
503 195 ▶ return $_[0] eq "start_element"
Condition100%6/6
$_[0] eq 'start_element' || $_[0] eq 'start_elt' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
1110678
$_[0] eq "start_element" or $_[0] eq "start_elt"
l!l&&r!l&&!r
74184
MC/DC100%3/3
$_[0] eq 'start_element' || $_[0] eq 'start_elt' || $self->SUPER::isa(@_)
$_[0] eq "start_element" $_[0] eq "start_elt" $self->SUPER::isa(@_)
Truth table100%4/4
A or B || C
A= $_[0] eq "start_element"B= $_[0] eq "start_elt"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
504 || $_[0] eq "start_elt"
505 || $self->SUPER::isa( @_ );
506 }
507  
508 60 ▶ sub generate_SAX {
Subroutine generate_SAX: called CC=3 SCAR=20.0
509 60 my $self = shift;
510 return $self->SUPER::generate_SAX( @_ )
511 60 ▶ unless exists $$self->{_TiedAttributes};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self->{"_TiedAttributes"} 60 0 60
512  
513 0 my $ta = delete $$self->{_TiedAttributes};
514  
515 0 my $r;
516  
517 0 my $ok = eval {
518 0 $r = $self->SUPER::generate_SAX( @_ );
519 0 1;
520 };
521 0 $$self->{_TiedAttributes} = $ta;
522 0 ▶ die $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
523  
524 0 return $r;
525 }
526  
527 9 @XML::Essex::Event::start_elt::ISA = qw( XML::Essex::Event::start_element );
528 1 ▶ sub XML::Essex::Event::start_elt::new {
Subroutine new: called CC=3 SCAR=12.5
529 1 my $proto = shift;
530 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::start_elt" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::start_elt"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::start_elt"
not ref $proto $proto eq "XML:Essex::start_elt"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::start_elt"
ABresult
0X0
100
111
531 if ! ref $proto && $proto eq "XML:Essex::start_elt";
532 1 $proto->XML::Essex::Event::start_element::new( @_ )
533 }
534  
535 =item name
536  
537 Returns the name of the node according to the namespace stringification
538 rules.
539  
540 =cut
541  
542 3 ▶ sub name {
Subroutine name: called CC=1 SCAR=0.0
543 3 my $self = shift;
544 3 return XML::Essex::Model::_render_event_name( $$self );
545 }
546  
547 =item jclark_name
548  
549 Returns the name of the node in James Clark notation.
550  
551 =cut
552  
553 0 ▶ sub jclark_name {
Subroutine jclark_name: not called CC=2 SCAR=17.9
554 0 my $self = shift;
555 return join( "",
556 "{",
557 defined $$self->{NamespaceURI}
558 ? $$self->{NamespaceURI} : "",
559 "}",
560 $$self->{LocalName}
561 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $$self->{'NamespaceURI'} ? : 0 0 0
562 }
563  
564 =item jclark_keys
565  
566 my @keys = $e->jclark_keys
567  
568 Returns a list of attribute names in jclark notation ("{...}name").
569  
570 =cut
571  
572 2 ▶ sub jclark_keys { keys %{${shift()}->{Attributes}} }
Subroutine jclark_keys: called CC=1 SCAR=0.0
573  
574 package XML::Essex::Event::_tied_attributes;
575  
576 ## tie %h, "XML...", $event;
577  
578 2 ▶ sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
579 2 my $proto = shift;
580 2 return bless {
581 Attributes => shift,
582 Wrappers => {},
583 }, $proto;
584 }
585  
586 0 ▶ sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
587 return
588 0 exists shift->{Attributes}->{XML::Essex::Model::_jclarkify shift};
589 }
590  
591 6 ▶ sub FETCH {
Subroutine FETCH: called CC=3 SCAR=11.2
592 6 my $self = shift;
593 6 my $name = XML::Essex::Model::_jclarkify shift;
594  
595 return $self->{Wrappers}->{$name}
596 ||= XML::Essex::Event::attribute->new(
597 6 ▶ $self->{Attributes}->{$name} ||= do {
Condition66%4/6
$self->{"Wrappers"}{$name} ||= "XML::Essex::Event::attribute"->new($self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} })
l!l&&r!l&&!r
240
$self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} }
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"Wrappers"}{$name} ||= "XML::Essex::Event::attribute"->new($self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} })
$self->{"Wrappers"}{$name} "XML::Essex::Event::attribute"->new($self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} })
MC/DC0%0/2
$self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} }
$self->{"Attributes"}{$name} do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} }
Truth table66%2/3
A ||= B
A= $self->{"Wrappers"}{$name}B= "XML::Essex::Event::attribute"->new($self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} })
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $self->{"Attributes"}{$name}B= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns, "Value", ""} }
ABresult
1X1
011
000
598 1 my ( $ns, $name ) = XML::Essex::Model::_split_name $name;
599 {
600 1 LocalName => $name,
601 NamespaceURI => $ns,
602 Value => "",
603 }
604 }
605 );
606 }
607  
608 2 ▶ sub STORE {
Subroutine STORE: called CC=2 SCAR=7.0
609 2 my $self = shift;
610 2 my $name = XML::Essex::Model::_jclarkify shift;
611 2 my $value = shift;
612  
613 2 ▶ $self->{Attributes}->{$name} ||= do {
Condition66%2/3
$self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns} }
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"Attributes"}{$name} ||= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns} }
$self->{"Attributes"}{$name} do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns} }
Truth table66%2/3
A ||= B
A= $self->{"Attributes"}{$name}B= do { () = XML::Essex::Model::_split_name($name); +{"LocalName", $name, "NamespaceURI", $ns} }
ABresult
1X1
011
000
614 1 my ( $ns, $name ) = XML::Essex::Model::_split_name $name;
615 {
616 1 LocalName => $name,
617 NamespaceURI => $ns,
618 }
619 };
620  
621 2 $self->{Attributes}->{$name}->{Value} = $value;
622 }
623  
624 1 ▶ sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
625 1 my $self = shift;
626 1 my $name = XML::Essex::Model::_jclarkify shift;
627 1 delete $self->{Attributes}->{$name};
628 1 delete $self->{Wrappers}->{$name};
629 }
630  
631 7 ▶ sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
632 7 my $self = shift;
633 7 keys %{$self->{Attributes}}; ## reset each()'s state
634 ## TODO: apply ns=>prefix mappings
635 7 my $r = each %{$self->{Attributes}};
636 7 $r =~ s/^\{\}//;
637 7 return $r;
638 }
639  
640 12 ▶ sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
641 12 my $self = shift;
642 ## TODO: apply ns=>prefix mappings
643 12 my $r = each %{$self->{Attributes}};
644 12 $r =~ s/^\{\}//;
645 12 return $r;
646 }
647 }
648  
649 =back
650  
651 =head1 attribute
652  
653 aka: attr
654  
655 my $name_attr = $start_elt->{name};
656 my $attr = attr $name;
657 my $attr = attr $name => $value;
658 my $attr = attr {
659 LocalName => $local_name,
660 NamespaceURI => $ns_uri,
661 Value => $value,
662 };
663  
664  
665 Stringifies as its value: C<< harvey >>
666  
667 This is not a SAX event, but an object returned from within element or
668 start_element objects that gives you access to the C<NamespaceUri>,
669 C<LocalName>, and C<Value> fields of the attribute. Does not give
670 access to the Name or Prefix fields present in SAX events.
671  
672 If you create an attribute with an undefined value, it will stringify
673 as the C<undef>ined value. Attributes that are created without an
674 explicit C<undef>ined C<Value> field will be given the defaul value
675 of "", including attributes that are autovivified. This allows
676  
677 get "*" until $_->{id} eq "10";
678  
679 to work. This has the side effect of addingan C<id=""> attribute to all
680 elements without an C<id> attribute. To avoid the side effect, use the
681 C<exists> function to detect nonexistant attributes:
682  
683 get "*" until exists $_->{id} and $_->{id} eq "10";
684  
685 =cut
686  
687 {
688 9 package XML::Essex::Event::attribute;
689  
690 9 @ISA = qw( XML::Essex::Event );
691  
692 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
693  
694 9 ▶ use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
695  
696 4 ▶ sub new {
Subroutine new: called CC=3 SCAR=14.2
697 my $self = shift->SUPER::new(
698 ! ref $_[0]
699 4 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 0 4 4
700 0 my ( $ns, $name ) = XML::Essex::Model::_split_name shift;
701 0 ▶ my $value = @_ ? shift : "";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
702 (
703 0 NamespaceURI => $ns,
704 LocalName => $name,
705 Value => $value,
706 Name => undef,
707 Prefix => undef,
708 )
709 }
710 : @_
711 );
712  
713 4 delete $$self->{StartElement}; ## In case an ::element was passed in
714 4 delete $$self->{EndElement}; ## In case an ::element was passed in
715 4 delete $$self->{Content}; ## In case an ::element was passed in
716 4 return $self;
717 }
718  
719 0 ▶ sub type { "attribute" }
Subroutine type: not called CC=1 SCAR=6.9
720  
721 0 ▶ sub types { ( __PACKAGE__, "attribute", "attr" ) }
Subroutine types: not called CC=1 SCAR=6.9
722  
723 8 ▶ sub _stringify { ${shift()}->{Value} }
Subroutine _stringify: called CC=1 SCAR=0.0
724  
725 0 ▶ sub isa {
Subroutine isa: not called CC=3 SCAR=24.8
726 0 my $self = shift;
727 0 ▶ return $_[0] eq "attribute"
Condition0%0/6
$_[0] eq 'attribute' || $_[0] eq 'attr' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
000
$_[0] eq "attribute" or $_[0] eq "attr"
l!l&&r!l&&!r
000
MC/DC0%0/3
$_[0] eq 'attribute' || $_[0] eq 'attr' || $self->SUPER::isa(@_)
$_[0] eq "attribute" $_[0] eq "attr" $self->SUPER::isa(@_)
Truth table0%0/4
A or B || C
A= $_[0] eq "attribute"B= $_[0] eq "attr"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
728 || $_[0] eq "attr"
729 || $self->SUPER::isa( @_ );
730 }
731  
732 9 @XML::Essex::Event::attr::ISA = qw( XML::Essex::Event::attribute );
733 0 ▶ sub XML::Essex::Event::attr::new {
Subroutine new: not called CC=3 SCAR=24.8
734 0 my $proto = shift;
735 0 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::attr" 0 0 0
Condition0%0/3
not ref $proto and $proto eq "XML:Essex::attr"
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::attr"
not ref $proto $proto eq "XML:Essex::attr"
Truth table0%0/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::attr"
ABresult
0X0
100
111
736 if ! ref $proto && $proto eq "XML:Essex::attr";
737 0 $proto->XML::Essex::Event::attribute::new( @_ )
738 }
739 }
740  
741  
742 =head1 end_element
743  
744 aka: end_elt
745  
746 my $e = end_element "foo";
747 my $e = end_element $start_elt;
748 my $e = end_element $end_elt;
749 my $e = end_element $elt;
750  
751 Stringifies as: C<< </foo> >>
752  
753 See L<start_element|/start_element> for details on namespace handling.
754  
755 =cut
756  
757 {
758 9 package XML::Essex::Event::end_element;
759  
760 9 @ISA = qw( XML::Essex::Event );
761  
762 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
763  
764 9 ▶ use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
765  
766 22 ▶ sub new {
Subroutine new: called CC=2 SCAR=6.9
767 my $self = shift->SUPER::new(
768 ! ref $_[0]
769 22 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 14 8 22
770 14 my $elt_name = shift;
771 (
772 14 Name => $elt_name,
773 LocalName => $elt_name,
774 )
775 }
776 : shift
777 );
778  
779 22 delete $$self->{StartElement}; ## In case an ::element was passed in
780 22 delete $$self->{Content}; ## In case an ::element was passed in
781 22 delete $$self->{EndElement}; ## In case an ::element was passed in
782 22 delete $$self->{Attributes}; ## ::element or ::start_element
783 22 return $self;
784 }
785  
786 61 ▶ sub type { "end_element" }
Subroutine type: called CC=1 SCAR=0.0
787  
788 0 ▶ sub types { ( __PACKAGE__, "end_element", "end_elt" ) }
Subroutine types: not called CC=1 SCAR=6.9
789  
790 18 ▶ sub _stringify {
Subroutine _stringify: called CC=3 SCAR=11.8
791 18 my $self = shift;
792  
793 18 my $name = $$self->{LocalName};
794  
795 18 ▶ if ( defined $$self->{NamespaceURI}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}) 0 18 18
Condition66%2/3
defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}
!ll&&!rl&&r
1530
MC/DC0%0/2
defined $$self->{"NamespaceURI"} and length $$self->{"NamespaceURI"}
defined $$self->{"NamespaceURI"} length $$self->{"NamespaceURI"}
Truth table66%2/3
A and B
A= defined $$self->{"NamespaceURI"}B= length $$self->{"NamespaceURI"}
ABresult
0X0
100
111
796 && length $$self->{NamespaceURI}
797 ) {
798 ## TODO namespace -> prefix translation
799 0 $name = "foo:$name";
800 }
801  
802 18 return join $name, qq[</], qq[>];
803 }
804  
805 250 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
806 250 my $self = shift;
807 250 ▶ return $_[0] eq "end_element"
Condition100%6/6
$_[0] eq 'end_element' || $_[0] eq 'end_elt' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
5109136
$_[0] eq "end_element" or $_[0] eq "end_elt"
l!l&&r!l&&!r
41245
MC/DC100%3/3
$_[0] eq 'end_element' || $_[0] eq 'end_elt' || $self->SUPER::isa(@_)
$_[0] eq "end_element" $_[0] eq "end_elt" $self->SUPER::isa(@_)
Truth table100%4/4
A or B || C
A= $_[0] eq "end_element"B= $_[0] eq "end_elt"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
808 || $_[0] eq "end_elt"
809 || $self->SUPER::isa( @_ );
810 }
811  
812 9 @XML::Essex::Event::end_elt::ISA = qw( XML::Essex::Event::end_element );
813 1 ▶ sub XML::Essex::Event::end_elt::new {
Subroutine new: called CC=3 SCAR=12.5
814 1 my $proto = shift;
815 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::end_elt" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::end_elt"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::end_elt"
not ref $proto $proto eq "XML:Essex::end_elt"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::end_elt"
ABresult
0X0
100
111
816 if ! ref $proto && $proto eq "XML:Essex::end_elt";
817 1 $proto->XML::Essex::Event::end_element::new( @_ )
818 }
819 }
820  
821 =head1 element
822  
823 aka: elt
824  
825 my $e = elt foo => "content", $other_elt, "more content", $pi, ...;
826 my $e = elt foo => { attr1 => "val1" }, "content", ...;
827  
828 Stringifies as: C<< <foo attr1="val1">content</foo> >>
829  
830 Never stringifies as an empty element tag (C<< <foo/> >>), although
831 downstream filters and handlers may choose to do that.
832  
833 Constructs an element. An element is a sequence of events between a
834 matching start_element and end_element, inclusive.
835  
836 Attributes may be accessed using Perl hash dereferencing, as with
837 start_element events, see L</start_element> for details.
838  
839 Content may be accessed using Perl array dereferencing:
840  
841 my @content = @$_;
842 unshift @$_, "prefixed content";
843 push @$_, "appended content";
844  
845 Note that
846  
847 my $elt2 = elt $elt1; ## doesn't copy content, just name+attra
848  
849 only copies the name and attributes, it does I<not> copy the content.
850 To copy content do either of:
851  
852 my $elt2 = elt $elt1, @$elt1;
853 my $elt2 = $elt1->clone;
854  
855 This is because the first parameter is converted to a start/end_element
856 pair and any content is ignored. This is so that:
857  
858 my $elt2 = elt $elt1, "new content";
859  
860 creates an element with the indicated content.
861  
862 =head2 Methods
863  
864 =over
865  
866 =cut
867  
868  
869 {
870 9 package XML::Essex::Event::element;
871  
872 9 @ISA = qw( XML::Essex::Event );
873  
874 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
875  
876 9 ▶ use overload(
Subroutine BEGIN: called CC=1 SCAR=0.0
877 '""' => \&_stringify,
878 2 ▶ '%{}' => sub { ${shift()}->{StartElement}->_hash_deref },
Subroutine __ANON__: called CC=1 SCAR=0.0
879 5 ▶ '@{}' => sub { ${shift()}->{Content} },
Subroutine __ANON__: called CC=1 SCAR=0.0
880 9 );
881  
882 9 ▶ sub new {
Subroutine new: called CC=9 SCAR=22.9
883 9 my $proto = shift;
884 9 my $self = $proto->SUPER::new;
885  
886 9 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 0 9
887 9 my $arg1 = shift;
888  
889 $arg1 = $$arg1->{StartElement}
890 9 ▶ if UNIVERSAL::isa( $arg1, __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($arg1, __PACKAGE__) 1 8 9
891  
892 9 $$self->{StartElement} = do {
893 9 my @args;
894 9 ▶ push @args, shift while @_ && ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
@_ and ref $_[0] eq "HASH" and do { push @args, shift() } 4 9 13
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
544
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
895 9 XML::Essex::Event::start_element->new( $arg1, @args );
896 };
897  
898 9 ▶ if ( @_ && UNIVERSAL::isa( $_[-1], "XML::Essex::Model::end_element" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and UNIVERSAL::isa($_[-1], "XML::Essex::Model::end_element")) 0 9 9
Condition66%2/3
@_ and UNIVERSAL::isa($_[-1], "XML::Essex::Model::end_element")
!ll&&!rl&&r
540
MC/DC0%0/2
@_ and UNIVERSAL::isa($_[-1], "XML::Essex::Model::end_element")
@_ UNIVERSAL::isa($_[-1], "XML::Essex::Model::end_element")
Truth table66%2/3
A and B
A= @_B= UNIVERSAL::isa($_[-1], "XML::Essex::Model::end_element")
ABresult
0X0
100
111
899 0 $$self->{EndElement} = pop;
900 }
901  
902 9 ▶ @{$$self->{Content}} = map
Branch: 2 branches
BranchTrueFalseTotal
!ref($_) ? : 4 0 4
ref $_ eq 'ARRAY' ? : 0 0 0
903 ! ref $_ ? XML::Essex::Event::characters->new( $_ )
904 : ref eq "ARRAY" ? XML::Essex::Event::element->new( @$_ )
905 : Carp::croak( "$_ is not a content for $self" ),
906 @_;
907 }
908  
909 9 return $self;
910 }
911  
912 9 ▶ sub _set_default_end_element {
Subroutine _set_default_end_element: called CC=2 SCAR=6.9
913 9 my $self = shift;
914  
915 9 ▶ return if $$self->{EndElement};
Branch: 1 branch
BranchTrueFalseTotal
if $$self->{"EndElement"} 3 6 9
916  
917 $$self->{EndElement} =
918 6 XML::Essex::Event::end_element->new( $$self->{StartElement} );
919 }
920  
921 ## Utility functions used in XML::Handler::Essex to build elements
922 ## from incoming events.
923 0 ▶ sub _start_element {
Subroutine _start_element: not called CC=2 SCAR=17.9
924 0 my $self = shift;
925 0 ▶ $$self->{StartElement} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
926 0 $$self->{StartElement};
927 }
928  
929 3 ▶ sub _add_content {
Subroutine _add_content: called CC=1 SCAR=0.0
930 3 my $self = shift;
931 3 push @{$$self->{Content}}, shift;
932 }
933  
934 3 ▶ sub _end_element {
Subroutine _end_element: called CC=2 SCAR=7.1
935 3 my $self = shift;
936 3 ▶ $$self->{EndElement} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 0 3
937 3 $$self->{EndElement};
938 }
939  
940 0 ▶ sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
941 0 my $clone = shift->SUPER::clone;
942  
943 0 $_ = $_->clone for $clone->{StartElement},
944 0 @{$clone->{Content}},
945 $clone->{EndElement};
946  
947 0 return $clone;
948 }
949  
950 1 ▶ sub type { "element" }
Subroutine type: called CC=1 SCAR=0.0
951  
952 0 ▶ sub types { ( __PACKAGE__, "element", "elt" ) }
Subroutine types: not called CC=1 SCAR=6.9
953  
954 6 ▶ sub _stringify {
Subroutine _stringify: called CC=2 SCAR=7.0
955 6 my $self = shift;
956  
957 Carp::croak "Can't stringify element with no start_element event"
958 6 ▶ unless $$self->{StartElement};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self->{"StartElement"} 0 6 6
959  
960 6 $self->_set_default_end_element;
961  
962 return join "",
963 $$self->{StartElement},
964 6 @{$$self->{Content}},
965 6 $$self->{EndElement};
966 }
967  
968  
969 4 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
970 4 my $self = shift;
971 4 ▶ return $_[0] eq "element"
Condition83%5/6
$_[0] eq 'element' || $_[0] eq 'elt' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
220
$_[0] eq "element" or $_[0] eq "elt"
l!l&&r!l&&!r
112
MC/DC0%0/3
$_[0] eq 'element' || $_[0] eq 'elt' || $self->SUPER::isa(@_)
$_[0] eq "element" $_[0] eq "elt" $self->SUPER::isa(@_)
Truth table75%3/4
A or B || C
A= $_[0] eq "element"B= $_[0] eq "elt"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
972 || $_[0] eq "elt"
973 || $self->SUPER::isa( @_ );
974 }
975  
976 3 ▶ sub generate_SAX {
Subroutine generate_SAX: called CC=3 SCAR=11.1
977 3 my $self = shift;
978 Carp::croak "Can't generate SAX events for element with no start_element event"
979 3 ▶ unless $$self->{StartElement};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self->{"StartElement"} 0 3 3
980  
981 3 $self->_set_default_end_element;
982  
983 $_->generate_SAX( @_ )
984 3 for $$self->{StartElement},
985 3 @{$$self->{Content}},
986 $$self->{EndElement};
987 }
988  
989 9 @XML::Essex::Event::elt::ISA = qw( XML::Essex::Event::element );
990 1 ▶ sub XML::Essex::Event::elt::new {
Subroutine new: called CC=3 SCAR=12.5
991 1 my $proto = shift;
992 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::elt" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::elt"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::elt"
not ref $proto $proto eq "XML:Essex::elt"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::elt"
ABresult
0X0
100
111
993 if ! ref $proto && $proto eq "XML:Essex::elt";
994 1 $proto->XML::Essex::Event::element::new( @_ )
995 }
996  
997 =item jclark_keys
998  
999 Returns the names of attributes as a list of JamesClarkified
1000 keys, just like start_element's C<jclark_keys()>.
1001  
1002 =cut
1003  
1004 1 ▶ sub jclark_keys { ${shift()}->{StartElement}->jclark_keys }
Subroutine jclark_keys: called CC=1 SCAR=0.0
1005  
1006 =item name
1007  
1008 Returns the name of the node according to the namespace stringification
1009 rules.
1010  
1011 =cut
1012  
1013 0 ▶ sub name { ${shift()}->{StartElement}->name }
Subroutine name: not called CC=1 SCAR=6.9
1014  
1015 =item jclark_name
1016  
1017 Returns the name of the node in James Clark notation.
1018  
1019 =cut
1020  
1021 0 ▶ sub jclark_name { ${shift()}->{StartElement}->jclark_name }
Subroutine jclark_name: not called CC=1 SCAR=6.9
1022 }
1023  
1024 =back
1025  
1026 =head1 characters
1027  
1028 aka: chars
1029  
1030 my $e = chars "A stitch", " in time", " saves nine";
1031 my $e = chars {
1032 Data => "A stitch in time saves nine",
1033 };
1034  
1035 Stringifies like a string: C<< A stitch in time saves nine. >>
1036  
1037 Character events are aggregated.
1038  
1039 TODO: make that aggregation happen.
1040  
1041 =cut
1042  
1043 {
1044 9 package XML::Essex::Event::characters;
1045  
1046 9 @ISA = qw( XML::Essex::Event );
1047  
1048 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1049  
1050 9 ▶ use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
1051  
1052 11 ▶ sub new {
Subroutine new: called CC=2 SCAR=6.9
1053 11 ▶ my $self = shift->SUPER::new(
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 9 2 11
1054 ! ref $_[0]
1055 ? (
1056 Data => join( "", @_ ),
1057 )
1058 : shift
1059 );
1060  
1061 11 return $self;
1062 }
1063  
1064 35 ▶ sub type { "characters" }
Subroutine type: called CC=1 SCAR=0.0
1065  
1066 0 ▶ sub types { ( __PACKAGE__, "characters", "chars" ) }
Subroutine types: not called CC=1 SCAR=6.9
1067  
1068 16 ▶ sub _stringify { ${shift()}->{Data} };
Subroutine _stringify: called CC=1 SCAR=0.0
1069  
1070 145 ▶ sub isa {
Subroutine isa: called CC=3 SCAR=11.0
1071 145 my $self = shift;
1072 145 ▶ return $_[0] eq "characters"
Condition100%6/6
$_[0] eq 'characters' || $_[0] eq 'chars' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
532108
$_[0] eq "characters" or $_[0] eq "chars"
l!l&&r!l&&!r
41140
MC/DC100%3/3
$_[0] eq 'characters' || $_[0] eq 'chars' || $self->SUPER::isa(@_)
$_[0] eq "characters" $_[0] eq "chars" $self->SUPER::isa(@_)
Truth table100%4/4
A or B || C
A= $_[0] eq "characters"B= $_[0] eq "chars"C= $self->SUPER::isa(@_)
ABCresult
1XX1
01X1
0011
0000
1073 || $_[0] eq "chars"
1074 || $self->SUPER::isa( @_ );
1075 }
1076  
1077 9 @XML::Essex::Event::chars::ISA = qw( XML::Essex::Event::characters );
1078 1 ▶ sub XML::Essex::Event::chars::new {
Subroutine new: called CC=3 SCAR=12.5
1079 1 my $proto = shift;
1080 1 ▶ $proto = __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if not ref $proto and $proto eq "XML:Essex::chars" 0 1 1
Condition33%1/3
not ref $proto and $proto eq "XML:Essex::chars"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $proto and $proto eq "XML:Essex::chars"
not ref $proto $proto eq "XML:Essex::chars"
Truth table33%1/3
not(A) and B
A= not ref $protoB= $proto eq "XML:Essex::chars"
ABresult
0X0
100
111
1081 if ! ref $proto && $proto eq "XML:Essex::chars";
1082 1 $proto->XML::Essex::Event::characters::new( @_ )
1083 }
1084 }
1085  
1086 =head1 comment
1087  
1088 aka: (no abbreviation)
1089  
1090 my $e = comment "A stitch in time saves nine";
1091 my $e = comment {
1092 Data => "A stitch in time saves nine",
1093 };
1094  
1095 Stringifies like a string: C<< A stitch in time saves nine. >>
1096  
1097 =cut
1098  
1099 {
1100 9 package XML::Essex::Event::comment;
1101  
1102 9 @ISA = qw( XML::Essex::Event );
1103  
1104 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1105  
1106 9 ▶ use overload '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
1107  
1108 2 ▶ sub new {
Subroutine new: called CC=2 SCAR=6.9
1109 2 ▶ my $self = shift->SUPER::new(
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 1 1 2
1110 ! ref $_[0]
1111 ? (
1112 Data => shift,
1113 )
1114 : shift
1115 );
1116  
1117 2 return $self;
1118 }
1119  
1120 1 ▶ sub type { "comment" }
Subroutine type: called CC=1 SCAR=0.0
1121  
1122 0 ▶ sub types { ( __PACKAGE__, "comment" ) }
Subroutine types: not called CC=1 SCAR=6.9
1123  
1124 2 ▶ sub _stringify { ${shift()}->{Data} };
Subroutine _stringify: called CC=1 SCAR=0.0
1125  
1126 2 ▶ sub isa {
Subroutine isa: called CC=2 SCAR=7.1
1127 2 my $self = shift;
1128 2 ▶ return $_[0] eq "comment"
Condition66%2/3
$_[0] eq 'comment' || $self->SUPER::isa(@_)
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[0] eq 'comment' || $self->SUPER::isa(@_)
$_[0] eq 'comment' $self->SUPER::isa(@_)
Truth table66%2/3
A || B
A= $_[0] eq 'comment'B= $self->SUPER::isa(@_)
ABresult
1X1
011
000
1129 || $self->SUPER::isa( @_ );
1130 }
1131 }
1132  
1133 =head1 Implementation Details
1134  
1135 =head2 References and blessed, tied or overloaded SAX events.
1136  
1137 Instances of the Essex object model classes carry a reference to the
1138 original data (SAX events), rather than copying it. This means that
1139 there are fewer copies (a good thing; though there is an increased cost
1140 of getting at any data in the events) and that upstream filters may send
1141 blessed, tied, or overloaded objects to us and they will not be molested
1142 unless the Essex filter messes with them. There is also an
1143 implementation reason for this, it makes overloading hash accesses
1144 like C<$_->{}> easier to implement.
1145  
1146 Passing an Essex event to a constructor for a new Essex event does
1147 result in a deep copy of the referenced data (via
1148 C<XML::Essex::Event::clone()>).
1149  
1150 =head2 Class files, or the lack thereof
1151  
1152 The objects in the Essex object model are not available independantly
1153 as class files. You must use C<XML::Essex::Model> to get at them. This
1154 is because there is a set of event types used in almost all SAX filters
1155 and it is cheaper to compile one file containing these than to open
1156 multiple files.
1157  
1158 This does not mean that all classes are loaded when the
1159 XML::Essex::Model is C<use()>ed or C<require()>ed, rare events are
1160 likely to be autoloaded.
1161  
1162 =head2 Class names
1163  
1164 In order to allow
1165  
1166 my $e = XML::Essex::start_elt( ... );
1167  
1168 to work as expected--in case the calling package prefers not to import
1169 C<start_elt()>, for instance--the objects in the model are all in the
1170 XML::Essex::Event::... namespace, like
1171 C<XML::Essex::Event::start_element>.
1172  
1173 =head1 TODO
1174  
1175 =over
1176  
1177 =item Allow escaping to be configured
1178  
1179 =item Allow " vs. ' for attr quotes to be configured.
1180  
1181 =item Allow CDATA to be tested for, either by stringifying it or by
1182 allowing it to be returned as an array or something.
1183  
1184 =back
1185  
1186 =for the future
1187 =head1 LIMITATIONS
1188  
1189 =head1 COPYRIGHT
1190  
1191 Copyright 2002, R. Barrie Slaymaker, Jr., All Rights Reserved
1192  
1193 =head1 LICENSE
1194  
1195 You may use this module under the terms of the BSD, Artistic, oir GPL licenses,
1196 any version.
1197  
1198 =head1 AUTHOR
1199  
1200 Barrie Slaymaker <barries@slaysys.com>
1201  
1202 =cut
1203  
1204 9 1;
« Essex/Constants.pm ↑ Summary Essex/Event.pm »