blib/lib/XML/Struct/Writer.pm

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