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

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