| 1 | package Data::Stag::XMLWriter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Data::Stag::XMLWriter - writes stag events into XML files | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =cut | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 PUBLIC METHODS - | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use base qw(Data::Stag::Writer); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | use Data::Stag::Util qw(rearrange); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 14 | $VERSION="0.14"; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 663 | ▶ | sub unclosed { | ||||||||||||||||||||||||||
|
Subroutine
unclosed: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 663 | my $self = shift; | |||||||||||||||||||||||||||
| 27 | 663 | ▶ | $self->{_unclosed} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 663 | return $self->{_unclosed}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1768 | ▶ | sub in_attr { | ||||||||||||||||||||||||||
|
Subroutine
in_attr: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 1768 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 1768 | ▶ | $self->{_in_attr} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1768 | return $self->{_in_attr}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1768 | ▶ | sub ensure_closed { | ||||||||||||||||||||||||||
|
Subroutine
ensure_closed: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 1768 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 1768 | ▶ | if ($self->{_unclosed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 663 | $self->o(">") ; | |||||||||||||||||||||||||||
| 42 | 663 | $self->{_unclosed} = 0; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 1768 | return; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub fmtstr { | ||||||||||||||||||||||||||
|
Subroutine
fmtstr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | return 'xml'; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 884 | ▶ | sub indent_txt { | ||||||||||||||||||||||||||
|
Subroutine
indent_txt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 884 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 884 | my $stack = $self->stack; | |||||||||||||||||||||||||||
| 54 | 884 | return " " x scalar(@$stack); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub this_line { | ||||||||||||||||||||||||||
|
Subroutine
this_line: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | $self->{_this_line} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | return $self->{_this_line}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2708 | ▶ | sub o { | ||||||||||||||||||||||||||
|
Subroutine
o: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 2708 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 2708 | my $o = "@_"; | |||||||||||||||||||||||||||
| 66 | 2708 | $self->addtext( $o ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 28 | ▶ | sub first_line { | ||||||||||||||||||||||||||
|
Subroutine
first_line: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 28 | ▶ | $self->{_first_line} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 28 | return $self->{_first_line}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 663 | ▶ | sub start_event { | ||||||||||||||||||||||||||
|
Subroutine
start_event: called CC=8 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 663 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 663 | my $ev = shift; | |||||||||||||||||||||||||||
| 79 | 663 | ▶ | if (!defined($ev)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $ev = ''; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 663 | my $stack = $self->stack; | |||||||||||||||||||||||||||
| 83 | 663 | ▶ | if (!@$stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 28 | ▶ | $self->o($self->first_line || "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->first_line || '<?xml version="1.0" encoding="UTF-8"?>'
MC/DC0%0/1
$self->first_line || '<?xml version="1.0" encoding="UTF-8"?>'
$self->first_line
Truth table50%1/2
A
A= $self->first_line
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 663 | ▶ | if ($ev eq '@') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->in_attr(1); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif ($ev eq '.') { | ||||||||||||||||||||||||||||
| 91 | # pcdata for element with attributes | ||||||||||||||||||||||||||||
| 92 | # do nothing | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif ($self->in_attr) { | ||||||||||||||||||||||||||||
| 95 | 0 | $self->o(" $ev="); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | elsif ($ev eq '') { | ||||||||||||||||||||||||||||
| 98 | # ignore null nodes | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | 663 | $self->ensure_closed; | |||||||||||||||||||||||||||
| 102 | 663 | $self->o("\n". $self->indent_txt . "<$ev"); | |||||||||||||||||||||||||||
| 103 | 663 | $self->unclosed(1); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 663 | push(@$stack, $ev); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 663 | ▶ | sub end_event { | ||||||||||||||||||||||||||
|
Subroutine
end_event: called CC=10 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 663 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 663 | my $ev = shift; | |||||||||||||||||||||||||||
| 111 | 663 | my $stack = $self->stack; | |||||||||||||||||||||||||||
| 112 | 663 | my $popped = pop(@$stack); | |||||||||||||||||||||||||||
| 113 | 663 | ▶ | return '' unless $popped; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 663 | ▶ | if ($ev && $popped ne $ev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ev and $popped ne $ev
MC/DC0%0/2
$ev and $popped ne $ev
$ev
$popped ne $ev
Truth table33%1/3
A and B
A= $evB= $popped ne $ev
| |||||||||||||||||||||||||||||
| 115 | 0 | warn("uh oh; $ev ne $popped"); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 663 | ▶ | if (!$ev) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $ev = $popped; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 663 | ▶ | if ($self->in_attr) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($ev eq '@') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $self->in_attr(0); | |||||||||||||||||||||||||||
| 123 | 0 | $self->ensure_closed; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | elsif ($ev eq '.') { | ||||||||||||||||||||||||||||
| 127 | # end of pcdata for element with attributes | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 663 | $self->ensure_closed; | |||||||||||||||||||||||||||
| 131 | 663 | ▶ | if ($self->{_nl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 221 | $self->o("\n" . $self->indent_txt) | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 663 | $self->o("</$ev>"); | |||||||||||||||||||||||||||
| 135 | 663 | $self->{_nl} = 1; | |||||||||||||||||||||||||||
| 136 | 663 | ▶ | if (!@$stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 28 | $self->o("\n"); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 663 | return $ev; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 442 | ▶ | sub evbody { | ||||||||||||||||||||||||||
|
Subroutine
evbody: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 442 | my $self = shift; | |||||||||||||||||||||||||||
| 144 | 442 | my $body = shift; | |||||||||||||||||||||||||||
| 145 | 442 | my $str= xmlesc($body); | |||||||||||||||||||||||||||
| 146 | 442 | ▶ | if ($self->in_attr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $self->o("\"$str\""); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | 442 | $self->ensure_closed; | |||||||||||||||||||||||||||
| 151 | 442 | $self->{_nl} = 0; | |||||||||||||||||||||||||||
| 152 | 442 | $self->o($str); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 442 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 14 | our $escapes = { '&' => '&', | |||||||||||||||||||||||||||
| 158 | '<' => '<', | ||||||||||||||||||||||||||||
| 159 | '>' => '>', | ||||||||||||||||||||||||||||
| 160 | '"' => '"' | ||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 442 | ▶ | sub xmlesc { | ||||||||||||||||||||||||||
|
Subroutine
xmlesc: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 442 | my $w = shift; | |||||||||||||||||||||||||||
| 165 | 442 | ▶ | if (!defined $w) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | $w = ''; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | $w =~ s/([\&\<\>])/$escapes->{$1}/ge; | |||||||||||||||||||||||||||
| 169 | 442 | $w; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 14 | 1; | |||||||||||||||||||||||||||