blib/lib/XML/LibXML/SAX/Parser.pm

filter: Statementstmt 80.7% 88 / 109 Branchbran 53.8% 28 / 52 Conditioncond 45.0% 9 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 55.0% 11 / 20 Podpod 0.0% 0 / 3 totaltotal 64.3% 139 / 216 CC 40 SCAR 31.2
CC
40
Cov
69%
process_node
60.5
process_element
16.1
generate
8.9
CRAP 22.7
« blib/lib/XML/LibXML/SAX/Generator.pm ↑ Summary blib/lib/XML/LibXML/SAX/Builder.pm »
1 # $Id$
2 #
3 # This is free software, you may use it and distribute it under the same terms as
4 # Perl itself.
5 #
6 # Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
7 #
8 #
9  
10 package XML::LibXML::SAX::Parser;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use XML::LibXML::Common qw(:libxml);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use XML::SAX::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use XML::SAX::DocumentLocator;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = "2.0213"; # VERSION TEMPLATE: DO NOT CHANGE
21 1 our @ISA = ('XML::SAX::Base');
22  
23 0 ▶sub CLONE_SKIP {
Subroutine CLONE_SKIP: not called CC=2 SCAR=17.9
24 0 ▶ return $XML::LibXML::__threads_shared ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$XML::LibXML::__threads_shared ? : 0 0 0
25 }
26  
27 0 ▶sub _parse_characterstream {
Subroutine _parse_characterstream: not called CC=1 SCAR=6.9
28 0 my ($self, $fh, $options) = @_;
29 0 die "parsing a characterstream is not supported at this time";
30 }
31  
32 0 ▶sub _parse_bytestream {
Subroutine _parse_bytestream: not called CC=2 SCAR=17.9
33 0 my ($self, $fh, $options) = @_;
34 0 my $parser = XML::LibXML->new();
35 0 ▶ my $doc = exists($options->{Source}{SystemId}) ? $parser->parse_fh($fh, $options->{Source}{SystemId}) : $parser->parse_fh($fh);
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'Source'}{'SystemId'} ? : 0 0 0
36 0 $self->generate($doc);
37 }
38  
39 3 ▶sub _parse_string {
Subroutine _parse_string: called CC=2 SCAR=7.0
40 3 my ($self, $str, $options) = @_;
41 3 my $parser = XML::LibXML->new();
42 3 ▶ my $doc = exists($options->{Source}{SystemId}) ? $parser->parse_string($str, $options->{Source}{SystemId}) : $parser->parse_string($str);
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'Source'}{'SystemId'} ? : 0 3 3
43 2 $self->generate($doc);
44 }
45  
46 2 ▶sub _parse_systemid {
Subroutine _parse_systemid: called CC=1 SCAR=0.0
47 2 my ($self, $sysid, $options) = @_;
48 2 my $parser = XML::LibXML->new();
49 2 my $doc = $parser->parse_file($sysid);
50 2 $self->generate($doc);
51 }
52  
53 6 ▶sub generate {
Subroutine generate: called CC=6 SCAR=21.9
Pod undocumented
54 6 my $self = shift;
55 6 my ($node) = @_;
56  
57 6 my $doc = $node->ownerDocument();
58 {
59 # precompute some DocumentLocator values
60 6 my %locator = (
61 PublicId => undef,
62 SystemId => undef,
63 Encoding => undef,
64 XMLVersion => undef,
65 );
66 6 ▶ my $dtd = defined $doc ? $doc->externalSubset() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $doc ? : 6 0 6
67 6 ▶ if (defined $dtd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dtd) 0 6 6
68 0 $locator{PublicId} = $dtd->publicId();
69 0 $locator{SystemId} = $dtd->systemId();
70 }
71 6 ▶ if (defined $doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) 6 0 6
72 6 $locator{Encoding} = $doc->encoding();
73 6 $locator{XMLVersion} = $doc->version();
74 }
75 $self->set_document_locator(
76 XML::SAX::DocumentLocator->new(
77 0 ▶ sub { $locator{PublicId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 ▶ sub { $locator{SystemId} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ sub { defined($self->{current_node}) ? $self->{current_node}->line_number() : undef },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'current_node'} ? : 0 0 0
80 0 ▶ sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 ▶ sub { $locator{Encoding} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 ▶ sub { $locator{XMLVersion} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 6 ),
84 );
85 }
86  
87 6 ▶ if ( $node->nodeType() == XML_DOCUMENT_NODE
Branch: 1 branch
BranchTrueFalseTotal
if ($node->nodeType == 9 or $node->nodeType == 13) 6 0 6
Condition33%1/3
$node->nodeType == 9 or $node->nodeType == 13
l!l&&r!l&&!r
600
MC/DC0%0/2
$node->nodeType == 9 or $node->nodeType == 13
$node->nodeType == 9 $node->nodeType == 13
Truth table33%1/3
A or B
A= $node->nodeType == 9B= $node->nodeType == 13
ABresult
1X1
011
000
88 || $node->nodeType == XML_HTML_DOCUMENT_NODE ) {
89 6 $self->start_document({});
90 6 $self->xml_decl({Version => $node->getVersion, Encoding => $node->getEncoding});
91 6 $self->process_node($node);
92 6 $self->end_document({});
93 }
94 }
95  
96 104 ▶sub process_node {
Subroutine process_node: called CC=16 SCAR=41.0
Pod undocumented
97 104 my ($self, $node) = @_;
98  
99 104 local $self->{current_node} = $node;
100  
101 104 my $node_type = $node->nodeType();
102 104 ▶ if ($node_type == XML_COMMENT_NODE) {
Branch: 9 branches
BranchTrueFalseTotal
if ($node_type == 8) { } 0 104 104
elsif ($node_type == 3 or $node_type == 4) { } 62 42 104
elsif ($node_type == 1) { } 36 6 42
elsif ($node_type == 5) { } 0 6 6
elsif ($node_type == 9 or $node_type == 13 or $node_type == 11) { } 6 0 6
elsif ($node_type == 7) { } 0 0 0
elsif ($node_type == 8) { } 0 0 0
elsif ($node_type == 19 or $node_type == 20) { } 0 0 0
elsif ($node_type == 14) { } 0 0 0
103 0 $self->comment( { Data => $node->getData } );
104 }
105 ▶ elsif ($node_type == XML_TEXT_NODE
Condition66%2/3
$node_type == 3 or $node_type == 4
l!l&&r!l&&!r
62042
MC/DC50%1/2
$node_type == 3 or $node_type == 4
$node_type == 3 $node_type == 4
Truth table66%2/3
A or B
A= $node_type == 3B= $node_type == 4
ABresult
1X1
011
000
106 || $node_type == XML_CDATA_SECTION_NODE) {
107 # warn($node->getData . "\n");
108 62 $self->characters( { Data => $node->nodeValue } );
109 }
110 elsif ($node_type == XML_ELEMENT_NODE) {
111 # warn("<" . $node->getName . ">\n");
112 36 $self->process_element($node);
113 # warn("</" . $node->getName . ">\n");
114 }
115 elsif ($node_type == XML_ENTITY_REF_NODE) {
116 0 foreach my $kid ($node->childNodes) {
117 # warn("child of entity ref: " . $kid->getType() . " called: " . $kid->getName . "\n");
118 0 $self->process_node($kid);
119 }
120 }
121 ▶ elsif ($node_type == XML_DOCUMENT_NODE
Condition33%2/6
$node_type == 9 or $node_type == 13 or $node_type == 11
l!l&&r!l&&!r
600
$node_type == 9 or $node_type == 13
l!l&&r!l&&!r
600
MC/DC0%0/3
$node_type == 9 or $node_type == 13 or $node_type == 11
$node_type == 9 $node_type == 13 $node_type == 11
Truth table25%1/4
A or B or C
A= $node_type == 9B= $node_type == 13C= $node_type == 11
ABCresult
1XX1
01X1
0011
0000
122 || $node_type == XML_HTML_DOCUMENT_NODE
123 || $node_type == XML_DOCUMENT_FRAG_NODE) {
124 # sometimes it is just useful to generate SAX events from
125 # a document fragment (very good with filters).
126 6 foreach my $kid ($node->childNodes) {
127 6 $self->process_node($kid);
128 }
129 }
130 elsif ($node_type == XML_PI_NODE) {
131 0 $self->processing_instruction( { Target => $node->getName, Data => $node->getData } );
132 }
133 elsif ($node_type == XML_COMMENT_NODE) {
134 0 $self->comment( { Data => $node->getData } );
135 }
136 ▶ elsif ( $node_type == XML_XINCLUDE_START
Condition0%0/3
$node_type == 19 or $node_type == 20
l!l&&r!l&&!r
000
MC/DC0%0/2
$node_type == 19 or $node_type == 20
$node_type == 19 $node_type == 20
Truth table0%0/3
A or B
A= $node_type == 19B= $node_type == 20
ABresult
1X1
011
000
137 || $node_type == XML_XINCLUDE_END ) {
138 # ignore!
139 # i may want to handle this one day, dunno yet
140 }
141 elsif ($node_type == XML_DTD_NODE ) {
142 # ignore!
143 # i will support DTDs, but had no time yet.
144 }
145 else {
146 # warn("unsupported node type: $node_type");
147 }
148  
149 }
150  
151 36 ▶sub process_element {
Subroutine process_element: called CC=16 SCAR=27.8
Pod undocumented
152 36 my ($self, $element) = @_;
153  
154 36 my $attribs = {};
155 36 my @ns_maps = $element->getNamespaces;
156  
157 36 foreach my $ns (@ns_maps) {
158 4 ▶ $self->start_prefix_mapping(
Branch: 1 branch
BranchTrueFalseTotal
defined $ns->localname ? : 2 2 4
159 {
160 NamespaceURI => $ns->href,
161 Prefix => ( defined $ns->localname ? $ns->localname : ''),
162 }
163 );
164 }
165  
166 36 foreach my $attr ($element->attributes) {
167 17 my $key;
168 # warn("Attr: $attr -> ", $attr->getName, " = ", $attr->getData, "\n");
169 # this isa dump thing...
170 17 ▶ if ($attr->isa('XML::LibXML::Namespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->isa('XML::LibXML::Namespace')) { } 4 13 17
171 # TODO This needs fixing modulo agreeing on what
172 # is the right thing to do here.
173 4 ▶ unless ( defined $attr->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr->name) { } 4 0 4
174 ## It's an atter like "xmlns='foo'"
175 0 $attribs->{"{}xmlns"} =
176 {
177 Name => "xmlns",
178 LocalName => "xmlns",
179 Prefix => "",
180 Value => $attr->href,
181 NamespaceURI => "",
182 };
183 }
184 else {
185 4 my $prefix = "xmlns";
186 4 my $localname = $attr->localname;
187 4 my $key = "{http://www.w3.org/2000/xmlns/}";
188 4 my $name = "xmlns";
189  
190 4 ▶ if ( defined $localname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $localname) 2 2 4
191 2 $key .= $localname;
192 2 $name.= ":".$localname;
193 }
194  
195 4 $attribs->{$key} =
196 {
197 Name => $name,
198 Value => $attr->href,
199 NamespaceURI => "http://www.w3.org/2000/xmlns/",
200 Prefix => $prefix,
201 LocalName => $localname,
202 };
203 }
204 }
205 else {
206 13 my $ns = $attr->namespaceURI;
207  
208 13 ▶ $ns = '' unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 11 2 13
209 13 $key = "{$ns}".$attr->localname;
210 ## Not sure why, but $attr->name is coming through stripped
211 ## of its prefix, so we need to hand-assemble a real name.
212 13 my $name = $attr->name;
213 13 ▶ $name = "" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 13 13
214  
215 13 my $prefix = $attr->prefix;
216 13 ▶ $prefix = "" unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 11 2 13
217 13 ▶ $name = "$prefix:$name"
Branch: 1 branch
BranchTrueFalseTotal
if index($name, ":") == -1 and length $prefix 2 11 13
Condition66%2/3
index($name, ":") == -1 and length $prefix
!ll&&!rl&&r
0112
MC/DC50%1/2
index($name, ":") == -1 and length $prefix
index($name, ":") == -1 length $prefix
Truth table66%2/3
A and B
A= index($name, ":") == -1B= length $prefix
ABresult
0X0
100
111
218 if index( $name, ":" ) < 0 && length $prefix;
219  
220 13 $attribs->{$key} =
221 {
222 Name => $name,
223 Value => $attr->value,
224 NamespaceURI => $ns,
225 Prefix => $prefix,
226 LocalName => $attr->localname,
227 };
228 }
229 # use Data::Dumper;
230 # warn("Attr made: ", Dumper($attribs->{$key}), "\n");
231 }
232  
233 36 ▶ my $node = {
Condition100%2/2
$element->prefix || ''
l!l
135
MC/DC100%1/1
$element->prefix || ''
$element->prefix
Truth table100%2/2
A
A= $element->prefix
Aresult
11
00
234 Name => $element->nodeName,
235 Attributes => $attribs,
236 NamespaceURI => $element->namespaceURI,
237 Prefix => $element->prefix || "",
238 LocalName => $element->localname,
239 };
240  
241 36 $self->start_element($node);
242  
243 36 foreach my $child ($element->childNodes) {
244 92 $self->process_node($child);
245 }
246  
247 36 my $end_node = { %$node };
248  
249 36 delete $end_node->{Attributes};
250  
251 36 $self->end_element($end_node);
252  
253 36 foreach my $ns (@ns_maps) {
254 4 ▶ $self->end_prefix_mapping(
Branch: 1 branch
BranchTrueFalseTotal
defined $ns->localname ? : 2 2 4
255 {
256 NamespaceURI => $ns->href,
257 Prefix => ( defined $ns->localname ? $ns->localname : ''),
258 }
259 );
260 }
261 }
262  
263 1 1;
264  
265 __END__
« blib/lib/XML/LibXML/SAX/Generator.pm ↑ Summary blib/lib/XML/LibXML/SAX/Builder.pm »