blib/lib/XML/Writer.pm

filter: Statementstmt 100% 651 / 651 Branchbran 100% 212 / 212 Conditioncond 100% 99 / 99 MC/DCmcdc 100% 58 / 58 Subroutinesub 100% 100 / 100 Podpod 100% 27 / 27 totaltotal 100% 1147 / 1147 CC 144 SCAR 15.1
CC
144
Cov
100%
__ANON__
12.0
__ANON__
10.0
_checkNSNames
9.0
CRAP 4.5
↑ Summary
1 ########################################################################
2 # Writer.pm - write an XML document.
3 # Copyright (c) 1999 by Megginson Technologies.
4 # Copyright (c) 2003 Ed Avis <ed@membled.com>
5 # Copyright (c) 2004-2010 Joseph Walton <joe@kafsemo.org>
6 # Redistribution and use in source and compiled forms, with or without
7 # modification, are permitted under any circumstances. No warranty.
8 ########################################################################
9  
10 package XML::Writer;
11  
12 3 require 5.004;
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 $VERSION = "0.900";
19  
20 3 ▶use overload '""' => \&_overload_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22
23 ########################################################################
24 # Constructor.
25 ########################################################################
26  
27 #
28 # Public constructor.
29 #
30 # This actually does most of the work of the module: it defines closures
31 # for all of the real processing, and selects the appropriate closures
32 # to use based on the value of the UNSAFE parameter. The actual methods
33 # are just stubs.
34 #
35 329 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
36 329 my ($class, %params) = (@_);
37  
38 # If the user wants namespaces,
39 # intercept the request here; it will
40 # come back to this constructor
41 # from within XML::Writer::Namespaces::new()
42 329 ▶ if ($params{NAMESPACES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"NAMESPACES"}) 159 170 329
43 159 delete $params{NAMESPACES};
44 159 return XML::Writer::Namespaces->new(%params);
45 }
46  
47 # Set up $self and basic parameters
48 170 my $self;
49 my $output;
50 170 my $unsafe = $params{UNSAFE};
51 170 my $newlines = $params{NEWLINES};
52 170 my $dataMode = $params{DATA_MODE};
53 170 my $dataIndent;
54 my $selfcontained_output;
55 170 my $use_selfcontained_output = 0;
56  
57 # If the NEWLINES parameter is specified,
58 # set the $nl variable appropriately
59 170 my $nl = '';
60 170 ▶ if ($newlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newlines) 1 169 170
61 1 $nl = "\n";
62 }
63  
64 170 ▶ my $outputEncoding = $params{ENCODING} || "";
Condition100%2/2
$params{'ENCODING'} || ''
l!l
23147
MC/DC100%1/1
$params{'ENCODING'} || ''
$params{'ENCODING'}
Truth table100%2/2
A
A= $params{'ENCODING'}
Aresult
11
00
65 170 my ($checkUnencodedRepertoire, $escapeEncoding);
66 170 ▶ if (lc($outputEncoding) eq 'us-ascii') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $outputEncoding eq 'us-ascii') { } 15 155 170
67 15 $checkUnencodedRepertoire = \&_croakUnlessASCII;
68 15 $escapeEncoding = \&_escapeASCII;
69 } else {
70 155 ▶ my $doNothing = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
71 155 $checkUnencodedRepertoire = $doNothing;
72 155 $escapeEncoding = $doNothing;
73 }
74  
75 # Parse variables
76 170 my @elementStack = ();
77 170 my $elementLevel = 0;
78 170 my %seen = ();
79  
80 170 my $hasData = 0;
81 170 my @hasDataStack = ();
82 170 my $hasElement = 0;
83 170 my @hasElementStack = ();
84 170 my $hasHeading = 0; # Does this document have anything before the first element?
85  
86 #
87 # Private method to show attributes.
88 #
89 186 ▶ my $showAttributes = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
90 186 my $atts = $_[0];
91 186 my $i = 1;
92 186 ▶ while ($atts->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts->[$i]) 72 186 258
93 72 my $aname = $atts->[$i++];
94 72 my $value = _escapeLiteral($atts->[$i++]);
95 72 $value =~ s/\x0a/\&#10\;/g;
96 72 $value =~ s/\x0d/\&#13\;/g;
97 72 $value =~ s/\x09/\&#9\;/g;
98 72 &{$escapeEncoding}($value);
99 72 $output->print(" $aname=\"$value\"");
100 }
101 170 };
102  
103 # Method implementations: the SAFE_
104 # versions perform error checking
105 # and then call the regular ones.
106 88 ▶ my $end = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
107 88 $output->print("\n");
108  
109 88 ▶ return $selfcontained_output
Branch: 1 branch
BranchTrueFalseTotal
if $use_selfcontained_output and defined wantarray 1 87 88
Condition100%3/3
$use_selfcontained_output and defined wantarray
!ll&&!rl&&r
8611
MC/DC100%2/2
$use_selfcontained_output and defined wantarray
$use_selfcontained_output defined wantarray
Truth table100%3/3
A and B
A= $use_selfcontained_outputB= defined wantarray
ABresult
0X0
100
111
110 if $use_selfcontained_output and defined wantarray;
111 170 };
112  
113 80 ▶ my $SAFE_end = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
114 80 ▶ if (!$seen{ELEMENT}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $seen{'ELEMENT'}) { } 1 79 80
elsif ($elementLevel > 0) { } 1 78 79
115 1 croak("Document cannot end without a document element");
116 } elsif ($elementLevel > 0) {
117 1 croak("Document ended with unmatched start tag(s): @elementStack");
118 } else {
119 78 @elementStack = ();
120 78 $elementLevel = 0;
121 78 %seen = ();
122 78 &{$end};
123 }
124 170 };
125  
126 22 ▶ my $xmlDecl = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
127 22 my ($encoding, $standalone) = (@_);
128 22 ▶ if ($standalone && $standalone ne 'no') {
Branch: 1 branch
BranchTrueFalseTotal
if ($standalone and $standalone ne "no") 1 21 22
Condition100%3/3
$standalone and $standalone ne "no"
!ll&&!rl&&r
2011
MC/DC100%2/2
$standalone and $standalone ne "no"
$standalone $standalone ne "no"
Truth table100%3/3
A and B
A= $standaloneB= $standalone ne "no"
ABresult
0X0
100
111
129 1 $standalone = 'yes';
130 }
131  
132 # Only include an encoding if one has been explicitly supplied,
133 # either here or on construction. Allow the empty string
134 # to suppress it.
135 22 ▶ if (!defined($encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $encoding) 18 4 22
136 18 $encoding = $outputEncoding;
137 }
138 22 $output->print("<?xml version=\"1.0\"");
139 21 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 7 14 21
140 7 $output->print(" encoding=\"$encoding\"");
141 }
142 21 ▶ if ($standalone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($standalone) 2 19 21
143 2 $output->print(" standalone=\"$standalone\"");
144 }
145 21 $output->print("?>\n");
146 21 $hasHeading = 1;
147 170 };
148  
149 23 ▶ my $SAFE_xmlDecl = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 23 ▶ if ($seen{ANYTHING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{'ANYTHING'}) { } 2 21 23
151 2 croak("The XML declaration is not the first thing in the document");
152 } else {
153 21 $seen{ANYTHING} = 1;
154 21 $seen{XMLDECL} = 1;
155 21 &{$xmlDecl};
156 }
157 170 };
158  
159 8 ▶ my $pi = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
160 8 my ($target, $data) = (@_);
161 8 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 3 5 8
162 3 $output->print("<?$target $data?>");
163 } else {
164 5 $output->print("<?$target?>");
165 }
166 8 ▶ if ($elementLevel == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elementLevel == 0) 7 1 8
167 7 $output->print("\n");
168 7 $hasHeading = 1;
169 }
170 170 };
171  
172 11 ▶ my $SAFE_pi = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
173 11 my ($name, $data) = (@_);
174 11 $seen{ANYTHING} = 1;
175 11 ▶ if (($name =~ /^xml/i) && ($name !~ /^xml-(stylesheet|model)$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^xml/i and not $name =~ /^xml-(stylesheet|model)$/i) 1 10 11
Condition100%3/3
$name =~ /^xml/i and not $name =~ /^xml-(stylesheet|model)$/i
!ll&&!rl&&r
821
MC/DC100%2/2
$name =~ /^xml/i and not $name =~ /^xml-(stylesheet|model)$/i
$name =~ /^xml/i not $name =~ /^xml-(stylesheet|model)$/i
Truth table100%3/3
A and not(B)
A= $name =~ /^xml/iB= not $name =~ /^xml-(stylesheet|model)$/i
ABresult
0X0
100
111
176 1 carp("Processing instruction target begins with 'xml'");
177 }
178  
179 11 ▶ if ($name =~ /\?\>/ || (defined($data) && $data =~ /\?\>/)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /\?\>/ or defined $data and $data =~ /\?\>/) { } 2 9 11
elsif ($name =~ /\s/) { } 1 8 9
Condition100%6/6
$name =~ /\?\>/ or defined $data and $data =~ /\?\>/
l!l&&r!l&&!r
119
defined $data and $data =~ /\?\>/
!ll&&!rl&&r
541
MC/DC100%3/3
$name =~ /\?\>/ or defined $data and $data =~ /\?\>/
$name =~ /\?\>/ defined $data $data =~ /\?\>/
Truth table100%4/4
A or B and C
A= $name =~ /\?\>/B= defined $dataC= $data =~ /\?\>/
ABCresult
1XX1
0111
00X0
0100
180 2 croak("Processing instruction may not contain '?>'");
181 } elsif ($name =~ /\s/) {
182 1 croak("Processing instruction name may not contain whitespace");
183 } else {
184 8 &{$pi};
185 }
186 170 };
187  
188 14 ▶ my $comment = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
189 14 my $data = $_[0];
190 14 ▶ if ($dataMode && $elementLevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode and $elementLevel) 5 9 14
Condition100%3/3
$dataMode and $elementLevel
!ll&&!rl&&r
545
MC/DC100%2/2
$dataMode and $elementLevel
$dataMode $elementLevel
Truth table100%3/3
A and B
A= $dataModeB= $elementLevel
ABresult
0X0
100
111
191 5 $output->print("\n");
192 5 $output->print($dataIndent x $elementLevel);
193 }
194 14 $output->print("<!-- $data -->");
195 14 ▶ if ($dataMode && $elementLevel) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dataMode and $elementLevel) { } 5 9 14
elsif ($elementLevel == 0) { } 8 1 9
Condition100%3/3
$dataMode and $elementLevel
!ll&&!rl&&r
545
MC/DC100%2/2
$dataMode and $elementLevel
$dataMode $elementLevel
Truth table100%3/3
A and B
A= $dataModeB= $elementLevel
ABresult
0X0
100
111
196 5 $hasElement = 1;
197 } elsif ($elementLevel == 0) {
198 8 $output->print("\n");
199 8 $hasHeading = 1;
200 }
201 170 };
202  
203 14 ▶ my $SAFE_comment = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
204 14 my $data = $_[0];
205 14 ▶ if ($data =~ /--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /--/) 2 12 14
206 2 carp("Interoperability problem: \"--\" in comment text");
207 }
208  
209 14 ▶ if ($data =~ /-->/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /-->/) { } 1 13 14
210 1 croak("Comment may not contain '-->'");
211 } else {
212 13 &{$checkUnencodedRepertoire}($data);
213 12 $seen{ANYTHING} = 1;
214 12 &{$comment};
215 }
216 170 };
217  
218 10 ▶ my $doctype = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
219 10 my ($name, $publicId, $systemId) = (@_);
220 10 $output->print("<!DOCTYPE $name");
221 10 ▶ if ($publicId) {
Branch: 2 branches
BranchTrueFalseTotal
if ($publicId) { } 5 5 10
elsif (defined $systemId) { } 2 3 5
222 5 ▶ unless ( defined $systemId) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $systemId) 2 3 5
223 2 croak("A DOCTYPE declaration with a public ID must also have a system ID");
224 }
225 3 $output->print(" PUBLIC \"$publicId\" \"$systemId\"");
226 } elsif ( defined $systemId ) {
227 2 $output->print(" SYSTEM \"$systemId\"");
228 }
229 8 $output->print(">\n");
230 8 $hasHeading = 1;
231 170 };
232  
233 11 ▶ my $SAFE_doctype = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
234 11 my $name = $_[0];
235 11 ▶ if ($seen{DOCTYPE}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seen{'DOCTYPE'}) { } 1 10 11
elsif ($seen{'ELEMENT'}) { } 1 9 10
236 1 croak("Attempt to insert second DOCTYPE declaration");
237 } elsif ($seen{ELEMENT}) {
238 1 croak("The DOCTYPE declaration must come before the first start tag");
239 } else {
240 9 $seen{ANYTHING} = 1;
241 9 $seen{DOCTYPE} = $name;
242 9 &{$doctype};
243 }
244 170 };
245  
246 106 ▶ my $startTag = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
247 106 my $name = $_[0];
248 106 ▶ if ($dataMode && ($hasHeading || $elementLevel)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode and $hasHeading || $elementLevel) 29 77 106
Condition100%6/6
$dataMode and $hasHeading || $elementLevel
!ll&&!rl&&r
631429
$hasHeading or $elementLevel
l!l&&r!l&&!r
151414
MC/DC100%3/3
$dataMode and $hasHeading || $elementLevel
$dataMode $hasHeading $elementLevel
Truth table100%4/4
A and B or C
A= $dataModeB= $hasHeadingC= $elementLevel
ABCresult
0XX0
1000
11X1
1011
249 29 $output->print("\n");
250 29 $output->print($dataIndent x $elementLevel);
251 }
252 106 $elementLevel++;
253 106 push @elementStack, $name;
254 106 $output->print("<$name");
255 105 &{$showAttributes}(\@_);
256 105 $output->print("$nl>");
257 105 ▶ if ($dataMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode) 43 62 105
258 43 $hasElement = 1;
259 43 push @hasDataStack, $hasData;
260 43 $hasData = 0;
261 43 push @hasElementStack, $hasElement;
262 43 $hasElement = 0;
263 }
264 170 };
265  
266 103 ▶ my $SAFE_startTag = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
267 103 my $name = $_[0];
268  
269 103 _croakUnlessValidName($name);
270 103 &{$checkUnencodedRepertoire}($name);
271 103 _checkAttributes(\@_, $checkUnencodedRepertoire);
272  
273 103 ▶ if ($seen{ELEMENT} && $elementLevel == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($seen{'ELEMENT'} and $elementLevel == 0) { } 1 102 103
elsif ($elementLevel == 0 and $seen{'DOCTYPE'} and $name ne $seen{'DOCTYPE'}) { } 1 101 102
elsif ($dataMode and $hasData) { } 2 99 101
Condition100%3/3
$seen{"ELEMENT"} and $elementLevel == 0
!ll&&!rl&&r
66361
MC/DC100%2/2
$seen{"ELEMENT"} and $elementLevel == 0
$seen{"ELEMENT"} $elementLevel == 0
Truth table100%3/3
A and B
A= $seen{"ELEMENT"}B= $elementLevel == 0
ABresult
0X0
100
111
274 1 croak("Attempt to insert start tag after close of document element");
275 ▶ } elsif ($elementLevel == 0 && $seen{DOCTYPE} && $name ne $seen{DOCTYPE}) {
Condition100%6/6
$elementLevel == 0 and $seen{"DOCTYPE"} and $name ne $seen{"DOCTYPE"}
!ll&&!rl&&r
10011
$elementLevel == 0 and $seen{"DOCTYPE"}
!ll&&!rl&&r
36642
MC/DC100%3/3
$elementLevel == 0 and $seen{"DOCTYPE"} and $name ne $seen{"DOCTYPE"}
$elementLevel == 0 $seen{"DOCTYPE"} $name ne $seen{"DOCTYPE"}
Truth table100%4/4
A and B and C
A= $elementLevel == 0B= $seen{"DOCTYPE"}C= $name ne $seen{"DOCTYPE"}
ABCresult
0XX0
10X0
1100
1111
276 croak("Document element is \"$name\", but DOCTYPE is \""
277 . $seen{DOCTYPE}
278 1 . "\"");
279 ▶ } elsif ($dataMode && $hasData) {
Condition100%3/3
$dataMode and $hasData
!ll&&!rl&&r
58412
MC/DC100%2/2
$dataMode and $hasData
$dataMode $hasData
Truth table100%3/3
A and B
A= $dataModeB= $hasData
ABresult
0X0
100
111
280 2 croak("Mixed content not allowed in data mode: element $name");
281 } else {
282 99 $seen{ANYTHING} = 1;
283 99 $seen{ELEMENT} = 1;
284 99 &{$startTag};
285 }
286 170 };
287  
288 81 ▶ my $emptyTag = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
289 81 my $name = $_[0];
290 81 ▶ if ($dataMode && ($hasHeading || $elementLevel)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode and $hasHeading || $elementLevel) 18 63 81
Condition100%6/6
$dataMode and $hasHeading || $elementLevel
!ll&&!rl&&r
62118
$hasHeading or $elementLevel
l!l&&r!l&&!r
6121
MC/DC100%3/3
$dataMode and $hasHeading || $elementLevel
$dataMode $hasHeading $elementLevel
Truth table100%4/4
A and B or C
A= $dataModeB= $hasHeadingC= $elementLevel
ABCresult
0XX0
1000
11X1
1011
291 18 $output->print("\n");
292 18 $output->print($dataIndent x $elementLevel);
293 }
294 81 $output->print("<$name");
295 81 &{$showAttributes}(\@_);
296 81 $output->print("$nl />");
297 81 ▶ if ($dataMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode) 19 62 81
298 19 $hasElement = 1;
299 }
300 170 };
301  
302 93 ▶ my $SAFE_emptyTag = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
303 93 my $name = $_[0];
304  
305 93 _croakUnlessValidName($name);
306 88 &{$checkUnencodedRepertoire}($name);
307 86 _checkAttributes(\@_, $checkUnencodedRepertoire);
308  
309 80 ▶ if ($seen{ELEMENT} && $elementLevel == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($seen{'ELEMENT'} and $elementLevel == 0) { } 1 79 80
elsif ($elementLevel == 0 and $seen{'DOCTYPE'} and $name ne $seen{'DOCTYPE'}) { } 1 78 79
elsif ($dataMode and $hasData) { } 1 77 78
Condition100%3/3
$seen{"ELEMENT"} and $elementLevel == 0
!ll&&!rl&&r
44351
MC/DC100%2/2
$seen{"ELEMENT"} and $elementLevel == 0
$seen{"ELEMENT"} $elementLevel == 0
Truth table100%3/3
A and B
A= $seen{"ELEMENT"}B= $elementLevel == 0
ABresult
0X0
100
111
310 1 croak("Attempt to insert empty tag after close of document element");
311 ▶ } elsif ($elementLevel == 0 && $seen{DOCTYPE} && $name ne $seen{DOCTYPE}) {
Condition100%6/6
$elementLevel == 0 and $seen{"DOCTYPE"} and $name ne $seen{"DOCTYPE"}
!ll&&!rl&&r
7531
$elementLevel == 0 and $seen{"DOCTYPE"}
!ll&&!rl&&r
35404
MC/DC100%3/3
$elementLevel == 0 and $seen{"DOCTYPE"} and $name ne $seen{"DOCTYPE"}
$elementLevel == 0 $seen{"DOCTYPE"} $name ne $seen{"DOCTYPE"}
Truth table100%4/4
A and B and C
A= $elementLevel == 0B= $seen{"DOCTYPE"}C= $name ne $seen{"DOCTYPE"}
ABCresult
0XX0
10X0
1100
1111
312 croak("Document element is \"$name\", but DOCTYPE is \""
313 . $seen{DOCTYPE}
314 1 . "\"");
315 ▶ } elsif ($dataMode && $hasData) {
Condition100%3/3
$dataMode and $hasData
!ll&&!rl&&r
58191
MC/DC100%2/2
$dataMode and $hasData
$dataMode $hasData
Truth table100%3/3
A and B
A= $dataModeB= $hasData
ABresult
0X0
100
111
316 1 croak("Mixed content not allowed in data mode: element $name");
317 } else {
318 77 $seen{ANYTHING} = 1;
319 77 $seen{ELEMENT} = 1;
320 77 &{$emptyTag};
321 }
322 170 };
323  
324 81 ▶ my $endTag = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
325 81 my $name = $_[0];
326 81 my $currentName = pop @elementStack;
327 81 ▶ $name = $currentName unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 80 81
328 81 $elementLevel--;
329 81 ▶ if ($dataMode && $hasElement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode and $hasElement) 20 61 81
Condition100%3/3
$dataMode and $hasElement
!ll&&!rl&&r
451620
MC/DC100%2/2
$dataMode and $hasElement
$dataMode $hasElement
Truth table100%3/3
A and B
A= $dataModeB= $hasElement
ABresult
0X0
100
111
330 20 $output->print("\n");
331 20 $output->print($dataIndent x $elementLevel);
332 }
333 81 $output->print("</$name$nl>");
334 81 ▶ if ($dataMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataMode) 36 45 81
335 36 $hasData = pop @hasDataStack;
336 36 $hasElement = pop @hasElementStack;
337 }
338 170 };
339  
340 76 ▶ my $SAFE_endTag = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
341 76 my $name = $_[0];
342 76 my $oldName = $elementStack[$#elementStack];
343 76 ▶ if ($elementLevel <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elementLevel <= 0) { } 1 75 76
elsif ($name and $name ne $oldName) { } 1 74 75
344 1 croak("End tag \"$name\" does not close any open element");
345 ▶ } elsif ($name && ($name ne $oldName)) {
Condition100%3/3
$name and $name ne $oldName
!ll&&!rl&&r
1731
MC/DC100%2/2
$name and $name ne $oldName
$name $name ne $oldName
Truth table100%3/3
A and B
A= $nameB= $name ne $oldName
ABresult
0X0
100
111
346 1 croak("Attempt to end element \"$oldName\" with \"$name\" tag");
347 } else {
348 74 &{$endTag};
349 }
350 170 };
351  
352 40 ▶ my $characters = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
353 40 my $data = $_[0];
354 40 ▶ if ($data =~ /[\&\<\>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /[\&\<\>]/) 1 39 40
355 1 $data =~ s/\&/\&amp\;/g;
356 1 $data =~ s/\</\&lt\;/g;
357 1 $data =~ s/\>/\&gt\;/g;
358 }
359 40 &{$escapeEncoding}($data);
360 40 $output->print($data);
361 40 $hasData = 1;
362 170 };
363  
364 42 ▶ my $SAFE_characters = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
365 42 ▶ if ($elementLevel < 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elementLevel < 1) { } 1 41 42
elsif ($dataMode and $hasElement) { } 1 40 41
366 1 croak("Attempt to insert characters outside of document element");
367 ▶ } elsif ($dataMode && $hasElement) {
Condition100%3/3
$dataMode and $hasElement
!ll&&!rl&&r
29111
MC/DC100%2/2
$dataMode and $hasElement
$dataMode $hasElement
Truth table100%3/3
A and B
A= $dataModeB= $hasElement
ABresult
0X0
100
111
368 1 croak("Mixed content not allowed in data mode: characters");
369 } else {
370 40 _croakUnlessDefinedCharacters($_[0]);
371 38 &{$characters};
372 }
373 170 };
374  
375 1 ▶ my $raw = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
376 1 $output->print($_[0]);
377 # Don't set $hasData or any other information: we know nothing
378 # about what was just written.
379 #
380 170 };
381  
382 1 ▶ my $SAFE_raw = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
383 1 croak('raw() is only available when UNSAFE is set');
384 170 };
385  
386 10 ▶ my $cdata = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
387 10 my $data = $_[0];
388 10 $data =~ s/\]\]>/\]\]\]\]><!\[CDATA\[>/g;
389 10 $output->print("<![CDATA[$data]]>");
390 10 $hasData = 1;
391 170 };
392  
393 11 ▶ my $SAFE_cdata = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
394 11 ▶ if ($elementLevel < 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elementLevel < 1) { } 1 10 11
elsif ($dataMode and $hasElement) { } 1 9 10
395 1 croak("Attempt to insert characters outside of document element");
396 ▶ } elsif ($dataMode && $hasElement) {
Condition100%3/3
$dataMode and $hasElement
!ll&&!rl&&r
541
MC/DC100%2/2
$dataMode and $hasElement
$dataMode $hasElement
Truth table100%3/3
A and B
A= $dataModeB= $hasElement
ABresult
0X0
100
111
397 1 croak("Mixed content not allowed in data mode: characters");
398 } else {
399 9 _croakUnlessDefinedCharacters($_[0]);
400 8 &{$checkUnencodedRepertoire}($_[0]);
401 7 &{$cdata};
402 }
403 170 };
404  
405 # Assign the correct closures based on
406 # the UNSAFE parameter
407 170 ▶ if ($unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsafe) { } 10 160 170
408 10 $self = {'END' => $end,
409 'XMLDECL' => $xmlDecl,
410 'PI' => $pi,
411 'COMMENT' => $comment,
412 'DOCTYPE' => $doctype,
413 'STARTTAG' => $startTag,
414 'EMPTYTAG' => $emptyTag,
415 'ENDTAG' => $endTag,
416 'CHARACTERS' => $characters,
417 'RAW' => $raw,
418 'CDATA' => $cdata
419 };
420 } else {
421 160 $self = {'END' => $SAFE_end,
422 'XMLDECL' => $SAFE_xmlDecl,
423 'PI' => $SAFE_pi,
424 'COMMENT' => $SAFE_comment,
425 'DOCTYPE' => $SAFE_doctype,
426 'STARTTAG' => $SAFE_startTag,
427 'EMPTYTAG' => $SAFE_emptyTag,
428 'ENDTAG' => $SAFE_endTag,
429 'CHARACTERS' => $SAFE_characters,
430 'RAW' => $SAFE_raw, # This will intentionally fail
431 'CDATA' => $SAFE_cdata
432 };
433 }
434  
435 # Query methods
436 1 ▶ $self->{'IN_ELEMENT'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
437 1 my ($ancestor) = (@_);
438 1 return $elementStack[$#elementStack] eq $ancestor;
439 170 };
440  
441 3 ▶ $self->{'WITHIN_ELEMENT'} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
442 3 my ($ancestor) = (@_);
443 3 my $el;
444 3 foreach $el (@elementStack) {
445 4 ▶ return 1 if $el eq $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $el eq $ancestor 2 2 4
446 }
447 1 return 0;
448 170 };
449  
450 1 ▶ $self->{'CURRENT_ELEMENT'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
451 1 return $elementStack[$#elementStack];
452 170 };
453  
454 5 ▶ $self->{'ANCESTOR'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
455 5 my ($n) = (@_);
456 5 ▶ if ($n < scalar(@elementStack)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < scalar @elementStack) { } 3 2 5
457 3 return $elementStack[$#elementStack-$n];
458 } else {
459 2 return undef;
460 }
461 170 };
462  
463 # Set and get the output destination.
464 7 ▶ $self->{'GETOUTPUT'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
465 7 ▶ if (ref($output) ne 'XML::Writer::_PrintChecker') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output ne 'XML::Writer::_PrintChecker') { } 4 3 7
466 4 return $output;
467 } else {
468 3 return $output->{HANDLE};
469 }
470 170 };
471  
472 176 ▶ $self->{'SETOUTPUT'} = sub {
Subroutine __ANON__: called CC=12 SCAR=24.8
473 176 my $newOutput = $_[0];
474  
475 176 ▶ if (defined($newOutput) && !ref($newOutput)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newOutput and not ref $newOutput) 3 173 176
Condition100%3/3
defined $newOutput and not ref $newOutput
!ll&&!rl&&r
11723
MC/DC100%2/2
defined $newOutput and not ref $newOutput
defined $newOutput not ref $newOutput
Truth table100%3/3
A and not(B)
A= defined $newOutputB= not ref $newOutput
ABresult
0X0
100
111
476 3 ▶ if ('self' eq $newOutput ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('self' eq $newOutput) { } 2 1 3
477 2 $newOutput = \$selfcontained_output;
478 2 $use_selfcontained_output = 1;
479 } else {
480 1 die "Output must be a handle, a reference or 'self'";
481 }
482 }
483  
484 175 ▶ if (ref($newOutput) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newOutput eq 'SCALAR') { } 7 168 175
485 7 $output = XML::Writer::_String->new($newOutput);
486 } else {
487 # If there is no OUTPUT parameter,
488 # use standard output
489 168 ▶ $output = $newOutput || \*STDOUT;
Condition100%2/2
$newOutput || \*STDOUT
l!l
1671
MC/DC100%1/1
$newOutput || \*STDOUT
$newOutput
Truth table100%2/2
A
A= $newOutput
Aresult
11
00
490 168 ▶ if ($outputEncoding && (ref($output) eq 'GLOB' || $output->isa('IO::Handle'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputEncoding and ref $output eq "GLOB" || $output->isa("IO::Handle")) 21 147 168
Condition100%6/6
$outputEncoding and ref $output eq "GLOB" || $output->isa("IO::Handle")
!ll&&!rl&&r
146121
ref $output eq "GLOB" or $output->isa("IO::Handle")
l!l&&r!l&&!r
1201
MC/DC100%3/3
$outputEncoding and ref $output eq "GLOB" || $output->isa("IO::Handle")
$outputEncoding ref $output eq "GLOB" $output->isa("IO::Handle")
Truth table100%4/4
A and B or C
A= $outputEncodingB= ref $output eq "GLOB"C= $output->isa("IO::Handle")
ABCresult
0XX0
1000
11X1
1011
491 21 ▶ if (lc($outputEncoding) eq 'utf-8') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $outputEncoding eq 'utf-8') { } 4 17 21
elsif (lc $outputEncoding eq 'us-ascii') { } 15 2 17
492 4 binmode($output, ':encoding(utf-8)');
493 } elsif (lc($outputEncoding) eq 'us-ascii') {
494 15 binmode($output, ':encoding(us-ascii)');
495 } else {
496 2 die 'The only supported encodings are utf-8 and us-ascii';
497 }
498 }
499 }
500  
501 173 ▶ if ($params{CHECK_PRINT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"CHECK_PRINT"}) 6 167 173
502 6 $output = XML::Writer::_PrintChecker->new($output);
503 }
504 170 };
505  
506 5 ▶ $self->{OVERLOADSTRING} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
507 # if we don't use the self-contained output,
508 # simple passthrough
509 5 ▶ return $use_selfcontained_output ? $selfcontained_output : undef;
Branch: 1 branch
BranchTrueFalseTotal
$use_selfcontained_output ? : 2 3 5
510 170 };
511  
512 2 ▶ $self->{TOSTRING} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
513 2 ▶ die "'to_string' can only be used with self-contained output\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $use_selfcontained_output 1 1 2
514 unless $use_selfcontained_output;
515  
516 1 return $selfcontained_output;
517 170 };
518  
519 7 ▶ $self->{'SETDATAMODE'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
520 7 $dataMode = $_[0];
521 170 };
522  
523 2 ▶ $self->{'GETDATAMODE'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
524 2 return $dataMode;
525 170 };
526  
527 178 ▶ $self->{'SETDATAINDENT'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
528 178 ▶ if ($_[0] =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*$/) { } 168 10 178
529 168 $dataIndent = $_[0];
530 } else {
531 10 $dataIndent = ' ' x $_[0];
532 }
533 170 };
534  
535 8 ▶ $self->{'GETDATAINDENT'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
536 8 ▶ if ($dataIndent =~ /^ *$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataIndent =~ /^ *$/) { } 7 1 8
537 7 return length($dataIndent);
538 } else {
539 1 return $dataIndent;
540 }
541 170 };
542  
543 # Set the indent.
544 170 ▶ &{$self->{'SETDATAINDENT'}}($params{'DATA_INDENT'} || '');
Condition100%2/2
$params{'DATA_INDENT'} || ''
l!l
7163
MC/DC100%1/1
$params{'DATA_INDENT'} || ''
$params{'DATA_INDENT'}
Truth table100%2/2
A
A= $params{'DATA_INDENT'}
Aresult
11
00
545  
546 # Set the output.
547 170 &{$self->{'SETOUTPUT'}}($params{'OUTPUT'});
548  
549 # Return the blessed object.
550 167 return bless $self, $class;
551 }
552  
553  
554
555 ########################################################################
556 # Public methods
557 ########################################################################
558  
559 #
560 # Finish writing the document.
561 #
562 90 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
563 90 my $self = shift;
564 90 &{$self->{END}};
565 }
566  
567 #
568 # Write an XML declaration.
569 #
570 24 ▶sub xmlDecl {
Subroutine xmlDecl: called CC=1 SCAR=0.0
Pod documented
571 24 my $self = shift;
572 24 &{$self->{XMLDECL}};
573 }
574  
575 #
576 # Write a processing instruction.
577 #
578 12 ▶sub pi {
Subroutine pi: called CC=1 SCAR=0.0
Pod documented
579 12 my $self = shift;
580 12 &{$self->{PI}};
581 }
582  
583 #
584 # Write a comment.
585 #
586 16 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
587 16 my $self = shift;
588 16 &{$self->{COMMENT}};
589 }
590  
591 #
592 # Write a DOCTYPE declaration.
593 #
594 12 ▶sub doctype {
Subroutine doctype: called CC=1 SCAR=0.0
Pod documented
595 12 my $self = shift;
596 12 &{$self->{DOCTYPE}};
597 }
598  
599 #
600 # Write a start tag.
601 #
602 110 ▶sub startTag {
Subroutine startTag: called CC=1 SCAR=0.0
Pod documented
603 110 my $self = shift;
604 110 &{$self->{STARTTAG}};
605 }
606  
607 #
608 # Write an empty tag.
609 #
610 102 ▶sub emptyTag {
Subroutine emptyTag: called CC=1 SCAR=0.0
Pod documented
611 102 my $self = shift;
612 102 &{$self->{EMPTYTAG}};
613 }
614  
615 #
616 # Write an end tag.
617 #
618 83 ▶sub endTag {
Subroutine endTag: called CC=1 SCAR=0.0
Pod documented
619 83 my $self = shift;
620 83 &{$self->{ENDTAG}};
621 }
622  
623 #
624 # Write a simple data element.
625 #
626 20 ▶sub dataElement {
Subroutine dataElement: called CC=1 SCAR=0.0
Pod documented
627 20 my ($self, $name, $data, @atts) = (@_);
628 20 $self->startTag($name, @atts);
629 20 $self->characters($data);
630 20 $self->endTag($name);
631 }
632  
633 #
634 # Write a simple CDATA element.
635 #
636 1 ▶sub cdataElement {
Subroutine cdataElement: called CC=1 SCAR=0.0
Pod documented
637 1 my ($self, $name, $data, %atts) = (@_);
638 1 $self->startTag($name, %atts);
639 1 $self->cdata($data);
640 1 $self->endTag($name);
641 }
642  
643 #
644 # Write character data.
645 #
646 44 ▶sub characters {
Subroutine characters: called CC=1 SCAR=0.0
Pod documented
647 44 my $self = shift;
648 44 &{$self->{CHARACTERS}};
649 }
650  
651 #
652 # Write raw, unquoted, completely unchecked character data.
653 #
654 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
655 2 my $self = shift;
656 2 &{$self->{RAW}};
657 }
658  
659 #
660 # Write CDATA.
661 #
662 14 ▶sub cdata {
Subroutine cdata: called CC=1 SCAR=0.0
Pod documented
663 14 my $self = shift;
664 14 &{$self->{CDATA}};
665 }
666  
667 #
668 # Query the current element.
669 #
670 1 ▶sub in_element {
Subroutine in_element: called CC=1 SCAR=0.0
Pod documented
671 1 my $self = shift;
672 1 return &{$self->{IN_ELEMENT}};
673 }
674  
675 #
676 # Query the ancestors.
677 #
678 3 ▶sub within_element {
Subroutine within_element: called CC=1 SCAR=0.0
Pod documented
679 3 my $self = shift;
680 3 return &{$self->{WITHIN_ELEMENT}};
681 }
682  
683 #
684 # Get the name of the current element.
685 #
686 1 ▶sub current_element {
Subroutine current_element: called CC=1 SCAR=0.0
Pod documented
687 1 my $self = shift;
688 1 return &{$self->{CURRENT_ELEMENT}};
689 }
690  
691 #
692 # Get the name of the numbered ancestor (zero-based).
693 #
694 5 ▶sub ancestor {
Subroutine ancestor: called CC=1 SCAR=0.0
Pod documented
695 5 my $self = shift;
696 5 return &{$self->{ANCESTOR}};
697 }
698  
699 #
700 # Get the current output destination.
701 #
702 7 ▶sub getOutput {
Subroutine getOutput: called CC=1 SCAR=0.0
Pod documented
703 7 my $self = shift;
704 7 return &{$self->{GETOUTPUT}};
705 }
706  
707  
708 #
709 # Set the current output destination.
710 #
711 6 ▶sub setOutput {
Subroutine setOutput: called CC=1 SCAR=0.0
Pod documented
712 6 my $self = shift;
713 6 return &{$self->{SETOUTPUT}};
714 }
715  
716 #
717 # Set the current data mode (true or false).
718 #
719 7 ▶sub setDataMode {
Subroutine setDataMode: called CC=1 SCAR=0.0
Pod documented
720 7 my $self = shift;
721 7 return &{$self->{SETDATAMODE}};
722 }
723  
724  
725 #
726 # Get the current data mode (true or false).
727 #
728 2 ▶sub getDataMode {
Subroutine getDataMode: called CC=1 SCAR=0.0
Pod documented
729 2 my $self = shift;
730 2 return &{$self->{GETDATAMODE}};
731 }
732  
733  
734 #
735 # Set the current data indent step.
736 #
737 8 ▶sub setDataIndent {
Subroutine setDataIndent: called CC=1 SCAR=0.0
Pod documented
738 8 my $self = shift;
739 8 return &{$self->{SETDATAINDENT}};
740 }
741  
742  
743 #
744 # Get the current data indent step.
745 #
746 8 ▶sub getDataIndent {
Subroutine getDataIndent: called CC=1 SCAR=0.0
Pod documented
747 8 my $self = shift;
748 8 return &{$self->{GETDATAINDENT}};
749 }
750  
751  
752 #
753 # Empty stub.
754 #
755 1 ▶sub addPrefix {
Subroutine addPrefix: called CC=1 SCAR=0.0
Pod documented
756 }
757  
758  
759 #
760 # Empty stub.
761 #
762 1 ▶sub removePrefix {
Subroutine removePrefix: called CC=1 SCAR=0.0
Pod documented
763 }
764  
765 2 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
766 2 my $self = shift;
767  
768 2 $self->{TOSTRING}->();
769 }
770  
771  
772
773 ########################################################################
774 # Private functions.
775 ########################################################################
776  
777 #
778 # Private: check for duplicate attributes and bad characters.
779 # Note - this starts at $_[1], because $_[0] is assumed to be an
780 # element name.
781 #
782 189 ▶sub _checkAttributes {
Subroutine _checkAttributes: called CC=3 SCAR=11.0
783 189 my %anames;
784 189 my $i = 1;
785 189 my $checkUnencodedRepertoire = $_[1];
786  
787 189 ▶ while ($_[0]->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][$i]) 73 183 256
788 73 my $name = $_[0]->[$i];
789 73 $i += 1;
790 73 ▶ if ($anames{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anames{$name}) { } 2 71 73
791 2 croak("Two attributes named \"$name\"");
792 } else {
793 71 $anames{$name} = 1;
794 }
795 71 _croakUnlessValidName($name);
796 69 &{$checkUnencodedRepertoire}($name);
797 68 _croakUnlessDefinedCharacters($_[0]->[$i]);
798 67 $i += 1;
799 }
800 }
801  
802 #
803 # Private: escape an attribute value literal.
804 #
805 72 ▶sub _escapeLiteral {
Subroutine _escapeLiteral: called CC=2 SCAR=6.9
806 72 my $data = $_[0];
807 72 ▶ if ($data =~ /[\&\<\>\"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /[\&\<\>\"]/) 2 70 72
808 2 $data =~ s/\&/\&amp\;/g;
809 2 $data =~ s/\</\&lt\;/g;
810 2 $data =~ s/\>/\&gt\;/g;
811 2 $data =~ s/\"/\&quot\;/g;
812 }
813 72 return $data;
814 }
815  
816 7 ▶sub _escapeASCII($) {
Subroutine _escapeASCII: called CC=1 SCAR=0.0
817 7 $_[0] =~ s/([^\x00-\x7F])/sprintf('&#x%X;', ord($1))/ge;
818 }
819  
820 22 ▶sub _croakUnlessASCII($) {
Subroutine _croakUnlessASCII: called CC=2 SCAR=6.9
821 22 ▶ if ($_[0] =~ /[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /[^\x00-\x7F]/) 5 17 22
822 5 croak('Non-ASCII characters are not permitted in this part of a US-ASCII document');
823 }
824 }
825  
826 # Enforce XML 1.0, section 2.2's definition of "Char" (only reject low ASCII,
827 # so as not to require Unicode support from perl)
828 117 ▶sub _croakUnlessDefinedCharacters($) {
Subroutine _croakUnlessDefinedCharacters: called CC=2 SCAR=6.9
829 117 ▶ if ($_[0] =~ /([\x00-\x08\x0B-\x0C\x0E-\x1F])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /([\x00-\x08\x0B-\x0C\x0E-\x1F])/) 4 113 117
830 4 croak(sprintf('Code point \u%04X is not a valid character in XML', ord($1)));
831 }
832 }
833  
834 # Ensure element and attribute names are non-empty, contain no whitespace and are
835 # otherwise valid XML names
836 267 ▶sub _croakUnlessValidName($) {
Subroutine _croakUnlessValidName: called CC=4 SCAR=13.9
837 267 ▶ if ($_[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "") 2 265 267
838 2 croak('Empty identifiers are not permitted in this part of an XML document');
839 }
840 265 ▶ if ($_[0] =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\s/) 4 261 265
841 4 croak('Space characters are not permitted in this part of an XML identifier');
842 }
843  
844 # From REC-xml-20081126
845 # [4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
846 # [4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
847 # [5] Name ::= NameStartChar (NameChar)*
848  
849 261 ▶ if ($_[0] !~ /^[:A-Z_a-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}\x{EFFFF}][-.0-9\x{B7}\x{0300}-\x{036F}\x{203F}-\x{2040}:A-Z_a-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}\x{EFFFF}]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ /^[:A-Z_a-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}\x{EFFFF}][-.0-9\x{B7}\x{0300}-\x{036F}\x{203F}-\x{2040}:A-Z_a-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}\x{EFFFF}]*$/) 1 260 261
850 1 croak('Not a valid XML name: '.$_[0]);
851 }
852  
853 # ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
854  
855 # | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
856 }
857  
858 5 ▶sub _overload_string {
Subroutine _overload_string: called CC=2 SCAR=6.9
859 5 my $self = shift;
860 5 ▶ $self->{OVERLOADSTRING}->() || overload::StrVal($self);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"OVERLOADSTRING"}->() or overload::StrVal($self) 2 3 5
861 }
862
863 ########################################################################
864 # XML::Writer::Namespaces - subclass for Namespace processing.
865 ########################################################################
866  
867 package XML::Writer::Namespaces;
868 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
869 3 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
870 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
871  
872 3 @ISA = qw(XML::Writer);
873  
874 #
875 # Constructor
876 #
877 159 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
878 159 my ($class, %params) = (@_);
879  
880 159 my $unsafe = $params{UNSAFE};
881  
882 # Snarf the prefix map, if any, and
883 # note the default prefix.
884 159 my %prefixMap = ();
885 159 ▶ if ($params{PREFIX_MAP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"PREFIX_MAP"}) 8 151 159
886 8 %prefixMap = (%{$params{PREFIX_MAP}});
887 8 delete $params{PREFIX_MAP};
888 }
889 159 $prefixMap{'http://www.w3.org/XML/1998/namespace'} = 'xml';
890  
891 # Generate the reverse map for URIs
892 159 my $uriMap = {};
893 159 my $key;
894 159 foreach $key (keys(%prefixMap)) {
895 168 $uriMap->{$prefixMap{$key}} = $key;
896 }
897  
898 159 my $defaultPrefix = $uriMap->{''};
899 159 ▶ delete $prefixMap{$defaultPrefix} if ($defaultPrefix);
Branch: 1 branch
BranchTrueFalseTotal
if $defaultPrefix 4 155 159
900  
901 # Create an instance of the parent.
902 159 my $self = XML::Writer->new(%params);
903  
904 # Snarf the parent's methods that we're
905 # going to override.
906 156 my $OLD_startTag = $self->{STARTTAG};
907 156 my $OLD_emptyTag = $self->{EMPTYTAG};
908 156 my $OLD_endTag = $self->{ENDTAG};
909  
910 # State variables
911 156 my @stack;
912 156 my $prefixCounter = 1;
913 156 my $nsDecls = {'http://www.w3.org/XML/1998/namespace' => 'xml'};
914 156 my $nsDefaultDecl = undef;
915 156 my $nsCopyFlag = 0;
916 156 my @forcedNSDecls = ();
917  
918 156 ▶ if ($params{FORCED_NS_DECLS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"FORCED_NS_DECLS"}) 2 154 156
919 2 @forcedNSDecls = @{$params{FORCED_NS_DECLS}};
920 2 delete $params{FORCED_NS_DECLS};
921 }
922  
923 #
924 # Push the current declaration state.
925 #
926 194 ▶ my $pushState = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
927 194 push @stack, [$nsDecls, $nsDefaultDecl, $nsCopyFlag, $uriMap];
928 194 $nsCopyFlag = 0;
929 156 };
930  
931  
932 #
933 # Pop the current declaration state.
934 #
935 150 ▶ my $popState = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
936 150 ($nsDecls, $nsDefaultDecl, $nsCopyFlag, $uriMap) = @{pop @stack};
937 156 };
938  
939 #
940 # Generate a new prefix.
941 #
942 34 ▶ my $genPrefix = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
943 34 my $uri = $_[0];
944 34 my $prefixCounter = 1;
945 34 my $prefix = $prefixMap{$uri};
946 34 my %clashMap = %{$uriMap};
947 34 ▶ while( my ($u, $p) = each(%prefixMap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %prefixMap) 45 34 79
948 45 $clashMap{$p} = $u;
949 }
950  
951 34 ▶ if (!defined($prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix) 24 10 34
952 do {
953 36 $prefix = "__NS$prefixCounter";
954 36 $prefixCounter++;
955 24 ▶ } while ($clashMap{$prefix});
Branch: 1 branch
BranchTrueFalseTotal
$clashMap{$prefix} and do { do { $prefix = "__NS$prefixCounter"; ++$prefixCounter } } 12 24 36
956 }
957  
958 34 return $prefix;
959 156 };
960  
961 #
962 # Perform namespace processing on a single name.
963 #
964 71 ▶ my $processName = sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
965 71 my ($nameref, $atts, $attFlag) = (@_);
966 71 my ($uri, $local) = @{$$nameref};
967 71 my $prefix = $nsDecls->{$uri};
968  
969 # Is this an element name that matches
970 # the default NS?
971 71 ▶ if (!$attFlag && $defaultPrefix && ($uri eq $defaultPrefix)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $attFlag and $defaultPrefix and $uri eq $defaultPrefix) { } 17 54 71
elsif ($prefix) { } 20 34 54
Condition100%6/6
not $attFlag and $defaultPrefix and $uri eq $defaultPrefix
!ll&&!rl&&r
411317
not $attFlag and $defaultPrefix
!ll&&!rl&&r
162530
MC/DC100%3/3
not $attFlag and $defaultPrefix and $uri eq $defaultPrefix
not $attFlag $defaultPrefix $uri eq $defaultPrefix
Truth table100%4/4
not(A) and B and C
A= not $attFlagB= $defaultPrefixC= $uri eq $defaultPrefix
ABCresult
0XX0
10X0
1100
1111
972 17 ▶ unless ($nsDefaultDecl && ($nsDefaultDecl eq $uri)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nsDefaultDecl and $nsDefaultDecl eq $uri) 12 5 17
Condition100%3/3
$nsDefaultDecl and $nsDefaultDecl eq $uri
!ll&&!rl&&r
1025
MC/DC100%2/2
$nsDefaultDecl and $nsDefaultDecl eq $uri
$nsDefaultDecl $nsDefaultDecl eq $uri
Truth table100%3/3
A and B
A= $nsDefaultDeclB= $nsDefaultDecl eq $uri
ABresult
0X0
100
111
973 12 push @{$atts}, 'xmlns';
974 12 push @{$atts}, $uri;
975 12 $nsDefaultDecl = $uri;
976 }
977 17 $$nameref = $local;
978  
979 17 ▶ if (defined($uriMap->{''})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uriMap->{""}) 9 8 17
980 9 delete ($nsDecls->{$uriMap->{''}});
981 }
982  
983 17 $nsDecls->{$uri} = '';
984 17 ▶ unless ($nsCopyFlag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nsCopyFlag) 16 1 17
985 16 $uriMap = {%{$uriMap}};
986 16 $nsDecls = {%{$nsDecls}};
987 16 $nsCopyFlag = 1;
988 }
989 17 $uriMap->{''} = $uri;
990  
991 # Is there a straight-forward prefix?
992 } elsif ($prefix) {
993 20 $$nameref = "$prefix:$local";
994 } else {
995 34 $prefix = &{$genPrefix}($uri);
996 34 ▶ unless ($nsCopyFlag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nsCopyFlag) 30 4 34
997 30 $uriMap = {%{$uriMap}};
998 30 $nsDecls = {%{$nsDecls}};
999 30 $nsCopyFlag = 1;
1000 }
1001 34 $uriMap->{$prefix} = $uri;
1002 34 $nsDecls->{$uri} = $prefix;
1003 34 push @{$atts}, "xmlns:$prefix";
1004 34 push @{$atts}, $uri;
1005 34 $$nameref = "$prefix:$local";
1006 }
1007 156 };
1008  
1009  
1010 #
1011 # Perform namespace processing on element and attribute names.
1012 #
1013 194 ▶ my $nsProcess = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1014 194 ▶ if (ref($_[0]->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0][0] eq "ARRAY") 54 140 194
1015 54 my $x = \@{$_[0]->[0]};
1016 54 &{$processName}(\$x, $_[0], 0);
1017 54 splice(@{$_[0]}, 0, 1, $x);
1018 }
1019 194 my $i = 1;
1020 194 ▶ while ($_[0]->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][$i]) 74 194 268
1021 74 ▶ if (ref($_[0]->[$i]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0][$i] eq "ARRAY") 14 60 74
1022 14 my $x = \@{$_[0]->[$i]};
1023 14 &{$processName}(\$x, $_[0], 1);
1024 14 splice(@{$_[0]}, $i, 1, $x);
1025 }
1026 74 $i += 2;
1027 }
1028  
1029 # We do this if any declarations are forced, due either to
1030 # constructor arguments or to a call during processing.
1031 194 ▶ if (@forcedNSDecls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@forcedNSDecls) 3 191 194
1032 3 foreach (@forcedNSDecls) {
1033 3 my @dummy = ($_, 'dummy');
1034 3 my $d2 = \@dummy;
1035 3 ▶ if ($defaultPrefix && ($_ eq $defaultPrefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaultPrefix and $_ eq $defaultPrefix) { } 1 2 3
Condition100%3/3
$defaultPrefix and $_ eq $defaultPrefix
!ll&&!rl&&r
111
MC/DC100%2/2
$defaultPrefix and $_ eq $defaultPrefix
$defaultPrefix $_ eq $defaultPrefix
Truth table100%3/3
A and B
A= $defaultPrefixB= $_ eq $defaultPrefix
ABresult
0X0
100
111
1036 1 &{$processName}(\$d2, $_[0], 0);
1037 } else {
1038 2 &{$processName}(\$d2, $_[0], 1);
1039 }
1040 }
1041 3 @forcedNSDecls = ();
1042 }
1043 156 };
1044  
1045  
1046 # Indicate that a namespace should be declared by the next open element
1047 1 ▶ $self->{FORCENSDECL} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1048 1 push @forcedNSDecls, $_[0];
1049 156 };
1050  
1051  
1052 #
1053 # Start tag, with NS processing
1054 #
1055 101 ▶ $self->{STARTTAG} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1056 101 my $name = $_[0];
1057 101 ▶ unless ($unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unsafe) 94 7 101
1058 94 _checkNSNames(\@_);
1059 }
1060 101 &{$pushState}();
1061 101 &{$nsProcess}(\@_);
1062 101 &{$OLD_startTag};
1063 156 };
1064  
1065  
1066 #
1067 # Empty tag, with NS processing
1068 #
1069 98 ▶ $self->{EMPTYTAG} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1070 98 ▶ unless ($unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unsafe) 94 4 98
1071 94 _checkNSNames(\@_);
1072 }
1073 93 &{$pushState}();
1074 93 &{$nsProcess}(\@_);
1075 93 &{$OLD_emptyTag};
1076 78 &{$popState}();
1077 156 };
1078  
1079  
1080 #
1081 # End tag, with NS processing
1082 #
1083 74 ▶ $self->{ENDTAG} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1084 74 my $name = $_[0];
1085 74 ▶ if (ref($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 22 52 74
1086 22 my $pfx = $nsDecls->{$_[0]->[0]};
1087 22 ▶ if ($pfx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx) { } 17 5 22
1088 17 $_[0] = $pfx . ':' . $_[0]->[1];
1089 } else {
1090 5 $_[0] = $_[0]->[1];
1091 }
1092 } else {
1093 52 $_[0] = $_[0];
1094 }
1095 # &{$nsProcess}(\@_);
1096 74 &{$OLD_endTag};
1097 72 &{$popState}();
1098 156 };
1099  
1100  
1101 #
1102 # Processing instruction, but only if not UNSAFE.
1103 #
1104 156 ▶ unless ($unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unsafe) 146 10 156
1105 146 my $OLD_pi = $self->{PI};
1106 12 ▶ $self->{PI} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1107 12 my $target = $_[0];
1108 12 ▶ if (index($target, ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($target, ":") != -1) 1 11 12
1109 1 croak "PI target '$target' contains a colon.";
1110 }
1111 11 &{$OLD_pi};
1112 }
1113 146 };
1114  
1115  
1116 #
1117 # Add a prefix to the prefix map.
1118 #
1119 18 ▶ $self->{ADDPREFIX} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1120 18 my ($uri, $prefix) = (@_);
1121 18 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) { } 7 11 18
1122 7 $prefixMap{$uri} = $prefix;
1123 } else {
1124 11 ▶ if (defined($defaultPrefix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defaultPrefix) 4 7 11
1125 4 delete($prefixMap{$defaultPrefix});
1126 }
1127 11 $defaultPrefix = $uri;
1128 }
1129 156 };
1130  
1131  
1132 #
1133 # Remove a prefix from the prefix map.
1134 #
1135 3 ▶ $self->{REMOVEPREFIX} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1136 3 my ($uri) = (@_);
1137 3 ▶ if ($defaultPrefix && ($defaultPrefix eq $uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaultPrefix and $defaultPrefix eq $uri) 1 2 3
Condition100%3/3
$defaultPrefix and $defaultPrefix eq $uri
!ll&&!rl&&r
111
MC/DC100%2/2
$defaultPrefix and $defaultPrefix eq $uri
$defaultPrefix $defaultPrefix eq $uri
Truth table100%3/3
A and B
A= $defaultPrefixB= $defaultPrefix eq $uri
ABresult
0X0
100
111
1138 1 $defaultPrefix = undef;
1139 }
1140 3 delete $prefixMap{$uri};
1141 156 };
1142  
1143  
1144 #
1145 # Bless and return the object.
1146 #
1147 156 return bless $self, $class;
1148 }
1149  
1150  
1151 #
1152 # Add a preferred prefix for a namespace URI.
1153 #
1154 18 ▶sub addPrefix {
Subroutine addPrefix: called CC=1 SCAR=0.0
1155 18 my $self = shift;
1156 18 return &{$self->{ADDPREFIX}};
1157 }
1158  
1159  
1160 #
1161 # Remove a preferred prefix for a namespace URI.
1162 #
1163 3 ▶sub removePrefix {
Subroutine removePrefix: called CC=1 SCAR=0.0
1164 3 my $self = shift;
1165 3 return &{$self->{REMOVEPREFIX}};
1166 }
1167  
1168  
1169 #
1170 # Check names.
1171 #
1172 188 ▶sub _checkNSNames {
Subroutine _checkNSNames: called CC=9 SCAR=22.0
1173 188 my $names = $_[0];
1174 188 my $i = 1;
1175 188 my $name = $names->[0];
1176  
1177 # Check the element name.
1178 188 ▶ if (ref($name) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $name eq 'ARRAY') { } 55 133 188
elsif (index($name, ':') != -1) { } 1 132 133
1179 55 ▶ if (index($name->[1], ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($name->[1], ":") != -1) 1 54 55
1180 1 croak("Local part of element name '" .
1181 $name->[1] .
1182 "' contains a colon.");
1183 }
1184 } elsif (index($name, ':') >= 0) {
1185 1 croak("Element name '$name' contains a colon.");
1186 }
1187  
1188 # Check the attribute names.
1189 186 ▶ while ($names->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($names->[$i]) 29 183 212
1190 29 my $name = $names->[$i];
1191 29 ▶ if (ref($name) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq 'ARRAY') { } 13 16 29
1192 13 my $local = $name->[1];
1193 13 ▶ if (index($local, ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($local, ":") != -1) 1 12 13
1194 1 croak "Local part of attribute name '$local' contains a colon.";
1195 }
1196 } else {
1197 16 ▶ if ($name =~ /^xmlns/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^xmlns/) { } 1 15 16
elsif (index($name, ':') != -1) { } 1 14 15
1198 1 croak "Attribute name '$name' begins with 'xmlns'";
1199 } elsif (index($name, ':') >= 0) {
1200 1 croak "Attribute name '$name' contains ':'";
1201 }
1202 }
1203 26 $i += 2;
1204 }
1205 }
1206  
1207 sub forceNSDecl
1208 1 ▶{
Subroutine forceNSDecl: called CC=1 SCAR=0.0
1209 1 my $self = shift;
1210 1 return &{$self->{FORCENSDECL}};
1211 }
1212  
1213  
1214 package XML::Writer::_String;
1215  
1216 # Internal class, behaving sufficiently like an IO::Handle,
1217 # that stores written output in a string
1218 #
1219 # Heavily inspired by Simon Oliver's XML::Writer::String
1220  
1221 sub new
1222 7 ▶{
Subroutine new: called CC=1 SCAR=0.0
1223 7 my $class = shift;
1224 7 my $scalar_ref = shift;
1225 7 return bless($scalar_ref, $class);
1226 }
1227  
1228 sub print
1229 43 ▶{
Subroutine print: called CC=1 SCAR=0.0
1230 43 ${(shift)} .= join('', @_);
1231 43 return 1;
1232 }
1233  
1234  
1235 package XML::Writer::_PrintChecker;
1236  
1237 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1238  
1239 sub new
1240 6 ▶{
Subroutine new: called CC=1 SCAR=0.0
1241 6 my $class = shift;
1242 6 return bless({HANDLE => shift}, $class);
1243 }
1244  
1245 sub print
1246 4 ▶{
Subroutine print: called CC=2 SCAR=6.9
1247 4 my $self = shift;
1248 4 ▶ if ($self->{HANDLE}->print(shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'HANDLE'}->print(shift())) { } 2 2 4
1249 2 return 1;
1250 } else {
1251 2 croak "Failed to write output: $!";
1252 }
1253 }
1254  
1255 3 1;
1256 __END__
↑ Summary