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