XML/Node.pm

filter: Statementstmt 65.2% 60 / 92 Branchbran 66.6% 12 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.7% 11 / 17 Podpod 92.3% 12 / 13 totaltotal 67.8% 95 / 140 CC 13 SCAR 13.5
CC
13
Cov
65%
getAttributes
12.0
toString
6.0
setAttributes
3.1
CRAP 3.8
« XML.pm ↑ Summary XML/Handler.pm »
1 package POE::Filter::XML::Node;
2 {
3 3 $POE::Filter::XML::Node::VERSION = '1.140700';
4 }
5  
6 #ABSTRACT: A XML::LibXML::Element subclass that adds streaming semantics
7  
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use MooseX::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 extends 'XML::LibXML::Element';
14  
15 3 ▶use XML::LibXML(':libxml');
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 3 has stream_start => (is => 'ro', writer => '_set_stream_start', isa => 'Bool', default => 0);
19 3 has stream_end => (is => 'ro', writer => '_set_stream_end', isa => 'Bool', default => 0);
20  
21  
22 12 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
23 12 my ($self, $name) = @_;
24 #only a name should be passed
25 12 return { name => $name };
26 }
27  
28  
29 1 ▶sub cloneNode {
Subroutine cloneNode: called CC=1 SCAR=0.0
Pod documented
30
31 1 my ($self, $deep) = @_;
32 1 my $clone = $self->SUPER::cloneNode($deep);
33
34 1 bless($clone, $self->meta->name());
35
36 1 $clone->_set_stream_start($self->stream_start());
37 1 $clone->_set_stream_end($self->stream_end());
38
39 1 return $clone;
40 }
41  
42 4 ▶sub getChildrenByTagName {
Subroutine getChildrenByTagName: called CC=1 SCAR=0.0
Pod documented
43 4 my ($self, $name) = @_;
44 4 my $CLASS = $self->meta->name();
45 4 return (map { bless($_, $CLASS) } @{ $self->SUPER::getChildrenByTagName($name) });
46 }
47  
48 0 ▶sub getChildrenByTagNameNS {
Subroutine getChildrenByTagNameNS: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($self, $nsURI, $localname) = @_;
50 0 my $CLASS = $self->meta->name();
51 0 return (map { bless($_, $CLASS) } @{ $self->SUPER::getChildrenByTagNameNS($nsURI, $localname) });
52 }
53  
54 0 ▶sub getChildrenByLocalName {
Subroutine getChildrenByLocalName: not called CC=1 SCAR=6.9
Pod documented
55 0 my ($self, $localname) = @_;
56 0 my $CLASS = $self->meta->name();
57 0 return (map { bless($_, $CLASS) } @{ $self->SUPER::getChildrenByLocalName($localname) });
58 }
59  
60 0 ▶sub getElementsByTagName {
Subroutine getElementsByTagName: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($self, $name) = @_;
62 0 my $CLASS = $self->meta->name();
63 0 return (map { bless($_, $CLASS) } @{ $self->SUPER::getElementsByTagName($name) });
64 }
65  
66 0 ▶sub getElementsByTagNameNS {
Subroutine getElementsByTagNameNS: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($self, $nsURI, $localname) = @_;
68 0 my $CLASS = $self->meta->name();
69 0 return (map { bless($_, $CLASS) } @{ $self->SUPER::getElementsByTagNameNS($nsURI, $localname) });
70 }
71  
72 0 ▶sub getElementsByLocalName {
Subroutine getElementsByLocalName: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($self, $localname) = @_;
74 0 my $CLASS = $self->meta->name();
75 0 return (map { bless($_, $CLASS) } @{ $self->SUPER::getElementsByLocalName($localname) });
76 }
77
78  
79 5 ▶sub toString {
Subroutine toString: called CC=6 SCAR=18.0
Pod documented
80 5 my ($self, $formatted, $docencoding) = @_;
81
82 5 ▶ $formatted = defined($formatted) ? $formatted : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $formatted ? : 0 5 5
83 5 ▶ $docencoding = defined($docencoding) ? $docencoding : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $docencoding ? : 0 5 5
84
85 5 ▶ if($self->stream_start())
Branch: 2 branches
BranchTrueFalseTotal
if ($self->stream_start) { } 1 4 5
elsif ($self->stream_end) { } 1 3 4
86 {
87 1 my $string = '<';
88 1 $string .= $self->nodeName();
89 1 foreach my $attr ($self->attributes())
90 {
91 3 $string .= sprintf(' %s="%s"', $attr->nodeName(), $attr->value());
92 }
93 1 $string .= '>';
94 1 return $string;
95 }
96 elsif ($self->stream_end())
97 {
98 1 return sprintf('</%s>', $self->nodeName());
99 }
100 else
101 {
102 3 return $self->SUPER::toString($formatted, $docencoding);
103 }
104 }
105  
106  
107 1 ▶sub setAttributes {
Subroutine setAttributes: called CC=3 SCAR=11.3
Pod documented
108 1 my ($self, $array) = @_;
109 1 ▶ for(my $i = 0; $i < scalar(@$array); $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$array) 3 1 4
110 {
111 3 ▶ if($array->[$i] eq 'xmlns')
Branch: 1 branch
BranchTrueFalseTotal
if ($array->[$i] eq 'xmlns') { } 0 3 3
112 {
113 0 $self->setNamespace($array->[++$i], '', 0);
114 }
115 else
116 {
117 3 $self->setAttribute($array->[$i], $array->[++$i]);
118 }
119 }
120 }
121  
122  
123 0 ▶sub getAttributes {
Subroutine getAttributes: not called CC=3 SCAR=24.8
Pod documented
124 0 my ($self) = @_;
125 0 my $attributes = {};
126  
127 0 foreach my $attrib ($self->attributes())
128 {
129 0 ▶ if($attrib->nodeType == XML_ATTRIBUTE_NODE)
Branch: 1 branch
BranchTrueFalseTotal
if ($attrib->nodeType == 2) 0 0 0
130 {
131 0 $attributes->{$attrib->nodeName()} = $attrib->value();
132 }
133 }
134  
135 0 return $attributes;
136 }
137  
138  
139 2 ▶sub getSingleChildByTagName {
Subroutine getSingleChildByTagName: called CC=2 SCAR=7.0
Pod undocumented
140 2 my ($self, $name) = @_;
141 2 my $node = ($self->getChildrenByTagName($name))[0];
142 2 ▶ return undef if not defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 2 2
143 2 return $node;
144 }
145  
146  
147 2 ▶sub getChildrenHash {
Subroutine getChildrenHash: called CC=3 SCAR=11.0
Pod documented
148 2 my ($self) = @_;
149 2 my $children = {};
150  
151 2 foreach my $child ($self->getChildrenByTagName("*"))
152 {
153 20 my $name = $child->nodeName();
154
155 20 ▶ if(!exists($children->{$name}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $children->{$name}) 4 16 20
156 {
157 4 $children->{$name} = [];
158 }
159
160 20 push(@{$children->{$name}}, $child);
161 }
162  
163 2 return $children;
164 }
165  
166 3 1;
167  
168  
169  
170 =pod
171  
172 =head1 NAME
173  
174 POE::Filter::XML::Node - A XML::LibXML::Element subclass that adds streaming semantics
175  
176 =head1 VERSION
177  
178 version 1.140700
179  
180 =head1 SYNOPSIS
181  
182 use POE::Filter::XML::Node;
183  
184 my $node = POE::Filter::XML::Node->new('iq');
185  
186 $node->setAttributes(
187 ['to', 'foo@other',
188 'from', 'bar@other',
189 'type', 'get']
190 );
191  
192 my $query = $node->addNewChild('jabber:iq:foo', 'query');
193  
194 $query->appendTextChild('foo_tag', 'bar');
195  
196 say $node->toString();
197  
198 --
199  
200 (newlines and tabs for example only)
201  
202 <iq to='foo@other' from='bar@other' type='get'>
203 <query xmlns='jabber:iq:foo'>
204 <foo_tag>bar</foo_tag>
205 </query>
206 </iq>
207  
208 =head1 DESCRIPTION
209  
210 POE::Filter::XML::Node is a XML::LibXML::Element subclass that aims to provide
211 a few extra convenience methods and light integration into a streaming context.
212  
213 This module can be used to create arbitrarily complex XML data structures that
214 know how to stringify themselves.
215  
216 =head1 PUBLIC_ATTRIBUTES
217  
218 =head2 stream_[start|end]
219  
220 is: ro, isa: Bool, default: false
221  
222 These two attributes define behaviors to toString() for the node. In the case
223 of stream_start, this means dropping all children and merely leaving the tag
224 unterminated (eg. <start>). For stream_end, it will drop any children and treat
225 the tag like a terminator (eg. </end>).
226  
227 Each attribute has a private writer ('_set_stream_[start|end]') if it necessary
228 to manipulate these attributes post construction.
229  
230 =head1 PUBLIC_METHODS
231  
232 =head2 override cloneNode
233  
234 (Bool $deep)
235  
236 cloneNode is overriden to carry forward the stream_[end|start] attributes
237  
238 =head2 override toString
239  
240 (Bool $formatted)
241  
242 toString was overridden to provide special stringification semantics for when
243 stream_start or stream_end are boolean true.
244  
245 =head2 setAttributes
246  
247 (ArrayRef $array_of_tuples)
248  
249 setAttributes() accepts a single arguement: an array reference. Basically you
250 pair up all the attributes you want to be into the node (ie. [attrib, value])
251 and this method will process them using setAttribute(). This is just a
252 convenience method.
253  
254 If one of the attributes is 'xmlns', setNamespace() will be called with the
255 value used as the $nsURI argument, with no prefix, and not activated.
256  
257 eg.
258 ['xmlns', 'http://foo']
259 |
260 V
261 setNamespace($value, '', 0)
262 |
263 V
264 <node xmlns="http://foo"/>
265  
266 =head2 getAttributes
267  
268 returns (HashRef)
269  
270 This method returns all of the attribute nodes on the Element (filtering out
271 namespace declarations) as a HashRef.
272  
273 =head2 getFirstChildByTagName(Str $name)
274  
275 returns (Maybe[POE::Filter::XML::Node])
276  
277 This is a convenience method that basically does:
278 (getChildrenByTagName($name))[0]
279  
280 =head2 getChildrenHash
281  
282 returns (HashRef)
283  
284 getChildrenHash() returns a hash reference to all the children of that node.
285 Each key in the hash will be node name, and each value will be an array
286 reference with all of the children with that name.
287  
288 =head1 AUTHOR
289  
290 Nicholas R. Perez <nperez@cpan.org>
291  
292 =head1 COPYRIGHT AND LICENSE
293  
294 This software is copyright (c) 2014 by Nicholas R. Perez <nperez@cpan.org>.
295  
296 This is free software; you can redistribute it and/or modify it under
297 the same terms as the Perl 5 programming language system itself.
298  
299 =cut
300  
301  
302 __END__
« XML.pm ↑ Summary XML/Handler.pm »