LibXML.pm

filter: Statementstmt 45.1% 84 / 186 Branchbran 29.0% 25 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.6% 15 / 26 Podpod 0.0% 0 / 20 totaltotal 38.9% 124 / 318 CC 53 SCAR 29.4
CC
53
Cov
40%
replace_names_values
72.0
children
30.4
attribute
28.5
CRAP 18.9
↑ Summary ex.pl »
1 package XML::SimpleObject::LibXML;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use XML::LibXML 1.53;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use XML::LibXML::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.60';
8  
9 0 ▶sub attributes {
Subroutine attributes: not called CC=2 SCAR=17.9
Pod undocumented
10 0 my $self = shift;
11 0 my $name = shift;
12 0 my %attributes;
13 0 my @attrs = $self->{_DOM}->getAttributes;
14 0 foreach my $attribute (@attrs) {
15 0 $attributes{$attribute->getName} = $attribute->value;
16 }
17 0 return %attributes;
18 }
19  
20 2 ▶sub attribute {
Subroutine attribute: called CC=9 SCAR=33.5
Pod undocumented
21 2 my $self = shift;
22 2 my $name = shift;
23 2 my $newvalue = shift;
24 2 ▶ if ($self->hasNamespaces()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasNamespaces) { } 2 0 2
25 2 my ($namespaceURI, $localName) = $self->_parseTagName($name, ATTRIBUTE_NODE);
26  
27 2 ▶ if (defined($namespaceURI)) { # Attribute has an explicit namespace
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespaceURI) { } 0 2 2
28 0 my ($found) = $self->{_DOM}->getAttributeNodeNS($namespaceURI, $localName);
29 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
30 0 ▶ if ($newvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newvalue) { } 0 0 0
31 0 $found->setValue($newvalue);
32 } else {
33 0 return $found->value;
34 }
35 }
36 } else { # Attribute has no namespace
37 2 my ($found) = $self->{_DOM}->getAttributeNode($name);
38 2 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 2 0 2
39 2 ▶ if ($newvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newvalue) { } 0 2 2
40 0 $found->setValue($newvalue);
41 } else {
42 2 return $found->value;
43 }
44 }
45 }
46  
47 } else {
48 0 my ($found) = $self->{_DOM}->findnodes("\@$name");
49 0 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
50 0 ▶ if ($newvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newvalue) { } 0 0 0
51 0 $found->setValue($newvalue);
52 } else {
53 0 return $found->value;
54 }
55 }
56 }
57 }
58  
59 1 ▶sub add_attribute {
Subroutine add_attribute: called CC=1 SCAR=0.0
Pod undocumented
60 1 my $self = shift;
61 1 $self->{_DOM}->setAttribute( $_[0], $_[1] );
62 1 return $self;
63 }
64  
65 3 ▶sub value {
Subroutine value: called CC=3 SCAR=11.6
Pod undocumented
66 3 my $node = shift;
67 3 my $newvalue = shift;
68 3 my ($found) = $node->{_DOM}->findnodes("text()");
69 3 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 3 0 3
70 3 ▶ if (defined $newvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newvalue) { } 0 3 3
71 0 return $found->setData($newvalue);
72 } else {
73 3 return $found->getData();
74 }
75 }
76 }
77  
78 1 ▶sub name {
Subroutine name: called CC=2 SCAR=7.4
Pod undocumented
79 1 my $node = shift;
80 1 my $newname = shift;
81 1 ▶ if ($newname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newname) { } 0 1 1
82 0 $node->{_DOM}->setNodeName($newname);
83 } else {
84 1 $node->{_DOM}->getName;
85 }
86 }
87  
88 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
89 0 $_[0]->{_DOM}->nodeType;
90 }
91  
92 0 ▶sub xpath_search {
Subroutine xpath_search: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my $self = shift;
94 0 my $xpath = shift;
95 0 my @nodes;
96 0 foreach my $node ($self->{_DOM}->findnodes($xpath)) {
97 0 my $newobj = new XML::SimpleObject::LibXML ($node);
98 0 ▶ return $newobj unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
99 0 push @nodes, $newobj;
100 }
101 0 return @nodes;
102 }
103  
104 0 ▶sub namespaceURI() {
Subroutine namespaceURI: not called CC=1 SCAR=6.9
Pod undocumented
105 0 $_[0]->{_DOM}->getNamespaceURI();
106 }
107  
108 0 ▶sub namespace() {
Subroutine namespace: not called CC=4 SCAR=30.0
Pod undocumented
109 0 my $node = shift;
110 0 my $namespaceURI = $node->namespaceURI();
111  
112 0 ▶ return unless (defined $namespaceURI);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespaceURI 0 0 0
113  
114 0 my (@found) = $node->{_DOM}->findnodes("namespace::*"); for (@found) {
115 0 ▶ return $_->getLocalName() if $namespaceURI eq $_->getData();
Branch: 1 branch
BranchTrueFalseTotal
if $namespaceURI eq $_->getData 0 0 0
116 }
117 }
118  
119 13 ▶sub hasNamespaces() {
Subroutine hasNamespaces: called CC=1 SCAR=0.0
Pod undocumented
120 13 return $_[0]->{_NAMESPACES};
121 }
122  
123 13 ▶sub _parseTagName($$) {
Subroutine _parseTagName: called CC=5 SCAR=19.3
124 13 my ($self, $tag, $type) = @_;
125  
126 13 my ($namespaceURI, $localName);
127  
128 13 ▶ if ($tag =~ /^([^:]+):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^([^:]+):(.*)/) { } 0 13 13
129 # Tag has an explicit namespace
130 0 $namespaceURI = $self->{_NAMESPACES}->{$1};
131 0 $localName = $2;
132 0 ▶ die("Unknown namespace $1") unless ($namespaceURI);
Branch: 1 branch
BranchTrueFalseTotal
unless $namespaceURI 0 0 0
133 }
134 else {
135 13 $localName = $tag;
136 # Tag has no explicit namespace.
137 13 ▶ if ($type eq ELEMENT_NODE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 1) { } 11 2 13
elsif ($type eq 2) { } 2 0 2
138 # Elements live in the default namespace.
139 # Go with the default namespace. If one was specified, it will be ''.
140 11 $namespaceURI = $self->{_NAMESPACES}->{''};
141 }
142 elsif ($type eq ATTRIBUTE_NODE) {
143 # The default namespace does not apply to attribute names.
144 # (See http://www.w3.org/TR/1998/PR-xml-names-19981117, section 5.3)
145 }
146 else {
147 # Nothing else has tagnames.
148 0 die("Unexpected type $type");
149 }
150 }
151  
152 13 return ($namespaceURI, $localName);
153 }
154  
155 11 ▶sub child {
Subroutine child: called CC=5 SCAR=18.2
Pod undocumented
156 11 my $self = shift;
157 11 my $tag = shift;
158 11 ▶ if (ref $self->{_DOM} eq "XML::LibXML::Document") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'_DOM'} eq 'XML::LibXML::Document') { } 6 5 11
elsif ($self->hasNamespaces) { } 5 0 5
159 6 my $node = new XML::SimpleObject::LibXML ($self->{_DOM}->documentElement());
160 6 return $node;
161 }
162 elsif ($self->hasNamespaces()) {
163 5 my ($namespaceURI, $localName) = $self->_parseTagName($tag, ELEMENT_NODE);
164 5 my ($element) = $self->{_DOM}->getElementsByTagNameNS($namespaceURI, $localName);
165 5 ▶ return unless ($element);
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 5 5
166 5 my $node = new XML::SimpleObject::LibXML ($element);
167 5 return $node;
168 }
169 else
170 {
171 0 my ($element) = $self->{_DOM}->getElementsByTagName($tag);
172 0 ▶ return unless ($element);
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 0 0
173 0 my $node = new XML::SimpleObject::LibXML ($element);
174 0 return $node;
175 }
176 }
177  
178 1 ▶sub add_child {
Subroutine add_child: called CC=2 SCAR=7.0
Pod undocumented
179 1 my $self = shift;
180 1 my $element = $self->{_DOM}->addNewChild( undef, $_[0]);
181 1 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 1 0 1
182 1 $element->appendTextNode($_[1]);
183 }
184 1 my $node = new XML::SimpleObject::LibXML ($element);
185 1 return $node;
186 }
187  
188 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my $self = shift;
190 0 $self->{_DOM}->unbindNode;
191 }
192  
193 0 ▶sub children_names {
Subroutine children_names: not called CC=3 SCAR=24.8
Pod undocumented
194 0 my $self = shift;
195 0 my @elements;
196 0 foreach my $node ($self->{_DOM}->getChildnodes)
197 {
198 0 ▶ next if ($node->nodeType == 3);
Branch: 1 branch
BranchTrueFalseTotal
if $node->nodeType == 3 0 0 0
199 0 push @elements, $node->getName;
200 }
201 0 return @elements;
202 }
203  
204 6 ▶sub children {
Subroutine children: called CC=10 SCAR=34.1
Pod undocumented
205 6 my $self = shift;
206 6 my $tag = shift;
207 6 ▶ if (ref $self->{_DOM} eq "XML::LibXML::Document") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'_DOM'} eq 'XML::LibXML::Document') { } 0 6 6
elsif (defined $tag) { } 6 0 6
208 0 my $node = new XML::SimpleObject::LibXML ($self->{_DOM}->documentElement());
209 0 return $node;
210 }
211 elsif (defined($tag)) { # tag: return matching children
212 6 ▶ if ($self->hasNamespaces()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasNamespaces) { } 6 0 6
213 6 my ($namespaceURI, $localName) = $self->_parseTagName($tag, ELEMENT_NODE);
214 6 my @nodelist;
215 6 foreach my $node ($self->{_DOM}->getElementsByTagNameNS($namespaceURI, $localName)) {
216 12 ▶ next if ($node->nodeType == TEXT_NODE);
Branch: 1 branch
BranchTrueFalseTotal
if $node->nodeType == 3 0 12 12
217 12 push @nodelist, new XML::SimpleObject::LibXML ($node);
218 }
219 6 return @nodelist;
220 }
221 else {
222 0 my @nodelist;
223 0 foreach my $node ($self->{_DOM}->getElementsByTagName($tag)) {
224 0 ▶ next if ($node->nodeType == TEXT_NODE);
Branch: 1 branch
BranchTrueFalseTotal
if $node->nodeType == 3 0 0 0
225 0 push @nodelist, new XML::SimpleObject::LibXML ($node);
226 }
227 0 return @nodelist;
228 }
229 }
230 else # no tag: return all children
231 {
232 0 my @nodelist;
233 0 foreach my $node ($self->{_DOM}->getChildnodes()) {
234 0 ▶ next if ($node->nodeType == 3);
Branch: 1 branch
BranchTrueFalseTotal
if $node->nodeType == 3 0 0 0
235 0 push @nodelist, new XML::SimpleObject::LibXML ($node);
236 }
237 0 return @nodelist;
238 }
239 }
240  
241 0 ▶sub output_xml {
Subroutine output_xml: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my $self = shift;
243 0 my %args = @_;
244 0 return $self->{_DOM}->toString($args{indent},$args{original_encoding});
245 }
246  
247 0 ▶sub output_xml_file {
Subroutine output_xml_file: not called CC=2 SCAR=17.9
Pod undocumented
248 0 my $self = shift;
249 0 my %args = @_;
250 0 ▶ open FILE, ">" . $args{file} or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, '>' . $args{"file"} or die $! 0 0 0
251 0 print FILE $self->output_xml(%args);
252 0 close FILE;
253 }
254  
255 0 ▶sub replace_names_values {
Subroutine replace_names_values: not called CC=8 SCAR=42.8
Pod undocumented
256 0 my $self = shift;
257 0 my %args = @_;
258 0 foreach my $node ($self->{_DOM}->findnodes($args{xpath})) {
259 0 ▶ $args{name} && do { $node->setNodeName($args{name}) };
Branch: 1 branch
BranchTrueFalseTotal
$args{"name"} and do { $node->setNodeName($args{"name"}) } 0 0 0
260 0 my $nodetype = $node->nodeType;
261 0 ▶ if ($nodetype == 1) { # Element, try text node
Branch: 3 branches
BranchTrueFalseTotal
if ($nodetype == 1) { } 0 0 0
elsif ($nodetype == 2) { } 0 0 0
elsif ($nodetype == 3) { } 0 0 0
262 0 ▶ $args{value} && do {
Branch: 1 branch
BranchTrueFalseTotal
$args{"value"} and do { my($found) = $node->findnodes("text()"); $found->setData($args{"value"}) } 0 0 0
263 0 my ($found) = $node->findnodes("text()");
264 0 $found->setData($args{value});
265 };
266 } elsif ($nodetype == 2) { # Attribute
267 0 ▶ $args{value} && do { $node->setValue($args{value}) };
Branch: 1 branch
BranchTrueFalseTotal
$args{"value"} and do { $node->setValue($args{"value"}) } 0 0 0
268 } elsif ($nodetype == 3) { # Text
269 0 $node->setData($args{value});
270 }
271 }
272 }
273  
274 0 ▶sub delete_nodes {
Subroutine delete_nodes: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my $self = shift;
276 0 my %args = @_;
277 0 foreach my $node ($self->{_DOM}->findnodes($args{xpath})) {
278 0 $node->unbindNode();
279 }
280 }
281  
282 25 ▶sub _build_namespace_map() {
Subroutine _build_namespace_map: called CC=4 SCAR=14.0
283 25 my $self = shift;
284 25 ▶ return if $self->{_NAMESPACES};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_NAMESPACES"} 0 25 25
285  
286 25 ▶ my %map = map { my $key = $_->getLocalName(); $key = '' unless defined $key; $key => $_->getData() } $self->{_DOM}->findnodes("namespace::*");
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 24 24
287 25 ▶ $self->{_NAMESPACES} = \%map if (scalar(%map));
Branch: 1 branch
BranchTrueFalseTotal
if scalar %map 24 1 25
288 }
289  
290 25 ▶sub _init() {
Subroutine _init: called CC=1 SCAR=0.0
291 25 my $self = shift;
292 25 $self->_build_namespace_map();
293 }
294  
295 25 ▶sub new {
Subroutine new: called CC=4 SCAR=22.3
Pod undocumented
296 25 my $class = shift;
297 25 ▶ if (ref($_[0]) =~ /^XML\:\:LibXML/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /^XML\:\:LibXML/) { } 25 0 25
298 25 my $self = {};
299 25 bless ($self,$class);
300 25 $self->{_DOM} = $_[0];
301 25 $self->_init();
302 25 return $self;
303 } else {
304 0 my %args = @_;
305 0 my $parser = new XML::LibXML;
306 0 my $dom;
307 0 ▶ if ($args{XML}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'XML'}) { } 0 0 0
elsif ($args{'file'}) { } 0 0 0
308 0 $dom = $parser->parse_string($args{XML});
309 } elsif ($args{file}) {
310 0 $dom = $parser->parse_file($args{file});
311 } else {
312 0 die "new() not called with DOM, XML string, or filename";
313 }
314 0 my $self = {};
315 0 bless ($self,$class);
316 0 $self->{_NAME} = "";
317 0 $self->{_DOM} = $dom;
318 0 $self->_init();
319 0 return $self;
320 }
321 }
322  
323  
324 1 1;
325 __END__
↑ Summary ex.pl »