TreePuller/Element.pm

filter: Statementstmt 77.5% 124 / 160 Branchbran 84.6% 22 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 50.0% 28 / 56 Podpod 0.0% 0 / 20 totaltotal 65.0% 177 / 272 CC 19 SCAR 8.9
CC
19
Cov
77%
xpath_get_parent_node
6.0
text
5.0
_recursive_get_child_elements
5.0
CRAP 2.4
« TreePuller.pm ↑ Summary TreePuller/CookBook/Intro.pm »
1 #!/usr/bin/env perl
2  
3 #things got ugly in here when XPath
4 #support was hammered in - should be cleaned up
5  
6 package XML::TreePuller::Element;
7  
8 6 our $VERSION = '0.1.0';
9  
10 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Tree::XPathEngine::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Tree::XPathEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use XML::TreePuller::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
23 11 my ($class, $tree) = @_;
24
25 11 ▶ if ($tree->[XML_TREEPULLER_ELEMENT_TYPE] != XML_READER_TYPE_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->[0] != 1) 0 11 11
26 0 croak("must specify an element node");
27 }
28
29 11 bless($tree, $class);
30  
31 11 $tree->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE] = Tree::XPathEngine->new;
32 11 $tree->_init($tree, 0);
33
34 11 return $tree;
35 }
36  
37 34 ▶sub get_elements {
Subroutine get_elements: called CC=3 SCAR=11.0
Pod undocumented
38 34 my ($self, $path) = @_;
39 34 my @results;
40  
41 34 ▶ if (! defined($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path) { } 30 4 34
42 30 @results = _extract_elements(@{$self->[XML_TREEPULLER_ELEMENT_CHILDREN]});
43 } else {
44 4 @results = $self->_recursive_get_child_elements(split('/', $path));
45 }
46  
47 34 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 30 4 34
48 30 return @results;
49 }
50
51 4 return shift(@results);
52 }
53  
54 1 ▶sub xpath {
Subroutine xpath: called CC=2 SCAR=7.6
Pod undocumented
55 1 my @return = $_[0]->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE]->findnodes($_[1], XML::TreePuller::Element::Document->new($_[0]));
56
57 1 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 0 1
58 1 return @return;
59 }
60
61 0 return shift(@return);
62 }
63  
64 3 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
65 3 my ($tree) = @_;
66
67 3 return $tree->[XML_TREEPULLER_ELEMENT_NAME];
68 }
69  
70 12 ▶sub text {
Subroutine text: called CC=5 SCAR=16.1
Pod undocumented
71 12 my ($self) = @_;
72 12 my @content;
73
74 12 foreach (@{$self->[XML_TREEPULLER_ELEMENT_CHILDREN]}) {
75 16 ▶ if ($_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_TEXT || $_->[0] == XML_READER_TYPE_CDATA) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->[0] == 3 or $_->[0] == 4) { } 10 6 16
elsif ($_->[0] == 1) { } 3 3 6
Condition66%2/3
$_->[0] == 3 or $_->[0] == 4
l!l&&r!l&&!r
1006
MC/DC50%1/2
$_->[0] == 3 or $_->[0] == 4
$_->[0] == 3 $_->[0] == 4
Truth table66%2/3
A or B
A= $_->[0] == 3B= $_->[0] == 4
ABresult
1X1
011
000
76 10 push(@content, $_->[XML_TREEPULLER_ELEMENT_NAME]);
77 } elsif ($_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_ELEMENT) {
78 3 push(@content, $_->text);
79 }
80 }
81
82 12 return join('', @content);
83 }
84  
85 10 ▶sub attribute {
Subroutine attribute: called CC=3 SCAR=11.0
Pod undocumented
86 10 my ($tree, $name) = @_;
87 10 my $attr = $tree->[XML_TREEPULLER_ELEMENT_ATTRIBUTES];
88
89 10 ▶ $attr = {} unless defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 10 10
90  
91 10 ▶ if (! defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 2 8 10
92 2 return $attr;
93 }
94
95 8 return $attr->{$name};
96 }
97  
98 #private methods
99 34 ▶sub _extract_elements {
Subroutine _extract_elements: called CC=1 SCAR=0.0
100 34 return grep { $_->[XML_TREEPULLER_ELEMENT_TYPE] == XML_READER_TYPE_ELEMENT } @_;
101 }
102  
103 #an easier to understand algorithm would be nice
104 11 ▶sub _recursive_get_child_elements {
Subroutine _recursive_get_child_elements: called CC=5 SCAR=16.1
105 11 my ($tree, @path) = @_;
106 11 my $child_nodes = $tree->[XML_TREEPULLER_ELEMENT_CHILDREN];
107 11 my @results;
108 my $target;
109
110 11 ▶ if (! scalar(@path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @path) 7 4 11
111 7 return $tree;
112 }
113
114 4 $target = shift(@path);
115
116 4 ▶ return () unless defined $child_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_nodes 0 4 4
117
118 4 foreach (_extract_elements(@$child_nodes)) {
119 12 ▶ next unless $_->[XML_TREEPULLER_ELEMENT_NAME] eq $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[1] eq $target 5 7 12
120
121 7 push(@results, _recursive_get_child_elements($_, @path));
122 }
123
124 4 return @results;
125 }
126  
127 29 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
128 29 my ($self, $root, $depth) = @_;
129 29 my @elements = $self->get_elements;
130
131 29 $self->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE] = $root->[XML_TREEPULLER_ELEMENT_XPATH_ENGINE];
132
133 29 $self->[XML_TREEPULLER_ELEMENT_PARENT] = undef;
134 29 $self->[XML_TREEPULLER_ELEMENT_ROOT] = $root;
135 29 $self->[XML_TREEPULLER_ELEMENT_DEPTH] = $depth;
136
137 29 weaken($self->[XML_TREEPULLER_ELEMENT_ROOT]);
138
139 29 $depth++;
140  
141 29 ▶ for(my $i = 0; $i < @elements; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @elements) 18 29 47
142 18 my $before = $elements[$i - 1];
143 18 my $after = $elements[$i + 1];
144
145 18 ▶ if ($i - 1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i - 1 < 0) 8 10 18
146 8 $before = undef;
147 }
148
149 18 $elements[$i]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING] = $before;
150 18 $elements[$i]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING] = $after;
151
152 18 weaken($elements[$i]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING]);
153 18 weaken($elements[$i]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING]);
154 }
155
156 29 foreach (@elements) {
157 #set the parent and root of each element
158 18 $_->[XML_TREEPULLER_ELEMENT_PARENT] = $self;
159 18 $_->[XML_TREEPULLER_ELEMENT_ROOT] = $root;
160
161 18 $_[XML_TREEPULLER_ELEMENT_DEPTH] = $depth;
162
163 18 weaken($_->[XML_TREEPULLER_ELEMENT_PARENT]);
164 18 weaken($_->[XML_TREEPULLER_ELEMENT_ROOT]);
165
166 18 bless($_, 'XML::TreePuller::Element');
167
168 18 $_->_init($root, $depth);
169 }
170 }
171  
172 #methods for Tree::XPathEngine
173 1 ▶sub xpath_get_name {
Subroutine xpath_get_name: called CC=1 SCAR=0.0
Pod undocumented
174 1 return name(@_);
175 }
176  
177 0 ▶sub xpath_string_value {
Subroutine xpath_string_value: not called CC=1 SCAR=6.9
Pod undocumented
178 0 return (text(@_));
179 }
180  
181 0 ▶sub xpath_get_parent_node {
Subroutine xpath_get_parent_node: not called CC=2 SCAR=17.9
Pod undocumented
182 0 ▶ return $_[0]->[XML_TREEPULLER_ELEMENT_ROOT] || XML::TreePuller::Element::Document->new($_[0]);
Condition0%0/3
$_[0][6] || 'XML::TreePuller::Element::Document'->new($_[0])
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0][6] || 'XML::TreePuller::Element::Document'->new($_[0])
$_[0][6] 'XML::TreePuller::Element::Document'->new($_[0])
Truth table0%0/3
A || B
A= $_[0][6]B= 'XML::TreePuller::Element::Document'->new($_[0])
ABresult
1X1
011
000
183 }
184  
185 0 ▶sub xpath_get_child_nodes {
Subroutine xpath_get_child_nodes: not called CC=1 SCAR=6.9
Pod undocumented
186 0 return $_[0]->get_elements;
187 }
188  
189 1 ▶sub xpath_is_element_node {
Subroutine xpath_is_element_node: called CC=1 SCAR=0.0
Pod undocumented
190 1 return 1;
191 }
192  
193 0 ▶sub xpath_is_document_node {
Subroutine xpath_is_document_node: not called CC=1 SCAR=6.9
Pod undocumented
194 0 return 0;
195 }
196  
197 0 ▶sub xpath_is_attribute_node {
Subroutine xpath_is_attribute_node: not called CC=1 SCAR=6.9
Pod undocumented
198 0 return 0;
199 }
200  
201 0 ▶sub xpath_to_string {
Subroutine xpath_to_string: not called CC=1 SCAR=6.9
Pod undocumented
202 0 return $_[0];
203 }
204  
205 0 ▶sub xpath_to_number {
Subroutine xpath_to_number: not called CC=1 SCAR=6.9
Pod undocumented
206 0 return Tree::XPathEngine::Number->new($_[0]->xpath_to_string);
207 }
208  
209 0 ▶sub xpath_cmp {
Subroutine xpath_cmp: not called CC=1 SCAR=6.9
Pod undocumented
210 0 return $_[0]->[XML_TREEPULLER_ELEMENT_DEPTH] cmp $_[1]->[XML_TREEPULLER_ELEMENT_DEPTH];
211 }
212  
213 0 ▶sub xpath_get_attributes {
Subroutine xpath_get_attributes: not called CC=1 SCAR=6.9
Pod undocumented
214
215 0 my $elt= shift;
216 0 my $atts= $elt->attribute;
217 0 my $rank=-1;
218 0 my @atts= map { bless( { name => $_, value => $atts->{$_}, elt => $elt, rank => $rank -- },
219 'XML::TreePuller::Element::Attribute')
220 }
221 sort keys %$atts;
222 0 return @atts;
223 }
224  
225 0 ▶sub xpath_get_next_sibling {
Subroutine xpath_get_next_sibling: not called CC=1 SCAR=6.9
Pod undocumented
226 0 return $_[0]->[XML_TREEPULLER_ELEMENT_NEXT_SIBLING];
227 }
228  
229 0 ▶sub xpath_get_prev_sibling {
Subroutine xpath_get_prev_sibling: not called CC=1 SCAR=6.9
Pod undocumented
230 0 return $_[0]->[XML_TREEPULLER_ELEMENT_PREV_SIBLING];
231 }
232  
233 0 ▶sub xpath_get_root_node {
Subroutine xpath_get_root_node: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my ($node) = @_;
235
236 0 return $node->[XML_TREEPULLER_ELEMENT_ROOT]->xpath_get_parent_node;
237 }
238  
239 package XML::TreePuller::Element::Document;
240  
241 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 6 ▶use XML::TreePuller::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
247 1 my ($class, $root) = @_;
248 1 my $self = [ $root ];
249
250 1 $self->[XML_TREEPULLER_ELEMENT_DEPTH] = -1;
251
252 1 return bless($self, $class);
253 }
254  
255 1 ▶sub xpath_get_child_nodes { return( $_[0]->[0] ); }
Subroutine xpath_get_child_nodes: called CC=1 SCAR=0.0
256 0 ▶sub xpath_get_attributes { return (); }
Subroutine xpath_get_attributes: not called CC=1 SCAR=6.9
257 0 ▶sub xpath_is_document_node { return 1 }
Subroutine xpath_is_document_node: not called CC=1 SCAR=6.9
258 0 ▶sub xpath_is_element_node { return 0 }
Subroutine xpath_is_element_node: not called CC=1 SCAR=6.9
259 0 ▶sub xpath_is_attribute_node { return 0 }
Subroutine xpath_is_attribute_node: not called CC=1 SCAR=6.9
260 0 ▶sub xpath_get_parent_node { return; }
Subroutine xpath_get_parent_node: not called CC=1 SCAR=6.9
261 1 ▶sub xpath_get_root_node { return $_[0] }
Subroutine xpath_get_root_node: called CC=1 SCAR=0.0
262 0 ▶sub xpath_get_name { return; }
Subroutine xpath_get_name: not called CC=1 SCAR=6.9
263 0 ▶sub xpath_get_next_sibling { return; }
Subroutine xpath_get_next_sibling: not called CC=1 SCAR=6.9
264 0 ▶sub xpath_get_previous_sibling { return; }
Subroutine xpath_get_previous_sibling: not called CC=1 SCAR=6.9
265  
266 package XML::TreePuller::Element::Attribute;
267  
268 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 0 ▶sub xpath_get_value { return $_[0]->{value}; }
Subroutine xpath_get_value: not called CC=1 SCAR=6.9
272 0 ▶sub xpath_get_name { return $_[0]->{name} ; }
Subroutine xpath_get_name: not called CC=1 SCAR=6.9
273 0 ▶sub xpath_string_value { return $_[0]->{value}; }
Subroutine xpath_string_value: not called CC=1 SCAR=6.9
274 0 ▶sub xpath_to_number { return Tree::XPathEngine::Number->new( $_[0]->{value}); }
Subroutine xpath_to_number: not called CC=1 SCAR=6.9
275 0 ▶sub xpath_is_document_node { 0 }
Subroutine xpath_is_document_node: not called CC=1 SCAR=6.9
276 0 ▶sub xpath_is_element_node { 0 }
Subroutine xpath_is_element_node: not called CC=1 SCAR=6.9
277 0 ▶sub xpath_is_attribute_node { 1 }
Subroutine xpath_is_attribute_node: not called CC=1 SCAR=6.9
278 0 ▶sub to_string { return qq{$_[0]->{name}="$_[0]->{value}"}; }
Subroutine to_string: not called CC=1 SCAR=6.9
279  
280 6 1;
« TreePuller.pm ↑ Summary TreePuller/CookBook/Intro.pm »