| 1 | package XML::Atom::Syndication::Writer; | ||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use base qw( Class::ErrorHandler ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use XML::Writer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use XML::Elemental::Util qw( process_name ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 21 | my %NSPrefix = ( # default prefix table. | |||||||||||||||||||||||||||
| 10 | # '' => "http://www.w3.org/2005/Atom", | ||||||||||||||||||||||||||||
| 11 | dc => "http://purl.org/dc/elements/1.1/", | ||||||||||||||||||||||||||||
| 12 | dcterms => "http://purl.org/dc/terms/", | ||||||||||||||||||||||||||||
| 13 | sy => "http://purl.org/rss/1.0/modules/syndication/", | ||||||||||||||||||||||||||||
| 14 | trackback => "http://madskills.com/public/xml/rss/module/trackback/", | ||||||||||||||||||||||||||||
| 15 | xhtml => "http://www.w3.org/1999/xhtml", | ||||||||||||||||||||||||||||
| 16 | xml => "http://www.w3.org/XML/1998/namespace" | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 17 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 17 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 22 | 17 | $self->init(@_); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub init { | ||||||||||||||||||||||||||||
| 26 | 17 | ▶ | my %nsp = %NSPrefix; # clone | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 17 | $_[0]->{__PREFIX} = \%nsp; | |||||||||||||||||||||||||||
| 28 | 17 | $_[0]->{__NS} = {reverse %nsp}; | |||||||||||||||||||||||||||
| 29 | 17 | $_[0]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub set_prefix { | ||||||||||||||||||||||||||||
| 33 | 17 | ▶ | $_[0]->{__NS}->{$_[2]} = $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
set_prefix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 17 | $_[0]->{__PREFIX}->{$_[1]} = $_[2]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub get_prefix { $_[0]->{__NS}->{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
get_prefix: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub get_namespace { $_[0]->{__PREFIX}->{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
get_namespace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub as_xml { | ||||||||||||||||||||||||||||
| 41 | 26 | ▶ | my ($self, $node, $is_full) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=2 SCAR=8.5
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 26 | my $xml = ''; | |||||||||||||||||||||||||||
| 43 | 26 | my $w; | |||||||||||||||||||||||||||
| 44 | 26 | ▶ | if ($is_full) { # full doc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($name, $ns) = process_name($node->name); | |||||||||||||||||||||||||||
| 46 | $w = XML::Writer->new( | ||||||||||||||||||||||||||||
| 47 | OUTPUT => \$xml, | ||||||||||||||||||||||||||||
| 48 | NAMESPACES => 1, | ||||||||||||||||||||||||||||
| 49 | PREFIX_MAP => $self->{__NS}, # FORCED_NS_DECLS => [ $ns ] | ||||||||||||||||||||||||||||
| 50 | 0 | ); | |||||||||||||||||||||||||||
| 51 | 0 | $w->xmlDecl('utf-8'); | |||||||||||||||||||||||||||
| 52 | } else { # fragment | ||||||||||||||||||||||||||||
| 53 | 26 | $w = XML::Writer->new(OUTPUT => \$xml, UNSAFE => 1); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 26 | my $dumper; | |||||||||||||||||||||||||||
| 56 | $dumper = sub { | ||||||||||||||||||||||||||||
| 57 | 35 | ▶ | my $node = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=29.5
| |||||||||||||||||||||||||||||
| 58 | 35 | ▶ | return encode_xml($w, $node->data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | if (ref $node eq 'XML::Elemental::Characters'); | ||||||||||||||||||||||||||||
| 60 | 9 | my ($name, $ns) = | |||||||||||||||||||||||||||
| 61 | process_name($node->name); # it must be an element then. | ||||||||||||||||||||||||||||
| 62 | 9 | ▶ | my $tag = $is_full ? [$ns, $name] : $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 9 | my @attr; | |||||||||||||||||||||||||||
| 64 | 9 | my $a = $node->attributes; | |||||||||||||||||||||||||||
| 65 | 9 | my $children = $node->contents; | |||||||||||||||||||||||||||
| 66 | 9 | foreach (keys %$a) { | |||||||||||||||||||||||||||
| 67 | 0 | my ($aname, $ans) = process_name($_); | |||||||||||||||||||||||||||
| 68 | next | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $ans eq 'http://www.w3.org/2000/xmlns/' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ans eq "http://www.w3.org/2000/xmlns/" or $aname eq "xmlns"
MC/DC0%0/2
$ans eq "http://www.w3.org/2000/xmlns/" or $aname eq "xmlns"
$ans eq "http://www.w3.org/2000/xmlns/"
$aname eq "xmlns"
Truth table0%0/3
A or B
A= $ans eq "http://www.w3.org/2000/xmlns/"B= $aname eq "xmlns"
| |||||||||||||||||||||||||||||
| 70 | || $aname eq 'xmlns'); | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $key = $is_full && $ans ? [$ans, $aname] : $aname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_full and $ans
MC/DC0%0/2
$is_full and $ans
$is_full
$ans
Truth table0%0/3
A and B
A= $is_fullB= $ans
| |||||||||||||||||||||||||||||
| 72 | 0 | push @attr, $key, $a->{$_}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 9 | ▶ | if (@$children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 9 | $w->startTag($tag, @attr); | |||||||||||||||||||||||||||
| 76 | 9 | map { $dumper->($_) } @$children; | |||||||||||||||||||||||||||
| 77 | 9 | $w->endTag($tag); | |||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | $w->emptyTag($tag, @attr); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 26 | }; | |||||||||||||||||||||||||||
| 82 | 26 | $dumper->($node); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # $w->end; # this adds a character return we don't want. | ||||||||||||||||||||||||||||
| 85 | 26 | $xml; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # utility for intelligent use of cdata. | ||||||||||||||||||||||||||||
| 89 | sub encode_xml { | ||||||||||||||||||||||||||||
| 90 | 26 | ▶ | my ($w, $data, $nocdata) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode_xml: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 26 | ▶ | return unless defined($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 26 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $nocdata and $data =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
MC/DC50%1/2
not $nocdata and $data =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
not $nocdata
$data =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
Truth table66%2/3
not(A) and B
A= not $nocdataB= $data =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
| |||||||||||||||||||||||||||||
| 93 | !$nocdata | ||||||||||||||||||||||||||||
| 94 | && $data =~ m/ | ||||||||||||||||||||||||||||
| 95 | <[^>]+> ## HTML markup | ||||||||||||||||||||||||||||
| 96 | | ## or | ||||||||||||||||||||||||||||
| 97 | &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); | ||||||||||||||||||||||||||||
| 98 | ## something that looks like an HTML entity. | ||||||||||||||||||||||||||||
| 99 | /x | ||||||||||||||||||||||||||||
| 100 | ) { | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # $w->cdata($data); # this was inserting a extra character into returned strings. | ||||||||||||||||||||||||||||
| 103 | 8 | my $str = $w->characters($data); | |||||||||||||||||||||||||||
| 104 | 8 | $str =~ s/]]>/]]>/g; | |||||||||||||||||||||||||||
| 105 | 8 | '<![CDATA[' . $str . ']]>'; | |||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 18 | $w->characters($data); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 21 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||