Generator.pm

filter: Statementstmt 94.9% 433 / 456 Branchbran 84.8% 246 / 290 Conditioncond 77.0% 67 / 87 MC/DCmcdc 53.8% 28 / 52 Subroutinesub 92.5% 50 / 54 Podpod 85.7% 6 / 7 totaltotal 87.7% 830 / 946 CC 189 SCAR 30.5
CC
189
Cov
90%
__ANON__
54.0
new
31.4
_fixupNS
18.4
CRAP 21.2
↑ Summary Generator/DOM.pm »
1 package XML::Generator;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw/$VERSION $AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '1.04';
8  
9 =head1 NAME
10  
11 XML::Generator - Perl extension for generating XML
12  
13 =head1 SYNOPSIS
14  
15 use XML::Generator ':pretty';
16  
17 print foo(bar({ baz => 3 }, bam()),
18 bar([ 'qux' => 'http://qux.com/' ],
19 "Hey there, world"));
20  
21 # OR
22  
23 require XML::Generator;
24  
25 my $X = XML::Generator->new(':pretty');
26  
27 print $X->foo($X->bar({ baz => 3 }, $X->bam()),
28 $X->bar([ 'qux' => 'http://qux.com/' ],
29 "Hey there, world"));
30
31 Either of the above yield:
32  
33 <foo xmlns:qux="http://qux.com/">
34 <bar baz="3">
35 <bam />
36 </bar>
37 <qux:bar>Hey there, world</qux:bar>
38 </foo>
39  
40 =head1 DESCRIPTION
41  
42 In general, once you have an XML::Generator object, you then simply call
43 methods on that object named for each XML tag you wish to generate.
44  
45 XML::Generator can also arrange for undefined subroutines in the caller's
46 package to generate the corresponding XML, by exporting an C<AUTOLOAD>
47 subroutine to your package. Just supply an ':import' argument to
48 your C<use XML::Generator;> call. If you already have an C<AUTOLOAD>
49 defined then XML::Generator can be configured to cooperate with it.
50 See L<"STACKABLE AUTOLOADs">.
51  
52 Say you want to generate this XML:
53  
54 <person>
55 <name>Bob</name>
56 <age>34</age>
57 <job>Accountant</job>
58 </person>
59  
60 Here's a snippet of code that does the job, complete with pretty printing:
61  
62 use XML::Generator;
63 my $gen = XML::Generator->new(':pretty');
64 print $gen->person(
65 $gen->name("Bob"),
66 $gen->age(34),
67 $gen->job("Accountant")
68 );
69  
70 The only problem with this is if you want to use a tag name that
71 Perl's lexer won't understand as a method name, such as "shoe-size".
72 Fortunately, since you can store the name of a method in a variable,
73 there's a simple work-around:
74  
75 my $shoe_size = "shoe-size";
76 $xml = $gen->$shoe_size("12 1/2");
77  
78 Which correctly generates:
79  
80 <shoe-size>12 1/2</shoe-size>
81  
82 You can use a hash ref as the first parameter if the tag should include
83 atributes. Normally this means that the order of the attributes will be
84 unpredictable, but if you have the L<Tie::IxHash> module, you can use it
85 to get the order you want, like this:
86  
87 use Tie::IxHash;
88 tie my %attr, 'Tie::IxHash';
89  
90 %attr = (name => 'Bob',
91 age => 34,
92 job => 'Accountant',
93 'shoe-size' => '12 1/2');
94  
95 print $gen->person(\%attr);
96  
97 This produces
98  
99 <person name="Bob" age="34" job="Accountant" shoe-size="12 1/2" />
100  
101 An array ref can also be supplied as the first argument to indicate
102 a namespace for the element and the attributes.
103  
104 If there is one element in the array, it is considered the URI of
105 the default namespace, and the tag will have an xmlns="URI" attribute
106 added automatically. If there are two elements, the first should be
107 the tag prefix to use for the namespace and the second element should
108 be the URI. In this case, the prefix will be used for the tag and an
109 xmlns:PREFIX attribute will be automatically added. Prior to version
110 0.99, this prefix was also automatically added to each attribute name.
111 Now, the default behavior is to leave the attributes alone (although you
112 may always explicitly add a prefix to an attribute name). If the prior
113 behavior is desired, use the constructor option C<qualified_attributes>.
114  
115 If you specify more than two elements, then each pair should correspond
116 to a tag prefix and the corresponding URL. An xmlns:PREFIX attribute
117 will be added for each pair, and the prefix from the first such pair
118 will be used as the tag's namespace. If you wish to specify a default
119 namespace, use '#default' for the prefix. If the default namespace is
120 first, then the tag will use the default namespace itself.
121  
122 If you want to specify a namespace as well as attributes, you can make
123 the second argument a hash ref. If you do it the other way around,
124 the array ref will simply get stringified and included as part of the
125 content of the tag.
126  
127 Here's an example to show how the attribute and namespace parameters work:
128  
129 $xml = $gen->account(
130 $gen->open(['transaction'], 2000),
131 $gen->deposit(['transaction'], { date => '1999.04.03'}, 1500)
132 );
133  
134 This generates:
135  
136 <account>
137 <open xmlns="transaction">2000</open>
138 <deposit xmlns="transaction" date="1999.04.03">1500</deposit>
139 </account>
140  
141 Because default namespaces inherit, XML::Generator takes care to output
142 the xmlns="URI" attribute as few times as strictly necessary. For example,
143  
144 $xml = $gen->account(
145 $gen->open(['transaction'], 2000),
146 $gen->deposit(['transaction'], { date => '1999.04.03'},
147 $gen->amount(['transaction'], 1500)
148 )
149 );
150  
151 This generates:
152  
153 <account>
154 <open xmlns="transaction">2000</open>
155 <deposit xmlns="transaction" date="1999.04.03">
156 <amount>1500</amount>
157 </deposit>
158 </account>
159  
160 Notice how C<xmlns="transaction"> was left out of the C<<amount>> tag.
161  
162 Here is an example that uses the two-argument form of the namespace:
163  
164 $xml = $gen->widget(['wru' => 'http://www.widgets-r-us.com/xml/'],
165 {'id' => 123}, $gen->contents());
166  
167 <wru:widget xmlns:wru="http://www.widgets-r-us.com/xml/" id="123">
168 <contents />
169 </wru:widget>
170  
171 Here is an example that uses multiple namespaces. It generates the
172 first example from the RDF primer (L<http://www.w3.org/TR/rdf-primer/>).
173  
174 my $contactNS = [contact => "http://www.w3.org/2000/10/swap/pim/contact#"];
175 $xml = $gen->xml(
176 $gen->RDF([ rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
177 @$contactNS ],
178 $gen->Person($contactNS, { 'rdf:about' => "http://www.w3.org/People/EM/contact#me" },
179 $gen->fullName($contactNS, 'Eric Miller'),
180 $gen->mailbox($contactNS, {'rdf:resource' => "mailto:em@w3.org"}),
181 $gen->personalTitle($contactNS, 'Dr.'))));
182  
183 <?xml version="1.0" standalone="yes"?>
184 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
185 xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#">
186 <contact:Person rdf:about="http://www.w3.org/People/EM/contact#me">
187 <contact:fullName>Eric Miller</contact:fullName>
188 <contact:mailbox rdf:resource="mailto:em@w3.org" />
189 <contact:personalTitle>Dr.</contact:personalTitle>
190 </Person>
191 </rdf:RDF>
192  
193 =head1 CONSTRUCTOR
194  
195 XML::Generator-E<gt>new(':option', ...);
196  
197 XML::Generator-E<gt>new(option => 'value', ...);
198  
199 (Both styles may be combined)
200  
201 The following options are available:
202  
203 =head2 :std, :standard
204  
205 Equivalent to
206  
207 escape => 'always',
208 conformance => 'strict',
209  
210 =head2 :strict
211  
212 Equivalent to
213  
214 conformance => 'strict',
215  
216 =head2 :pretty[=N]
217  
218 Equivalent to
219  
220 escape => 'always',
221 conformance => 'strict',
222 pretty => N # N defaults to 2
223  
224 =head2 namespace
225  
226 This value of this option must be an array reference containing one or
227 two values. If the array contains one value, it should be a URI and will
228 be the value of an 'xmlns' attribute in the top-level tag. If there are
229 two or more elements, the first of each pair should be the namespace
230 tag prefix and the second the URI of the namespace. This will enable
231 behavior similar to the namespace behavior in previous versions; the tag
232 prefix will be applied to each tag. In addition, an xmlns:NAME="URI"
233 attribute will be added to the top-level tag. Prior to version 0.99,
234 the tag prefix was also automatically added to each attribute name,
235 unless overridden with an explicit prefix. Now, the attribute names are
236 left alone, but if the prior behavior is desired, use the constructor
237 option C<qualified_attributes>.
238  
239 The value of this option is used as the global default namespace.
240 For example,
241  
242 my $html = XML::Generator->new(
243 pretty => 2,
244 namespace => [HTML => "http://www.w3.org/TR/REC-html40"]);
245 print $html->html(
246 $html->body(
247 $html->font({ face => 'Arial' },
248 "Hello, there")));
249  
250 would yield
251  
252 <HTML:html xmlns:HTML="http://www.w3.org/TR/REC-html40">
253 <HTML:body>
254 <HTML:font face="Arial">Hello, there</HTML:font>
255 </HTML:body>
256 </HTML:html>
257  
258 Here is the same example except without all the prefixes:
259  
260 my $html = XML::Generator->new(
261 pretty => 2,
262 namespace => ["http://www.w3.org/TR/REC-html40"]);
263 print $html->html(
264 $html->body(
265 $html->font({ 'face' => 'Arial' },
266 "Hello, there")));
267  
268 would yield
269  
270 <html xmlns="http://www.w3.org/TR/REC-html40">
271 <body>
272 <font face="Arial">Hello, there</font>
273 </body>
274 </html>
275  
276 =head2 qualifiedAttributes, qualified_attributes
277  
278 Set this to a true value to emulate the attribute prefixing behavior of
279 XML::Generator prior to version 0.99. Here is an example:
280  
281 my $foo = XML::Generator->new(
282 namespace => [foo => "http://foo.com/"],
283 qualifiedAttributes => 1);
284 print $foo->bar({baz => 3});
285  
286 yields
287  
288 <foo:bar xmlns:foo="http://foo.com/" foo:baz="3" />
289  
290 =head2 escape
291  
292 The contents and the values of each attribute have any illegal XML
293 characters escaped if this option is supplied. If the value is 'always',
294 then &, < and > (and " within attribute values) will be converted into
295 the corresponding XML entity, although & will not be converted if it looks
296 like it could be part of a valid entity (but see below). If the value is
297 'unescaped', then the escaping will be turned off character-by- character
298 if the character in question is preceded by a backslash, or for the
299 entire string if it is supplied as a scalar reference. So, for example,
300  
301 use XML::Generator escape => 'always';
302  
303 one('<'); # <one>&lt;</one>
304 two('\&'); # <two>\&amp;</two>
305 three(\'>'); # <three>&gt;</three> (scalar refs always allowed)
306 four('&lt;'); # <four>&lt;</four> (looks like an entity)
307 five('&#34;'); # <five>&#34;</five> (looks like an entity)
308  
309 but
310  
311 use XML::Generator escape => 'unescaped';
312  
313 one('<'); # <one>&lt;</one>
314 two('\&'); # <two>&</two>
315 three(\'>'); # <three>></three> (aiee!)
316 four('&lt;'); # <four>&amp;lt;</four> (no special case for entities)
317  
318 By default, high-bit data will be passed through unmodified, so that
319 UTF-8 data can be generated with pre-Unicode perls. If you know that
320 your data is ASCII, use the value 'high-bit' for the escape option
321 and bytes with the high bit set will be turned into numeric entities.
322 You can combine this functionality with the other escape options by
323 comma-separating the values:
324  
325 my $a = XML::Generator->new(escape => 'always,high-bit');
326 print $a->foo("<\242>");
327  
328 yields
329  
330 <foo>&lt;&#162;&gt;</foo>
331  
332 Because XML::Generator always uses double quotes ("") around attribute
333 values, it does not escape single quotes. If you want single quotes
334 inside attribute values to be escaped, use the value 'apos' along with
335 'always' or 'unescaped' for the escape option. For example:
336  
337 my $gen = XML::Generator->new(escape => 'always,apos');
338 print $gen->foo({'bar' => "It's all good"});
339  
340 <foo bar="It&apos;s all good" />
341  
342 If you actually want & to be converted to &amp; even if it looks like it
343 could be part of a valid entity, use the value 'even-entities' along with
344 'always'. Supplying 'even-entities' to the 'unescaped' option is meaningless
345 as entities are already escaped with that option.
346  
347 =head2 pretty
348  
349 To have nice pretty printing of the output XML (great for config files
350 that you might also want to edit by hand), supply an integer for the
351 number of spaces per level of indenting, eg.
352  
353 my $gen = XML::Generator->new(pretty => 2);
354 print $gen->foo($gen->bar('baz'),
355 $gen->qux({ tricky => 'no'}, 'quux'));
356  
357 would yield
358  
359 <foo>
360 <bar>baz</bar>
361 <qux tricky="no">quux</qux>
362 </foo>
363  
364 You may also supply a non-numeric string as the argument to 'pretty', in
365 which case the indents will consist of repetitions of that string. So if
366 you want tabbed indents, you would use:
367  
368 my $gen = XML::Generator->new(pretty => "\t");
369  
370 Pretty printing does not apply to CDATA sections or Processing Instructions.
371  
372 =head2 conformance
373  
374 If the value of this option is 'strict', a number of syntactic
375 checks are performed to ensure that generated XML conforms to the
376 formal XML specification. In addition, since entity names beginning
377 with 'xml' are reserved by the W3C, inclusion of this option enables
378 several special tag names: xmlpi, xmlcmnt, xmldecl, xmldtd, xmlcdata,
379 and xml to allow generation of processing instructions, comments, XML
380 declarations, DTD's, character data sections and "final" XML documents,
381 respectively.
382  
383 Invalid characters (http://www.w3.org/TR/xml11/#charsets) will be filtered
384 out. To disable this behavior, supply the 'filter_invalid_chars' option with
385 the value 0.
386  
387 See L<"XML CONFORMANCE"> and L<"SPECIAL TAGS"> for more information.
388  
389 =head2 filterInvalidChars, filter_invalid_chars
390  
391 Set this to a 1 to enable filtering of invalid characters, or to 0 to disable
392 the filtering. See http://www.w3.org/TR/xml11/#charsets for the set of valid
393 characters.
394  
395 =head2 allowedXMLTags, allowed_xml_tags
396  
397 If you have specified 'conformance' => 'strict' but need to use tags
398 that start with 'xml', you can supply a reference to an array containing
399 those tags and they will be accepted without error. It is not an error
400 to supply this option if 'conformance' => 'strict' is not supplied,
401 but it will have no effect.
402  
403 =head2 empty
404  
405 There are 5 possible values for this option:
406  
407 self - create empty tags as <tag /> (default)
408 compact - create empty tags as <tag/>
409 close - close empty tags as <tag></tag>
410 ignore - don't do anything (non-compliant!)
411 args - use count of arguments to decide between <x /> and <x></x>
412  
413 Many web browsers like the 'self' form, but any one of the forms besides
414 'ignore' is acceptable under the XML standard.
415  
416 'ignore' is intended for subclasses that deal with HTML and other
417 SGML subsets which allow atomic tags. It is an error to specify both
418 'conformance' => 'strict' and 'empty' => 'ignore'.
419  
420 'args' will produce <x /> if there are no arguments at all, or if there
421 is just a single undef argument, and <x></x> otherwise.
422  
423 =head2 version
424  
425 Sets the default XML version for use in XML declarations.
426 See L<"xmldecl"> below.
427  
428 =head2 encoding
429  
430 Sets the default encoding for use in XML declarations.
431  
432 =head2 dtd
433  
434 Specify the dtd. The value should be an array reference with three
435 values; the type, the name and the uri.
436  
437 =head1 IMPORT ARGUMENTS
438  
439 use XML::Generator ':option';
440  
441 use XML::Generator option => 'value';
442  
443 (Both styles may be combined)
444  
445 =head2 :import
446  
447 Cause C<use XML::Generator;> to export an C<AUTOLOAD> to your package that
448 makes undefined subroutines generate XML tags corresponding to their name.
449 Note that if you already have an C<AUTOLOAD> defined, it will be overwritten.
450  
451 =head2 :stacked
452  
453 Implies :import, but if there is already an C<AUTOLOAD> defined, the
454 overriding C<AUTOLOAD> will still give it a chance to run. See L<"STACKED
455 AUTOLOADs">.
456  
457 =head2 ANYTHING ELSE
458  
459 If you supply any other options, :import is implied and the XML::Generator
460 object that is created to generate tags will be constructed with those options.
461  
462 =cut
463  
464 package XML::Generator;
465  
466 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
467 1 require Carp;
468  
469 # If no value is provided for these options, they will be set to ''
470  
471 1 my @optionsToInit = qw(
472 allowed_xml_tags
473 conformance
474 dtd
475 escape
476 namespace
477 pretty
478 version
479 empty
480 qualified_attributes
481 filter_invalid_chars
482 );
483  
484 1 my %tag_factory;
485  
486 sub import {
487 13 ▶ my $type = shift;
Subroutine import: called CC=7 SCAR=19.6
488  
489 # check for attempt to use tag 'import'
490 13 ▶ if (ref $type && defined $tag_factory{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and defined $tag_factory{$type}) 1 12 13
Condition66%2/3
ref $type and defined $tag_factory{$type}
!ll&&!rl&&r
1201
MC/DC50%1/2
ref $type and defined $tag_factory{$type}
ref $type defined $tag_factory{$type}
Truth table66%2/3
A and B
A= ref $typeB= defined $tag_factory{$type}
ABresult
0X0
100
111
491 1 unshift @_, $type, 'import';
492 1 goto &{ $tag_factory{$type} };
493 }
494  
495 12 my $pkg = caller;
496  
497 1 ▶ no strict 'refs'; # Let's get serious
Subroutine BEGIN: called CC=1 SCAR=0.0
498  
499 # should we import an AUTOLOAD?
500 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
501  
502 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 2 12
503 10 my $STACKED;
504  
505 # are we supposed to call their AUTOLOAD first?
506 10 ▶ if (grep /^:stacked$/, @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^:stacked$/, @_) 2 8 10
507 2 $STACKED = \&{"${pkg}::AUTOLOAD"};
508 }
509  
510 10 my $this = $type->new(@_);
511  
512 1 ▶ no warnings 'redefine'; # No, I mean SERIOUS
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 10 *{"${pkg}::AUTOLOAD"} =
515 sub {
516 20 ▶ if ($STACKED) {
Subroutine __ANON__: called CC=7 SCAR=21.3
Branch: 1 branch
BranchTrueFalseTotal
if ($STACKED) 7 13 20
517 7 ${"${pkg}::AUTOLOAD"} = our $AUTOLOAD;
518 7 my @ret = $STACKED->(@_);
519 7 ▶ return wantarray ? @ret : $ret[0] if @ret;
Branch: 2 branches
BranchTrueFalseTotal
if @ret 3 4 7
wantarray ? : 2 1 3
520 }
521  
522 # The tag is whatever our sub name is.
523 17 my($tag) = our $AUTOLOAD =~ /.*::(.*)/;
524  
525 # Special-case for xml... tags
526 17 ▶ if ($tag =~ /^xml/ && $this->{'conformance'} eq 'strict') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^xml/ and $this->{"conformance"} eq "strict") 0 17 17
Condition33%1/3
$tag =~ /^xml/ and $this->{"conformance"} eq "strict"
!ll&&!rl&&r
1700
MC/DC0%0/2
$tag =~ /^xml/ and $this->{"conformance"} eq "strict"
$tag =~ /^xml/ $this->{"conformance"} eq "strict"
Truth table33%1/3
A and B
A= $tag =~ /^xml/B= $this->{"conformance"} eq "strict"
ABresult
0X0
100
111
527 0 ▶ if (my $func = $this->can($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $this->can($tag)) 0 0 0
528 0 unshift @_, $this;
529 0 goto &$func;
530 }
531 }
532  
533 17 unshift @_, $this, $tag;
534  
535 17 goto &{ $tag_factory{$this} };
536 10 };
537  
538 # convenience feature for stacked autoloads; give them
539 # an import() that aliases AUTOLOAD.
540 10 ▶ if ($STACKED && ! defined *{"${pkg}::import"}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STACKED and not defined *{"${pkg}::import";}{"CODE"}) 2 8 10
Condition66%2/3
$STACKED and not defined *{"${pkg}::import";}{"CODE"}
!ll&&!rl&&r
802
MC/DC50%1/2
$STACKED and not defined *{"${pkg}::import";}{"CODE"}
$STACKED not defined *{"${pkg}::import";}{"CODE"}
Truth table66%2/3
A and not(B)
A= $STACKEDB= not defined *{"${pkg}::import";}{"CODE"}
ABresult
0X0
100
111
541 2 *{"${pkg}::import"} =
542 sub {
543 1 ▶ my $p = caller;
Subroutine __ANON__: called CC=1 SCAR=0.0
544 1 *{"${p}::AUTOLOAD"} = \&{"${pkg}::AUTOLOAD"};
545 2 };
546 }
547 }
548  
549 12 return;
550 }
551  
552 # The constructor method
553  
554 sub new {
555 49 ▶ my $class = shift;
Subroutine new: called CC=30 SCAR=34.5
Pod undocumented
556  
557 # If we already have a ref in $class, this means that the
558 # person wants to generate a <new> tag!
559 49 ▶ return $class->XML::Generator::util::tag('new', @_) if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 48 49
560  
561 my %options =
562 map {
563 48 /^:(std|standard) $/x ? ( escape => 'always',
564 conformance => 'strict' )
565 : /^:strict $/x ? ( conformance => 'strict' )
566 : /^:pretty(?:=(.+))?$/x ? ( escape => 'always',
567 conformance => 'strict',
568 pretty => ( defined $1 ? $1 : 2 ) )
569 : /^:(import |
570 99 ▶ stacked )$/x ? ( do { Carp::carp("Useless use of $_")
Branch: 6 branches
BranchTrueFalseTotal
/^:(std|standard) $/x ? : 1 98 99
/^:strict $/x ? : 4 94 98
/^:pretty(?:=(.+))?$/x ? : 7 87 94
defined $1 ? : 0 7 7
/^:(import | stacked )$/x ? : 5 82 87
unless (caller 1)[3] =~ /::import/ 1 4 5
571 unless (caller(1))[3] =~ /::import/;
572 5 ▶ () } )
Branch: 3 branches
BranchTrueFalseTotal
/^allowedXMLTags$/ ? : 0 82 82
/^qualifiedAttributes$/ ? : 1 81 82
/^filterInvalidChars$/ ? : 0 81 81
573 : /^allowedXMLTags$/ ? 'allowed_xml_tags'
574 : /^qualifiedAttributes$/ ? 'qualified_attributes'
575 : /^filterInvalidChars$/ ? 'filter_invalid_chars'
576 : $_
577 } @_;
578  
579 # We used to only accept certain options, but unfortunately this
580 # means that subclasses can't extend the list. As such, we now
581 # just make sure our default options are defined.
582 48 for (@optionsToInit) {
583 480 ▶ if (not defined $options{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{$_}) 413 67 480
584 413 $options{$_} = '';
585 }
586 }
587  
588 48 ▶ if ($options{'dtd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"dtd"}) 1 47 48
589 1 $options{'dtdtree'} = $class->XML::Generator::util::parse_dtd($options{'dtd'});
590 }
591  
592 48 ▶ if ($options{'conformance'} eq 'strict' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"conformance"} eq "strict" and $options{"empty"} eq "ignore") 1 47 48
Condition100%3/3
$options{"conformance"} eq "strict" and $options{"empty"} eq "ignore"
!ll&&!rl&&r
28191
MC/DC100%2/2
$options{"conformance"} eq "strict" and $options{"empty"} eq "ignore"
$options{"conformance"} eq "strict" $options{"empty"} eq "ignore"
Truth table100%3/3
A and B
A= $options{"conformance"} eq "strict"B= $options{"empty"} eq "ignore"
ABresult
0X0
100
111
593 $options{'empty'} eq 'ignore') {
594 1 Carp::croak "option 'empty' => 'ignore' not allowed while 'conformance' => 'strict'";
595 }
596  
597 47 ▶ if ($options{'escape'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'escape'}) { } 15 32 47
598 15 my $e = $options{'escape'};
599 15 $options{'escape'} = 0;
600 15 ▶ while ($e =~ /([-\w]+),?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /([-\w]+),?/g) 19 15 34
601 19 ▶ if ($1 eq 'always') {
Branch: 5 branches
BranchTrueFalseTotal
if ($1 eq 'always') { } 13 6 19
elsif ($1 eq 'high-bit') { } 3 3 6
elsif ($1 eq 'apos') { } 1 2 3
elsif ($1 eq 'even-entities') { } 1 1 2
elsif ($1) { } 1 0 1
602 13 $options{'escape'} |= XML::Generator::util::ESCAPE_ALWAYS()
603 | XML::Generator::util::ESCAPE_GT();
604 } elsif ($1 eq 'high-bit') {
605 3 $options{'escape'} |= XML::Generator::util::ESCAPE_HIGH_BIT();
606 } elsif ($1 eq 'apos') {
607 1 $options{'escape'} |= XML::Generator::util::ESCAPE_APOS();
608 } elsif ($1 eq 'even-entities') {
609 1 $options{'escape'} |= XML::Generator::util::ESCAPE_EVEN_ENTITIES();
610 } elsif ($1) {
611 1 ▶ if ($1 ne 'unescaped') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne "unescaped") 0 1 1
612 0 Carp::carp "option 'escape' => '$1' deprecated; use 'escape' => 'unescaped'";
613 }
614 1 $options{'escape'} |= XML::Generator::util::ESCAPE_TRUE()
615 | XML::Generator::util::ESCAPE_GT();
616 }
617 }
618 } else {
619 32 $options{'escape'} = 0;
620 }
621  
622 47 ▶ if (ref $options{'namespace'} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $options{'namespace'} eq 'ARRAY') { } 8 39 47
elsif ($options{'namespace'}) { } 0 39 39
623 8 ▶ if (@{ $options{'namespace'} } > 2 && (@{ $options{'namespace'} } % 2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$options{"namespace"};} > 2 and @{$options{"namespace"};} % 2 != 0) 0 8 8
Condition66%2/3
@{$options{"namespace"};} > 2 and @{$options{"namespace"};} % 2 != 0
!ll&&!rl&&r
710
MC/DC0%0/2
@{$options{"namespace"};} > 2 and @{$options{"namespace"};} % 2 != 0
@{$options{"namespace"};} > 2 @{$options{"namespace"};} % 2 != 0
Truth table66%2/3
A and B
A= @{$options{"namespace"};} > 2B= @{$options{"namespace"};} % 2 != 0
ABresult
0X0
100
111
624 0 Carp::croak "odd number of arguments for namespace";
625 }
626 } elsif ($options{'namespace'}) {
627 0 Carp::croak "namespace must be an array reference";
628 }
629  
630 47 ▶ if ($options{'conformance'} eq 'strict' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"conformance"} eq "strict" and $options{"filter_invalid_chars"} eq "") 18 29 47
Condition100%3/3
$options{"conformance"} eq "strict" and $options{"filter_invalid_chars"} eq ""
!ll&&!rl&&r
28118
MC/DC100%2/2
$options{"conformance"} eq "strict" and $options{"filter_invalid_chars"} eq ""
$options{"conformance"} eq "strict" $options{"filter_invalid_chars"} eq ""
Truth table100%3/3
A and B
A= $options{"conformance"} eq "strict"B= $options{"filter_invalid_chars"} eq ""
ABresult
0X0
100
111
631 $options{'filter_invalid_chars'} eq '') {
632 18 $options{'filter_invalid_chars'} = 1;
633 }
634  
635 47 my $this = bless \%options, $class;
636 47 $tag_factory{$this} = XML::Generator::util::c_tag($this);
637 47 return $this;
638 }
639  
640 # We use AUTOLOAD as a front-end to TAG so that we can
641 # create tags by name at will.
642  
643 sub AUTOLOAD {
644 108 ▶ my $this = shift;
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
645  
646 # The tag is whatever our sub name is, or 'AUTOLOAD'
647 108 ▶ my ($tag) = defined our $AUTOLOAD ? $AUTOLOAD =~ /.*::(.*)/ : 'AUTOLOAD';
Branch: 1 branch
BranchTrueFalseTotal
defined our $AUTOLOAD ? : 108 0 108
648  
649 108 undef $AUTOLOAD; # this ensures that future attempts to use tag 'AUTOLOAD' work.
650  
651 108 unshift @_, $this, $tag;
652  
653 108 goto &{ $tag_factory{$this} };
654 }
655  
656 # I wish there were a way to allow people to use tag 'DESTROY!'
657 # hmm, maybe xmlDESTROY?
658 32 ▶sub DESTROY { delete $tag_factory{$_[0]} }
Subroutine DESTROY: called CC=1 SCAR=0.0
659  
660 =head1 XML CONFORMANCE
661  
662 When the 'conformance' => 'strict' option is supplied, a number of
663 syntactic checks are enabled. All entity and attribute names are
664 checked to conform to the XML specification, which states that they must
665 begin with either an alphabetic character or an underscore and may then
666 consist of any number of alphanumerics, underscores, periods or hyphens.
667 Alphabetic and alphanumeric are interpreted according to the current
668 locale if 'use locale' is in effect and according to the Unicode standard
669 for Perl versions >= 5.6. Furthermore, entity or attribute names are not
670 allowed to begin with 'xml' (in any case), although a number of special
671 tags beginning with 'xml' are allowed (see L<"SPECIAL TAGS">). Note
672 that you can also supply an explicit list of allowed tags with the
673 'allowed_xml_tags' option.
674  
675 Also, the filter_invalid_chars option is automatically set to 1 unless it
676 is explicitly set to 0.
677  
678 =head1 SPECIAL TAGS
679  
680 The following special tags are available when running under strict
681 conformance (otherwise they don't act special):
682  
683 =head2 xmlpi
684  
685 Processing instruction; first argument is target, remaining arguments
686 are attribute, value pairs. Attribute names are syntax checked, values
687 are escaped.
688  
689 =cut
690  
691 # We handle a few special tags, but only if the conformance
692 # is 'strict'. If not, we just fall back to XML::Generator::util::tag.
693  
694 sub xmlpi {
695 4 ▶ my $this = shift;
Subroutine xmlpi: called CC=4 SCAR=13.9
Pod documented
696  
697 return $this->XML::Generator::util::tag('xmlpi', @_)
698 4 ▶ unless $this->{conformance} eq 'strict';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"conformance"} eq "strict" 0 4 4
699  
700 4 my $xml;
701 4 my $tgt = shift;
702  
703 4 $this->XML::Generator::util::ck_syntax($tgt);
704  
705 4 $xml = "<?$tgt";
706 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3 4
707 1 my %atts = @_;
708 1 ▶ while (my($k, $v) = each %atts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %atts) 1 1 2
709 1 $this->XML::Generator::util::ck_syntax($k);
710 XML::Generator::util::escape($v,
711 XML::Generator::util::ESCAPE_FILTER_INVALID_CHARS() |
712 XML::Generator::util::ESCAPE_ATTR() |
713 1 $this->{'escape'});
714 1 $xml .= qq{ $k="$v"};
715 }
716 }
717 4 $xml .= "?>";
718  
719 4 return XML::Generator::pi->new([$xml]);
720 }
721  
722 =head2 xmlcmnt
723  
724 Comment. Arguments are concatenated and placed inside <!-- ... -->
725 comment delimiters. Any occurences of '--' in the concatenated arguments
726 are converted to '&#45;&#45;'
727  
728 =cut
729  
730 sub xmlcmnt {
731 5 ▶ my $this = shift;
Subroutine xmlcmnt: called CC=2 SCAR=7.0
Pod documented
732  
733 return $this->XML::Generator::util::tag('xmlcmnt', @_)
734 5 ▶ unless $this->{conformance} eq 'strict';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"conformance"} eq "strict" 0 5 5
735  
736 5 my $xml = join '', @_;
737  
738 # double dashes are illegal; change them to '&#45;&#45;'
739 5 $xml =~ s/--/&#45;&#45;/g;
740 5 XML::Generator::util::filter($xml);
741 5 $xml = "<!-- $xml -->";
742  
743 5 return XML::Generator::comment->new([$xml]);
744 }
745  
746 =head2 xmldecl(@args)
747  
748 Declaration. This can be used to specify the version, encoding, and
749 other XML-related declarations (i.e., anything inside the <?xml?> tag).
750 @args can be used to control what is output, as keyword-value pairs.
751  
752 By default, the version is set to the value specified in the constructor,
753 or to 1.0 if it was not specified. This can be overridden by providing a
754 'version' key in @args. If you do not want the version at all, explicitly
755 provide undef as the value in @args.
756  
757 By default, the encoding is set to the value specified in the constructor;
758 if no value was specified, the encoding will be left out altogether.
759 Provide an 'encoding' key in @args to override this.
760  
761 If a dtd was set in the constructor, the standalone attribute of the
762 declaration will be set to 'no' and the doctype declaration will be
763 appended to the XML declartion, otherwise the standalone attribute will
764 be set to 'yes'. This can be overridden by providing a 'standalone'
765 key in @args. If you do not want the standalone attribute to show up,
766 explicitly provide undef as the value.
767  
768 =cut
769  
770 sub xmldecl {
771 8 ▶ my($this, @args) = @_;
Subroutine xmldecl: called CC=15 SCAR=27.1
Pod documented
772  
773 return $this->XML::Generator::util::tag('xmldecl', @_)
774 8 ▶ unless $this->{conformance} eq 'strict';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"conformance"} eq "strict" 0 8 8
775  
776 8 ▶ my $version = $this->{'version'} || '1.0';
Condition100%2/2
$this->{'version'} || '1.0'
l!l
35
MC/DC100%1/1
$this->{'version'} || '1.0'
$this->{'version'}
Truth table100%2/2
A
A= $this->{'version'}
Aresult
11
00
777  
778 # there's no explicit support for encodings yet, but at the
779 # least we can know to put it in the declaration
780 8 my $encoding = $this->{'encoding'};
781  
782 # similarly, although we don't do anything with DTDs yet, we
783 # recognize a 'dtd' => [ ... ] option to the constructor, and
784 # use it to create a <!DOCTYPE ...> and to indicate that this
785 # document can't stand alone.
786 8 my $doctype = $this->xmldtd($this->{dtd});
787 8 ▶ my $standalone = $doctype ? "no" : "yes";
Branch: 1 branch
BranchTrueFalseTotal
$doctype ? : 1 7 8
788  
789 8 ▶ for (my $i = 0; $i < $#args; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#args) 6 8 14
790 6 ▶ if ($args[$i] eq 'version' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args[$i] eq 'version') { } 2 4 6
elsif ($args[$i] eq 'encoding') { } 2 2 4
elsif ($args[$i] eq 'standalone') { } 2 0 2
791 2 $version = $args[$i + 1];
792 } elsif ($args[$i] eq 'encoding' ) {
793 2 $encoding = $args[$i + 1];
794 } elsif ($args[$i] eq 'standalone') {
795 2 $standalone = $args[$i + 1];
796 } else {
797 0 Carp::croak("Unrecognized argument '$args[$i]'");
798 }
799 }
800  
801 8 ▶ $version = qq{ version="$version"} if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 7 1 8
802 8 ▶ $encoding = qq{ encoding="$encoding"} if defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoding 2 6 8
803 8 ▶ $standalone = qq{ standalone="$standalone"} if defined $standalone;
Branch: 1 branch
BranchTrueFalseTotal
if defined $standalone 7 1 8
804  
805 8 ▶ $encoding ||= '';
Condition100%2/2
$encoding ||= ""
l!l
26
MC/DC100%1/1
$encoding ||= ""
$encoding
Truth table100%2/2
A
A= $encoding
Aresult
11
00
806 8 ▶ $version ||= '';
Condition100%2/2
$version ||= ""
l!l
71
MC/DC100%1/1
$version ||= ""
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
807 8 ▶ $standalone ||= '';
Condition100%2/2
$standalone ||= ""
l!l
71
MC/DC100%1/1
$standalone ||= ""
$standalone
Truth table100%2/2
A
A= $standalone
Aresult
11
00
808  
809 8 my $xml = "<?xml$version$encoding$standalone?>";
810 8 ▶ $xml .= "\n$doctype" if $doctype;
Branch: 1 branch
BranchTrueFalseTotal
if $doctype 1 7 8
811  
812 8 $xml = "$xml\n";
813  
814 8 return $xml;
815 }
816  
817 =head2 xmldtd
818  
819 DTD <!DOCTYPE> tag creation. The format of this method is different from
820 others. Since DTD's are global and cannot contain namespace information,
821 the first argument should be a reference to an array; the elements are
822 concatenated together to form the DTD:
823  
824 print $xml->xmldtd([ 'html', 'PUBLIC', $xhtml_w3c, $xhtml_dtd ])
825  
826 This would produce the following declaration:
827  
828 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
829 "DTD/xhtml1-transitional.dtd">
830  
831 Assuming that $xhtml_w3c and $xhtml_dtd had the correct values.
832  
833 Note that you can also specify a DTD on creation using the new() method's
834 dtd option.
835  
836 =cut
837  
838 sub xmldtd {
839 8 ▶ my $this = shift;
Subroutine xmldtd: called CC=3 SCAR=11.0
Pod documented
840 8 ▶ my $dtd = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
17
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
841  
842 # return the appropriate <!DOCTYPE> thingy
843 1 ▶ $dtd ? return(qq{<!DOCTYPE } . (join ' ', @{$dtd}) . q{>})
Branch: 1 branch
BranchTrueFalseTotal
$dtd ? : 1 0 1
844 : return('');
845 }
846  
847 =head2 xmlcdata
848  
849 Character data section; arguments are concatenated and placed inside
850 <![CDATA[ ... ]]> character data section delimiters. Any occurences of
851 ']]>' in the concatenated arguments are converted to ']]&gt;'.
852  
853 =cut
854  
855 sub xmlcdata {
856 3 ▶ my $this = shift;
Subroutine xmlcdata: called CC=2 SCAR=7.0
Pod documented
857  
858 $this->XML::Generator::util::tag('xmlcdata', @_)
859 3 ▶ unless $this->{conformance} eq 'strict';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"conformance"} eq "strict" 0 3 3
860  
861 3 my $xml = join '', @_;
862  
863 # ]]> is not allowed; change it to ]]&gt;
864 3 $xml =~ s/]]>/]]&gt;/g;
865 3 XML::Generator::util::filter($xml);
866 3 $xml = "<![CDATA[$xml]]>";
867  
868 3 return XML::Generator::cdata->new([$xml]);
869 }
870  
871 =head2 xml
872  
873 "Final" XML document. Must be called with one and exactly one
874 XML::Generator-produced XML document. Any combination of
875 XML::Generator-produced XML comments or processing instructions may
876 also be supplied as arguments. Prepends an XML declaration, and
877 re-blesses the argument into a "final" class that can't be embedded.
878  
879 =cut
880  
881 sub xml {
882 6 ▶ my $this = shift;
Subroutine xml: called CC=8 SCAR=20.8
Pod documented
883  
884 return $this->XML::Generator::util::tag('xml', @_)
885 6 ▶ unless $this->{conformance} eq 'strict';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"conformance"} eq "strict" 1 5 6
886  
887 5 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 4 5
888 1 Carp::croak "usage: object->xml( (COMMENT | PI)* XML (COMMENT | PI)* )";
889 }
890  
891 4 my $got_root = 0;
892 4 foreach my $arg (@_) {
893 6 ▶ next if UNIVERSAL::isa($arg, 'XML::Generator::comment') ||
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($arg, "XML::Generator::comment") or UNIVERSAL::isa($arg, "XML::Generator::pi") 2 4 6
Condition100%3/3
UNIVERSAL::isa($arg, "XML::Generator::comment") or UNIVERSAL::isa($arg, "XML::Generator::pi")
l!l&&r!l&&!r
114
MC/DC100%2/2
UNIVERSAL::isa($arg, "XML::Generator::comment") or UNIVERSAL::isa($arg, "XML::Generator::pi")
UNIVERSAL::isa($arg, "XML::Generator::comment") UNIVERSAL::isa($arg, "XML::Generator::pi")
Truth table100%3/3
A or B
A= UNIVERSAL::isa($arg, "XML::Generator::comment")B= UNIVERSAL::isa($arg, "XML::Generator::pi")
ABresult
1X1
011
000
894 UNIVERSAL::isa($arg, 'XML::Generator::pi');
895 4 ▶ if (UNIVERSAL::isa($arg, 'XML::Generator::overload')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'XML::Generator::overload')) { } 3 1 4
896 3 ▶ if ($got_root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_root) 0 3 3
897 0 Carp::croak "arguments to xml() can contain only one XML document";
898 }
899 3 $got_root = 1;
900 } else {
901 1 Carp::croak "arguments to xml() must be comments, processing instructions or XML documents";
902 }
903 }
904  
905 3 return XML::Generator::final->new([$this->xmldecl(), @_]);
906 }
907  
908 =head1 CREATING A SUBCLASS
909  
910 For a simpler way to implement subclass-like behavior, see L<"STACKABLE
911 AUTOLOADs">.
912  
913 At times, you may find it desireable to subclass XML::Generator. For
914 example, you might want to provide a more application-specific interface
915 to the XML generation routines provided. Perhaps you have a custom
916 database application and would really like to say:
917  
918 my $dbxml = new XML::Generator::MyDatabaseApp;
919 print $dbxml->xml($dbxml->custom_tag_handler(@data));
920  
921 Here, custom_tag_handler() may be a method that builds a recursive XML
922 structure based on the contents of @data. In fact, it may even be named
923 for a tag you want generated, such as authors(), whose behavior changes
924 based on the contents (perhaps creating recursive definitions in the
925 case of multiple elements).
926  
927 Creating a subclass of XML::Generator is actually relatively
928 straightforward, there are just three things you have to remember:
929  
930 1. All of the useful utilities are in XML::Generator::util.
931  
932 2. To construct a tag you simply have to call SUPER::tagname,
933 where "tagname" is the name of your tag.
934  
935 3. You must fully-qualify the methods in XML::Generator::util.
936  
937 So, let's assume that we want to provide a custom HTML table() method:
938  
939 package XML::Generator::CustomHTML;
940 use base 'XML::Generator';
941  
942 sub table {
943 my $self = shift;
944
945 # parse our args to get namespace and attribute info
946 my($namespace, $attr, @content) =
947 $self->XML::Generator::util::parse_args(@_)
948  
949 # check for strict conformance
950 if ( $self->XML::Generator::util::config('conformance') eq 'strict' ) {
951 # ... special checks ...
952 }
953  
954 # ... special formatting magic happens ...
955  
956 # construct our custom tags
957 return $self->SUPER::table($attr, $self->tr($self->td(@content)));
958 }
959  
960 That's pretty much all there is to it. We have to explicitly call
961 SUPER::table() since we're inside the class's table() method. The others
962 can simply be called directly, assuming that we don't have a tr() in the
963 current package.
964  
965 If you want to explicitly create a specific tag by name, or just want a
966 faster approach than AUTOLOAD provides, you can use the tag() method
967 directly. So, we could replace that last line above with:
968  
969 # construct our custom tags
970 return $self->XML::Generator::util::tag('table', $attr, ...);
971  
972 Here, we must explicitly call tag() with the tag name itself as its first
973 argument so it knows what to generate. These are the methods that you might
974 find useful:
975  
976 =over 4
977  
978 =item XML::Generator::util::parse_args()
979  
980 This parses the argument list and returns the namespace (arrayref), attributes
981 (hashref), and remaining content (array), in that order.
982  
983 =item XML::Generator::util::tag()
984  
985 This does the work of generating the appropriate tag. The first argument must
986 be the name of the tag to generate.
987  
988 =item XML::Generator::util::config()
989  
990 This retrieves options as set via the new() method.
991  
992 =item XML::Generator::util::escape()
993  
994 This escapes any illegal XML characters.
995  
996 =back
997  
998 Remember that all of these methods must be fully-qualified with the
999 XML::Generator::util package name. This is because AUTOLOAD is used by
1000 the main XML::Generator package to create tags. Simply calling parse_args()
1001 will result in a set of XML tags called <parse_args>.
1002  
1003 Finally, remember that since you are subclassing XML::Generator, you do
1004 not need to provide your own new() method. The one from XML::Generator
1005 is designed to allow you to properly subclass it.
1006  
1007 =head1 STACKABLE AUTOLOADs
1008  
1009 As a simpler alternative to traditional subclassing, the C<AUTOLOAD>
1010 that C<use XML::Generator;> exports can be configured to work with a
1011 pre-defined C<AUTOLOAD> with the ':stacked' option. Simply ensure that
1012 your C<AUTOLOAD> is defined before C<use XML::Generator ':stacked';>
1013 executes. The C<AUTOLOAD> will get a chance to run first; the subroutine
1014 name will be in your C<$AUTOLOAD> as normal. Return an empty list to let
1015 the default XML::Generator C<AUTOLOAD> run or any other value to abort it.
1016 This value will be returned as the result of the original method call.
1017  
1018 If there is no C<import> defined, XML::Generator will create one.
1019 All that this C<import> does is export AUTOLOAD, but that lets your
1020 package be used as if it were a subclass of XML::Generator.
1021  
1022 An example will help:
1023  
1024 package MyGenerator;
1025  
1026 my %entities = ( copy => '&copy;',
1027 nbsp => '&nbsp;', ... );
1028  
1029 sub AUTOLOAD {
1030 my($tag) = our $AUTOLOAD =~ /.*::(.*)/;
1031  
1032 return $entities{$tag} if defined $entities{$tag};
1033 return;
1034 }
1035  
1036 use XML::Generator qw(:pretty :stacked);
1037  
1038 This lets someone do:
1039  
1040 use MyGenerator;
1041  
1042 print html(head(title("My Title", copy())));
1043  
1044 Producing:
1045  
1046 <html>
1047 <head>
1048 <title>My Title&copy;</title>
1049 </head>
1050 </html>
1051  
1052 =cut
1053  
1054 package XML::Generator::util;
1055  
1056 # The ::util package space actually has all the utilities
1057 # that do all the work. It must be separate from the
1058 # main XML::Generator package space since named subs will
1059 # interfere with the workings of AUTOLOAD otherwise.
1060  
1061 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1062 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1063  
1064 1 ▶use constant ESCAPE_TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1065 1 ▶use constant ESCAPE_ALWAYS => 1<<1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1066 1 ▶use constant ESCAPE_HIGH_BIT => 1<<2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1067 1 ▶use constant ESCAPE_APOS => 1<<3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1068 1 ▶use constant ESCAPE_ATTR => 1<<4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1069 1 ▶use constant ESCAPE_GT => 1<<5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1070 1 ▶use constant ESCAPE_EVEN_ENTITIES => 1<<6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1071 1 ▶use constant ESCAPE_FILTER_INVALID_CHARS => 1<<7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1072  
1073 sub parse_args {
1074 # this parses the args and returns a namespace and attr
1075 # if either were specified, with the remainer of the
1076 # arguments (the content of the tag) in @args. call as:
1077 #
1078 # ($namespace, $attr, @args) = parse_args(@args);
1079
1080 128 ▶ my($this, @args) = @_;
Subroutine parse_args: called CC=14 SCAR=26.6
1081  
1082 128 my($namespace);
1083 128 my($attr) = ('');
1084  
1085 # check for supplied namespace
1086 128 ▶ if (ref $args[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "ARRAY") 30 98 128
1087 30 ▶ $namespace = [ map { defined $_ && $_ eq '#default' ? undef : $_ } @{shift @args} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $_ eq '#default' ? : 3 49 52
Condition100%3/3
defined $_ and $_ eq "#default"
!ll&&!rl&&r
2473
MC/DC100%2/2
defined $_ and $_ eq "#default"
defined $_ $_ eq "#default"
Truth table100%3/3
A and B
A= defined $_B= $_ eq "#default"
ABresult
0X0
100
111
1088 30 ▶ if (@$namespace > 2 && (@$namespace % 2) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$namespace > 2 and @$namespace % 2 != 0) 0 30 30
Condition66%2/3
@$namespace > 2 and @$namespace % 2 != 0
!ll&&!rl&&r
2730
MC/DC0%0/2
@$namespace > 2 and @$namespace % 2 != 0
@$namespace > 2 @$namespace % 2 != 0
Truth table66%2/3
A and B
A= @$namespace > 2B= @$namespace % 2 != 0
ABresult
0X0
100
111
1089 0 croak "odd number of arguments for namespace";
1090 }
1091 }
1092  
1093 # get globally-set namespace (from new)
1094 128 ▶ unless ($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace) 98 30 128
1095 98 ▶ $namespace = [ map { defined $_ && $_ eq '#default' ? undef : $_ } @{ $this->{'namespace'} || [] } ];
Branch: 2 branches
BranchTrueFalseTotal
defined $_ && $_ eq '#default' ? : 0 35 35
$this->{"namespace"} or [] 76 22 98
Condition66%2/3
defined $_ and $_ eq "#default"
!ll&&!rl&&r
1340
MC/DC0%0/2
defined $_ and $_ eq "#default"
defined $_ $_ eq "#default"
Truth table66%2/3
A and B
A= defined $_B= $_ eq "#default"
ABresult
0X0
100
111
1096 }
1097  
1098 128 ▶ if (@$namespace == 1) { unshift @$namespace, undef }
Branch: 1 branch
BranchTrueFalseTotal
if (@$namespace == 1) 25 103 128
1099  
1100 # check for supplied attributes
1101 128 ▶ if (ref $args[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 28 100 128
1102 28 $attr = shift @args;
1103 28 ▶ if ($this->{conformance} eq 'strict') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"conformance"} eq "strict") 11 17 28
1104 $this->XML::Generator::util::ck_syntax($_)
1105 11 for map split(/:/), keys %$attr;
1106 }
1107 }
1108  
1109 126 return ($namespace, $attr, @args);
1110 }
1111  
1112 # This routine is what handles all the automatic tag creation.
1113 # We maintain it as a separate method so that subclasses can
1114 # override individual tags and then call SUPER::tag() to create
1115 # the tag automatically. This is not possible if only AUTOLOAD
1116 # is used, since there is no way to then pass in the name of
1117 # the tag.
1118  
1119 sub tag {
1120 2 ▶ my $sub = XML::Generator::util::c_tag($_[0]);
Subroutine tag: called CC=2 SCAR=7.1
1121 2 ▶ goto &{ $sub } if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 2 0 2
1122 }
1123
1124 # Generate a closure that encapsulates all the behavior to generate a tag
1125 sub c_tag {
1126 49 ▶ my $arg = shift;
Subroutine c_tag: called CC=5 SCAR=16.1
1127  
1128 49 my $strict = $arg->{'conformance'} eq 'strict';
1129 49 my $escape = $arg->{'escape'};
1130 49 my $empty = $arg->{'empty'};
1131 my $indent = $arg->{'pretty'} =~ /^[^0-9]/
1132 ? $arg->{'pretty'}
1133 : $arg->{'pretty'}
1134 49 ▶ ? " " x $arg->{'pretty'}
Branch: 2 branches
BranchTrueFalseTotal
$arg->{'pretty'} =~ /^[^0-9]/ ? : 0 49 49
$arg->{'pretty'} ? : 13 36 49
1135 : "";
1136 49 ▶ if ($arg->{'filter_invalid_chars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"filter_invalid_chars"}) 19 30 49
1137 19 $escape |= ESCAPE_FILTER_INVALID_CHARS;
1138 }
1139  
1140 49 ▶ my $blessClass = $indent ? 'XML::Generator::pretty' : 'XML::Generator::overload';
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 13 36 49
1141  
1142 return sub {
1143 128 ▶ my $this = shift;
Subroutine __ANON__: called CC=52 SCAR=39.9
1144 128 ▶ my $tag = shift || return undef; # catch for bad usage
Condition50%1/2
shift() || (return undef)
l!l
1280
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1145  
1146 # parse our argument list to check for hashref/arrayref properties
1147 128 my($namespace, $attr, @args) = $this->XML::Generator::util::parse_args(@_);
1148  
1149 126 ▶ $this->XML::Generator::util::ck_syntax($tag) if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 52 74 126
1150  
1151 # check for attempt to embed "final" document
1152 123 for (@args) {
1153 159 ▶ if (UNIVERSAL::isa($_, 'XML::Generator::final')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_, "XML::Generator::final")) 1 158 159
1154 1 croak("cannot embed XML document");
1155 }
1156 }
1157  
1158 # Deal with escaping if required
1159 122 ▶ if ($escape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape) { } 53 69 122
1160 53 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 12 41 53
1161 12 foreach my $key (keys %{$attr}) {
1162 14 ▶ next unless defined($attr->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr->{$key} 0 14 14
1163 14 XML::Generator::util::escape($attr->{$key}, ESCAPE_ATTR() | $escape);
1164 }
1165 }
1166 53 for (@args) {
1167 112 ▶ next unless defined($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 112 112
1168  
1169 # perform escaping, except on sub-documents or simple scalar refs
1170 112 ▶ if (ref $_ eq "SCALAR") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'SCALAR') { } 2 110 112
elsif (not UNIVERSAL::isa($_, 'XML::Generator::overload')) { } 80 30 110
1171 # un-ref it
1172 2 $_ = $$_;
1173 } elsif (! UNIVERSAL::isa($_, 'XML::Generator::overload') ) {
1174 80 XML::Generator::util::escape($_, $escape);
1175 }
1176 }
1177 } else {
1178 # un-ref simple scalar refs
1179 69 for (@args) {
1180 46 ▶ $_ = $$_ if ref $_ eq "SCALAR";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "SCALAR" 0 46 46
1181 }
1182 }
1183  
1184 122 my $prefix = '';
1185 122 ▶ $prefix = $namespace->[0] . ":" if $namespace && defined $namespace->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $namespace and defined $namespace->[0] 25 97 122
Condition66%2/3
$namespace and defined $namespace->[0]
!ll&&!rl&&r
09725
MC/DC50%1/2
$namespace and defined $namespace->[0]
$namespace defined $namespace->[0]
Truth table66%2/3
A and B
A= $namespaceB= defined $namespace->[0]
ABresult
0X0
100
111
1186 122 my $xml = "<$prefix$tag";
1187  
1188 122 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 26 96 122
1189 26 ▶ while (my($k, $v) = each %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 28 26 54
1190 28 ▶ next unless defined $k and defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and defined $v 0 28 28
Condition33%1/3
defined $k and defined $v
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $k and defined $v
defined $k defined $v
Truth table33%1/3
A and B
A= defined $kB= defined $v
ABresult
0X0
100
111
1191 28 ▶ if ($strict) {
Branch: 2 branches
BranchTrueFalseTotal
if ($strict) { } 11 17 28
elsif ($this->{'qualified_attributes'}) { } 2 15 17
1192 # allow supplied namespace in attribute names
1193 11 ▶ if ($k =~ s/^([^:]+)://) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ s/^([^:]+)://) { } 4 7 11
elsif ($prefix and $this->{'qualified_attributes'}) { } 0 7 7
1194 4 $this->XML::Generator::util::ck_syntax($k);
1195 4 $k = "$1:$k";
1196 ▶ } elsif ($prefix && $this->{'qualified_attributes'}) {
Condition66%2/3
$prefix and $this->{"qualified_attributes"}
!ll&&!rl&&r
520
MC/DC0%0/2
$prefix and $this->{"qualified_attributes"}
$prefix $this->{"qualified_attributes"}
Truth table66%2/3
A and B
A= $prefixB= $this->{"qualified_attributes"}
ABresult
0X0
100
111
1197 0 $this->XML::Generator::util::ck_syntax($k);
1198 0 $k = "$prefix$k";
1199 } else {
1200 7 $this->XML::Generator::util::ck_syntax($k);
1201 }
1202 } elsif ($this->{'qualified_attributes'}) {
1203 2 ▶ if ($k !~ /^[^:]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /^[^:]+:/) 1 1 2
1204 1 $k = "$prefix$k";
1205 }
1206 }
1207 28 $xml .= qq{ $k="$v"};
1208 }
1209 }
1210  
1211 122 my @xml;
1212  
1213 122 ▶ if (@args || $empty eq 'close') {
Branch: 3 branches
BranchTrueFalseTotal
if (@args or $empty eq 'close') { } 85 37 122
elsif ($empty eq 'ignore') { } 1 36 37
elsif ($empty eq 'compact') { } 1 35 36
Condition100%3/3
@args or $empty eq "close"
l!l&&r!l&&!r
84137
MC/DC100%2/2
@args or $empty eq "close"
@args $empty eq "close"
Truth table100%3/3
A or B
A= @argsB= $empty eq "close"
ABresult
1X1
011
000
1214 85 ▶ if ($empty eq 'args' && @args == 1 && ! defined $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty eq 'args' and @args == 1 and not defined $args[0]) { } 1 84 85
Condition83%5/6
$empty eq "args" and @args == 1 and not defined $args[0]
!ll&&!rl&&r
8221
$empty eq "args" and @args == 1
!ll&&!rl&&r
8203
MC/DC66%2/3
$empty eq "args" and @args == 1 and not defined $args[0]
$empty eq "args" @args == 1 not defined $args[0]
Truth table75%3/4
A and B and not(C)
A= $empty eq "args"B= @args == 1C= not defined $args[0]
ABCresult
0XX0
10X0
1100
1111
1215 1 @xml = ($xml .= ' />');
1216 } else {
1217 84 $xml .= '>';
1218 84 ▶ if ($indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent) { } 45 39 84
1219 45 my $prettyend = '';
1220  
1221 45 foreach my $arg (@args) {
1222 57 ▶ next unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 57 57
1223 57 ▶ if ( UNIVERSAL::isa($arg, 'XML::Generator::cdata' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, 'XML::Generator::cdata')) { } 2 55 57
1224 2 my $copy = $xml;
1225 2 push @xml, $copy, $arg;
1226 2 $xml = '';
1227 } else {
1228 55 ▶ if ( UNIVERSAL::isa($arg, 'XML::Generator::overload') &&
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::pi")) 33 22 55
Condition100%3/3
UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::pi")
!ll&&!rl&&r
21133
MC/DC100%2/2
UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::pi")
UNIVERSAL::isa($arg, "XML::Generator::overload") not UNIVERSAL::isa($arg, "XML::Generator::pi")
Truth table100%3/3
A and not(B)
A= UNIVERSAL::isa($arg, "XML::Generator::overload")B= not UNIVERSAL::isa($arg, "XML::Generator::pi")
ABresult
0X0
100
111
1229 ! UNIVERSAL::isa($arg, 'XML::Generator::pi') ) {
1230 33 $xml .= "\n$indent";
1231 33 $prettyend = "\n";
1232 33 ▶ XML::Generator::util::_fixupNS($namespace, $arg) if ref $arg->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg->[0] 32 1 33
1233  
1234 33 my @cdata;
1235 33 for my $i (0..$#$arg) {
1236 64 ▶ if (UNIVERSAL::isa($arg->[$i], 'XML::Generator::cdata')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg->[$i], "XML::Generator::cdata")) 1 63 64
1237 1 push @cdata, $arg->[$i];
1238 1 $arg->[$i] = "\001";
1239 }
1240 }
1241  
1242 33 $arg =~ s/\n/\n$indent/gs;
1243  
1244 33 ▶ if (@cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cdata) 1 32 33
1245 1 my @pieces = split "\001", $arg;
1246  
1247 1 my $copy = $xml;
1248 1 push @xml, $copy;
1249 1 $xml = '';
1250 1 $arg = '';
1251  
1252 1 for my $i (0..$#pieces) {
1253 2 ▶ if (defined $cdata[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cdata[$i]) { } 1 1 2
1254 1 push @xml, $pieces[$i], $cdata[$i];
1255 } else {
1256 1 push @xml, $pieces[$i];
1257 }
1258 }
1259 }
1260 }
1261 55 $xml .= "$arg";
1262 }
1263 }
1264 45 $xml .= $prettyend;
1265 45 push @xml, ($xml, "</$prefix$tag>");
1266 } else {
1267 39 @xml = $xml;
1268 39 foreach my $arg (grep defined, @args) {
1269 100 ▶ if ( UNIVERSAL::isa($arg, 'XML::Generator::overload') &&
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::cdata") || UNIVERSAL::isa($arg, "XML::Generator::pi")) 16 84 100
Condition83%5/6
UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::cdata") || UNIVERSAL::isa($arg, "XML::Generator::pi")
!ll&&!rl&&r
83116
UNIVERSAL::isa($arg, 'XML::Generator::cdata') || UNIVERSAL::isa($arg, 'XML::Generator::pi')
l!l&&r!l&&!r
0116
MC/DC66%2/3
UNIVERSAL::isa($arg, "XML::Generator::overload") and not UNIVERSAL::isa($arg, "XML::Generator::cdata") || UNIVERSAL::isa($arg, "XML::Generator::pi")
UNIVERSAL::isa($arg, "XML::Generator::overload") UNIVERSAL::isa($arg, 'XML::Generator::cdata') UNIVERSAL::isa($arg, 'XML::Generator::pi')
Truth table75%3/4
A and not(B || C)
A= UNIVERSAL::isa($arg, "XML::Generator::overload")B= UNIVERSAL::isa($arg, 'XML::Generator::cdata')C= UNIVERSAL::isa($arg, 'XML::Generator::pi')
ABCresult
0XX0
11X0
1010
1001
1270 (! ( UNIVERSAL::isa($arg, 'XML::Generator::cdata' ) ||
1271 UNIVERSAL::isa($arg, 'XML::Generator::pi' )))) {
1272 16 ▶ XML::Generator::util::_fixupNS($namespace, $arg) if ref $arg->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg->[0] 15 1 16
1273 }
1274 100 push @xml, $arg;
1275 }
1276 39 push @xml, "</$prefix$tag>";
1277 }
1278 }
1279 } elsif ($empty eq 'ignore') {
1280 1 @xml = ($xml .= '>');
1281 } elsif ($empty eq 'compact') {
1282 1 @xml = ($xml .= '/>');
1283 } else {
1284 35 @xml = ($xml .= ' />');
1285 }
1286  
1287 122 ▶ unshift @xml, $namespace if $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace 122 0 122
1288  
1289 122 return $blessClass->new(\@xml);
1290 49 };
1291 }
1292  
1293 sub _fixupNS {
1294 # remove namespaces
1295 # if prefix
1296 # if prefix and uri match one we have, remove them from child
1297 # if prefix does not match one we have, remove it and uri
1298 # from child and add them to us
1299 # no prefix
1300 # if we have an explicit default namespace and the child has the
1301 # same one, remove it from the child
1302 # if we have an explicit default namespace and the child has a
1303 # different one, leave it alone
1304 # if we have an explicit default namespace and the child has none,
1305 # add an empty default namespace to child
1306 47 ▶ my($namespace, $o) = @_;
Subroutine _fixupNS: called CC=18 SCAR=29.1
1307 47 my @n = @{$o->[0]};
1308 47 my $sawDefault = 0;
1309 47 ▶ for (my $i = 0; $i < $#n; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#n) 33 47 80
1310 33 ▶ if (defined $n[$i]) { # namespace w/ prefix
Branch: 2 branches
BranchTrueFalseTotal
if (defined $n[$i]) { } 19 14 33
elsif (defined $n[$i + 1]) { } 11 3 14
1311 19 my $flag = 0;
1312 19 ▶ for (my $j = 0; $j < $#$namespace; $j+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $namespace->$#*) 18 12 30
1313 18 ▶ next unless defined $namespace->[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespace->[$j] 10 8 18
1314 8 ▶ if ($namespace->[$j] eq $n[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace->[$j] eq $n[$i]) 7 1 8
1315 7 $flag = 1;
1316 7 ▶ if ($namespace->[$j+1] ne $n[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$namespace[$j + 1] ne $n[$i + 1]) 0 7 7
1317 0 $flag = 2;
1318 }
1319 7 last;
1320 }
1321 }
1322 19 ▶ if (!$flag) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $flag) { } 12 7 19
elsif ($flag == 1) { } 7 0 7
1323 12 push @$namespace, splice @n, $i, 2;
1324 12 $i-=2;
1325 } elsif ($flag == 1) {
1326 7 splice @n, $i, 2;
1327 7 $i-=2;
1328 }
1329 } elsif (defined $n[$i+1]) { # default namespace
1330 11 $sawDefault = 1;
1331 11 ▶ for (my $j = 0; $j < $#$namespace; $j+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $namespace->$#*) 7 11 18
1332 7 ▶ next if defined $namespace->[$j];
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace->[$j] 2 5 7
1333 5 ▶ if ($namespace->[$j+1] eq $n[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$namespace[$j + 1] eq $n[$i + 1]) 4 1 5
1334 4 splice @n, $i, 2;
1335 4 $i-=2;
1336 }
1337 }
1338 }
1339 }
1340  
1341 # check to see if we need to add explicit default namespace of "" to child
1342 47 ▶ if (! @{ $o->[0] } &&
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$o->[0];} || $sawDefault and grep {defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2];} 0 .. $namespace->$#* / 2) 0 47 47
Condition66%4/6
not @{$o->[0];} || $sawDefault and grep {defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2];} 0 .. $namespace->$#* / 2
!ll&&!rl&&r
29180
@{$o->[0];} || $sawDefault
l!l&&r!l&&!r
29018
MC/DC0%0/3
not @{$o->[0];} || $sawDefault and grep {defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2];} 0 .. $namespace->$#* / 2
@{$o->[0];} $sawDefault grep {defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2];} 0 .. $namespace->$#* / 2
Truth table50%2/4
not(A || B) and C
A= @{$o->[0];}B= $sawDefaultC= grep {defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2];} 0 .. $namespace->$#* / 2
ABCresult
1XX0
01X0
0000
0011
1343 ! $sawDefault &&
1344 18 ▶ grep { defined $namespace->[$_ * 2 + 1] &&
Branch: 1 branch
BranchTrueFalseTotal
defined $$namespace[$_ * 2 + 1] and not defined $$namespace[$_ * 2] 1 17 18
1345 ! defined $namespace->[$_ * 2 ] } 0..($#$namespace/2)) {
1346 0 push @n, undef, "";
1347 }
1348  
1349 47 ▶ if (@n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@n) { } 10 37 47
1350 10 $o->[0] = [@n];
1351 } else {
1352 37 splice @$o, 0, 1;
1353 }
1354 }
1355  
1356 # Fetch and store config values (those set via new())
1357 # This is only here for subclasses
1358  
1359 sub config {
1360 0 ▶ my $this = shift;
Subroutine config: not called CC=3 SCAR=24.8
1361 0 ▶ my $key = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1362 @_ ? $this->{$key} = $_[0]
1363 0 ▶ : $this->{$key};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1364 }
1365  
1366 # Collect all escaping into one place
1367 sub escape {
1368 # $_[0] is the argument, $_[1] are the flags
1369 140 ▶ return unless defined $_[0];
Subroutine escape: called CC=13 SCAR=25.7
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 3 137 140
1370  
1371 137 my $f = $_[1];
1372 137 ▶ if ($f & ESCAPE_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 2) { } 71 66 137
1373 71 ▶ if ($f & ESCAPE_EVEN_ENTITIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 64) { } 1 70 71
1374 1 $_[0] =~ s/&/&amp;/g;
1375 } else {
1376 70 $_[0] =~ s/&(?!(?:#[0-9]+|#x[0-9a-fA-F]+|\w+);)/&amp;/g;
1377 }
1378  
1379 71 $_[0] =~ s/</&lt;/g;
1380 71 ▶ $_[0] =~ s/>/&gt;/g if $f & ESCAPE_GT;
Branch: 1 branch
BranchTrueFalseTotal
if $f & 32 71 0 71
1381 71 ▶ $_[0] =~ s/"/&quot;/g if $f & ESCAPE_ATTR;
Branch: 1 branch
BranchTrueFalseTotal
if $f & 16 51 20 71
1382 71 ▶ $_[0] =~ s/'/&apos;/g if $f & ESCAPE_ATTR && $f & ESCAPE_APOS;
Branch: 1 branch
BranchTrueFalseTotal
if $f & 16 and $f & 8 1 70 71
Condition100%3/3
$f & 16 and $f & 8
!ll&&!rl&&r
20501
MC/DC100%2/2
$f & 16 and $f & 8
$f & 16 $f & 8
Truth table100%3/3
A and B
A= $f & 16B= $f & 8
ABresult
0X0
100
111
1383 } else {
1384 66 $_[0] =~ s/([^\\]|^)&/$1&amp;/g;
1385 66 $_[0] =~ s/\\&/&/g;
1386 66 $_[0] =~ s/([^\\]|^)</$1&lt;/g;
1387 66 $_[0] =~ s/\\</</g;
1388 66 ▶ if ($f & ESCAPE_GT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 32) 2 64 66
1389 2 $_[0] =~ s/([^\\]|^)>/$1&gt;/g;
1390 2 $_[0] =~ s/\\>/>/g;
1391 }
1392 66 ▶ if ($f & ESCAPE_ATTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 16) 6 60 66
1393 6 $_[0] =~ s/([^\\]|^)"/$1&quot;/g;
1394 6 $_[0] =~ s/\\"/"/g;
1395 6 ▶ if ($f & ESCAPE_APOS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 8) 0 6 6
1396 0 $_[0] =~ s/([^\\]|^)'/$1&apos;/g;
1397 0 $_[0] =~ s/\\'/'/g;
1398 }
1399 }
1400 }
1401 137 ▶ if ($f & ESCAPE_HIGH_BIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 4) 3 134 137
1402 3 $_[0] =~ s/([\200-\377])/'&#'.ord($1).';'/ge;
1403 }
1404 137 ▶ if ($f & ESCAPE_FILTER_INVALID_CHARS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f & 128) 88 49 137
1405 88 filter($_[0]);
1406 }
1407 }
1408  
1409 96 ▶sub filter { $_[0] =~ tr/\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F//d }
Subroutine filter: called CC=1 SCAR=0.0
1410  
1411 # verify syntax of supplied name; croak if it's not valid.
1412 # rules: 1. name must begin with a letter or an underscore
1413 # 2. name may contain any number of letters, numbers, hyphens,
1414 # periods or underscores
1415 # 3. name cannot begin with "xml" in any case
1416 sub ck_syntax {
1417 85 ▶ my($this, $name) = @_;
Subroutine ck_syntax: called CC=6 SCAR=17.9
1418 # use \w and \d so that everything works under "use locale" and
1419 # "use utf8"
1420 85 ▶ if ($name =~ /^\w[\w\-\.]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\w[\w\-\.]*$/) { } 84 1 85
1421 84 ▶ if ($name =~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d/) 2 82 84
1422 2 croak "name [$name] may not begin with a number";
1423 }
1424 } else {
1425 1 croak "name [$name] contains illegal character(s)";
1426 }
1427 82 ▶ if ($name =~ /^xml/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^xml/i) 3 79 82
1428 3 ▶ if (!$this->{'allowed_xml_tags'} || ! grep { $_ eq $name } @{ $this->{'allowed_xml_tags'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{"allowed_xml_tags"} && grep({$_ eq $name;} @{$$this{"allowed_xml_tags"};})) 2 1 3
Condition66%2/3
$this->{'allowed_xml_tags'} && grep({$_ eq $name;} @{$$this{'allowed_xml_tags'};})
!ll&&!rl&&r
201
MC/DC50%1/2
$this->{'allowed_xml_tags'} && grep({$_ eq $name;} @{$$this{'allowed_xml_tags'};})
$this->{'allowed_xml_tags'} grep({$_ eq $name;} @{$$this{'allowed_xml_tags'};})
Truth table66%2/3
A && B
A= $this->{'allowed_xml_tags'}B= grep({$_ eq $name;} @{$$this{'allowed_xml_tags'};})
ABresult
0X0
100
111
1429 2 croak "names beginning with 'xml' are reserved by the W3C";
1430 }
1431 }
1432 }
1433  
1434 my %DTDs;
1435 1 my $DTD;
1436  
1437 sub parse_dtd {
1438 1 ▶ my $this = shift;
Subroutine parse_dtd: called CC=6 SCAR=19.5
1439 1 my($dtd) = @_;
1440  
1441 1 my($root, $type, $name, $uri);
1442  
1443 1 ▶ croak "DTD must be supplied as an array ref" unless (ref $dtd eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dtd eq "ARRAY" 0 1 1
1444 1 ▶ croak "DTD must have at least 3 elements" unless (@{$dtd} >= 3);
Branch: 1 branch
BranchTrueFalseTotal
unless @{$dtd;} >= 3 0 1 1
1445  
1446 1 ($root, $type) = @{$dtd}[0,1];
1447 1 ▶ if ($type eq 'PUBLIC') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PUBLIC') { } 0 1 1
elsif ($type eq 'SYSTEM') { } 1 0 1
1448 0 ($name, $uri) = @{$dtd}[2,3];
1449 } elsif ($type eq 'SYSTEM') {
1450 1 $uri = $dtd->[2];
1451 } else {
1452 0 croak "unknown dtd type [$type]";
1453 }
1454 1 ▶ return $DTDs{$uri} if $DTDs{$uri};
Branch: 1 branch
BranchTrueFalseTotal
if $DTDs{$uri} 0 1 1
1455  
1456 # parse DTD into $DTD (not implemented yet)
1457 1 my $dtd_text = get_dtd($uri);
1458  
1459 1 return $DTDs{$uri} = $DTD;
1460 }
1461  
1462 sub get_dtd {
1463 1 ▶ my($uri) = @_;
Subroutine get_dtd: called CC=1 SCAR=0.0
1464 1 return;
1465 }
1466  
1467 # This package is needed so that embedded tags are correctly
1468 # interpreted as such and handled properly. Otherwise, you'd
1469 # get "<outer>&lt;inner /&gt;</outer>"
1470  
1471 package XML::Generator::overload;
1472  
1473 91 ▶use overload '""' => sub { $_[0]->stringify },
Subroutine __ANON__: called CC=1 SCAR=0.0
1474 0 ▶ '0+' => sub { $_[0]->stringify },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1475 0 ▶ 'bool' => sub { $_[0]->stringify },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1476 79 ▶ 'eq' => sub { (ref $_[0] ? $_[0]->stringify : $_[0]) eq
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 79 0 79
ref $_[1] ? : 0 79 79
1477 1 ▶ (ref $_[1] ? $_[1]->stringify : $_[1])};
Subroutine BEGIN: called CC=1 SCAR=0.0
1478  
1479 sub new {
1480 137 ▶ my($class, $xml) = @_;
Subroutine new: called CC=1 SCAR=0.0
1481 137 return bless $xml, $class;
1482 }
1483  
1484 sub stringify {
1485 170 ▶ return $_[0] unless UNIVERSAL::isa($_[0], 'XML::Generator::overload');
Subroutine stringify: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], "XML::Generator::overload") 0 170 170
1486 170 ▶ if (ref($_[0]->[0])) { # namespace
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0][0]) 85 85 170
1487 85 my $n = shift @{$_[0]};
1488 85 ▶ for (my $i = ($#$n - 1); $i >= 0; $i-=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 45 85 130
1489 45 my($prefix, $uri) = @$n[$i,$i+1];
1490 45 XML::Generator::util::escape($uri, XML::Generator::util::ESCAPE_ATTR |
1491 XML::Generator::util::ESCAPE_ALWAYS|
1492 XML::Generator::util::ESCAPE_GT);
1493 45 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) { } 21 24 45
1494 21 $_[0]->[0] =~ s/^([^ \/>]+)/$1 xmlns:$prefix="$uri"/;
1495 } else {
1496 24 ▶ $uri ||= '';
Condition100%2/2
$uri ||= ""
l!l
213
MC/DC100%1/1
$uri ||= ""
$uri
Truth table100%2/2
A
A= $uri
Aresult
11
00
1497 24 $_[0]->[0] =~ s/^([^ \/>]+)/$1 xmlns="$uri"/;
1498 }
1499 }
1500 }
1501  
1502 170 ▶ join $, || "", @{$_[0]}
Condition50%1/2
$, || ''
l!l
0170
MC/DC0%0/1
$, || ''
$,
Truth table50%1/2
A
A= $,
Aresult
11
00
1503 }
1504  
1505 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
1506  
1507 package XML::Generator::pretty;
1508  
1509 1 ▶use base 'XML::Generator::overload';
Subroutine BEGIN: called CC=1 SCAR=0.0
1510  
1511 sub stringify {
1512 84 ▶ my $this = shift;
Subroutine stringify: called CC=1 SCAR=0.0
1513 84 my $string = $this->SUPER::stringify();
1514  
1515 84 $string =~ s{^((\s*<(?:\w+:)?\w[-.\w]* )[^ "]+"[^"]+")( .{40,})}
1516 1 { my($a,$b,$c) = ($1, $2, $3);
1517 1 $c =~ s{ ((?:\w+:)?\w+="[^\"]+")}{"\n" . (' 'x(length $b)) . $1}ge;
1518 1 "$a$c" }gem;
1519  
1520 84 return $string;
1521 }
1522  
1523 package XML::Generator::final;
1524  
1525 1 ▶use base 'XML::Generator::overload';
Subroutine BEGIN: called CC=1 SCAR=0.0
1526  
1527 package XML::Generator::comment;
1528  
1529 1 ▶use base 'XML::Generator::overload';
Subroutine BEGIN: called CC=1 SCAR=0.0
1530  
1531 package XML::Generator::pi;
1532  
1533 1 ▶use base 'XML::Generator::overload';
Subroutine BEGIN: called CC=1 SCAR=0.0
1534  
1535 package XML::Generator::cdata;
1536  
1537 1 ▶use base 'XML::Generator::overload';
Subroutine BEGIN: called CC=1 SCAR=0.0
1538  
1539 1 1;
1540 __END__
↑ Summary Generator/DOM.pm »