XPath/Builder.pm

filter: Statementstmt 32.3% 34 / 105 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 10 / 20 Podpod 0.0% 0 / 8 totaltotal 26.5% 44 / 166 CC 15 SCAR 28.3
CC
15
Cov
26%
start_element
56.0
_scan_namespaces
20.0
characters
12.0
CRAP 16.9
« XPath/Node/Element.pm ↑ Summary XPath/Node.pm »
1 package XML::XPath::Builder;
2  
3 1 $VERSION = '1.26';
4  
5 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # to get array index constants
8 1 ▶use XML::XPath::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::XPath::Node::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::XPath::Node::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use XML::XPath::Node::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use XML::XPath::Node::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::XPath::Node::PI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use XML::XPath::Node::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use vars qw/$xmlns_ns $xml_ns/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $xmlns_ns = "http://www.w3.org/2000/xmlns/";
19 1 $xml_ns = "http://www.w3.org/XML/1998/namespace";
20  
21 sub new {
22 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
23 0 ▶ my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 0 0
24  
25 0 bless $self, $class;
26 }
27  
28 sub start_document {
29 0 ▶ my $self = shift;
Subroutine start_document: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 0 $self->{IdNames} = {};
32 $self->{InScopeNamespaceStack} = [ {
33 0 '_Default' => undef,
34 'xmlns' => $xmlns_ns,
35 'xml' => $xml_ns,
36 } ];
37  
38 0 $self->{NodeStack} = [ ];
39  
40 0 my $document = XML::XPath::Node::Element->new();
41 0 my $newns = XML::XPath::Node::Namespace->new('xml', $xml_ns);
42 0 $document->appendNamespace($newns);
43 0 $self->{current} = $self->{DOC_Node} = $document;
44 }
45  
46 sub end_document {
47 0 ▶ my $self = shift;
Subroutine end_document: not called CC=1 SCAR=6.9
Pod undocumented
48  
49 0 return $self->{DOC_Node};
50 }
51  
52 sub characters {
53 0 ▶ my $self = shift;
Subroutine characters: not called CC=3 SCAR=24.8
Pod undocumented
54 0 my $sarg = shift;
55 0 my $text = $sarg->{Data};
56  
57 0 my $parent = $self->{current};
58  
59 0 my $last = $parent->getLastChild;
60 0 ▶ if ($last && $last->isTextNode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and $last->isTextNode) 0 0 0
Condition0%0/3
$last and $last->isTextNode
!ll&&!rl&&r
000
MC/DC0%0/2
$last and $last->isTextNode
$last $last->isTextNode
Truth table0%0/3
A and B
A= $lastB= $last->isTextNode
ABresult
0X0
100
111
61 # append to previous text node
62 0 $last->appendText($text);
63 0 return;
64 }
65  
66 0 my $node = XML::XPath::Node::Text->new($text);
67 0 $parent->appendChild($node, 1);
68 }
69  
70 sub start_element {
71 0 ▶ my $self = shift;
Subroutine start_element: not called CC=7 SCAR=40.3
Pod undocumented
72 0 my $sarg = shift;
73 0 my $tag = $sarg->{'Name'};
74 0 my $attr = $sarg->{'Attributes'};
75  
76 0 push @{ $self->{InScopeNamespaceStack} },
77 0 { %{ $self->{InScopeNamespaceStack}[-1] } };
78 0 $self->_scan_namespaces(@_);
79  
80 0 my ($prefix, $namespace) = $self->_namespace($tag);
81  
82 0 my $node = XML::XPath::Node::Element->new($tag, $prefix);
83  
84 0 foreach my $name (keys %$attr) {
85 0 my $value = $attr->{$name};
86  
87 0 ▶ if ($name =~ /^xmlns(:(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^xmlns(:(.*))?$/) { } 0 0 0
88 # namespace node
89 0 ▶ my $prefix = $2 || '#default';
Condition0%0/2
$2 || '#default'
l!l
00
MC/DC0%0/1
$2 || '#default'
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
90 # warn "Creating NS node: $prefix = $value\n";
91 0 my $newns = XML::XPath::Node::Namespace->new($prefix, $value);
92 0 $node->appendNamespace($newns);
93 }
94 else {
95 0 my ($prefix, $namespace) = $self->_namespace($name);
96 0 ▶ undef $namespace unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
97  
98 0 my $newattr = XML::XPath::Node::Attribute->new($name, $value, $prefix);
99 0 $node->appendAttribute($newattr, 1);
100 0 ▶ if (exists($self->{IdNames}{$tag}) && ($self->{IdNames}{$tag} eq $name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"IdNames"}{$tag} and $self->{"IdNames"}{$tag} eq $name) 0 0 0
Condition0%0/3
exists $self->{"IdNames"}{$tag} and $self->{"IdNames"}{$tag} eq $name
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"IdNames"}{$tag} and $self->{"IdNames"}{$tag} eq $name
exists $self->{"IdNames"}{$tag} $self->{"IdNames"}{$tag} eq $name
Truth table0%0/3
A and B
A= exists $self->{"IdNames"}{$tag}B= $self->{"IdNames"}{$tag} eq $name
ABresult
0X0
100
111
101 # warn "appending Id Element: $val for ", $node->getName, "\n";
102 0 $self->{DOC_Node}->appendIdElement($value, $node);
103 }
104 }
105 }
106  
107 0 $self->{current}->appendChild($node, 1);
108 0 $self->{current} = $node;
109 }
110  
111 sub end_element {
112 0 ▶ my $self = shift;
Subroutine end_element: not called CC=1 SCAR=6.9
Pod undocumented
113 0 $self->{current} = $self->{current}->getParentNode;
114 }
115  
116 sub processing_instruction {
117 0 ▶ my $self = shift;
Subroutine processing_instruction: not called CC=1 SCAR=6.9
Pod undocumented
118 0 my $pi = shift;
119 0 my $node = XML::XPath::Node::PI->new($pi->{Target}, $pi->{Data});
120 0 $self->{current}->appendChild($node, 1);
121 }
122  
123 sub comment {
124 0 ▶ my $self = shift;
Subroutine comment: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my $comment = shift;
126 0 my $node = XML::XPath::Node::Comment->new($comment->{Data});
127 0 $self->{current}->appendChild($node, 1);
128 }
129  
130 sub _scan_namespaces {
131 0 ▶ my ($self, %attributes) = @_;
Subroutine _scan_namespaces: not called CC=4 SCAR=30.0
132  
133 0 ▶ while (my ($attr_name, $value) = each %attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attributes) 0 0 0
134 0 ▶ if ($attr_name eq 'xmlns') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_name eq 'xmlns') { } 0 0 0
elsif ($attr_name =~ /^xmlns:(.*)$/) { } 0 0 0
135 0 $self->{InScopeNamespaceStack}[-1]{'_Default'} = $value;
136 } elsif ($attr_name =~ /^xmlns:(.*)$/) {
137 0 my $prefix = $1;
138 0 $self->{InScopeNamespaceStack}[-1]{$prefix} = $value;
139 }
140 }
141 }
142  
143 sub _namespace {
144 0 ▶ my ($self, $name) = @_;
Subroutine _namespace: not called CC=3 SCAR=24.8
145  
146 0 my ($prefix, $localname) = split(/:/, $name);
147 0 ▶ if (!defined($localname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $localname) { } 0 0 0
148 0 ▶ if ($prefix eq 'xmlns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq 'xmlns') { } 0 0 0
149 0 return '', undef;
150 } else {
151 0 return '', $self->{InScopeNamespaceStack}[-1]{'_Default'};
152 }
153 } else {
154 0 return $prefix, $self->{InScopeNamespaceStack}[-1]{$prefix};
155 }
156 }
157  
158 1 1;
159  
160 __END__
« XPath/Node/Element.pm ↑ Summary XPath/Node.pm »