blib/lib/XML/DOM/Lite/NodeIterator.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 149 SCAR 64.0
CC
24
Cov
0%
nextNode
156.0
previousNode
132.0
new
6.0
CRAP 600.0
1 package XML::DOM::Lite::NodeIterator;
2  
3 use XML::DOM::Lite::Constants qw(:all);
4  
5 use constant BEFORE => -1;
6 use constant AFTER => 1;
7  
8 sub new {
9 my ($class, $root, $whatToShow, $nodeFilter) = @_;
10 my $self = bless {
11 root => $root,
12 whatToShow => $whatToShow
13 }, $class;
14 unless (defined $nodeFilter) {
15 $self->filter({ acceptNode => sub { return FILTER_ACCEPT } });
16 } else {
17 $self->filter($nodeFilter);
18 }
19 $self->{currentNode} = $root;
20 $self->{POSITION} = BEFORE;
21  
22 return $self;
23 }
24  
25 sub filter { $_[0]->{filter} = $_[1] if $_[1]; $_[0]->{filter} }
26  
27 sub nextNode {
28 my $self = shift;
29 for (;;) {
30 if ($self->{POSITION} == BEFORE) {
31 # do nothing so we test the currentNode
32 } elsif ($self->{currentNode}->childNodes->length) {
33 $self->{currentNode} = $self->{currentNode}->childNodes->[0];
34 } elsif ($self->{currentNode}->nextSibling) {
35 $self->{currentNode} = $self->{currentNode}->nextSibling;
36 } elsif ($self->{currentNode}->parentNode) {
37 my $p = $self->{currentNode}->parentNode;
38 while ($p and $p->nextSibling == undef and $p != $self->{root}) {
39 $p = $p->parentNode;
40 }
41 last unless ($p and $p->nextSibling);
42 $self->{currentNode} = $p->nextSibling;
43 } else {
44 last;
45 }
46 $self->{POSITION} = AFTER;
47 my $rv;
48 if ($self->filter->{whatToShow} & (1 << ($self->{currentNode}->nodeType - 1))) {
49 $rv = $self->filter->{acceptNode}->($self->{currentNode});
50 } else {
51 $rv = FILTER_REJECT;
52 }
53  
54 if ($rv == FILTER_ACCEPT) {
55 return $self->{currentNode};
56 }
57 elsif ($rv == FILTER_SKIP) {
58 if ($self->{currentNode}->nextSibling) {
59 $self->{currentNode} = $self->{currentNode}->nextSibling;
60 } else {
61 my $p = $self->{currentNode}->parentNode;
62 while ($p and $p->nextSibling == undef) {
63 $p = $p->parentNode;
64 }
65 last unless ($p and $p->nextSibling);
66 $self->{currentNode} = $p->nextSibling;
67 }
68 next;
69 }
70 elsif ($rv != FILTER_REJECT) {
71 die('filter returned unknown value: `'.$rv."'");
72 }
73 }
74 return undef;
75 }
76  
77 sub previousNode {
78 my $self = shift;
79 for (;;) {
80 if ($self->{POSITION} == AFTER) {
81 # do nothing
82 } elsif ($self->{currentNode}->previousSibling) {
83 my $p = $self->{currentNode}->previousSibling;
84 if ($p->childNodes->length) {
85 $self->{currentNode} = $p->childNodes->[$p->childNodes->length-1];
86 while ($self->{currentNode}->childNodes->length) {
87 $self->{currentNode} = $self->{currentNode}->childNodes->[$self->{currentNode}->childNodes->length - 1];
88 }
89 } else {
90 $self->{currentNode} = $p;
91 }
92 } elsif ($self->{currentNode}->parentNode and $self->{currentNode}->parentNode != $self->{root}) {
93 $self->{currentNode} = $self->{currentNode}->parentNode;
94 } else {
95 last;
96 }
97 $self->{POSITION} = BEFORE;
98 my $rv;
99 if ($self->filter->{whatToShow} & (1 << ($self->{currentNode}->nodeType - 1))) {
100 $rv = $self->filter->{acceptNode}->($self->{currentNode});
101 } else {
102 $rv = FILTER_REJECT;
103 }
104  
105 if ($rv == FILTER_ACCEPT) {
106 return $self->{currentNode};
107 }
108 elsif ($rv == FILTER_SKIP) {
109 if ($self->{currentNode}->previousSibling) {
110 $self->{currentNode} = $self->{currentNode}->previousSibling;
111 } elsif ($self->{currentNode}->parentNode) {
112 $self->{currentNode} = $self->{currentNode}->parentNode;
113 } else {
114 last;
115 }
116 next;
117 }
118 elsif ($rv != FILTER_REJECT) {
119 die('filter returned unknown value: `'.$rv."'");
120 }
121 }
122 return undef;
123 }
124  
125 1;