EasySQL/XMLobj/Node.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 278 CC 35 SCAR 34.0
CC
35
Cov
0%
getElement
90.0
AUTOLOAD
56.0
setAttr
20.0
CRAP 30.0
1 =head1 NAME
2  
3 XML::EasySQL::XMLobj::Node - Node interface. Derived from a fork of Robert Hanson's killer XML::EasyOBJ module, which offers Easy XML object navigation
4  
5 =head1 VERSION
6  
7 Version 1.2
8  
9 =head1 METHODS
10  
11 =cut
12  
13 package XML::EasySQL::XMLobj::Node;
14  
15 use XML::DOM;
16 use strict;
17  
18 use vars qw/$VERSION/;
19 $VERSION = '1.2';
20  
21 use vars qw/$AUTOLOAD/;
22  
23 sub new {
24 my $proto = shift;
25 my $params = shift;
26 my $class = ref($proto) || $proto;
27 my $self = {};
28 $self->{doc} = $params->{doc};
29 $self->{ptr} = $params->{ptr};
30 if(defined $params->{constructor_params}) {
31 $self->{constructor_params} = $params->{constructor_params};
32 } else {
33 $self->{constructor_params} = {};
34 }
35 bless $self, $class;
36 }
37  
38 $AUTOLOAD = '';
39  
40 sub DESTROY {
41 local $^W = 0;
42 my $self = $_[0];
43 $_[0] = '';
44 unless ( $_[0] ) {
45 $_[0] = $self;
46 $AUTOLOAD = 'DESTROY';
47 return AUTOLOAD( @_ );
48 }
49 }
50  
51 sub AUTOLOAD {
52 my $funcname = $AUTOLOAD || 'AUTOLOAD';
53 $funcname =~ s/.*:://;
54 $AUTOLOAD = '';
55  
56 my $self = shift;
57 my $index = shift;
58 my @nodes = ();
59  
60 die "Fatal error: lost pointer!" unless ( exists $self->{ptr} );
61  
62 for my $kid ( $self->{ptr}->getChildNodes ) {
63 if ( ( $kid->getNodeType == ELEMENT_NODE ) && ( $kid->getTagName eq $funcname ) ) {
64 my $node = $self->makeNewNode(undef, $kid);
65 push (@nodes, $node);
66 }
67 }
68  
69 if ( wantarray ) {
70 return @nodes;
71 } else {
72 if ( defined $index ) {
73 unless ( defined $nodes[$index] ) {
74 for ( my $i = scalar(@nodes); $i <= $index; $i++ ) {
75 my $node = $self->makeNewNode($funcname);
76 $nodes[$i] = $node;
77 }
78 }
79 return $nodes[$index];
80 } else {
81 if(!defined $nodes[0]) {
82 ;
83 my $node = $self->makeNewNode($funcname);
84 return $node;
85 }
86 return $nodes[0];
87 }
88 }
89 }
90  
91 =head2 makeNewNode( NEW_TAG )
92  
93 Append a new element node to the current node. Takes the tag name
94 as the parameter and returns the created node as a convienence.
95  
96 my $p_element = $doc->body->makeNewNode('p');
97  
98 =cut
99  
100 sub makeNewNode {
101 my $self = shift;
102 my $tag = shift;
103 my $new_ptr = shift;
104 if(!ref $new_ptr) {
105 $new_ptr = $self->{ptr}->appendChild( $self->{doc}->createElement($tag) );
106 }
107 my %constructor_params_copy = %{$self->{constructor_params}};
108 $constructor_params_copy{doc} = $self->{doc};
109 $constructor_params_copy{ptr} = $new_ptr;
110 $constructor_params_copy{constructor_params} = $self->{constructor_params};
111 my $node = $self->new(\%constructor_params_copy);
112 return $node;
113 }
114  
115 =head2 getString( )
116  
117 Recursively extracts text from the current node and all children
118 element nodes. Returns the extracted text as a single scalar value.
119  
120 =cut
121  
122 sub getString {
123 my $self = shift;
124 die "Fatal error: lost the pointer!" unless ( exists $self->{ptr} );
125 return $self->_extractText( $self->{ptr} );
126 }
127  
128 =head2 _extractText( )
129  
130 Utility method. Don't call this and don't overload it.
131  
132 =cut
133  
134 sub _extractText {
135 my $self = shift;
136 my $n = shift;
137 my $text = '';
138  
139 if ( $n->getNodeType == TEXT_NODE ) {
140 $text = $n->toString;
141 } elsif ( $n->getNodeType == ELEMENT_NODE ) {
142 foreach my $c ( $n->getChildNodes ) {
143 $text .= $self->_extractText($c);
144 }
145 }
146 return $text;
147 }
148  
149  
150 =head2 setString( STRING )
151  
152 Sets the text value of the specified element. This is done by
153 first removing all text node children of the current element
154 and then appending the supplied text as a new child element.
155  
156 Take this XML fragment and code for example:
157  
158 <p>This elment has <b>text</b> and <i>child</i> elements</p>
159  
160 $doc->p->setString('This is the new text');
161  
162 This will change the fragment to this:
163  
164 <p><b>text</b><i>child</i>This is the new text</p>
165  
166 Because the <b> and <i> tags are not text nodes they are left
167 unchanged, and the new text is added at the end of the specified
168 element.
169  
170 If you need more specific control on the change you should
171 either use the getDomObj() method and use the DOM methods
172 directly or remove all of the child nodes and rebuild the
173 <p> element from scratch. Also see the addString() method.
174  
175 =cut
176  
177 sub setString {
178 my $self = shift;
179 my $text = shift;
180  
181 die "Fatal error: lost the pointer!" unless ( exists $self->{ptr} );
182  
183 foreach my $n ( $self->{ptr}->getChildNodes ) {
184 if ( $n->getNodeType == TEXT_NODE ) {
185 $self->{ptr}->removeChild( $n );
186 }
187 }
188  
189 $self->{ptr}->appendChild( $self->{doc}->createTextNode( $text ) );
190 return $self->_extractText( $self->{ptr} );
191 }
192  
193  
194 =head2 addString( STRING )
195  
196 Adds to the the text value of the specified element. This
197 is done by appending the supplied text as a new child element.
198  
199 Take this XML fragment and code for example:
200  
201 <p>This elment has <b>text</b></p>
202  
203 $doc->p->addString(' and elements');
204  
205 This will change the fragment to this:
206  
207 <p>This elment has <b>text</b> and elements</p>
208  
209 =cut
210  
211 sub addString {
212 my $self = shift;
213 my $text = shift;
214  
215 die "Fatal error: lost the pointer!" unless ( exists $self->{ptr} );
216  
217 $self->{ptr}->appendChild( $self->{doc}->createTextNode( $text ) );
218 return $self->_extractText( $self->{ptr} );
219 };
220  
221  
222 =head2 getAttr( ATTR_NAME )
223  
224 Returns the value of the named attribute.
225  
226 my $val = $doc->body->img->getAttr('src');
227  
228 =cut
229  
230 sub getAttr {
231 my $self = shift;
232 my $attr = shift;
233  
234 die "Fatal error: lost the pointer!" unless( exists $self->{ptr} );
235  
236 if ( $self->{ptr}->getNodeType == ELEMENT_NODE ) {
237 return $self->{ptr}->getAttribute($attr);
238 }
239 return '';
240 }
241  
242 =head2 getTagName( )
243  
244 Returns the tag name of the specified element. This method is
245 useful when you are enumerating child elements and do not
246 know their element names.
247  
248 foreach my $element ( $doc->getElement() ) {
249 print $element->getTagName();
250 }
251  
252 =cut
253  
254 sub getTagName {
255 my $self = shift;
256
257 die "Fatal error: lost the pointer!" unless( exists $self->{ptr} );
258  
259 if ( $self->{ptr}->getNodeType == ELEMENT_NODE ) {
260 return $self->{ptr}->getTagName;
261 }
262 return '';
263 }
264  
265 =head2 setAttr( ATTR_NAME, ATTR_VALUE, [ATTR_NAME, ATTR_VALUE]... )
266  
267 For each name/value pair passed the attribute name and value will
268 be set for the specified element.
269  
270 =cut
271  
272 sub setAttr {
273 my $self = shift;
274 my %attr = @_;
275  
276 die "Fatal error: lost the pointer!" unless( exists $self->{ptr} );
277  
278 if ( $self->{ptr}->getNodeType == ELEMENT_NODE ) {
279 if ( scalar(keys %attr) == 1 ) {
280 for ( keys %attr ) {
281 return $self->{ptr}->setAttribute($_, $attr{$_});
282 }
283 } else {
284 for ( keys %attr ) {
285 $self->{ptr}->setAttribute($_, $attr{$_});
286 }
287 return 1;
288 }
289 }
290 return '';
291 }
292  
293 =head2 remAttr( ATTR_NAME )
294  
295 Removes the specified attribute from the current element.
296  
297 =cut
298  
299 sub remAttr {
300 my $self = shift;
301 my $attr = shift;
302
303 die "Fatal error: lost the pointer!" unless( exists $self->{ptr} );
304  
305 if ( $self->{ptr}->getNodeType == ELEMENT_NODE ) {
306 if ( $self->{ptr}->getAttributes->getNamedItem( $attr ) ) {
307 $self->{ptr}->getAttributes->removeNamedItem( $attr );
308 return 1;
309 }
310 }
311 return 0;
312 }
313  
314 =head2 remElement( TAG_NAME, INDEX )
315  
316 Removes a child element of the current element. The name of the
317 child element and the index must be supplied. An index of 0
318 will remove the first occurance of the named element, 1 the second,
319 2 the third, etc.
320  
321 =cut
322  
323 sub remElement {
324 my $self = shift;
325 my $name = shift;
326 my $index = shift;
327
328 my $node = ( $index ) ? $self->$name($index) : $self->$name();
329 $self->{ptr}->removeChild( $node->{ptr} );
330 }
331  
332 =head2 getElement( TAG_NAME, INDEX )
333  
334 Returns the node from the tag name and index. If no index is
335 given the first child with that name is returned. Use this
336 method when you have element names that include characters that
337 are not legal as a perl method name. For example:
338  
339 <foo> <!-- root element -->
340 <bar>
341 <foo-bar>test</foo-bar>
342 </bar>
343 </foo>
344  
345 # "foo-bar" is not a legal method name
346 print $doc->bar->getElement('foo-bar')->getString();
347  
348 =cut
349  
350 sub getElement {
351 my $self = shift;
352 my $funcname = shift;
353 my $index = shift;
354 my @nodes = ();
355  
356 die "Fatal error: lost pointer!" unless ( exists $self->{ptr} );
357  
358 foreach my $kid ( $self->{ptr}->getChildNodes ) {
359 if ( $funcname ) {
360 if ( ( $kid->getNodeType == ELEMENT_NODE ) && ( $kid->getTagName eq $funcname ) ) {
361 my $node = $self->makeNewNode(undef, $kid);
362 push (@nodes, $node);
363 }
364 } else {
365 if ( $kid->getNodeType == ELEMENT_NODE ) {
366 my $node = $self->makeNewNode(undef, $kid);
367 push (@nodes, $node);
368 }
369 }
370 }
371
372 if ( wantarray ) {
373 return @nodes;
374 } else {
375 $index = 0 unless ( defined $index );
376  
377 if ( defined $nodes[$index] ) {
378 return $nodes[$index];
379 } else {
380 # fail if no tag name given
381 return undef unless ( $funcname );
382 for ( my $i = scalar(@nodes); $i <= $index; $i++ ) {
383 my $node = $self->makeNewNode($funcname);
384 $nodes[$i] = $node;
385 }
386 return $nodes[$index];
387 }
388 }
389 }
390  
391 =head1 getDomObj( )
392  
393 Returns the DOM object associated with the current node. This
394 is useful when you need fine access via the DOM to perform
395 a specific function.
396  
397 =cut
398  
399 sub getDomObj {
400 my $self = shift;
401 return $self->{ptr};
402 }
403  
404 1;
405