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