| 1 | package XML::Atom::Syndication::Writer; | ||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use base qw( Class::ErrorHandler ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use XML::Writer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use XML::Elemental::Util qw( process_name ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 22 | 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 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 23 | my $class = shift; | |||||||||||||||||||||||||||
| 21 | 23 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 22 | 23 | $self->init(@_); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 23 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 23 | my %nsp = %NSPrefix; # clone | |||||||||||||||||||||||||||
| 27 | 23 | $_[0]->{__PREFIX} = \%nsp; | |||||||||||||||||||||||||||
| 28 | 23 | $_[0]->{__NS} = {reverse %nsp}; | |||||||||||||||||||||||||||
| 29 | 23 | $_[0]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 23 | ▶ | sub set_prefix { | ||||||||||||||||||||||||||
|
Subroutine
set_prefix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 23 | $_[0]->{__NS}->{$_[2]} = $_[1]; | |||||||||||||||||||||||||||
| 34 | 23 | $_[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 | 14 | ▶ | sub no_cdata { | ||||||||||||||||||||||||||
|
Subroutine
no_cdata: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 14 | ▶ | if (defined $_[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 14 | ▶ | $_[0]->{__NO_CDATA} = $_[1] ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 14 | $_[0]->{__NO_CDATA}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 32 | ▶ | sub as_xml { | ||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=2 SCAR=7.3
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 32 | my ($self, $node, $is_full) = @_; | |||||||||||||||||||||||||||
| 49 | 32 | my $xml = ''; | |||||||||||||||||||||||||||
| 50 | 32 | my $w; | |||||||||||||||||||||||||||
| 51 | 32 | ▶ | if ($is_full) { # full doc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 6 | my ($name, $ns) = process_name($node->name); | |||||||||||||||||||||||||||
| 53 | $w = XML::Writer->new( | ||||||||||||||||||||||||||||
| 54 | OUTPUT => \$xml, | ||||||||||||||||||||||||||||
| 55 | UNSAFE => 1, # consequence of not using buggy characters method | ||||||||||||||||||||||||||||
| 56 | NAMESPACES => 1, | ||||||||||||||||||||||||||||
| 57 | PREFIX_MAP => $self->{__NS}, # FORCED_NS_DECLS => [ $ns ] | ||||||||||||||||||||||||||||
| 58 | 6 | ); | |||||||||||||||||||||||||||
| 59 | 6 | $w->xmlDecl('utf-8'); | |||||||||||||||||||||||||||
| 60 | } else { # fragment | ||||||||||||||||||||||||||||
| 61 | 26 | $w = XML::Writer->new(OUTPUT => \$xml, UNSAFE => 1); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 32 | my $dumper; | |||||||||||||||||||||||||||
| 64 | 83 | ▶ | $dumper = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=27.0
| |||||||||||||||||||||||||||||
| 65 | 83 | my $node = shift; | |||||||||||||||||||||||||||
| 66 | return encode_xml($w, $node->data, $self->{__NO_CDATA}) | ||||||||||||||||||||||||||||
| 67 | 83 | ▶ | if (ref $node eq 'XML::Elemental::Characters'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 30 | my ($name, $ns) = | |||||||||||||||||||||||||||
| 69 | process_name($node->name); # it must be an element then. | ||||||||||||||||||||||||||||
| 70 | 30 | ▶ | my $tag = $is_full ? [$ns, $name] : $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 30 | my @attr; | |||||||||||||||||||||||||||
| 72 | 30 | my $a = $node->attributes; | |||||||||||||||||||||||||||
| 73 | 30 | my $children = $node->contents; | |||||||||||||||||||||||||||
| 74 | 30 | foreach (keys %$a) { | |||||||||||||||||||||||||||
| 75 | 6 | my ($aname, $ans) = process_name($_); | |||||||||||||||||||||||||||
| 76 | next | ||||||||||||||||||||||||||||
| 77 | 6 | ▶ | if ( $ans eq 'http://www.w3.org/2000/xmlns/' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/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 table33%1/3
A or B
A= $ans eq "http://www.w3.org/2000/xmlns/"B= $aname eq "xmlns"
| |||||||||||||||||||||||||||||
| 78 | || $aname eq 'xmlns'); | ||||||||||||||||||||||||||||
| 79 | 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
| |||||||||||||||||||||||||||||
| 80 | 0 | push @attr, $key, $a->{$_}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 30 | ▶ | if (@$children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 30 | $w->startTag($tag, @attr); | |||||||||||||||||||||||||||
| 84 | 30 | $dumper->($_) for @$children; | |||||||||||||||||||||||||||
| 85 | 30 | $w->endTag($tag); | |||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 0 | $w->emptyTag($tag, @attr); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 32 | }; | |||||||||||||||||||||||||||
| 90 | 32 | $dumper->($node); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # $w->end; # this adds a character return we don't want. | ||||||||||||||||||||||||||||
| 93 | 32 | $xml; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 22 | my %Map = ( | |||||||||||||||||||||||||||
| 97 | '&' => '&', | ||||||||||||||||||||||||||||
| 98 | '"' => '"', | ||||||||||||||||||||||||||||
| 99 | '<' => '<', | ||||||||||||||||||||||||||||
| 100 | '>' => '>', | ||||||||||||||||||||||||||||
| 101 | '\'' => ''' | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | 22 | my $RE = join '|', keys %Map; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub encode_xml | ||||||||||||||||||||||||||||
| 106 | 53 | ▶ | { # XML::Writer::character encoding is wrong so we handle this ourselves. | ||||||||||||||||||||||||||
|
Subroutine
encode_xml: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 53 | my ($w, $str, $nocdata) = @_; | |||||||||||||||||||||||||||
| 108 | 53 | ▶ | return '' unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 53 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $nocdata and $str =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
MC/DC100%2/2
not $nocdata and $str =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
not $nocdata
$str =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
Truth table100%3/3
not(A) and B
A= not $nocdataB= $str =~ /
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
| |||||||||||||||||||||||||||||
| 110 | !$nocdata | ||||||||||||||||||||||||||||
| 111 | && $str =~ m/ | ||||||||||||||||||||||||||||
| 112 | <[^>]+> ## HTML markup | ||||||||||||||||||||||||||||
| 113 | | ## or | ||||||||||||||||||||||||||||
| 114 | &(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); | ||||||||||||||||||||||||||||
| 115 | ## something that looks like an HTML entity. | ||||||||||||||||||||||||||||
| 116 | /x | ||||||||||||||||||||||||||||
| 117 | ) { | ||||||||||||||||||||||||||||
| 118 | ## If ]]> exists in the string, encode the > to >. | ||||||||||||||||||||||||||||
| 119 | 1 | $str =~ s/]]>/]]>/g; | |||||||||||||||||||||||||||
| 120 | 1 | $str = '<![CDATA[' . $str . ']]>'; | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 52 | $str =~ s!($RE)!$Map{$1}!g; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 53 | $w->raw($str); # forces UNSAFE mode at all times. | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 22 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||