blib/lib/XML/Constructor.pm

filter: Statementstmt 91.8% 90 / 98 Branchbran 58.0% 29 / 50 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 88.8% 16 / 18 Podpod 100% 2 / 2 totaltotal 76.4% 146 / 191 CC 34 SCAR 18.6
CC
34
Cov
78%
_from_scalar
18.6
_get_dispatch_method
10.4
_validate_parent_object
4.8
CRAP 6.4
↑ Summary
1 package XML::Constructor;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw/cluck croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 XML::Constructor - Generate XML from a markup syntax allowing for the abstraction of markup from code
12  
13 =cut
14  
15 4 our $VERSION = '0.01';
16  
17  
18 19 ▶sub generate {
Subroutine generate: called CC=2 SCAR=6.9
Pod documented
19 19 my ( $class, %args ) = @_;
20 my ( $parent_node, $data ) =
21 19 @args{ qw( parent_node data ) };
22  
23 19 my $parent = $class->_get_parent_node($parent_node);
24  
25 19 ▶ return $parent unless (ref $data eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 5 14 19
26  
27 14 return $class->_generate(parent => $parent, data => $data);
28 }
29  
30 0 ▶sub toString { shift->generate( @_ )->toString }
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
31  
32 19 ▶sub _get_parent_node {
Subroutine _get_parent_node: called CC=4 SCAR=13.9
33 19 my ( $class, $parent_node ) = @_;
34  
35 19 ▶ if(blessed $parent_node) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($parent_node)) 3 16 19
36 3 ▶ return $parent_node
Branch: 1 branch
BranchTrueFalseTotal
if $class->_validate_parent_object($parent_node) 3 0 3
37 if $class->_validate_parent_object($parent_node);
38 }
39  
40 16 ▶ return ( ref $parent_node eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
ref $parent_node eq 'ARRAY' ? : 1 15 16
41 ? $class->_create_parent_from_arrayref( element => $parent_node )
42 : $class->_create_parent_node( $parent_node );
43 }
44  
45 26 ▶sub _generate {
Subroutine _generate: called CC=3 SCAR=11.0
46 26 my ( $class, %args ) = @_;
47 26 my ($parent, $data ) = @args{ qw( parent data ) };
48  
49 26 for my $element ( @$data ) {
50 33 my $method = $class->_get_dispatch_method($element);
51  
52 33 ▶ next unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 33 33
53  
54 33 $class->$method( parent => $parent, element => $element );
55 }
56 26 return $parent;
57 }
58  
59 3 ▶sub _validate_parent_object {
Subroutine _validate_parent_object: called CC=4 SCAR=15.6
60 3 my ( $class, $parent_element ) = @_;
61 3 ▶ croak "parent element not an object"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($parent_element) 0 3 3
62 unless blessed $parent_element;
63  
64 3 ▶ croak "parent element not decendant of XML::LibXML::(Element|Document)"
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_element->isa("XML::LibXML::Node") or $parent_element->isa("XML::LibXML::Document") 0 3 3
Condition33%1/3
$parent_element->isa("XML::LibXML::Node") or $parent_element->isa("XML::LibXML::Document")
l!l&&r!l&&!r
300
MC/DC0%0/2
$parent_element->isa("XML::LibXML::Node") or $parent_element->isa("XML::LibXML::Document")
$parent_element->isa("XML::LibXML::Node") $parent_element->isa("XML::LibXML::Document")
Truth table33%1/3
A or B
A= $parent_element->isa("XML::LibXML::Node")B= $parent_element->isa("XML::LibXML::Document")
ABresult
1X1
011
000
65 unless ($parent_element->isa("XML::LibXML::Node") ||
66 $parent_element->isa("XML::LibXML::Document"));
67
68
69 3 return 1;
70 }
71  
72 15 ▶sub _create_parent_node {
Subroutine _create_parent_node: called CC=3 SCAR=11.0
73 15 my ( $class, $parent_node_name ) = @_;
74 15 ▶ cluck "creating an empty parent node"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent_node_name and $parent_node_name =~ /\w/ 2 13 15
Condition66%2/3
defined $parent_node_name and $parent_node_name =~ /\w/
!ll&&!rl&&r
2013
MC/DC50%1/2
defined $parent_node_name and $parent_node_name =~ /\w/
defined $parent_node_name $parent_node_name =~ /\w/
Truth table66%2/3
A and B
A= defined $parent_node_nameB= $parent_node_name =~ /\w/
ABresult
0X0
100
111
75 unless(defined $parent_node_name && $parent_node_name =~/\w/);
76  
77 15 return $class->_create_element($parent_node_name);
78 }
79  
80 33 ▶sub _get_dispatch_method {
Subroutine _get_dispatch_method: called CC=9 SCAR=23.4
81 33 my ( $class, $element) = @_;
82 33 my $method;
83  
84 33 for( ref $element ) {
85 33 ▶ $_ eq 'HASH' && do{$method = '_from_hash'; last};
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "HASH" and do { $method = "_from_hash"; last } 10 23 33
86 23 ▶ $_ eq 'ARRAY' && do{$method = '_from_array'; last};
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "ARRAY" and do { $method = "_from_array"; last } 14 9 23
87 9 ▶ $_ eq 'SCALAR' && do{$method = '_from_scalar'; last};
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "SCALAR" and do { $method = "_from_scalar"; last } 9 0 9
88 }
89  
90 # is element a XML::LibXML::Element object?
91 33 ▶ if(!$method && (blessed $element)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $method and builtin::blessed($element)) 0 33 33
Condition33%1/3
not $method and builtin::blessed($element)
!ll&&!rl&&r
3300
MC/DC0%0/2
not $method and builtin::blessed($element)
not $method builtin::blessed($element)
Truth table33%1/3
not(A) and B
A= not $methodB= builtin::blessed($element)
ABresult
0X0
100
111
92 0 ▶ $method = '_from_libxml'
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("XML::LibXML::Element") 0 0 0
93 if $element->isa("XML::LibXML::Element");
94 }
95  
96 33 ▶ carp "cannot process an element in markup [$element]"
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 33 33
97 if(!$method);
98  
99 33 return $method;
100 }
101  
102 1 ▶sub _create_parent_from_arrayref {
Subroutine _create_parent_from_arrayref: called CC=3 SCAR=11.3
103 1 my $class = shift;
104 1 my %args = @_;
105 my( $element )
106 1 = @args{qw/element/};
107  
108 1 my $root = XML::LibXML::Element->new("");
109 # save attribute_title
110 1 my $attribute_title = $element->[0];
111  
112 1 $class->_from_array( parent => $root, element => $element );
113  
114 1 my $parent = $root->getChildrenByTagName( $attribute_title );
115  
116 1 ▶ die "could not create parent node from ARRAYREF named ".$element->[0]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parent and $parent->[0] 0 1 1
Condition33%1/3
ref $parent and $parent->[0]
!ll&&!rl&&r
001
MC/DC0%0/2
ref $parent and $parent->[0]
ref $parent $parent->[0]
Truth table33%1/3
A and B
A= ref $parentB= $parent->[0]
ABresult
0X0
100
111
117 unless ( (ref $parent ) && $parent->[0] );
118  
119 # return 1st node found
120 1 return $parent->[0];
121 }
122  
123 40 ▶sub _create_element{
Subroutine _create_element: called CC=2 SCAR=6.9
124 40 my $class =shift;
125 40 ▶ return XML::LibXML::Element->new( shift || "" )
Condition100%2/2
shift() || ''
l!l
382
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126 }
127  
128 10 ▶sub _from_hash {
Subroutine _from_hash: called CC=4 SCAR=13.9
129 10 my ( $class, %args ) = @_;
130 my ( $parent, $element ) =
131 10 @args{ qw( parent element ) };
132  
133 10 foreach my $attribute ( keys %$element ) {
134 10 my $value = $element->{$attribute};
135 10 my $obj = $class->_create_element($attribute);
136  
137 10 ▶ if( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 10 0 10
138 10 ▶ if( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 1 9 10
139 # kick back to generate
140 1 $class->_generate( parent => $obj, data => [ $value ] );
141 }
142 else {
143 9 $obj->appendText( $value );
144 }
145 }
146 10 $parent->addChild( $obj );
147 }
148 }
149  
150 15 ▶sub _from_array {
Subroutine _from_array: called CC=3 SCAR=11.0
151 15 my ( $class, %args ) = @_;
152 my ( $parent, $array ) =
153 15 @args{ qw( parent element ) };
154  
155 15 my $node = $class->_create_element( shift @$array );
156  
157 15 ▶ while( my $attribute = shift @$array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute = shift @$array) 25 15 40
158 25 ▶ if( ref $attribute ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute) { } 11 14 25
159 11 $class->_generate( parent => $node, data => [ $attribute ] );
160 }
161 else {
162 # next element in array becomes attribute value
163 14 $node->setAttribute( $attribute, shift @$array );
164 }
165 }
166 15 $parent->addChild( $node );
167 }
168  
169 0 ▶sub _from_libxml {
Subroutine _from_libxml: not called CC=1 SCAR=6.9
170 0 my ( $class, %args ) = @_;
171 my ( $parent, $element ) =
172 0 @args{ qw( parent element ) };
173 0 $parent->addChild( $element );
174 }
175  
176 9 ▶sub _from_scalar {
Subroutine _from_scalar: called CC=7 SCAR=29.2
177 9 my ( $class, %args ) = @_;
178 my ( $parent, $element ) =
179 9 @args{ qw( parent element ) };
180  
181 9 ▶ if ( $$element ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$element) 9 0 9
182 9 my $string = $$element;
183 # removed doubly encoded entites
184 # et al XML::DoubleEncodedEntities et al XML::Tiny
185 9 ▶ if($string =~ /&(amp|lt|gt|quot|apos);/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /&(amp|lt|gt|quot|apos);/) 0 9 9
186 0 $string =~ s/&(lt;|gt;|quot;|apos;|amp;)/
187 0 ▶ $1 eq 'lt;' ? '<' :
Branch: 4 branches
BranchTrueFalseTotal
$1 eq 'lt;' ? : 0 0 0
$1 eq 'gt;' ? : 0 0 0
$1 eq 'apos;' ? : 0 0 0
$1 eq 'quot;' ? : 0 0 0
188 $1 eq 'gt;' ? '>' :
189 $1 eq 'apos;' ? "'" :
190 $1 eq 'quot;' ? '"' :
191 '&'
192 /ge;
193 0 $element = \$string;
194 }
195 9 $parent->appendText( $$element );
196 }
197 }
198 4 1;
199  
200 __END__
↑ Summary