blib/lib/XML/Struct/Writer.pm

filter: Statementstmt 95.8% 93 / 97 Branchbran 82.6% 38 / 46 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 23 / 23 Podpod 100% 8 / 8 totaltotal 91.9% 182 / 198 SCAR 34.9
CC
32
Cov
91%
microXML
17.5
writeElement
5.0
writeStart
4.0
CRAP 32.7
« blib/lib/XML/Struct/Reader.pm ↑ Summary blib/lib/XML/Struct/Simple.pm »
1 package XML::Struct::Writer;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use XML::LibXML::SAX::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use XML::Struct::Writer::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our $VERSION = '0.24';
11  
12 11 ▶has attributes => (is => 'rw', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 14 ▶has encoding => (is => 'rw', default => sub { 'UTF-8' });
Subroutine __ANON__: called CC=1 SCAR=0.0
14 14 ▶has version => (is => 'rw', default => sub { '1.0' });
Subroutine __ANON__: called CC=1 SCAR=0.0
15 7 has standalone => (is => 'rw');
16 12 ▶has pretty => (is => 'rw', default => sub { 0 }); # 0|1|2
Subroutine __ANON__: called CC=1 SCAR=0.0
17 6 ▶has xmldecl => (is => 'rw', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 has to => (
20 is => 'rw',
21 coerce => sub {
22 12 ▶ if (!ref $_[0]) {
Subroutine __ANON__: called CC=3 SCAR=13.3
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[0]) { } 0 12 12
elsif (builtin::reftype($_[0]) eq 'SCALAR') { } 12 0 12
23 0 return IO::File->new($_[0], 'w');
24 } elsif (reftype($_[0]) eq 'SCALAR') {
25 12 open my $io,">:utf8",$_[0];
26 12 return $io;
27 } else { # IO::Handle, GLOB, ...
28 0 return $_[0];
29 }
30 },
31 12 ▶ trigger => sub { delete $_[0]->{handler} }
Subroutine __ANON__: called CC=1 SCAR=0.0
32 7 );
33  
34 has handler => (
35 is => 'lazy',
36 builder => sub {
37 7 ▶ $_[0]->to ? XML::Struct::Writer::Stream->new(
Subroutine _build_handler: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->to ? : 7 0 7
$_[0]->to ? : 5 2 7
38 fh => $_[0]->to,
39 encoding => $_[0]->encoding,
40 version => $_[0]->version,
41 pretty => $_[0]->pretty,
42 ) : XML::LibXML::SAX::Builder->new( handler => $_[0] );
43 },
44 7 );
45  
46 sub write {
47 17 ▶ my ($self, $element, $name) = @_;
Subroutine write: called CC=3 SCAR=11.0
Pod documented
48  
49 17 $self->writeStart;
50 17 ▶ $self->writeElement(
Condition100%2/2
$name // "root"
l!l
116
MC/DC100%1/1
$name // "root"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
51 $self->microXML($element, $name // 'root')
52 );
53 17 $self->writeEnd;
54
55 17 ▶ $self->handler->can('result') ? $self->handler->result : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->handler->can('result') ? : 5 12 17
56 }
57  
58 7 *writeDocument = \&write;
59  
60 # TODO: Make available as function in XML::Struct or XML::Struct::Simple
61 sub microXML {
62 48 ▶ my ($self, $element, $name) = @_;
Subroutine microXML: called CC=17 SCAR=28.6
Pod documented
63  
64 48 my $type = reftype($element);
65 48 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 48 0 48
66 # MicroXML
67 48 ▶ if ($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 34 14 48
elsif ($type eq 'HASH') { } 14 0 14
68 34 ▶ if (@$element == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$element == 1) { } 2 32 34
elsif (@$element == 2) { } 10 22 32
69 2 return $element;
70 } elsif (@$element == 2) {
71 10 ▶ if ( (reftype($element->[1]) // '') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((builtin::reftype($element->[1]) // '') eq 'ARRAY') { } 7 3 10
elsif (not $self->attributes and %{$element->[1];}) { } 1 2 3
Condition50%1/2
builtin::reftype($element->[1]) // ""
l!l
100
MC/DC0%0/1
builtin::reftype($element->[1]) // ""
builtin::reftype($element->[1])
Truth table50%1/2
A
A= builtin::reftype($element->[1])
Aresult
11
00
72 7 return [ $element->[0], {}, $element->[1] ];
73 1 ▶ } elsif (!$self->attributes and %{$element->[1]}) {
Condition66%2/3
not $self->attributes and %{$element->[1];}
!ll&&!rl&&r
201
MC/DC50%1/2
not $self->attributes and %{$element->[1];}
not $self->attributes %{$element->[1];}
Truth table66%2/3
not(A) and B
A= not $self->attributesB= %{$element->[1];}
ABresult
0X0
100
111
74 1 return [ $element->[0] ];
75 } else {
76 2 return $element;
77 }
78 } else {
79 22 ▶ if (!$self->attributes and %{$element->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->attributes and %{$element->[1];}) { } 1 21 22
Condition100%3/3
not $self->attributes and %{$element->[1];}
!ll&&!rl&&r
1831
MC/DC100%2/2
not $self->attributes and %{$element->[1];}
not $self->attributes %{$element->[1];}
Truth table100%3/3
not(A) and B
A= not $self->attributesB= %{$element->[1];}
ABresult
0X0
100
111
80 1 return [ $element->[0], {}, $element->[2] ];
81 } else {
82 21 return $element;
83 }
84 }
85 # SimpleXML
86 } elsif ($type eq 'HASH') {
87 my $children = [
88 map {
89 14 my ($tag, $content) = ($_, $element->{$_});
90 # text
91 14 ▶ if (!ref $content) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $content) { } 2 12 14
elsif (builtin::reftype($content) eq 'ARRAY') { } 6 6 12
elsif (builtin::reftype($content) eq 'HASH') { } 6 0 6
92 2 [ $tag, {}, [$content] ]
93 } elsif (reftype($content) eq 'ARRAY') {
94 @$content
95 6 ▶ ? map { [ $tag, {}, [$_] ] } @$content
Branch: 1 branch
BranchTrueFalseTotal
@$content ? : 4 2 6
96 : [ $tag ];
97 } elsif (reftype $content eq 'HASH' ) {
98 6 [ $tag, {}, [ $content ] ];
99 } else {
100 0 ();
101 }
102 }
103 14 grep { defined $element->{$_} }
104 sort keys %$element
105 ];
106 14 ▶ return $name ? [ $name, {}, $children ] : @$children;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 5 9 14
107 }
108 }
109  
110 0 croak "expected XML as ARRAY or HASH reference";
111 }
112  
113 sub writeElement {
114 48 ▶ my $self = shift;
Subroutine writeElement: called CC=5 SCAR=16.1
Pod documented
115
116 48 foreach my $element (@_) {
117 46 $self->writeStartElement($element);
118 46 ▶ foreach my $child ( @{ $element->[2] // [] } ) {
Condition100%2/2
$element->[2] // []
l!l
406
MC/DC100%1/1
$element->[2] // []
$element->[2]
Truth table100%2/2
A
A= $element->[2]
Aresult
11
00
119 56 ▶ if (ref $child) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) { } 31 25 56
120 31 $self->writeElement( $self->microXML($child) );
121 } else {
122 25 $self->writeCharacters($child);
123 }
124 }
125  
126 46 $self->writeEndElement($element);
127 }
128 }
129  
130 sub writeStartElement {
131 46 ▶ my ($self, $element) = @_;
Subroutine writeStartElement: called CC=2 SCAR=6.9
Pod documented
132  
133 46 my $args = { Name => $element->[0] };
134 46 ▶ $args->{Attributes} = $element->[1] if $element->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $element->[1] 42 4 46
135  
136 46 $self->handler->start_element($args);
137 }
138  
139 sub writeEndElement {
140 46 ▶ my ($self, $element) = @_;
Subroutine writeEndElement: called CC=1 SCAR=0.0
Pod documented
141 46 $self->handler->end_element({ Name => $element->[0] });
142 }
143  
144 sub writeCharacters {
145 25 ▶ $_[0]->handler->characters({ Data => $_[1] });
Subroutine writeCharacters: called CC=1 SCAR=0.0
Pod documented
146 }
147  
148 sub writeStart {
149 17 ▶ my $self = shift;
Subroutine writeStart: called CC=4 SCAR=13.9
Pod documented
150 17 $self->handler->start_document;
151 17 ▶ if ($self->handler->can('xml_decl') && $self->xmldecl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->handler->can("xml_decl") and $self->xmldecl) 7 10 17
Condition100%3/3
$self->handler->can("xml_decl") and $self->xmldecl
!ll&&!rl&&r
197
MC/DC100%2/2
$self->handler->can("xml_decl") and $self->xmldecl
$self->handler->can("xml_decl") $self->xmldecl
Truth table100%3/3
A and B
A= $self->handler->can("xml_decl")B= $self->xmldecl
ABresult
0X0
100
111
152 7 $self->handler->xml_decl({
153 Version => $self->version,
154 Encoding => $self->encoding,
155 Standalone => $self->standalone,
156 });
157 }
158 17 ▶ $self->writeStartElement(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 17 17
159 }
160  
161 sub writeEnd {
162 17 ▶ my $self = shift;
Subroutine writeEnd: called CC=2 SCAR=7.1
Pod documented
163 17 ▶ $self->writeEndElement(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 17 17
164 17 $self->handler->end_document;
165 }
166  
167 7 1;
168 __END__
« blib/lib/XML/Struct/Reader.pm ↑ Summary blib/lib/XML/Struct/Simple.pm »