File Coverage

lib/XML/Compile/Iterator.pm
Criterion Covered Total %
statement 53 62 85.4
branch 21 36 58.3
condition 6 8 75.0
subroutine 20 24 83.3
pod 19 20 95.0
total 119 150 79.3


line stmt bran cond sub pod time code
1             # Copyrights 2006-2024 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.03.
5             # This code is part of distribution XML-Compile. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package XML::Compile::Iterator;{
10             our $VERSION = '1.64';
11             }
12              
13              
14 50     50   330 use warnings;
  50         91  
  50         3216  
15 50     50   277 use strict;
  50         88  
  50         1873  
16              
17 50     50   272 use XML::Compile::Util qw/pack_type type_of_node SCHEMA2001i/;
  50         102  
  50         3790  
18 50     50   391 use Log::Report 'xml-compile', syntax => 'SHORT';
  50         145  
  50         479  
19              
20              
21             sub new($@)
22 6299     6299 1 17675 { my ($class, $node, $path, $filter) = splice @_, 0, 4;
23 6299         33834 (bless {}, $class)
24             ->init( { node => $node, filter => $filter, path => $path, @_} );
25             }
26              
27             sub init($)
28 6299     6299 0 10792 { my ($self, $args) = @_;
29             $self->{node} = delete $args->{node}
30 6299 50       21950 or panic "no node specified";
31              
32             $self->{filter} = delete $args->{filter}
33 6299 50       43257 or panic "no filter specified";
34              
35             $self->{path} = delete $args->{path}
36 6299 50       17774 or panic "no path specified";
37              
38 6299         11396 $self->{current} = 0;
39 6299         28029 $self;
40             }
41              
42              
43             sub descend(;$$$)
44 5123     5123 1 12117 { my ($self, $node, $p, $filter) = @_;
45 5123   100     14668 $node ||= $self->currentChild;
46 5123 100       27047 defined $node or return undef;
47              
48 5122         10424 my $path = $self->path;
49 5122 100       13708 $path .= '/'.$p if defined $p;
50              
51             (ref $self)->new
52 5122   33     26593 ($node, $path, ($filter || $self->{filter}));
53             }
54              
55             #----------------
56              
57 14855     14855 1 35895 sub node() {shift->{node}}
58 4510     4510 1 13825 sub filter() {shift->{filter}}
59 11091     11091 1 26143 sub path() {shift->{path}}
60              
61             #----------------
62              
63             sub childs()
64 12733     12733 1 19670 { my $self = shift;
65 12733         17775 my $ln = $self->{childs};
66 12733 100       24430 unless(defined $ln)
67 4510         10542 { my $filter = $self->filter;
68             $ln = $self->{childs}
69 4510         9660 = [ grep {$filter->($_)} $self->node->childNodes ];
  10019         56183  
70             }
71 12733 100       64641 wantarray ? @$ln : $ln;
72             }
73              
74              
75 5863     5863 1 11990 sub currentChild() { $_[0]->childs->[$_[0]->{current}] }
76              
77              
78 1982     1982 1 4600 sub firstChild() {shift->childs->[0]}
79              
80              
81             sub lastChild()
82 0     0 1 0 { my $list = shift->childs;
83 0 0       0 @$list ? $list->[-1] : undef; # avoid error on empty list
84             }
85              
86              
87             sub nextChild()
88 1516     1516 1 19847 { my $self = shift;
89 1516         3420 my $list = $self->childs;
90 1516 50       6917 $self->{current} < @$list ? $list->[ ++$self->{current} ] : undef;
91             }
92              
93              
94             sub previousChild()
95 0     0 1 0 { my $self = shift;
96 0         0 my $list = $self->childs;
97 0 0       0 $self->{current} > 0 ? $list->[ --$self->{current} ] : undef;
98             }
99              
100              
101             sub nrChildren()
102 2723     2723 1 6248 { my $list = shift->childs;
103 2723         6751 scalar @$list;
104             }
105              
106             #---------
107              
108 859 50   859 1 2206 sub nodeType() { type_of_node(shift->node) || '' }
109              
110              
111             sub nodeLocal()
112 0 0   0 1 0 { my $node = shift->node or return '';
113 0         0 $node->localName;
114             }
115              
116              
117             sub nodeNil()
118 25 50   25 1 39 { my $node = shift->node or return 0;
119 25   100     138 my $nil = $node->getAttributeNS(SCHEMA2001i, 'nil') || '';
120 25 100       438 $nil eq 'true' || $nil eq '1';
121             }
122              
123              
124             sub textContent()
125 574 50   574 1 1387 { my $node = shift->node or return undef;
126 574         5521 $node->textContent;
127             }
128              
129              
130             sub currentType()
131 528 100   528 1 1293 { my $current = shift->currentChild or return '';
132 480         3128 type_of_node $current;
133             }
134              
135              
136             sub currentLocal()
137 1260 100   1260 1 2789 { my $current = shift->currentChild or return '';
138 674         8525 $current->localName;
139             }
140              
141              
142             sub currentContent()
143 0 0   0 1   { my $current = shift->currentChild or return undef;
144 0           $current->textContent;
145             }
146              
147             1;