Debian Codegraph @

lib/XML/LibXML/NodeList.pm

raw
1 # $Id$
2 #
3 # This is free software, you may use it and distribute it under the same terms as
4 # Perl itself.
5 #
6 # Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
7 #
8 #
9
10 package XML::LibXML::NodeList;
11
12 use strict;
13 use warnings;
14
15 use XML::LibXML::Boolean;
16 use XML::LibXML::Literal;
17 use XML::LibXML::Number;
18
19 use vars qw($VERSION);
20 $VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE
21
22 use overload
23 '""' => \&to_literal,
24 'bool' => \&to_boolean,
25 'cmp' => sub {
26 my($aa, $bb, $order) = @_;
27 return ($order ? ("$bb" cmp "$aa") : ("$aa" cmp "$bb"));
28 },
29 ;
30
31 sub new {
32 my $class = shift;
33 bless [@_], $class;
34 }
35
36 sub new_from_ref {
37 my ($class,$array_ref,$reuse) = @_;
38 return bless $reuse ? $array_ref : [@$array_ref], $class;
39 }
40
41 sub pop {
42 my $self = CORE::shift;
43 CORE::pop @$self;
44 }
45
46 sub push {
47 my $self = CORE::shift;
48 CORE::push @$self, @_;
49 }
50
51 sub append {
52 my $self = CORE::shift;
53 my ($nodelist) = @_;
54 CORE::push @$self, $nodelist->get_nodelist;
55 }
56
57 sub shift {
58 my $self = CORE::shift;
59 CORE::shift @$self;
60 }
61
62 sub unshift {
63 my $self = CORE::shift;
64 CORE::unshift @$self, @_;
65 }
66
67 sub prepend {
68 my $self = CORE::shift;
69 my ($nodelist) = @_;
70 CORE::unshift @$self, $nodelist->get_nodelist;
71 }
72
73 sub size {
74 my $self = CORE::shift;
75 scalar @$self;
76 }
77
78 sub get_node {
79 # uses array index starting at 1, not 0
80 # this is mainly because of XPath.
81 my $self = CORE::shift;
82 my ($pos) = @_;
83 $self->[$pos - 1];
84 }
85
86 sub item
87 {
88 my ($self, $pos) = @_;
89 return $self->[$pos];
90 }
91
92 sub get_nodelist {
93 my $self = CORE::shift;
94 @$self;
95 }
96
97 sub to_boolean {
98 my $self = CORE::shift;
99 return (@$self > 0) ? XML::LibXML::Boolean->True : XML::LibXML::Boolean->False;
100 }
101
102 # string-value of a nodelist is the string-value of the first node
103 sub string_value {
104 my $self = CORE::shift;
105 return '' unless @$self;
106 return $self->[0]->string_value;
107 }
108
109 sub to_literal {
110 my $self = CORE::shift;
111 return XML::LibXML::Literal->new(
112 join('', CORE::grep {defined $_} CORE::map { $_->string_value } @$self)
113 );
114 }
115
116 sub to_literal_delimited {
117 my $self = CORE::shift;
118 return XML::LibXML::Literal->new(
119 join(CORE::shift, CORE::grep {defined $_} CORE::map { $_->string_value } @$self)
120 );
121 }
122
123 sub to_literal_list {
124 my $self = CORE::shift;
125 my @nodes = CORE::map{ XML::LibXML::Literal->new($_->string_value())->value() } @{$self};
126
127 if (wantarray) {
128 return( @nodes );
129 }
130 return( \@nodes );
131 }
132
133 sub to_number {
134 my $self = CORE::shift;
135 return XML::LibXML::Number->new(
136 $self->to_literal
137 );
138 }
139
140 sub iterator {
141 warn "this function is obsolete!\nIt was disabled in version 1.54\n";
142 return undef;
143 }
144
145 sub map {
146 my $self = CORE::shift;
147 my $sub = __is_code(CORE::shift);
148 local $_;
149 my @results = CORE::map { @{[ $sub->($_) ]} } @$self;
150 return unless defined wantarray;
151 return wantarray ? @results : (ref $self)->new(@results);
152 }
153
154 sub grep {
155 my $self = CORE::shift;
156 my $sub = __is_code(CORE::shift);
157 local $_;
158 my @results = CORE::grep { $sub->($_) } @$self;
159 return unless defined wantarray;
160 return wantarray ? @results : (ref $self)->new(@results);
161 }
162
163 sub sort {
164 my $self = CORE::shift;
165 my $sub = __is_code(CORE::shift);
166 my @results = CORE::sort { $sub->($a,$b) } @$self;
167 return wantarray ? @results : (ref $self)->new(@results);
168 }
169
170 sub foreach {
171 my $self = CORE::shift;
172 my $sub = CORE::shift;
173
174 foreach my $item (@$self)
175 {
176 local $_ = $item;
177 $sub->($item);
178 }
179
180 return wantarray ? @$self : $self;
181 }
182
183 sub reverse {
184 my $self = CORE::shift;
185 my @results = CORE::reverse @$self;
186 return wantarray ? @results : (ref $self)->new(@results);
187 }
188
189 sub reduce {
190 my $self = CORE::shift;
191 my $sub = __is_code(CORE::shift);
192
193 my @list = @$self;
194 CORE::unshift @list, $_[0] if @_;
195
196 my $a = CORE::shift(@list);
197 foreach my $b (@list)
198 {
199 $a = $sub->($a, $b);
200 }
201 return $a;
202 }
203
204 sub __is_code {
205 my ($code) = @_;
206
207 if (ref $code eq 'CODE') {
208 return $code;
209 }
210
211 # There are better ways of doing this, but here I've tried to
212 # avoid adding any additional external dependencies.
213 #
214 if (UNIVERSAL::can($code, 'can') # is blessed (sort of)
215 and overload::Overloaded($code) # is overloaded
216 and overload::Method($code, '&{}')) { # overloads '&{}'
217 return $code;
218 }
219
220 # The other possibility is that $code is a coderef, but is
221 # blessed into a class that doesn't overload '&{}'. In which
222 # case... well, I'm stumped!
223
224 die "Not a subroutine reference\n";
225 }
226
227 1;
228 __END__
229
230 =head1 NAME
231
232 XML::LibXML::NodeList - a list of XML document nodes
233
234 =head1 DESCRIPTION
235
236 An XML::LibXML::NodeList object contains an ordered list of nodes, as
237 detailed by the W3C DOM documentation of Node Lists.
238
239 =head1 SYNOPSIS
240
241 my $results = $dom->findnodes('//somepath');
242 foreach my $context ($results->get_nodelist) {
243 my $newresults = $context->findnodes('./other/element');
244 ...
245 }
246
247 =head1 API
248
249 =head2 new(@nodes)
250
251 You will almost never have to create a new NodeList object, as it is all
252 done for you by XPath.
253
254 =head2 get_nodelist()
255
256 Returns a list of nodes, the contents of the node list, as a perl list.
257
258 =head2 string_value()
259
260 Returns the string-value of the first node in the list.
261 See the XPath specification for what "string-value" means.
262
263 =head2 to_literal()
264
265 Returns the concatenation of all the string-values of all
266 the nodes in the list.
267
268 =head2 to_literal_delimited($separator)
269
270 Returns the concatenation of all the string-values of all
271 the nodes in the list, delimited by the specified separator.
272
273 =head2 to_literal_list()
274
275 Returns all the string-values of all the nodes in the list as
276 a perl list.
277
278 =head2 get_node($pos)
279
280 Returns the node at $pos. The node position in XPath is based at 1, not 0.
281
282 =head2 size()
283
284 Returns the number of nodes in the NodeList.
285
286 =head2 pop()
287
288 Equivalent to perl's pop function.
289
290 =head2 push(@nodes)
291
292 Equivalent to perl's push function.
293
294 =head2 append($nodelist)
295
296 Given a nodelist, appends the list of nodes in $nodelist to the end of the
297 current list.
298
299 =head2 shift()
300
301 Equivalent to perl's shift function.
302
303 =head2 unshift(@nodes)
304
305 Equivalent to perl's unshift function.
306
307 =head2 prepend($nodelist)
308
309 Given a nodelist, prepends the list of nodes in $nodelist to the front of
310 the current list.
311
312 =head2 map($coderef)
313
314 Equivalent to perl's map function.
315
316 =head2 grep($coderef)
317
318 Equivalent to perl's grep function.
319
320 =head2 sort($coderef)
321
322 Equivalent to perl's sort function.
323
324 Caveat: Perl's magic C<$a> and C<$b> variables are not available in
325 C<$coderef>. Instead the two terms are passed to the coderef as arguments.
326
327 =head2 reverse()
328
329 Equivalent to perl's reverse function.
330
331 =head2 foreach($coderef)
332
333 Inspired by perl's foreach loop. Executes the coderef on each item in
334 the list. Similar to C<map>, but instead of returning the list of values
335 returned by $coderef, returns the original NodeList.
336
337 =head2 reduce($coderef, $init)
338
339 Equivalent to List::Util's reduce function. C<$init> is optional and
340 provides an initial value for the reduction.
341
342 Caveat: Perl's magic C<$a> and C<$b> variables are not available in
343 C<$coderef>. Instead the two terms are passed to the coderef as arguments.
344
345 =cut