| 1 | # $Id: Iterator.pm,v 1.1.1.1 2002/11/08 17:18:36 phish Exp $ | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | package XML::LibXML::NodeList::Iterator; | ||||||||||||||||||||||||||||
| 4 | 3 | $XML::LibXML::NodeList::Iterator::VERSION = '1.06'; | |||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use XML::NodeFilter qw(:results); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | $VERSION = "1.03"; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ## no critic | ||||||||||||||||||||||||||||
| 14 | use overload | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | '++' => sub { $_[0]->nextNode(); $_[0]; }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | '--' => sub { $_[0]->previousNode(); $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | '<>' => sub { return wantarray ? $_[0]->_get_all : $_[0]->nextNode(); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 3 | ; | |||||||||||||||||||||||||||
| 19 | ## use critic | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub new | ||||||||||||||||||||||||||||
| 22 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 24 | 9 | my $list = shift; | |||||||||||||||||||||||||||
| 25 | 9 | my $self = undef; | |||||||||||||||||||||||||||
| 26 | 9 | ▶ | if ( defined $list ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||
| 28 | 9 | $self = bless [ $list, -1, [], ], $class; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub set_filter | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_filter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | $self->[2] = [@_]; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub add_filter | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
add_filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 1 | push @{ $self->[2] }, @_; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # helper function. | ||||||||||||||||||||||||||||
| 47 | sub accept_node | ||||||||||||||||||||||||||||
| 48 | 29 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
accept_node: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 29 | foreach ( @{ $_[0][2] } ) | |||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 1 | my $r = $_->accept_node( $_[1] ); | |||||||||||||||||||||||||||
| 52 | 1 | ▶ | return $r if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # no filters or all decline ... | ||||||||||||||||||||||||||||
| 56 | 29 | return FILTER_ACCEPT; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub first | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
first: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 2 | $_[0][1] = 0; | |||||||||||||||||||||||||||
| 62 | 2 | my $s = scalar( @{ $_[0][0] } ); | |||||||||||||||||||||||||||
| 63 | 2 | ▶ | while ( $_[0][1] < $s ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | last if $_[0]->accept_node( $_[0][0][ $_[0][1] ] ) == FILTER_ACCEPT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $_[0][1]++; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | return undef if $_[0][1] == $s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 2 | return $_[0][0][ $_[0][1] ]; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub last | ||||||||||||||||||||||||||||
| 73 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
last: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 3 | my $i = scalar( @{ $_[0][0] } ) - 1; | |||||||||||||||||||||||||||
| 75 | 3 | ▶ | while ( $i >= 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if ( $_[0]->accept_node( $_[0][0][$i] ) == FILTER_ACCEPT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 3 | $_[0][1] = $i; | |||||||||||||||||||||||||||
| 80 | 3 | last; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | $i--; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | if ( $i < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | # this costs a lot, but is more safe | ||||||||||||||||||||||||||||
| 88 | 0 | return $_[0]->first; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 3 | return $_[0][0][$i]; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub current | ||||||||||||||||||||||||||||
| 94 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
current: called CC=3 SCAR=15.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( $_[0][1] >= 0 || $_[0][1] < scalar @{ $_[0][0] } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0][1] >= 0 or $_[0][1] < scalar @{$_[0][0];}
MC/DC0%0/2
$_[0][1] >= 0 or $_[0][1] < scalar @{$_[0][0];}
$_[0][1] >= 0
$_[0][1] < scalar @{$_[0][0];}
Truth table33%1/3
A or B
A= $_[0][1] >= 0B= $_[0][1] < scalar @{$_[0][0];}
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 14 | return $_[0][0][ $_[0][1] ]; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | return undef; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub index | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
index: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $_[0][1] >= 0 || $_[0][1] < scalar @{ $_[0][0] } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0][1] >= 0 or $_[0][1] < scalar @{$_[0][0];}
MC/DC0%0/2
$_[0][1] >= 0 or $_[0][1] < scalar @{$_[0][0];}
$_[0][1] >= 0
$_[0][1] < scalar @{$_[0][0];}
Truth table0%0/3
A or B
A= $_[0][1] >= 0B= $_[0][1] < scalar @{$_[0][0];}
| |||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 0 | return $_[0][1]; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | return undef; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 9 | ▶ | sub next { return $_[0]->nextNode(); } | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub previous { return $_[0]->previousNode(); } | ||||||||||||||||||||||||||
|
Subroutine
previous: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub nextNode | ||||||||||||||||||||||||||||
| 115 | 23 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
nextNode: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 23 | my $nlen = scalar @{ $_[0][0] }; | |||||||||||||||||||||||||||
| 117 | 23 | ▶ | if ( $nlen <= ( $_[0][1] + 1 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 5 | return undef; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 18 | my $i = $_[0][1]; | |||||||||||||||||||||||||||
| 122 | 18 | ▶ | $i = -1 if $i < 0; # assure that we end up with the first | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | # element in the first iteration | ||||||||||||||||||||||||||||
| 124 | 18 | while (1) | |||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 18 | $i++; | |||||||||||||||||||||||||||
| 127 | 18 | ▶ | return undef if $i >= $nlen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 18 | ▶ | if ( $_[0]->accept_node( $_[0][0]->[$i] ) == FILTER_ACCEPT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 18 | $_[0][1] = $i; | |||||||||||||||||||||||||||
| 131 | 18 | last; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 18 | return $_[0][0]->[ $_[0][1] ]; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub previousNode | ||||||||||||||||||||||||||||
| 138 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
previousNode: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 8 | ▶ | if ( $_[0][1] <= 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 2 | return undef; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 6 | my $i = $_[0][1]; | |||||||||||||||||||||||||||
| 144 | 6 | while (1) | |||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | 6 | $i--; | |||||||||||||||||||||||||||
| 147 | 6 | ▶ | return undef if $i < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 6 | ▶ | if ( $_[0]->accept_node( $_[0][0]->[$i] ) == FILTER_ACCEPT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 6 | $_[0][1] = $i; | |||||||||||||||||||||||||||
| 151 | 6 | last; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 6 | return $_[0][0][ $_[0][1] ]; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub iterate | ||||||||||||||||||||||||||||
| 158 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
iterate: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 160 | 2 | my $funcref = shift; | |||||||||||||||||||||||||||
| 161 | 2 | my $rv; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | return () unless defined $funcref && ref($funcref) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $funcref and ref $funcref eq "CODE"
MC/DC0%0/2
defined $funcref and ref $funcref eq "CODE"
defined $funcref
ref $funcref eq "CODE"
Truth table33%1/3
A and B
A= defined $funcrefB= ref $funcref eq "CODE"
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | $self->[1] = -1; # first element | |||||||||||||||||||||||||||
| 166 | 2 | ▶ | while ( my $node = $self->next ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 6 | $rv = $funcref->( $self, $node ); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 2 | return $rv; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # helper function for the <> operator | ||||||||||||||||||||||||||||
| 174 | # returns all nodes that have not yet been accessed | ||||||||||||||||||||||||||||
| 175 | sub _get_all | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_all: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 177 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | 0 | my @retval = (); | |||||||||||||||||||||||||||
| 179 | 0 | my $node; | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | while ( $node = $self->next() ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 0 | push @retval, $node; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | return @retval; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 3 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||