File Coverage

lib/XML/Compile/Iterator.pm
Criterion Covered Total %
statement 56 65 86.1
branch 21 36 58.3
condition 6 8 75.0
subroutine 21 25 84.0
pod 19 20 95.0
total 123 154 79.8


line stmt bran cond sub pod time code
1             # Copyrights 2006-2019 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.02.
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 50     50   5580 use vars '$VERSION';
  50         110  
  50         2654  
11             $VERSION = '1.62';
12              
13              
14 50     50   298 use warnings;
  50         71  
  50         1457  
15 50     50   241 use strict;
  50         87  
  50         2181  
16              
17 50     50   1042 use XML::Compile::Util qw/pack_type type_of_node SCHEMA2001i/;
  50         85  
  50         2826  
18 50     50   323 use Log::Report 'xml-compile', syntax => 'SHORT';
  50         91  
  50         354  
19              
20              
21             sub new($@)
22 6182     6182 1 14314 { my ($class, $node, $path, $filter) = splice @_, 0, 4;
23 6182         23878 (bless {}, $class)
24             ->init( { node => $node, filter => $filter, path => $path, @_} );
25             }
26              
27             sub init($)
28 6182     6182 0 9093 { my ($self, $args) = @_;
29             $self->{node} = delete $args->{node}
30 6182 50       17992 or panic "no node specified";
31              
32             $self->{filter} = delete $args->{filter}
33 6182 50       33943 or panic "no filter specified";
34              
35             $self->{path} = delete $args->{path}
36 6182 50       12453 or panic "no path specified";
37              
38 6182         8726 $self->{current} = 0;
39 6182         23160 $self;
40             }
41              
42              
43             sub descend(;$$$)
44 5018     5018 1 10026 { my ($self, $node, $p, $filter) = @_;
45 5018   100     11950 $node ||= $self->currentChild;
46 5018 100       23643 defined $node or return undef;
47              
48 5017         7855 my $path = $self->path;
49 5017 100       9975 $path .= '/'.$p if defined $p;
50              
51             (ref $self)->new
52 5017   33     19645 ($node, $path, ($filter || $self->{filter}));
53             }
54              
55             #----------------
56              
57 14585     14585 1 28202 sub node() {shift->{node}}
58 4430     4430 1 6317 sub filter() {shift->{filter}}
59 10872     10872 1 21155 sub path() {shift->{path}}
60              
61             #----------------
62              
63             sub childs()
64 11976     11976 1 13219 { my $self = shift;
65 11976         14101 my $ln = $self->{childs};
66 11976 100       18827 unless(defined $ln)
67 4430         7400 { my $filter = $self->filter;
68             $ln = $self->{childs}
69 4430         7395 = [ grep {$filter->($_)} $self->node->childNodes ];
  9829         40213  
70             }
71 11976 100       53214 wantarray ? @$ln : $ln;
72             }
73              
74              
75 5727     5727 1 8639 sub currentChild() { $_[0]->childs->[$_[0]->{current}] }
76              
77              
78 1454     1454 1 2474 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 1485     1485 1 15526 { my $self = shift;
89 1485         2620 my $list = $self->childs;
90 1485 50       5190 $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 2679     2679 1 5288 { my $list = shift->childs;
103 2679         6090 scalar @$list;
104             }
105              
106             #---------
107              
108 840 50   840 1 1644 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 49 { my $node = shift->node or return 0;
119 25   100     158 my $nil = $node->getAttributeNS(SCHEMA2001i, 'nil') || '';
120 25 100       547 $nil eq 'true' || $nil eq '1';
121             }
122              
123              
124             sub textContent()
125 563 50   563 1 1058 { my $node = shift->node or return undef;
126 563         5297 $node->textContent;
127             }
128              
129              
130             sub currentType()
131 513 100   513 1 1068 { my $current = shift->currentChild or return '';
132 465         2768 type_of_node $current;
133             }
134              
135              
136             sub currentLocal()
137 1228 100   1228 1 2107 { my $current = shift->currentChild or return '';
138 656         7018 $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;