Enhanced.pm

filter: Statementstmt 92.3% 85 / 92 Branchbran 72.7% 16 / 22 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 86.9% 20 / 23 Podpod 83.3% 5 / 6 totaltotal 85.3% 134 / 157 CC 16 SCAR 10.9
CC
16
Cov
87%
appendHash
7.0
parse_xslt_file
6.0
appendRow
3.1
CRAP 3.0
« Transformer.pm ↑ Summary Pipeline.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = "0.01";
5  
6 =head1 NAME
7  
8 XML::LibXML::Enhanced - adds convenience methods to XML::LibXML and LibXSLT
9  
10 =head1 SYNOPSIS
11  
12 use XML::LibXML::Enhanced;
13
14 my $xml = XML::LibXML::Singleton->instance;
15 my $xsl = XML::LibXSLT::Singleton->instance;
16
17 my $doc = $xml->parse_xml_string("<root/>");
18
19 my $root = $doc->getDocumentElement;
20
21 $root->appendHash({ name => 'Michael', email => 'mjs@beebo.org' });
22  
23 =head1 DESCRIPTION
24  
25 =head1 ADDED FUNCTIONS
26  
27 =over 4
28  
29 =item $xml = parse_xml_file($filename)
30  
31 =item $xml = parse_xml_string($string)
32  
33 =item $xml = parse_xml_chunk($string)
34  
35 Parses a file or string, and returns an XML::LibXML::Document
36 (parse_xml_file(), parse_xml_string()) or
37 XML::LibXML::DocumentFragment (parse_xml_chunk()).
38  
39 That is, the equivalent of
40 XML::LibXML::Singleton->instance->parse_file($filename), etc.
41  
42 =item $xsl = parse_xslt_file($filename)
43  
44 =item $xsl = parse_xslt_string($string)
45  
46 Parses a file or string, and returns an XML::LibXSLT::Stylesheet.
47 (L<XML::LibXSLT>.)
48  
49 That is, the equivalent of
50 XML::LibXSLT::Singleton->instance->parse_stylesheet(XML::LibXML::Singleton->instance->parse_file($filename)),
51 etc.
52  
53 =back
54  
55 =cut
56  
57 package XML::LibXML::Enhanced;
58  
59 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 2 @EXPORT_OK = qw(
63 parse_xml_file parse_xml_string parse_xml_chunk
64 parse_xslt_file parse_xslt_string promote
65 );
66  
67 1 ▶sub parse_xml_file {
Subroutine parse_xml_file: called CC=2 SCAR=7.6
Pod documented
68 1 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 0 1
69 return map {
70 1 XML::LibXML::Singleton->instance->parse_file($_)
71 } @_;
72 }
73 else {
74 0 return XML::LibXML::Singleton->instance->parse_file($_[0]);
75 }
76 }
77  
78 10 ▶sub parse_xml_string {
Subroutine parse_xml_string: called CC=1 SCAR=0.0
Pod documented
79 10 return XML::LibXML::Singleton->instance->parse_string(@_);
80 }
81  
82 1 ▶sub parse_xml_chunk {
Subroutine parse_xml_chunk: called CC=1 SCAR=0.0
Pod documented
83 1 return XML::LibXML::Singleton->instance->parse_balanced_chunk(@_);
84 }
85  
86 0 ▶sub parse_xslt_file {
Subroutine parse_xslt_file: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
88 return map {
89 0 XML::LibXSLT::Singleton->instance->parse_stylesheet(parse_xml_file($_))
90 } @_;
91 }
92 else {
93 0 return XML::LibXSLT::Singleton->instance->parse_stylesheet(parse_xml_file($_[0]));
94 }
95 }
96  
97 0 ▶sub parse_xslt_string {
Subroutine parse_xslt_string: not called CC=1 SCAR=6.9
Pod documented
98 0 return XML::LibXSLT::Singleton->instance->parse_stylesheet(parse_xml_string(@_));
99 }
100  
101 0 ▶sub promote {
Subroutine promote: not called CC=1 SCAR=6.9
Pod undocumented
102 0 return XML::LibXSLT::xpath_to_string(@_);
103 }
104  
105 =head1 ADDED CLASSES
106  
107 =head2 XML::LibXML::Singleton
108  
109 Singleton version of XML::LibXML; get an instance via
110 C<XML::LibXML::SIngleton-E<gt>instance>.
111  
112 Note that the methods C<load_ext_dtd(0)> and C<validation(0)> have
113 been called on the returned object; see C<XML::LibXML> for futher
114 details.
115  
116 =cut
117  
118 package XML::LibXML::Singleton;
119  
120 2 ▶use base qw(XML::LibXML Class::Singleton);
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 2 ▶sub _new_instance {
Subroutine _new_instance: called CC=2 SCAR=7.1
123 2 my ($proto, @args) = @_;
124
125 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
126
127 2 my $self = $class->SUPER::new(@args);
128
129 2 $self->load_ext_dtd(0);
130 2 $self->validation(0);
131
132 2 $self;
133 }
134  
135 =head2 XML::LibXSLT::Singleton
136  
137 Singleton version of XML::LibXSLT; get an instance via
138 C<XML::LibXSLT::SIngleton-E<gt>instance>.
139  
140 =cut
141  
142 package XML::LibXSLT::Singleton;
143  
144 2 ▶use base qw(XML::LibXSLT Class::Singleton);
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 =head1 ADDED METHODS
147  
148 =head2 TO XML::LibXML::Node
149  
150 =over 4
151  
152 =cut
153  
154 package XML::LibXML::Node;
155  
156 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 2 ▶use XML::LibXML qw(XML_ELEMENT_NODE XML_TEXT_NODE);
Subroutine BEGIN: called CC=1 SCAR=0.0
158 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 2 ▶use Data::Eacherator qw(eacherator);
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 =item $n->appendHash( $hash [, $values_are_text ])
162  
163 Converts C<$hash> to an XML element, and adds it to C<$n>. (That is,
164 the opposite of C<$hash = $n-E<gt>toHash>.)
165  
166 If C<$values_are_text> is 1, the values of the hash are treated as
167 text, and are XML-encoded (C<&> to C<&amp;> and so forth) before being
168 added to the node.
169  
170 If C<$values_are_text> is 0 or undefined, the values of the hash are
171 treated as XML, and are parsed before being added to the node. In
172 this case, the key values must be either be well-formed XML, or
173 well-balanced XML chunks. (i.e. "<foo/><bar/>" is okay, but not
174 "<foo>".) If neither is true, a comment will be inserted in the place
175 of value--C<NOT BALANCED XML>.
176  
177 For example, if
178  
179 $hash = {
180 name => "Clive",
181 email => "clive@example.com",
182 };
183
184 and
185  
186 $node
187
188 is
189  
190 <row>
191 </row>
192
193 then
194  
195 $node->appendHash($hash);
196
197 results in the C<$node>
198  
199 <row>
200 <name>Clive</name>
201 <email>clive@example.com</email>
202 </row>
203  
204 NOTE: attribute names (i.e. key values) are lowercased.
205  
206 =cut
207  
208 9 ▶sub appendHash {
Subroutine appendHash: called CC=7 SCAR=19.5
209 9 my ($self, $data, $values_are_text) = @_;
210
211 9 my $doc = $self->getOwnerDocument;
212  
213 9 my $iter = eacherator($data);
214  
215 9 ▶ while (my ($k, $v) = $iter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 17 9 26
216  
217 17 my $n = $doc->createElement($k);
218
219 17 ▶ if ((defined $v) && ($v ne '') && !ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v ne "" and not ref $v) 9 8 17
Condition83%5/6
defined $v and $v ne "" and not ref $v
!ll&&!rl&&r
809
defined $v and $v ne ""
!ll&&!rl&&r
629
MC/DC66%2/3
defined $v and $v ne "" and not ref $v
defined $v $v ne "" not ref $v
Truth table75%3/4
A and B and not(C)
A= defined $vB= $v ne ""C= not ref $v
ABCresult
0XX0
10X0
1100
1111
220  
221 9 ▶ if ($values_are_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values_are_text) { } 1 8 9
222 1 $n->appendChild($doc->createTextNode($v));
223 }
224  
225 else {
226 8 my $c = eval {
227 8 XML::LibXML::Singleton->instance->parse_balanced_chunk($v);
228 };
229 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 7 8
230 1 $n->appendChild(
231 $doc->createComment("NOT BALANCED XML")
232 );
233 }
234 else {
235 7 $n->appendChild(
236 $doc->adoptNode($c)
237 );
238 }
239 }
240 }
241  
242 17 $self->appendChild($n);
243 }
244
245 9 $data; # because $n->appendChild($c) returns $c
246 }
247  
248 =item $n->appendAttributeHash( $hash )
249  
250 Adds attributes to node C<$n>.
251  
252 =cut
253  
254 4 ▶sub appendAttributeHash {
Subroutine appendAttributeHash: called CC=2 SCAR=6.9
255 4 my ($self, $data) = @_;
256
257 4 my $iter = eacherator($data);
258  
259 4 ▶ while (my ($k, $v) = $iter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 5 4 9
260 5 $self->setAttribute($k, $v);
261 }
262
263 4 $data;
264 }
265  
266 =item $h = $n->toHash( [ $values_are_text ] )
267  
268 Returns a simple hash reference representation of node C<$n>. (That
269 is, the opposite of C<$n-E<gt>appendHash($h)>.)
270  
271 If C<$values_are_text> is 1, an XML decoding of the values is
272 performed. (C<&amp;> to C<&>, etc.)
273  
274 If C<$values_are_text> is 0 or undefined, then no transformation of
275 the values are performed.
276  
277 For example, when C<toHash()> is called on the C<row> node
278  
279 <row>
280 <name>Michael</name>
281 <email>mjs@beebo.org</email>
282 </row>
283  
284 the return value will be
285  
286 {
287 name => 'Michael',
288 email => 'mjs@beebo.org',
289 }
290  
291 B<NOTE>:
292  
293 =over 4
294  
295 =item o
296  
297 order is not necessarily preserved, and if two or more tags of the
298 same name are present, only one will be present in the hash.
299  
300 =item o
301  
302 attributes are discarded.
303  
304 =back
305
306 =cut
307  
308 5 ▶sub toHash {
Subroutine toHash: called CC=3 SCAR=11.0
309 5 my ($self, $values_are_text) = @_;
310
311 # The grep below filters out non element nodes because we need to
312 # skip text nodes in the event of mixed content.
313  
314 my $hash = {
315 map {
316 6 $_->nodeName, $_->childrenToString
317 }
318 grep {
319 5 $_->nodeType == XML_ELEMENT_NODE
320 }
321 $self->childNodes
322 };
323
324 5 ▶ if ($values_are_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values_are_text) 2 3 5
325
326 # createTextNode() encodes its argument; we need to perform
327 # the reverse process here. (&amp; => &, and so on.)
328  
329 2 foreach (values %$hash) {
330 2 $_ = decode_entities($_);
331 }
332  
333 }
334
335 5 return $hash;
336 }
337  
338 =item $h = $n->toAttributeHash
339  
340 =cut
341  
342 4 ▶sub toAttributeHash {
Subroutine toAttributeHash: called CC=1 SCAR=0.0
343 4 my ($self) = @_;
344
345 4 return { map { $_->nodeName, $_->value } $self->attributes };
346 }
347  
348 =item $n->appendRow($hash [, $name ])
349  
350 Similar to C<appendHash($hash)> except that the appended hash is added
351 as child of a C<$name> element. That is, if C<$n> is the node
352 "<root/>", C<$n-E<gt>appendRow({ name =E<gt> 'Michael' }, "row")> results
353 in
354  
355 <root>
356 <row>
357 <name>Michael</name>
358 </row>
359 </root>
360
361 C<$name> defaults to "row".
362  
363 =cut
364  
365 1 ▶sub appendRow {
Subroutine appendRow: called CC=3 SCAR=11.2
366 1 my ($self, $hash, $name, $values_are_text) = @_;
367  
368 1 ▶ $name = "row" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 0 1
369 1 ▶ $values_are_text = 0 unless defined $values_are_text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values_are_text 1 0 1
370  
371 1 my $doc = $self->getOwnerDocument;
372 1 my $row = $doc->createElement(lc($name));
373
374 1 $row->appendHash($hash, $values_are_text);
375
376 1 return $self->appendChild($row);
377 }
378  
379 =item $s = $n->childrenToString
380  
381 Like C<toString()> except that only the node's I<children> are
382 stringified--the opening and closing tags of the node itself are
383 omitted. (This may create a "balanced chunk.")
384  
385 =cut
386  
387 6 ▶sub childrenToString {
Subroutine childrenToString: called CC=2 SCAR=7.1
388 6 my ($self) = @_;
389
390 6 ▶ return join('', map { defined $_->toString ? $_->toString : '' } $self->childNodes);
Branch: 1 branch
BranchTrueFalseTotal
defined $_->toString ? : 6 0 6
391 }
392  
393 =back
394  
395 =head1 AUTHOR
396  
397 Michael Stillwell <mjs@beebo.org>
398  
399 =cut
400  
401 2 1;
402  
403 __END__
« Transformer.pm ↑ Summary Pipeline.pm »