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