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