This file is indexed.

/usr/share/perl5/XML/LibXML/Iterator.pm is in libxml-libxml-iterator-perl 1.04-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# 
#
package XML::LibXML::Iterator;

use strict;

use XML::NodeFilter qw(:results);

use vars qw($VERSION);

$VERSION = '1.04';

use overload
  '++' => sub { $_[0]->nextNode(); $_[0]; },
  '--' => sub { $_[0]->previousNode(); $_[0]; },
  '<>' => sub { return wantarray ? $_[0]->_get_all() : $_[0]->nextNode(); },
;


sub new {
    my $class = shift;
    my $node  = shift;

    return undef unless defined $node;

    my $self = bless {}, $class;

    $self->{FIRST} = $node;
    # $self->first;

    $self->{CURRENT} = undef;
    $self->{INDEX} = -1;

    $self->{ITERATOR} = \&default_iterator;

    $self->{FILTERS} = [];

    return $self;
}


sub iterator_function {
    my $self = shift;
    my $func = shift;

    return if defined $func and ref( $func ) ne "CODE";

    $self->first;
    if ( defined $func ) {
        $self->{ITERATOR} = $func;
    }
    else {
        $self->{ITERATOR} = \&default_iterator;
    }
}

sub set_filter {
    my $self = shift;
    $self->{FILTERS} = [ @_ ];
}

sub add_filter {
    my $self = shift;
    push @{$self->{FILTERS}}, @_;
}

sub current  { return $_[0]->{CURRENT}; }
sub index    { return $_[0]->{INDEX}; }

sub next { return $_[0]->nextNode(); }
sub previous { return $_[0]->previousNode(); }

sub nextNode     {
    my $self = shift;
    my @filters = @{$self->{FILTERS}};
    my $node = undef;
    
    if ( $self->{INDEX} != -1 ) {
        my $fv = FILTER_SKIP;
        unless ( scalar @filters > 0 ) {
            $fv = FILTER_DECLINED;
        }

        while ( 1 ) {
            $node = $self->{ITERATOR}->( $self, 1 );
            last unless defined $node;
            foreach my $f ( @filters ) {
                $fv = $f->accept_node( $node );
                last if $fv;
            }
            last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
        }
    }
    else {
        $node = $self->first();
    }
        
    if ( defined $node ) {
        $self->{CURRENT} = $node;
        if ( $node->isSameNode( $self->{FIRST} ) ) {
            $self->{INDEX} = 0;
        }
        else {
            $self->{INDEX}++;
        }
    }

    return $node;
}

sub previousNode {
    my $self = shift;
    my @filters = @{$self->{FILTERS}};
    my $node = undef;
    my $fv = FILTER_SKIP;
    unless ( scalar @filters > 0 ) {
        $fv = FILTER_DECLINED;
    }
    while ( 1 ) {
        $node = $self->{ITERATOR}->( $self, -1 );
        last unless defined $node;
        foreach my $f ( @filters ) {
            $fv = $f->accept_node( $node );
            last if $fv;
        }
        last if $fv == FILTER_ACCEPT or $fv == FILTER_DECLINED;
    }

    if ( defined $node ) {
        $self->{CURRENT} = $node;
        $self->{INDEX}--;
    }

    return $node;
}

sub first {
    my $self = shift;
    if ( scalar @_ ) {
        $self->{FIRST} = shift;
    }

    $self->{CURRENT} = $self->{FIRST};

    # this logic is required if the node is not allowed to be shown
    my @filters = @{$self->{FILTERS}||[]};
    my $fv = FILTER_DECLINED;

    foreach my $f ( @filters ) {
        $fv = $f->accept_node( $self->{CURRENT} );
        last if $fv;
    }

    $fv ||= FILTER_ACCEPT;

    unless ( $fv == FILTER_ACCEPT ) {
        return undef;
    }

    $self->{INDEX}   = 0;
    return $self->current;
}

sub last  {
    my $self = shift;
    while ($self->next) {}
    return $self->current;
}

sub iterate {
    my $self = shift;
    my $function = shift;
    return unless defined $function and ref( $function ) eq 'CODE' ;
    my $rv;
    my $node = $self->first;
    while ( $node ) {
        $rv = $function->($self,$node);
        $node = $self->next;
    }
    return $rv;
}

sub default_iterator {
    my $self = shift;
    my $dir  = shift;
    my $node = undef;


    if ( $dir < 0 ) {
        return undef if $self->{CURRENT}->isSameNode( $self->{FIRST} )
          and $self->{INDEX} <= 0;

        $node = $self->{CURRENT}->previousSibling;
        return $self->{CURRENT}->parentNode unless defined $node;

        while ( $node->hasChildNodes ) {
            $node = $node->lastChild;
        }
    }
    else {
        if ( defined $self->{CURRENT} ) {
            return undef if $self->{CURRENT}->isSameNode( $self->{FIRST} )
                and $self->{INDEX} > 0;

            if ( $self->{CURRENT}->hasChildNodes ) {
                $node = $self->{CURRENT}->firstChild;
            }
            else {
                $node = $self->{CURRENT}->nextSibling;
                my $pnode = $self->{CURRENT}->parentNode;
                while ( not defined $node ) {
                    last unless defined $pnode;
                    $node = $pnode->nextSibling;
                    $pnode = $pnode->parentNode unless defined $node;
                }
            }
        }
        else {
            $self->{CURRENT} = $self->{FIRST};
            $node = $self->{CURRENT};
        }
    }

    return $node;
}

# helper function for the <> operator
# returns all nodes that have not yet been accessed 
sub _get_all {
    my $self = shift;
    my @retval = ();
    my $node;
    while ( $node = $self->next() ) {
        push @retval, $node; 
    }
    return @retval;
}

1;
__END__

=pod


=head1 NAME

XML::LibXML::Iterator - XML::LibXML's Tree Iteration Class

=head1 SYNOPSIS

  use XML::LibXML;
  use XML::LibXML::Iterator;

  my $doc = XML::LibXML->new->parse_string( $somedata );
  my $iter= XML::LibXML::Iterator->new( $doc );

  $iter->iterator_function( \&iterator_function );

  # more control on the flow
  while ( $iter->nextNode ) {
      # do something
  }

  # operate on the entire tree
  $iter->iterate( \&callback_function );

=head1 DESCRIPTION

XML::LibXML::Iterator is an iterator class for XML::LibXML parsed
documents. This class allows to iterate the document tree as it were a
linear data structure. It is possible to step back and forth between
the nodes of the tree and do certain operations on that
nodes. Different to XPath the nodes are not prefetched but will be
calculated for each step. Therefore an iterator is sensible towards
the current state of a document tree on each step, while XPath is only
per query executed.

=head2 What is an iterator?

XML::LibXML offers by default a W3C DOM interface on the parsed XML
documents. This tree has per definition four directions to be
traversed: Up, down, foreward and backward. Therefore a tree can be
considered two dimensional. Although a tree is still one more simple
datastructure it is way to complex for some operations. So the
XML::LibXML::Iterator class breaks the for operations down to only
two: backward and forward. For some people this easier to understand
than DOM or SAX as this follows more the way one actually reads an XML
document.

Therefore an iterator has three basic functions:

=over 4

=item * nextNode()

=item * current()

=item * previousNode()

=back

That's it. With an iterator one does not have to decide when to dive
into a subtree or find a parent. It is not even required to care about
the boundaries of a certain level. The iterator will get the next node
for you until there is no node left to handle.

In short: An iterator will answer the question about what to do next.

=head2 How to use XML::LibXML::Iterator?

XML::LibXML::Iterator requires a parsed document or at least a node to
operate on. This node is passed to the iterator class and will be used
as the B<first> node of the iteration. One can allways reset the
iterator to the first node by using the first()-function.

Once XML::LibXML::Iterator is initialized the tree can be traversed by
using either next() or previous(). Both function will return a
XML::LibXML::Node object if there is such object available.

Since the current object hold by the iterator class is always
available via the current() function.

The following example may clearify this:

  # get the document from wherever you like
  my $doc = XML::LibXML->new->parse_stream( *SOMEINPUT );

  # get the iterator for the document root.
  my $iter = XML::LibXML::Iterator->new( $doc->documentElement );

  # walk through the document
  while ( $iter->nextNode() ) {
     my $curnode = $iter->current();
     print $curnode->nodeType();
  }

  # now get back to the beginning
  $iter->first();
  my $curnode = $iter->current();
  print $curnode->nodeType();

Actually the functions nextNode(), previousNode(), first(), last() and
current() do return the node which is current after the
operation. E.g. nextNode() moves to the next node if possible and then
returns the node. Thus the while-loop in the example can be written
as

  while ( $iter->nextNode() ) {
     print $_->nodeType();
  }

Note, that just relieing on the return value of next() and previous()
is somewhat dangerous, because both functions return B<undef> in case
of reaching the iteration boundaries. That means it is not possible
to iterate past the last element or before the first one.

=head2 Node Filters

XML::LibXML::Iterator accepts XML::NodeFilters to limit the nodes made
available to the caller. Any nodefilter applied to
XML::LibXML::Iterator will test if a node returned by the iteration
function is visible to the caller.

Different to the DOM Traversal Specification, XML::LibXML::Iterator
allows filter stacks. This means it is possible to apply more than a
single node filter to your node iterator.

=head2 Complex Iterations

By default XML::LibXML::Iterator will access all nodes of a given DOM
tree. An interation based on the default iterator will access each
single node in the given subtree once. The order how the nodes will be
accessed is given by the following order:

  node -> node's childnodes -> node's next sibling

In combination with XML::Nodefilter this is best for a wide range of
scripts and applications. Nevertheless this is still to restrictive
for some applications. XML::LibXML::Iterator allows to change that
behaviour. This is done by resetting XML::LibXML::Iterator's iterator
function. By using the method iterator_function() to override the
default iterator function, it is possible to implement iterations
based on any iteration rule imaginable.

A valid iterator function has to take two parameters: As the first
parameter it will recieve the iterator object itself, as second the
direction of the iteration will be passed. The direction is either 1
(for next()) or -1 (for previous()). As the iterator-function is
called by next() and previous() the interator-function has to be aware
about the iteration boundaries. In case the iteration would pass the
boundary for that operation, the function has to return
undefined. Also the iterator function has to return the new current node,
instead of setting it itself.

*DEVELOPER NOTE* In order a single stepping is rather limited, the
direction is given by the sign of the passed integer value. The value
of the passed parameter will be used as an indication how many steps
should be done.  Therefor the interation direction should be tested
relative to '0' and not as a equation. A basic template for a iterator
function therefore will look like this:

   sub iterator_func_templ {
      my $iter = shift;
      my $step = shift;
      my $node = undef;
      my $current = $iter->current();

      if ( $step > 0 ) {
          # move forward
      }
      else {
          # move backward
          $step *= -1; # remove the sign
      }

      return $node;
   }

=head2 Repeated Operation

Another feature of XML::LibXML::Iterator is the ability to repeat a
single operation on all nodes in scope. Instead of writing a loop one
can specify the opeation as a function, that it applied on each node
found. The function that does the trick, is named iterate().

iterate() takes again two parameter: First the iterator object, second
the node to operate on. iterate() will iterate through the entire
document starting with the first node. If one has already started an
iteration, the internal position will be reset to the first node.

The following example will show how this works:

  $iter->iterate( sub {my ($iter,$node)=@_; map {$iter->setNodeName( lc $iter->nodeName ) if $iter->nodeType != NAMESPACE_DECLARATION } ($node, $node->attributes);  } );

This extra long line lowercases all tagnames and the names of the
attributes in a given subtree.

=head2 Functions

=over 4

=item new($first_node)

=item first()

=item nextNode()

=item previousNode()

=item last()

=item current()

=item index()

=item iterator_function($funcion_ref)

=item set_filter(@filter_list)

=item add_filter(@filter_list)

=item iterate($function_ref)

=back

=head1 SEE ALSO

L<XML::LibXML::Node>, L<XML::NodeFilter>

=head1 AUTHOR

Christian Glahn, E<lt>phish@cpan.orgE<gt>

=head1 COPYRIGHT

(c) 2002-2007, Christian Glahn. All rights reserved.

This package is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut