blib/lib/XML/SAX/Builder.pm

filter: Statementstmt 99.5% 215 / 216 Branchbran 75.0% 33 / 44 Conditioncond 62.2% 28 / 45 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 100% 55 / 55 Podpod 100% 8 / 8 totaltotal 88.5% 349 / 394 CC 44 SCAR 12.8
CC
44
Cov
90%
_make_closure
10.1
__ANON__
8.2
_add_attributes
7.0
CRAP 3.6
↑ Summary
1 # @(#) $Id: Builder.pm,v 1.4 2003/04/24 12:48:43 dom Exp $
2 package XML::SAX::Builder;
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use XML::NamespaceSupport;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use XML::SAX::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.02';
12 4 our $AUTOLOAD;
13  
14 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
15 4 my $class = shift;
16 # Escape hatch.
17 4 ▶ return XML::SAX::Builder::Tag->new( $class->{Handler}, 'new', @_ )
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 4 4
18 if ref $class;
19 4 my ( $handler, %opts ) = @_;
20  
21 # Default to spitting out XML to STDOUT.
22 4 ▶ $handler ||= XML::SAX::Writer->new;
Condition33%1/3
$handler ||= "XML::SAX::Writer"->new
l!l&&r!l&&!r
400
MC/DC0%0/2
$handler ||= "XML::SAX::Writer"->new
$handler "XML::SAX::Writer"->new
Truth table33%1/3
A ||= B
A= $handlerB= "XML::SAX::Writer"->new
ABresult
1X1
011
000
23 4 bless { Handler => $handler, %opts }, $class;
24 }
25  
26 45 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
27 45 my $self = shift;
28 45 my @args = @_;
29 45 my $tag = $AUTOLOAD;
30 45 $tag =~ s/.*:://;
31 45 ▶ return if $tag eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "DESTROY" 4 41 45
32 $tag = "$self->{Prefix}:$tag"
33 41 ▶ if $self->{Prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Prefix"} 1 40 41
34 41 XML::SAX::Builder::Tag->new( $self->{ Handler }, $tag, @args );
35 }
36  
37 # Start a new namespace.
38 10 ▶sub xmlns {
Subroutine xmlns: called CC=1 SCAR=0.0
Pod documented
39 10 my $self = shift;
40 10 XML::SAX::Builder::Namespace->new( $self->{ Handler }, @_ );
41 }
42  
43 # Output unescaped stuff.
44 2 ▶sub xmlcdata {
Subroutine xmlcdata: called CC=1 SCAR=0.0
Pod documented
45 2 my $self = shift;
46 2 XML::SAX::Builder::CDATA->new( $self->{ Handler }, @_ );
47 }
48  
49 # Output an XML DOCTYPE
50 4 ▶sub xmldtd {
Subroutine xmldtd: called CC=1 SCAR=0.0
Pod documented
51 4 my $self = shift;
52 4 XML::SAX::Builder::Doctype->new( $self->{ Handler }, @_ );
53 }
54  
55 # Output an XML comment
56 1 ▶sub xmlcomment {
Subroutine xmlcomment: called CC=1 SCAR=0.0
Pod documented
57 1 my $self = shift;
58 1 XML::SAX::Builder::Comment->new( $self->{ Handler }, @_ );
59 }
60  
61 # Output an XML Processing Instruction.
62 2 ▶sub xmlpi {
Subroutine xmlpi: called CC=1 SCAR=0.0
Pod documented
63 2 my $self = shift;
64 2 XML::SAX::Builder::ProcessingInstruction->new( $self->{ Handler }, @_ );
65 }
66  
67 # Return a new generator which will automatically prefix elements.
68 1 ▶sub xmlprefix {
Subroutine xmlprefix: called CC=2 SCAR=7.0
Pod documented
69 1 my $self = shift;
70 1 my ($prefix) = @_;
71 1 ▶ croak "usage: xmlprefix(prefix)"
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 1 1
72 unless $prefix;
73 1 my $class = ref $self;
74 1 return $class->new( $self->{Handler}, Prefix => $prefix );
75 }
76  
77 23 ▶sub _only_one_element {
Subroutine _only_one_element: called CC=2 SCAR=6.9
78 23 my $self = shift;
79 23 my ( @builders ) = @_;
80  
81 # A namespace only allows one element child, so this rule is
82 # effectively propogated downwards.
83 my @tag = grep {
84 23 ▶ ref eq 'XML::SAX::Builder::Tag'
Condition100%3/3
ref $_ eq 'XML::SAX::Builder::Tag' || ref $_ eq 'XML::SAX::Builder::Namespace'
l!l&&r!l&&!r
1495
MC/DC100%2/2
ref $_ eq 'XML::SAX::Builder::Tag' || ref $_ eq 'XML::SAX::Builder::Namespace'
ref $_ eq 'XML::SAX::Builder::Tag' ref $_ eq 'XML::SAX::Builder::Namespace'
Truth table100%3/3
A || B
A= ref $_ eq 'XML::SAX::Builder::Tag'B= ref $_ eq 'XML::SAX::Builder::Namespace'
ABresult
1X1
011
000
85 || ref eq 'XML::SAX::Builder::Namespace'
86 } @builders;
87 23 return @tag == 1;
88 }
89  
90 # Finalise the document.
91 23 ▶sub xml {
Subroutine xml: called CC=5 SCAR=16.6
Pod documented
92 23 my $self = shift;
93 23 my ( @builders ) = @_;
94 23 ▶ croak "one and only one root element allowed"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_only_one_element(@builders) 2 21 23
95 unless $self->_only_one_element( @builders );
96 21 $self->{ Handler }->start_document( {} );
97 21 my $nsup = XML::NamespaceSupport->new( { xmlns => 1 } );
98 21 $nsup->push_context;
99 21 foreach ( @builders ) {
100 26 ▶ if ( ref && $_->can( 'run' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and $_->can('run')) { } 26 0 26
Condition33%1/3
ref $_ and $_->can("run")
!ll&&!rl&&r
0026
MC/DC0%0/2
ref $_ and $_->can("run")
ref $_ $_->can("run")
Truth table33%1/3
A and B
A= ref $_B= $_->can("run")
ABresult
0X0
100
111
101 26 $_->run( $nsup );
102 } else {
103 0 $self->{ Handler }->characters( $_ );
104 }
105 }
106 20 $self->{ Handler }->end_document( {} );
107 }
108  
109 #---------------------------------------------------------------------
110  
111 package XML::SAX::Builder::Base;
112 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 60 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
116 60 my ( $class, $handler, @args ) = @_;
117 60 bless $class->_make_closure( $handler, @args ), $class;
118 }
119  
120 52 ▶sub run { shift->(@_) }
Subroutine run: called CC=1 SCAR=0.0
121  
122 38 ▶sub is_valid_name {
Subroutine is_valid_name: called CC=1 SCAR=0.0
123 38 local $_ = $_[1];
124 # This is deliberately very simplistic...
125 38 return m/^[\w:][\w:.-]*$/;
126 }
127  
128 43 ▶sub _is_reserved_name {
Subroutine _is_reserved_name: called CC=1 SCAR=0.0
129 43 local $_ = $_[1];
130 43 return m/^xml/i;
131 }
132  
133 1 ▶sub _is_valid_lang {
Subroutine _is_valid_lang: called CC=1 SCAR=0.0
134 1 local $_ = $_[1];
135 1 return m/^
136 (
137 [a-zA-Z][a-zA-Z] # ISO639Code
138 |
139 i-[a-zA-Z]+ # IanaCode
140 |
141 x-[a-zA-Z]+ # UserCode
142 )
143 (-[a-zA-Z]+)* # Subcode
144 $/x;
145 }
146  
147 #---------------------------------------------------------------------
148  
149 package XML::SAX::Builder::Tag;
150 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 41 ▶sub _make_closure {
Subroutine _make_closure: called CC=4 SCAR=13.9
155 41 my $class = shift;
156 41 my ( $handler, $tag, @args ) = @_;
157 41 ▶ Carp::croak "names beginning with /xml/i are reserved"
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_reserved_name($tag) 2 39 41
158 if $class->_is_reserved_name( $tag );
159 Carp::croak "doctype must appear before the first element"
160 39 ▶ if grep { ref eq 'XML::SAX::Builder::Doctype' } @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ eq "XML::SAX::Builder::Doctype";} @args 1 38 39
161 38 ▶ Carp::croak "invalid character in name"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_valid_name($tag) 1 37 38
162 unless $class->is_valid_name( $tag );
163 35 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
164 35 my ($self, $nsup) = @_;
165 35 ▶ Carp::croak "usage self->(nsup)"
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $nsup 0 35 35
Condition33%1/3
$self and $nsup
!ll&&!rl&&r
0035
MC/DC0%0/2
$self and $nsup
$self $nsup
Truth table33%1/3
A and B
A= $selfB= $nsup
ABresult
0X0
100
111
166 unless $self && $nsup;
167 35 my $data = $self->_make_element_data( $nsup, $tag );
168 35 $nsup->push_context;
169 35 ▶ $self->_add_attributes( $nsup, $data, shift @args )
Branch: 1 branch
BranchTrueFalseTotal
if $args[0] and ref $args[0] eq "HASH" 5 30 35
Condition100%3/3
$args[0] and ref $args[0] eq "HASH"
!ll&&!rl&&r
9215
MC/DC100%2/2
$args[0] and ref $args[0] eq "HASH"
$args[0] ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= $args[0]B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
170 if $args[0] && ref $args[0] eq 'HASH';
171 34 $handler->start_element( $data );
172 34 foreach ( @args ) {
173 32 ▶ if ( ref && $_->can( 'run' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and $_->can('run')) { } 16 16 32
Condition66%2/3
ref $_ and $_->can("run")
!ll&&!rl&&r
16016
MC/DC50%1/2
ref $_ and $_->can("run")
ref $_ $_->can("run")
Truth table66%2/3
A and B
A= ref $_B= $_->can("run")
ABresult
0X0
100
111
174 16 $_->run( $nsup );
175 } else {
176 16 $handler->characters( { Data => $_ } );
177 }
178 }
179 34 $handler->end_element( $data );
180 34 $nsup->pop_context;
181 37 };
182 }
183  
184 35 ▶sub _make_element_data {
Subroutine _make_element_data: called CC=4 SCAR=13.9
185 35 my $self = shift;
186 35 my ( $nsup, $tag ) = @_;
187 35 my ( $uri, $prefix, $lname ) = $nsup->process_element_name( $tag );
188 35 ▶ $uri ||= ''; $prefix ||= ''; $lname ||= '';
Condition83%5/6
$uri ||= ""
l!l
1619
$prefix ||= ""
l!l
233
$lname ||= ""
l!l
350
MC/DC100%1/1
$uri ||= ""
$uri
MC/DC100%1/1
$prefix ||= ""
$prefix
MC/DC0%0/1
$lname ||= ""
$lname
Truth table100%2/2
A
A= $uri
Aresult
11
00
Truth table100%2/2
A
A= $prefix
Aresult
11
00
Truth table50%1/2
A
A= $lname
Aresult
11
00
189 35 my $data = {
190 LocalName => $lname,
191 Name => $tag,
192 NamespaceURI => $uri,
193 Prefix => $prefix,
194 };
195 35 $self->_add_namespace_attributes( $nsup, $data );
196 35 return $data;
197 }
198  
199 35 ▶sub _add_namespace_attributes {
Subroutine _add_namespace_attributes: called CC=3 SCAR=11.0
200 35 my $self = shift;
201 35 my ( $nsup, $data ) = @_;
202 my %new_namespaces =
203 35 map { $_ => $nsup->get_uri( $_ ) } $nsup->get_declared_prefixes;
204 35 foreach my $prefix ( keys %new_namespaces ) {
205 10 ▶ my $xmlns = length( $prefix ) ? "xmlns:$prefix" : "xmlns";
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 5 5 10
206 10 $new_namespaces{ $xmlns } = delete $new_namespaces{ $prefix };
207 }
208 35 $self->_add_attributes( $nsup, $data, \%new_namespaces );
209 }
210  
211 40 ▶sub _add_attributes {
Subroutine _add_attributes: called CC=7 SCAR=19.5
212 40 my $self = shift;
213 40 my ( $nsup, $data, $attr ) = @_;
214 Carp::croak "invalid LanguageID"
215 40 ▶ if $attr->{'xml:lang'} && !$self->_is_valid_lang( $attr->{'xml:lang'} );
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"xml:lang"} and not $self->_is_valid_lang($attr->{"xml:lang"}) 1 39 40
Condition66%2/3
$attr->{"xml:lang"} and not $self->_is_valid_lang($attr->{"xml:lang"})
!ll&&!rl&&r
3901
MC/DC50%1/2
$attr->{"xml:lang"} and not $self->_is_valid_lang($attr->{"xml:lang"})
$attr->{"xml:lang"} not $self->_is_valid_lang($attr->{"xml:lang"})
Truth table66%2/3
A and not(B)
A= $attr->{"xml:lang"}B= not $self->_is_valid_lang($attr->{"xml:lang"})
ABresult
0X0
100
111
216 39 foreach ( keys %$attr ) {
217 15 my ($uri, $prefix, $lname) = $nsup->process_attribute_name( $_ );
218 15 ▶ $uri ||= ''; $prefix ||= ''; $lname ||= '';
Condition83%5/6
$uri ||= ""
l!l
510
$prefix ||= ""
l!l
510
$lname ||= ""
l!l
150
MC/DC100%1/1
$uri ||= ""
$uri
MC/DC100%1/1
$prefix ||= ""
$prefix
MC/DC0%0/1
$lname ||= ""
$lname
Truth table100%2/2
A
A= $uri
Aresult
11
00
Truth table100%2/2
A
A= $prefix
Aresult
11
00
Truth table50%1/2
A
A= $lname
Aresult
11
00
219 $data->{ Attributes }->{ "{$uri}$_" } = {
220 Name => $_,
221 LocalName => $lname,
222 Prefix => $prefix,
223 NamespaceURI => $uri,
224 15 Value => $attr->{ $_ },
225 };
226 }
227 }
228  
229 #---------------------------------------------------------------------
230  
231 package XML::SAX::Builder::Namespace;
232 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
233 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
235  
236 10 ▶sub _make_closure {
Subroutine _make_closure: called CC=8 SCAR=23.2
237 10 my $class = shift;
238 10 my ( $handler, $prefix, $uri, @args ) = @_;
239 10 my $child = $args[0];
240 10 ▶ Carp::croak "new(handler,prefix,uri,child)"
Branch: 1 branch
BranchTrueFalseTotal
unless $handler and defined $prefix and $uri and $child 0 10 10
Condition33%3/9
$handler and defined $prefix and $uri and $child
!ll&&!rl&&r
0010
$handler and defined $prefix and $uri
!ll&&!rl&&r
0010
$handler and defined $prefix
!ll&&!rl&&r
0010
MC/DC0%0/4
$handler and defined $prefix and $uri and $child
$handler defined $prefix $uri $child
Truth table20%1/5
A and B and C and D
A= $handlerB= defined $prefixC= $uriD= $child
ABCDresult
0XXX0
10XX0
110X0
11100
11111
241 unless $handler && defined $prefix && $uri && $child;
242 10 ▶ Carp::croak "Only one child of a namespace element is permitted"
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 0 10 10
243 if @args > 1;
244 10 ▶ Carp::croak "Namespace child must be element or namespace: $child"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $child eq "XML::SAX::Builder::Tag" or ref $child eq __PACKAGE__ 0 10 10
Condition66%2/3
ref $child eq "XML::SAX::Builder::Tag" or ref $child eq __PACKAGE__
l!l&&r!l&&!r
910
MC/DC0%0/2
ref $child eq "XML::SAX::Builder::Tag" or ref $child eq __PACKAGE__
ref $child eq "XML::SAX::Builder::Tag" ref $child eq __PACKAGE__
Truth table66%2/3
A or B
A= ref $child eq "XML::SAX::Builder::Tag"B= ref $child eq __PACKAGE__
ABresult
1X1
011
000
245 unless ref($child) eq 'XML::SAX::Builder::Tag' || ref($child) eq __PACKAGE__;
246 10 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
247 10 my ( $self, $nsup ) = @_;
248 10 $nsup->declare_prefix( $prefix => $uri );
249 10 my $data = {
250 Prefix => $prefix,
251 NamespaceURI => $uri,
252 };
253 10 $handler->start_prefix_mapping( $data );
254 10 $child->run( $nsup );
255 10 $handler->end_prefix_mapping( $data );
256 10 };
257 }
258  
259 #---------------------------------------------------------------------
260  
261 package XML::SAX::Builder::CDATA;
262 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
263 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 2 ▶sub _make_closure {
Subroutine _make_closure: called CC=2 SCAR=6.9
267 2 my ( $class, $handler, @args ) = @_;
268 Carp::croak "arguments must be character data only"
269 2 ▶ if grep { ref } @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} @args 0 2 2
270 2 @args = grep { defined } @args;
271 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
272 2 my ( $self, $nsup ) = @_;
273 2 $handler->start_cdata( {} );
274 2 $handler->characters( { Data => join ( '', @args ) } );
275 2 $handler->end_cdata( {} );
276 2 };
277 }
278  
279 #---------------------------------------------------------------------
280  
281 package XML::SAX::Builder::Doctype;
282 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 4 ▶sub _make_closure {
Subroutine _make_closure: called CC=3 SCAR=11.1
287 4 my ( $class, $handler, $name, $system, $public ) = @_;
288 4 ▶ Carp::croak "doctype: must specify name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
289 4 ▶ Carp::croak "doctype: must specify system id" unless $system;
Branch: 1 branch
BranchTrueFalseTotal
unless $system 0 4 4
290 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
291 3 my ( $self, $nsup ) = @_;
292 3 my $data = {
293 Name => $name,
294 PublicId => $public,
295 SystemId => $system,
296 };
297 3 $handler->start_dtd( $data );
298 3 $handler->end_dtd( $data );
299 }
300 4 }
301  
302 #---------------------------------------------------------------------
303  
304 package XML::SAX::Builder::Comment;
305 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
307 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
308  
309 1 ▶sub _make_closure {
Subroutine _make_closure: called CC=1 SCAR=0.0
310 1 my ( $class, $handler, $data ) = @_;
311 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
312 1 my ( $self, $nsup ) = @_;
313 1 $handler->comment( { Data => $data } );
314 }
315 1 }
316  
317 #---------------------------------------------------------------------
318  
319 package XML::SAX::Builder::ProcessingInstruction;
320 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
321 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
322 4 ▶use base 'XML::SAX::Builder::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
323  
324 2 ▶sub _make_closure {
Subroutine _make_closure: called CC=3 SCAR=11.0
325 2 my ( $class, $handler, $target, $data ) = @_;
326 2 ▶ Carp::croak "usage: xmlpi(target,data)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 2 2
327 unless @_ == 4;
328 2 ▶ Carp::croak "names beginning with /xml/i are reserved"
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_reserved_name($target) 1 1 2
329 if $class->_is_reserved_name( $target );
330 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
331 1 my ( $self, $nsup ) = @_;
332 1 $handler->processing_instruction( {
333 Target => $target,
334 Data => $data,
335 } );
336 }
337 1 }
338  
339 4 1;
340 __END__
↑ Summary