blib/lib/XML/Struct/Writer/Stream.pm

filter: Statementstmt 95.3% 82 / 86 Branchbran 83.3% 25 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 6 totaltotal 88.3% 121 / 137 SCAR 29.0
CC
18
Cov
92%
start_element
9.0
end_element
5.0
characters
3.1
CRAP 18.1
« blib/lib/XML/Struct/Simple.pm ↑ Summary blib/lib/XML/Struct.pm »
1 package XML::Struct::Writer::Stream;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '0.26';
6  
7 0 ▶has fh => (is => 'rw', default => sub { *STDOUT });
Subroutine __ANON__: not called CC=1 SCAR=1.2
8 7 has pretty => (is => 'rw');
9  
10 7 our %ESCAPE = (
11 '&' => '&',
12 '<' => '&lt;',
13 '>' => '&gt;',
14 '"' => '&quot;',
15 );
16  
17 use constant {
18 7 DOCUMENT_STARTED => 0,
19 TAG_STARTED => 1,
20 CHAR_CONTENT => 2,
21 CHILD_ELEMENT => 3,
22 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub xml_decl {
25 3 ▶ my ($self, $data) = @_;
Subroutine xml_decl: called CC=3 SCAR=11.2
Pod undocumented
26  
27 3 my $xml = "<?xml version=\"$data->{Version}\"";
28 3 ▶ $xml .= " encoding=\"$data->{Encoding}\"" if $data->{Encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"Encoding"} 3 0 3
29 3 ▶ $xml .= " standalone=\"$data->{Standalone}\"" if $data->{Standalone};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"Standalone"} 0 3 3
30 3 $xml .= "?>\n";
31  
32 3 print {$self->fh} $xml;
33 }
34  
35 sub start_document {
36 12 ▶ my ($self) = @_;
Subroutine start_document: called CC=1 SCAR=0.0
Pod undocumented
37 12 $self->{_stack} = [];
38 12 $self->{_status} = DOCUMENT_STARTED;
39 }
40  
41 sub start_element {
42 36 ▶ my ($self, $data) = @_;
Subroutine start_element: called CC=9 SCAR=22.0
Pod undocumented
43  
44 36 my $tag = $data->{Name};
45 36 my $attr = $data->{Attributes};
46 36 my $xml = "<$tag";
47  
48 36 ▶ if ($self->{_status} == TAG_STARTED) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'_status'} == 1) { } 15 21 36
elsif ($self->{'_status'} == 3) { } 7 14 21
elsif ($self->{'_status'} == 2) { } 2 12 14
49 15 print {$self->fh} '>';
50 15 ▶ if ($self->pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pretty) 1 14 15
51 1 print {$self->fh} "\n".(' ' x (scalar @{$self->{_stack}}));
52 }
53 } elsif ($self->{_status} == CHILD_ELEMENT) {
54 7 ▶ if ($self->pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pretty) 1 6 7
55 1 print {$self->fh} "\n".(' ' x (scalar @{$self->{_stack}}));
56 }
57 } elsif ($self->{_status} == CHAR_CONTENT) {
58 2 print {$self->fh} $self->{_chars};
59 } # else: DOCUMENT_STARTED
60  
61 36 push @{$self->{_stack}}, $tag;
62  
63 36 ▶ if ($attr && %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr and %$attr) 6 30 36
Condition100%3/3
$attr and %$attr
!ll&&!rl&&r
3276
MC/DC100%2/2
$attr and %$attr
$attr %$attr
Truth table100%3/3
A and B
A= $attrB= %$attr
ABresult
0X0
100
111
64 6 foreach my $key (sort keys %$attr) {
65 6 my $value = $attr->{$key};
66 6 $value =~ s/([&<>"'])/$ESCAPE{$1}/geo;
67 6 $xml .= " $key=\"$value\"";
68 }
69 }
70  
71 36 $self->{_status} = TAG_STARTED;
72  
73 36 print {$self->fh} $xml;
74 }
75  
76 sub end_element {
77 36 ▶ my ($self) = @_;
Subroutine end_element: called CC=5 SCAR=16.1
Pod undocumented
78  
79 36 ▶ my $tag = pop @{$self->{_stack}} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tag = pop @{$$self{"_stack"};} or return 0 36 36
80  
81 36 ▶ if ($self->{_status} == TAG_STARTED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_status'} == 1) { } 7 29 36
elsif ($self->{'_status'} == 2) { } 12 17 29
82 7 print {$self->fh} '/>';
83 } elsif ($self->{_status} == CHAR_CONTENT) {
84 12 print {$self->fh} $self->{_chars} . "</$tag>";
85 12 $self->{_chars} = "";
86 } else { # CHILD_ELEMENT
87 17 ▶ if ($self->pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pretty) 1 16 17
88 1 print {$self->fh} "\n".(' ' x (scalar @{$self->{_stack}}));
89 }
90 17 print {$self->fh} "</$tag>";
91 }
92  
93 36 $self->{_status} = CHILD_ELEMENT;
94 }
95  
96 sub characters {
97 16 ▶ my ($self, $data) = @_;
Subroutine characters: called CC=3 SCAR=11.2
Pod undocumented
98  
99 16 my $xml = $data->{Data};
100 16 $xml =~ s/([&<>])/$ESCAPE{$1}/geo;
101  
102 16 ▶ if ($self->{_status} == TAG_STARTED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_status'} == 1) { } 14 2 16
elsif ($self->{'_status'} == 3) { } 2 0 2
103 14 print {$self->fh} '>';
104 14 $self->{_status} = CHAR_CONTENT;
105 14 $self->{_chars} = $xml;
106 } elsif ($self->{_status} == CHILD_ELEMENT) {
107 2 print {$self->fh} $xml;
108 } else {
109 0 $self->{_chars} .= $xml;
110 }
111 }
112  
113 sub end_document {
114 12 ▶ my ($self) = @_;
Subroutine end_document: called CC=2 SCAR=7.0
Pod undocumented
115 12 ▶ $self->end_element while @{$self->{_stack}};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"_stack"};} and do { $self->end_element } 0 12 12
116 12 print {$self->fh} "\n";
117 }
118  
119  
120 7 1;
121 __END__
« blib/lib/XML/Struct/Simple.pm ↑ Summary blib/lib/XML/Struct.pm »