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