blib/lib/XML/Writer/Simple.pm

filter: Statementstmt 97.8% 137 / 140 Branchbran 85.7% 48 / 56 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 21 / 21 Podpod 100% 3 / 3 totaltotal 90.8% 227 / 250 CC 37 SCAR 19.9
CC
37
Cov
92%
import
18.0
AUTOLOAD
5.0
_empty_tag
4.2
CRAP 7.3
↑ Summary
1 package XML::Writer::Simple;
2 7 $XML::Writer::Simple::VERSION = '0.12';
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use vars qw/@ISA @EXPORT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use XML::DT;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use XML::DTDParser qw/ParseDTDFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =encoding utf-8
11  
12 =head1 NAME
13  
14 XML::Writer::Simple - Create XML files easily!
15  
16 =cut
17  
18 7 @ISA = qw/Exporter/;
19 7 @EXPORT = (qw/powertag xml_header quote_entities/);
20 7 our %PTAGS = ();
21 7 our $MODULENAME = "XML::Writer::Simple";
22  
23 7 our $IS_HTML = 0;
24 7 our %TAG_SET = (
25 html => {
26 tags => [qw.a abbr acronym address area
27 b base bdo big blockquote body br button
28 caption cite code col colgroup
29 dd del dfn div dl dt
30 em
31 fieldset form frame frameset
32 h1 h2 h3 h4 h5 h6 head hr html
33 i iframe img input ins
34 kbd
35 label legend li link
36 map meta
37 noframes noscript
38 object ol optgroup option
39 p param pre
40 q
41 samp script select small span strong style sub sup
42 table tbody td textarea tfoot th thead title Tr tt
43 u ul var.]
44 },
45 );
46  
47 =head1 SYNOPSIS
48  
49 use XML::Writer::Simple dtd => "file.dtd";
50  
51 print xml_header(encoding => 'iso-8859-1');
52 print para("foo",b("bar"),"zbr");
53  
54  
55 # if you want CGI but you do not want CGI :)
56 use XML::Writer::Simple ':html';
57  
58 =head1 USAGE
59  
60 This module takes some ideas from CGI to make easier the life for
61 those who need to generated XML code. You can use the module in three
62 flavours (or combine them):
63  
64 =over 4
65  
66 =item tags
67  
68 When importing the module you can specify the tags you will be using:
69  
70 use XML::Writer::Simple tags => [qw/p b i tt/];
71  
72 print p("Hey, ",b("you"),"! ", i("Yes ", b("you")));
73  
74 that will generate
75  
76 <p>Hey <b>you</b>! <i>Yes <b>you</b></i></p>
77  
78 =item dtd
79  
80 You can supply a DTD, that will be analyzed, and the tags used:
81  
82 use XML::Writer::Simple dtd => "tmx.dtd";
83  
84 print tu(seg("foo"),seg("bar"));
85  
86 =item xml
87  
88 You can supply an XML (or a reference to a list of XML files). They
89 will be parsed, and the tags used:
90  
91 use XML::Writer::Simple xml => "foo.xml";
92  
93 print foo("bar");
94  
95 =item partial
96  
97 You can supply an 'partial' key, to generate prototypes for partial tags
98 construction. For instance:
99  
100 use XML::Writer::Simple tags => qw/foo bar/, partial => 1;
101  
102 print start_foo;
103 print ...
104 print end_foo;
105  
106 =back
107  
108 You can also use tagsets, where sets of tags from a well known format
109 are imported. For example, to use HTML:
110  
111 use XML::Writer::Simple ':html';
112  
113 =head1 EXPORT
114  
115 This module export one function for each element at the dtd or xml
116 file you are using. See below for details.
117  
118 =head1 FUNCTIONS
119  
120 =head2 import
121  
122 Used when you 'use' the module, should not be used directly.
123  
124 =head2 xml_header
125  
126 This function returns the xml header string, without encoding
127 definition, with a trailing new line. Default XML encoding should
128 be UTF-8, by the way.
129  
130 You can force an encoding passing it as argument:
131  
132 print xml_header(encoding=>'iso-8859-1');
133  
134 =head2 powertag
135  
136 Used to specify a powertag. For instance:
137  
138 powertag("ul","li");
139  
140 ul_li([qw/foo bar zbr ugh/]);
141  
142 will generate
143  
144 <ul>
145 <li>foo</li>
146 <li>bar</li>
147 <li>zbr</li>
148 <li>ugh</li>
149 </ul>
150  
151 You can also supply this information when loading the module, with
152  
153 use XML::Writer::Simple powertags=>["ul_li","ol_li"];
154  
155 Powertags support three level tags as well:
156  
157 use XML::Writer::Simple powertags=>["table_tr_td"];
158  
159 print table_tr_td(['a','b','c'],['d','e','f']);
160  
161 =head2 quote_entities
162  
163 To use the special characters C<< < >>, C<< > >> and C<< & >> on your PCDATA content you need
164 to protect them. You can either do that yourself or call this function.
165  
166 print f(quote_entities("a < b"));
167  
168 =cut
169  
170 2 ▶sub xml_header {
Subroutine xml_header: called CC=2 SCAR=6.9
Pod documented
171 2 my %ops = @_;
172 2 my $encoding = "";
173 2 ▶ $encoding =" encoding=\"$ops{encoding}\"" if exists $ops{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ops{"encoding"} 1 1 2
174 2 return "<?xml version=\"1.0\"$encoding?>\n";
175 }
176  
177 2 ▶sub powertag {
Subroutine powertag: called CC=1 SCAR=0.0
Pod documented
178 2 my $nfunc = join("_", @_);
179 2 $PTAGS{$nfunc}=[@_];
180 2 push @EXPORT, $nfunc;
181 2 XML::Writer::Simple->export_to_level(1, $MODULENAME, $nfunc);
182 }
183  
184 96 ▶sub _xml_from {
Subroutine _xml_from: called CC=2 SCAR=6.9
185 96 my ($tag, $attrs, @body) = @_;
186 return (ref($body[0]) eq "ARRAY")?
187 96 ▶ join("", map{ _toxml($tag, $attrs, $_) } @{$body[0]})
Branch: 1 branch
BranchTrueFalseTotal
ref $body[0] eq 'ARRAY' ? : 5 91 96
188 :_toxml($tag, $attrs, join("", @body));
189 }
190  
191 46 ▶sub _clean_attrs {
Subroutine _clean_attrs: called CC=3 SCAR=11.0
192 46 my $attrs = shift;
193 46 for (keys %$attrs) {
194 11 ▶ if (m!^-!) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-/) 5 6 11
195 5 $attrs->{$'}=$attrs->{$_};
196 5 delete($attrs->{$_});
197 }
198 }
199 46 return $attrs;
200 }
201  
202 111 ▶sub _toxml {
Subroutine _toxml: called CC=3 SCAR=11.0
203 111 my ($tag,$attr,$contents) = @_;
204 111 ▶ if (defined($contents) && $contents ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $contents and $contents ne '') { } 106 5 111
Condition66%2/3
defined $contents and $contents ne ""
!ll&&!rl&&r
05106
MC/DC50%1/2
defined $contents and $contents ne ""
defined $contents $contents ne ""
Truth table66%2/3
A and B
A= defined $contentsB= $contents ne ""
ABresult
0X0
100
111
205 106 return _start_tag($tag,$attr) . $contents . _close_tag($tag);
206 }
207 else {
208 5 return _empty_tag($tag,$attr);
209 }
210 }
211  
212 21 ▶sub _go_down {
Subroutine _go_down: called CC=3 SCAR=11.0
213 21 my ($tags, @values) = @_;
214 21 my $tag = shift @$tags;
215  
216 21 ▶ if (@$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tags) { } 7 14 21
217 join("",
218 map {
219 7 my $attrs = {};
220 14 ▶ if (ref($_->[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->[0] eq "HASH") 1 13 14
221 1 $attrs = _clean_attrs(shift @$_);
222 }
223 14 _xml_from($tag,$attrs,_go_down([@$tags],@$_)) } ### REALLY NEED TO COPY
224 @values)
225 } else {
226 join("",
227 14 map { _xml_from($tag,{},$_) } @values)
228 }
229 }
230  
231 45 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
232 45 my $attrs = {};
233 45 my $tag = our $AUTOLOAD;
234  
235 45 $tag =~ s!${MODULENAME}::!!;
236  
237 45 ▶ $attrs = shift if ref($_[0]) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 10 35 45
238 45 $attrs = _clean_attrs($attrs);
239  
240 45 ▶ if (exists($PTAGS{$tag})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $XML::Writer::Simple::PTAGS{$tag}) { } 7 38 45
241 7 my @tags = @{$PTAGS{$tag}};
242 7 my $toptag = shift @tags;
243 7 return _xml_from($toptag, $attrs,
244 _go_down(\@tags, @_));
245 }
246 else {
247 38 ▶ if ($tag =~ m/^end_(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag =~ /^end_(.*)$/) { } 1 37 38
elsif ($tag =~ /^start_(.*)$/) { } 2 35 37
248 1 return _close_tag($1)."\n";
249 }
250 elsif ($tag =~ m/^start_(.*)$/) {
251 2 return _start_tag($1, $attrs)."\n";
252 }
253 else {
254 35 return _xml_from($tag,$attrs,@_);
255 }
256 }
257 }
258  
259 2 ▶sub quote_entities {
Subroutine quote_entities: called CC=1 SCAR=0.0
Pod documented
260 2 my $s = shift;
261 2 $s =~ s/&/&amp;/g;
262 2 $s =~ s/</&lt;/g;
263 2 $s =~ s/>/&gt;/g;
264 2 return $s;
265 }
266  
267 11 ▶sub _quote_attr {
Subroutine _quote_attr: called CC=1 SCAR=0.0
268 11 my $s = shift;
269 11 $s =~ s/&/&amp;/g;
270 11 $s =~ s/"/&quot;/g;
271 11 return $s;
272 }
273  
274 113 ▶sub _attributes {
Subroutine _attributes: called CC=1 SCAR=0.0
275 113 my $attr = shift;
276 113 return join(" ", map { "$_=\"" . _quote_attr($attr->{$_}) . "\""} keys %$attr);
277 }
278  
279 108 ▶sub _start_tag {
Subroutine _start_tag: called CC=4 SCAR=13.9
280 108 my ($tag, $attr) = @_;
281 108 ▶ $tag = "tr" if $tag eq "Tr" && $IS_HTML;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "Tr" and $XML::Writer::Simple::IS_HTML 3 105 108
Condition66%2/3
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
!ll&&!rl&&r
10503
MC/DC50%1/2
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
$tag eq "Tr" $XML::Writer::Simple::IS_HTML
Truth table66%2/3
A and B
A= $tag eq "Tr"B= $XML::Writer::Simple::IS_HTML
ABresult
0X0
100
111
282 108 $attr = _attributes($attr);
283 108 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) { } 9 99 108
284 9 return "<$tag $attr>"
285 } else {
286 99 return "<$tag>"
287 }
288 }
289  
290 5 ▶sub _empty_tag {
Subroutine _empty_tag: called CC=4 SCAR=14.3
291 5 my ($tag, $attr) = @_;
292 5 ▶ $tag = "tr" if $tag eq "Tr" && $IS_HTML;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "Tr" and $XML::Writer::Simple::IS_HTML 0 5 5
Condition33%1/3
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
!ll&&!rl&&r
500
MC/DC0%0/2
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
$tag eq "Tr" $XML::Writer::Simple::IS_HTML
Truth table33%1/3
A and B
A= $tag eq "Tr"B= $XML::Writer::Simple::IS_HTML
ABresult
0X0
100
111
293 5 $attr = _attributes($attr);
294 5 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) { } 2 3 5
295 2 return "<$tag $attr/>"
296 } else {
297 3 return "<$tag/>"
298 }
299 }
300  
301 107 ▶sub _close_tag {
Subroutine _close_tag: called CC=3 SCAR=11.0
302 107 my $tag = shift;
303 107 ▶ $tag = "tr" if $tag eq "Tr" && $IS_HTML;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "Tr" and $XML::Writer::Simple::IS_HTML 3 104 107
Condition66%2/3
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
!ll&&!rl&&r
10403
MC/DC50%1/2
$tag eq "Tr" and $XML::Writer::Simple::IS_HTML
$tag eq "Tr" $XML::Writer::Simple::IS_HTML
Truth table66%2/3
A and B
A= $tag eq "Tr"B= $XML::Writer::Simple::IS_HTML
ABresult
0X0
100
111
304 107 return "</$tag>";
305 }
306  
307  
308 7 ▶sub import {
Subroutine import: called CC=17 SCAR=28.9
309 7 my $class = shift;
310  
311 7 my @tags;
312 my @ptags;
313 7 ▶ while ($_[0] && $_[0] =~ m!^:(.*)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] =~ /^:(.*)$/) 1 7 8
Condition100%3/3
$_[0] and $_[0] =~ /^:(.*)$/
!ll&&!rl&&r
251
MC/DC100%2/2
$_[0] and $_[0] =~ /^:(.*)$/
$_[0] $_[0] =~ /^:(.*)$/
Truth table100%3/3
A and B
A= $_[0]B= $_[0] =~ /^:(.*)$/
ABresult
0X0
100
111
314 1 shift;
315 1 my $pack = $1;
316 1 ▶ $IS_HTML = 1 if $pack eq "html";
Branch: 1 branch
BranchTrueFalseTotal
if $pack eq "html" 1 0 1
317 1 ▶ if (exists($TAG_SET{$pack})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $XML::Writer::Simple::TAG_SET{$pack}) { } 1 0 1
318 1 ▶ push @tags => exists $TAG_SET{$pack}{tags} ? @{$TAG_SET{$pack}{tags}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $XML::Writer::Simple::TAG_SET{$pack}{'tags'} ? : 1 0 1
319 0 ▶ push @ptags => exists $TAG_SET{$pack}{ptags} ? @{$TAG_SET{$pack}{ptags}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $XML::Writer::Simple::TAG_SET{$pack}{'ptags'} ? : 0 1 1
320 } else {
321 0 die "XML::Writer::Simple - Unknown tagset :$pack\n";
322 }
323 }
324  
325 7 my %opts = @_;
326  
327 7 ▶ if (exists($opts{tags})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"tags"}) 3 4 7
328 3 ▶ if (ref($opts{tags}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"tags"} eq "ARRAY") 3 0 3
329 3 push @tags => @{$opts{tags}};
330 }
331 }
332  
333 7 ▶ if (exists($opts{xml})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"xml"}) 1 6 7
334 0 ▶ my @xmls = (ref($opts{xml}) eq "ARRAY")?@{$opts{xml}}:($opts{xml});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'xml'} eq 'ARRAY' ? : 0 1 1
335 1 my $tags;
336 1 for my $xml (@xmls) {
337 1 ▶ dt($xml, -default => sub { $tags->{$q}++ });
Subroutine __ANON__: called CC=1 SCAR=0.0
338 }
339 1 push @tags => keys %$tags;
340 }
341  
342 7 ▶ if (exists($opts{dtd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"dtd"}) 1 6 7
343 1 my $DTD = ParseDTDFile($opts{dtd});
344 1 push @tags => keys %$DTD;
345 }
346  
347 7 push @EXPORT => @tags;
348 7 ▶ if (exists($opts{partial})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"partial"}) 1 6 7
349 1 push @EXPORT => map { "start_$_" } @tags;
350 1 push @EXPORT => map { "end_$_" } @tags;
351 }
352  
353 7 ▶ if (@ptags || exists($opts{powertags})) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ptags or exists $opts{"powertags"}) 1 6 7
Condition66%2/3
@ptags or exists $opts{"powertags"}
l!l&&r!l&&!r
016
MC/DC50%1/2
@ptags or exists $opts{"powertags"}
@ptags exists $opts{"powertags"}
Truth table66%2/3
A or B
A= @ptagsB= exists $opts{"powertags"}
ABresult
1X1
011
000
354 1 ▶ push @ptags => @{$opts{powertags}} if exists $opts{powertags};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"powertags"} 1 0 1
355 1 @PTAGS{@ptags} = map { [split/_/] } @ptags;
356 1 push @EXPORT => @ptags;
357 }
358  
359 7 XML::Writer::Simple->export_to_level(1, $class, @EXPORT);
360 }
361  
362 =head1 AUTHOR
363  
364 Alberto Simões, C<< <ambs@cpan.org> >>
365  
366 =head1 BUGS
367  
368 Please report any bugs or feature requests to
369 C<bug-xml-writer-simple@rt.cpan.org>, or through the web interface at
370 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-Writer-Simple>.
371 I will be notified, and then you'll automatically be notified of progress on
372 your bug as I make changes.
373  
374 =head1 COPYRIGHT AND LICENSE
375  
376 Copyright 1999-2012 Project Natura.
377  
378 This library is free software; you can redistribute it and/or modify
379 it under the same terms as Perl itself.
380  
381 =cut
382  
383 7 1; # End of XML::Writer::Simple
↑ Summary