File Coverage

blib/lib/XML/WriterX/Simple.pm
Criterion Covered Total %
statement 54 61 88.5
branch 40 54 74.0
condition 11 11 100.0
subroutine 7 7 100.0
pod 3 4 75.0
total 115 137 83.9


line stmt bran cond sub pod time code
1             package XML::WriterX::Simple;
2             $XML::WriterX::Simple::VERSION = '0.151380';
3             #ABSTRACT:Make XML production simpler.
4 2     2   45117 use 5.006;
  2         7  
  2         86  
5 2     2   9 use strict;
  2         2  
  2         79  
6 2     2   8 use warnings FATAL => 'all';
  2         9  
  2         2596  
7              
8              
9              
10             sub XML::Writer::produce{
11 17     17 0 5318 my $writer = shift;
12 17         21 my $tagName = shift;
13 17         26 my ($attr, $children, $text) = XML::WriterX::Simple::arrange_args( $writer, shift );
14 17 100 100     95 if(($text//'') eq '' and !@$children){
      100        
15 1         4 $writer->emptyTag( $tagName, @$attr );
16             }
17             else{
18 16 100       21 if(ref($text) eq 'CODE'){
19 1         5 XML::WriterX::Simple::produce_content( $writer, $tagName => $text );
20             }
21             else{
22 15         45 $writer->startTag( $tagName, @$attr );
23 15 50       504 $writer->characters( $text ) if defined $text;
24 15         24 while( @$children ){
25 23         130 my ($tag, $content) = (shift(@$children), shift(@$children));
26 23 100 100     90 $tag = ".$tagName" if $tag =~ /^[&"]$/ and ref $content eq 'CODE';
27 23         33 XML::WriterX::Simple::produce_content( $writer, $tag => $content );
28             }
29 15         219 $writer->endTag( $tagName );
30             }
31             }
32             }
33              
34              
35             sub stringify{
36 13     13 1 14 local $_ = shift;
37 13 50       46 return $_ unless ref;
38 0 0       0 if(ref eq 'ARRAY'){
    0          
    0          
    0          
39             }
40             elsif(ref eq 'HASH'){
41             }
42             elsif(ref eq 'SCALAR'){
43 0         0 return $$_;
44             }
45             elsif(ref eq 'REF'){
46 0         0 return stringify( $$_ );
47             }
48 0         0 return "$_";#? REGEXP or GLOB or blessed object... : let's use perl stringifier
49             }
50              
51             sub arrange_args{
52 17     17 1 21 my ($writer, $tagContent) = @_;
53 17         14 my (@attr, @children, $text);
54 17 100       46 if(ref $tagContent eq 'CODE'){
    100          
55 1         1 $text = $tagContent;
56             }
57             elsif(defined $tagContent){
58 15 100       36 @_ = @$tagContent if ref $tagContent eq 'ARRAY';
59 15 100       28 @_ = %$tagContent if ref $tagContent eq 'HASH';
60 15 100       36 @_ = ('"', $$tagContent) if ref($tagContent) =~ /^(SCALAR|REF)$/;
61 15 100       34 @_ = ('"', $tagContent) if !ref $tagContent;
62 15         28 while( @_ ){
63 28         25 my ($tag,$content) = (shift, shift);
64 28 100 100     96 if($tag =~ /^#/){
    100          
    100          
65             #comment is added as a children
66 2         5 push @children, $tag => $content;
67             }
68             elsif($tag =~ /^:attr/ and ref($content) =~ /^(ARRAY|HASH)$/){
69             #multiple attributs in $content
70 1 50       9 my @attrs = $1 eq 'ARRAY' ? @$content : %$content;
71             #!warning if %attr != @$content : Attribut name must be unique!
72 1         9 push @attr, shift(@attrs), shift(@attrs) while @attrs;
73             }
74             elsif($tag =~ /^:(.*)/){
75             #single attribut named $1 value is stringify($content)
76 4         13 push @attr, $1 => $content;
77             }
78             else{
79             #(processing instruction are added as a children like all other cases)
80 21         44 push @children, $tag => $content;
81             }
82             }
83             }
84 17         41 return (\@attr, \@children, $text);
85             }
86              
87              
88             #CANNOT:
89             # - produce a comment from a sub{}
90             # - produce a tag attributs from a sub{}
91             # - prodice attributs from a sub{} ?
92             sub produce_content{
93 24     24 1 22 my ($writer, $tag, $content) = @_;
94              
95 24 50       36 unless($tag){
96 0         0 warn "try to produce content without any tag name!";
97 0         0 $DB::single=1;
98 0         0 return;
99             }
100              
101 24 100       46 if(ref($content) eq 'CODE'){
102 3 100       11 $writer->startTag( $tag ) unless $tag =~ /^\./;
103 3         33 $content->( $writer, $tag );
104 3 100       49 $writer->endTag( $tag ) unless $tag =~ /^\./;
105 3         18 return;
106             }
107              
108 21 100       38 return $writer->characters( stringify( $content ) )
109             if $tag eq '"';
110              
111 10 100       23 return $writer->comment( stringify $content )
112             if $tag =~ /^#/;
113              
114 8 50       15 return $writer->pi( $1 => stringify $content )
115             if $tag =~ /^\?(.*)/;
116            
117 8 50       11 return $writer->produce( $tag, $$content )
118             if ref $content eq 'SCALAR';
119            
120 8         16 return $writer->produce( $tag, $content );
121             }
122              
123              
124             1; # End of XML::WriterX::Simple
125              
126             __END__