XML/XSH/Iterators.pm

filter: Statementstmt 10.5% 16 / 152 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 9.3% 3 / 32 Podpod 0.0% 0 / 24 totaltotal 5.8% 19 / 324 CC 49 SCAR 30.1
CC
49
Cov
6%
get_next_node
56.0
first_filtered
42.0
create_iterator
30.0
CRAP 20.4
« XML/XSH/Completion.pm ↑ Summary XML/XSH/GDOMECompat.pm »
1 # $Id: Iterators.pm,v 1.1 2003/03/12 14:01:31 pajas Exp $
2  
3 package XML::XSH::Iterators;
4  
5 #namespace ???
6 #attribute ??
7  
8 sub create_iterator {
9 0 ▶ my ($class,$node,$axis,$filter)=@_;
Subroutine create_iterator: not called CC=5 SCAR=34.0
Pod undocumented
10 0 my $iterator;
11  
12 0 ▶ die "Unsupported or unknown axis $axis\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("iterate_$axis") 0 0 0
13 unless $class->can("iterate_$axis");
14  
15 0 ▶ if ($class->can("init_$axis")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("init_$axis")) 0 0 0
16 0 $node=&{"init_$axis"}($node);
17 }
18 0 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
19  
20 0 $iterator= XML::XSH::FilteredIterator->new($node, $filter);
21 0 $iterator->iterator_function(\&{"iterate_$axis"});
22 0 ▶ $iterator->first_filtered() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$iterator->first_filtered or return undef 0 0 0
23 0 return $iterator;
24 }
25  
26 sub iterate_self {
27 0 ▶ return undef; # the most trivial iterator :-)
Subroutine iterate_self: not called CC=1 SCAR=6.9
Pod undocumented
28 }
29  
30 sub init_child {
31 0 ▶ my ($node)=@_;
Subroutine init_child: not called CC=1 SCAR=6.9
Pod undocumented
32 0 return $node->firstChild;
33 }
34  
35 sub iterate_child { # iteration must start at the first child!
36 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_child: not called CC=2 SCAR=17.9
Pod undocumented
37 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
38 0 return $iter->{CURRENT}->previousSibling;
39 } else {
40 0 return $iter->{CURRENT}->nextSibling;
41 }
42 }
43  
44 sub init_following_sibling {
45 0 ▶ my ($node)=@_;
Subroutine init_following_sibling: not called CC=1 SCAR=6.9
Pod undocumented
46 0 return $node->nextSibling();
47 }
48  
49 sub iterate_following_sibling {
50 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_following_sibling: not called CC=3 SCAR=24.8
Pod undocumented
51 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
52 0 ▶ if ($iter->{FIRST}->isSameNode( $iter->{CURRENT}->previousSibling )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->{"FIRST"}->isSameNode($iter->{"CURRENT"}->previousSibling)) 0 0 0
53 0 return undef;
54 }
55 0 return $iter->{CURRENT}->previousSibling;
56 } else {
57 0 return $iter->{CURRENT}->nextSibling;
58 }
59 }
60  
61  
62 sub init_preceding_sibling {
63 0 ▶ my ($node)=@_;
Subroutine init_preceding_sibling: not called CC=1 SCAR=6.9
Pod undocumented
64 0 return $node->nextSibling();
65 }
66  
67 sub iterate_preceding_sibling {
68 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_preceding_sibling: not called CC=3 SCAR=24.8
Pod undocumented
69 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
70 0 ▶ if ($iter->{FIRST}->isSameNode( $iter->{CURRENT}->nextSibling )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->{"FIRST"}->isSameNode($iter->{"CURRENT"}->nextSibling)) 0 0 0
71 0 return undef;
72 }
73 0 return $iter->{CURRENT}->nextSibling;
74 } else {
75 0 return $iter->{CURRENT}->previousSibling;
76 }
77 }
78  
79 sub iterate_ancestor_or_self {
80 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_ancestor_or_self: not called CC=5 SCAR=34.0
Pod undocumented
81 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
82 0 my $node = undef;
83 0 ▶ return undef if $iter->{CURRENT}->isSameNode( $iter->{FIRST} );
Branch: 1 branch
BranchTrueFalseTotal
if $iter->{"CURRENT"}->isSameNode($iter->{"FIRST"}) 0 0 0
84 0 $node=$iter->{FIRST};
85 0 ▶ while ($node->parentNode and
Branch: 1 branch
BranchTrueFalseTotal
if ($node->parentNode and not $node->parentNode->isSameNode($iter->{"CURRENT"})) 0 0 0
Condition0%0/3
$node->parentNode and not $node->parentNode->isSameNode($iter->{"CURRENT"})
!ll&&!rl&&r
000
MC/DC0%0/2
$node->parentNode and not $node->parentNode->isSameNode($iter->{"CURRENT"})
$node->parentNode not $node->parentNode->isSameNode($iter->{"CURRENT"})
Truth table0%0/3
A and not(B)
A= $node->parentNodeB= not $node->parentNode->isSameNode($iter->{"CURRENT"})
ABresult
0X0
100
111
86 not($node->parentNode->isSameNode($iter->{CURRENT}))) {
87 0 $node=$node->parentNode;
88 }
89 0 return $node;
90 } else {
91 0 return $iter->{CURRENT}->parentNode;
92 }
93 }
94  
95 sub init_ancestor {
96 0 ▶ my ($node)=@_;
Subroutine init_ancestor: not called CC=1 SCAR=6.9
Pod undocumented
97 0 return $node->parentNode();
98 }
99 4 *iterate_ancestor = *iterate_ancestor_or_self;
100  
101 4 *init_parent = *init_ancestor;
102 4 *iterate_parent = *iterate_ancestor_or_self;
103  
104 sub iterate_descendant_or_self {
105 0 ▶ my $iter = shift;
Subroutine iterate_descendant_or_self: not called CC=3 SCAR=24.8
Pod undocumented
106 0 my $dir = shift;
107 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
108 0 ▶ return undef if $iter->{CURRENT}->isSameNode( $iter->{FIRST} );
Branch: 1 branch
BranchTrueFalseTotal
if $iter->{"CURRENT"}->isSameNode($iter->{"FIRST"}) 0 0 0
109 0 return get_prev_node($iter->{CURRENT});
110 } else {
111 0 return get_next_node($iter->{CURRENT},$iter->{FIRST});
112 }
113 0 return $node;
114 }
115  
116 sub init_descendant {
117 0 ▶ my ($node)=@_;
Subroutine init_descendant: not called CC=1 SCAR=6.9
Pod undocumented
118 0 return $node->firstChild;
119 }
120 sub iterate_descendant { # iteration must start at the first child!
121 0 ▶ my $iter = shift;
Subroutine iterate_descendant: not called CC=3 SCAR=24.8
Pod undocumented
122 0 my $dir = shift;
123 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
124 0 ▶ return undef if $iter->{CURRENT}->isSameNode( $iter->{FIRST} );
Branch: 1 branch
BranchTrueFalseTotal
if $iter->{"CURRENT"}->isSameNode($iter->{"FIRST"}) 0 0 0
125 0 return get_prev_node($iter->{CURRENT});
126 } else {
127 0 return get_next_node($iter->{CURRENT},$iter->{FIRST}->parentNode);
128 }
129 }
130  
131 sub iterate_following_or_self {
132 0 ▶ my $iter = shift;
Subroutine iterate_following_or_self: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my $dir = shift;
134 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
135 0 ▶ return undef if $iter->{CURRENT}->isSameNode( $iter->{FIRST} );
Branch: 1 branch
BranchTrueFalseTotal
if $iter->{"CURRENT"}->isSameNode($iter->{"FIRST"}) 0 0 0
136 0 return get_prev_node($iter->{CURRENT});
137 } else {
138 0 return get_next_node($iter->{CURRENT});
139 }
140 }
141  
142 sub iterate_preceding_or_self {
143 0 ▶ my $iter = shift;
Subroutine iterate_preceding_or_self: not called CC=3 SCAR=24.8
Pod undocumented
144 0 my $dir = shift;
145 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
146 0 ▶ return undef if $iter->{CURRENT}->isSameNode( $iter->{FIRST} );
Branch: 1 branch
BranchTrueFalseTotal
if $iter->{"CURRENT"}->isSameNode($iter->{"FIRST"}) 0 0 0
147 0 return get_next_node($iter->{CURRENT});
148 } else {
149 0 return get_prev_node($iter->{CURRENT});
150 }
151 }
152  
153 sub init_following {
154 0 ▶ return get_next_node($_[0]);
Subroutine init_following: not called CC=1 SCAR=6.9
Pod undocumented
155 }
156 4 *iterate_following = *iterate_following_or_self;
157  
158 sub init_preceding {
159 0 ▶ return get_prev_node($_[0]);
Subroutine init_preceding: not called CC=1 SCAR=6.9
Pod undocumented
160 }
161 4 *iterate_preceding = *iterate_preceding_or_self;
162  
163 sub init_first_descendant {
164 0 ▶ return $_[0]->firstChild;
Subroutine init_first_descendant: not called CC=1 SCAR=6.9
Pod undocumented
165 }
166  
167 sub iterate_first_descendant_or_self {
168 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_first_descendant_or_self: not called CC=3 SCAR=24.8
Pod undocumented
169  
170 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
171 0 ▶ if ($iter->{FIRST}->isSameNode( $iter->{CURRENT} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->{"FIRST"}->isSameNode($iter->{"CURRENT"})) 0 0 0
172 0 return undef;
173 }
174 0 return $iter->{CURRENT}->parent;
175 } else {
176 0 return $iter->{CURRENT}->firstChild;
177 }
178 }
179 sub iterate_first_descendant {
180 0 ▶ iterate_first_descendant_or_self(@_);
Subroutine iterate_first_descendant: not called CC=1 SCAR=6.9
Pod undocumented
181 }
182 #*iterate_first_descendant = *iterate_first_descendant_or_self;
183  
184 sub init_last_descendant {
185 0 ▶ return $_[0]->lastChild;
Subroutine init_last_descendant: not called CC=1 SCAR=6.9
Pod undocumented
186 }
187 4 *iterate_last_descendant = *iterate_last_descendant_or_self;
188  
189 sub iterate_last_descendant_or_self {
190 0 ▶ my ($iter, $dir) = @_;
Subroutine iterate_last_descendant_or_self: not called CC=3 SCAR=24.8
Pod undocumented
191 0 ▶ if ( $dir < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir < 0) { } 0 0 0
192 0 ▶ if ($iter->{FIRST}->isSameNode( $iter->{CURRENT} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->{"FIRST"}->isSameNode($iter->{"CURRENT"})) 0 0 0
193 0 return undef;
194 }
195 0 return $iter->{CURRENT}->parent;
196 } else {
197 0 return $iter->{CURRENT}->lastChild;
198 }
199 }
200  
201  
202 #STATIC
203 sub get_prev_node {
204 0 ▶ my ($node) = @_;
Subroutine get_prev_node: not called CC=3 SCAR=24.8
Pod undocumented
205 0 ▶ if ($node->previousSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->previousSibling) 0 0 0
206 0 $node = $node->previousSibling;
207 0 ▶ if ($node->hasChildNodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasChildNodes) { } 0 0 0
208 0 return $node->lastChild;
209 } else {
210 0 return $node;
211 }
212 }
213 0 return $node->parentNode;
214 }
215  
216 #STATIC
217 sub get_next_node {
218 0 ▶ my ($node,$stop) = @_;
Subroutine get_next_node: not called CC=7 SCAR=40.3
Pod undocumented
219  
220 0 ▶ if ( $node->hasChildNodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasChildNodes) { } 0 0 0
221 0 return $node->firstChild;
222 } else {
223 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
224 0 ▶ return undef if ($stop and $node->isSameNode($stop) or
Branch: 1 branch
BranchTrueFalseTotal
if $stop and $node->isSameNode($stop) or not $node->parentNode 0 0 0
Condition0%0/6
$stop and $node->isSameNode($stop) or not $node->parentNode
l!l&&r!l&&!r
000
$stop and $node->isSameNode($stop)
!ll&&!rl&&r
000
MC/DC0%0/3
$stop and $node->isSameNode($stop) or not $node->parentNode
$stop $node->isSameNode($stop) not $node->parentNode
Truth table0%0/5
A and B or not(C)
A= $stopB= $node->isSameNode($stop)C= not $node->parentNode
ABCresult
11X1
0X11
1011
0X00
1000
225 not $node->parentNode);
226 0 ▶ return $node->nextSibling if $node->nextSibling;
Branch: 1 branch
BranchTrueFalseTotal
if $node->nextSibling 0 0 0
227 0 $node = $node->parentNode;
228 }
229 0 return undef;
230 }
231 }
232  
233 package XML::XSH::FilteredIterator;
234 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 4 ▶use XML::LibXML::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 4 ▶use base qw(XML::LibXML::Iterator);
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 sub new {
239 0 ▶ my ($class,$node,$filter)=@_;
Subroutine new: not called CC=2 SCAR=17.9
240 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
241 0 my $self = $class->SUPER::new($node);
242 0 $self->{FILTER}=$filter;
243 0 return $self;
244 }
245  
246 sub filter {
247 0 ▶ my $self=shift;
Subroutine filter: not called CC=2 SCAR=17.9
248 0 ▶ if (@_) { $self->{FILTER}=$_[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
249 0 return $self->{FILTER};
250 }
251  
252 sub first_filtered {
253 0 ▶ my $self=shift;
Subroutine first_filtered: not called CC=6 SCAR=37.4
254 0 my ($node)=@_;
255 0 my $filter=$self->filter;
256 0 ▶ $self->first(@_) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->first(@_) or return undef 0 0 0
257 0 ▶ if (ref($filter) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter eq "CODE") 0 0 0
258 0 ▶ while ($self->current() and not &$filter($self->current())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current and not &$filter($self->current)) 0 0 0
Condition0%0/3
$self->current and not &$filter($self->current)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->current and not &$filter($self->current)
$self->current not &$filter($self->current)
Truth table0%0/3
A and not(B)
A= $self->currentB= not &$filter($self->current)
ABresult
0X0
100
111
259 0 ▶ $self->next() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->next or return undef 0 0 0
260 }
261 }
262 0 return $self->current();
263 }
264  
265 sub next {
266 0 ▶ my $self=shift;
Subroutine next: not called CC=5 SCAR=34.0
267 0 my ($node)=@_;
268 0 my $filter=$self->filter;
269 0 ▶ $self->SUPER::next() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::next or return undef 0 0 0
270 0 ▶ while ($self->current() and not &$filter($self->current())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current and not &$filter($self->current)) 0 0 0
Condition0%0/3
$self->current and not &$filter($self->current)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->current and not &$filter($self->current)
$self->current not &$filter($self->current)
Truth table0%0/3
A and not(B)
A= $self->currentB= not &$filter($self->current)
ABresult
0X0
100
111
271 0 ▶ $self->SUPER::next() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::next or return undef 0 0 0
272 }
273 0 return $self->current();
274 }
275  
276 sub prev {
277 0 ▶ my $self=shift;
Subroutine prev: not called CC=5 SCAR=34.0
278 0 my ($node)=@_;
279 0 my $filter=$self->filter;
280 0 ▶ $self->SUPER::prev() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::prev or return undef 0 0 0
281 0 ▶ while ($self->current() and not &$filter($self->current())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current and not &$filter($self->current)) 0 0 0
Condition0%0/3
$self->current and not &$filter($self->current)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->current and not &$filter($self->current)
$self->current not &$filter($self->current)
Truth table0%0/3
A and not(B)
A= $self->currentB= not &$filter($self->current)
ABresult
0X0
100
111
282 0 ▶ $self->SUPER::prev() || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::prev or return undef 0 0 0
283 }
284 0 return $self->current();
285 }
286  
287 4 1;
« XML/XSH/Completion.pm ↑ Summary XML/XSH/GDOMECompat.pm »