blib/lib/XML/Atom/Util.pm

filter: Statementstmt 84.8% 56 / 66 Branchbran 59.0% 13 / 22 Conditioncond 75.0% 12 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 71.4% 10 / 14 Podpod 22.2% 2 / 9 totaltotal 71.3% 97 / 136 SCAR 31.3
CC
19
Cov
78%
iso2dt
9.3
first
6.0
nodelist
6.0
CRAP 22.9
« blib/lib/XML/Atom/Content.pm ↑ Summary blib/lib/XML/Atom/ErrorHandler.pm »
1 # $Id$
2  
3 package XML::Atom::Util;
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use vars qw( @EXPORT_OK @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 @EXPORT_OK = qw( set_ns first nodelist childlist textValue iso2dt encode_xml create_element );
11 22 @ISA = qw( Exporter );
12  
13 22 our %NS_MAP = (
14 '0.3' => 'http://purl.org/atom/ns#',
15 '1.0' => 'http://www.w3.org/2005/Atom',
16 );
17  
18 22 our %NS_VERSION = reverse %NS_MAP;
19  
20 sub set_ns {
21 169 ▶ my $thing = shift;
Subroutine set_ns: called CC=5 SCAR=16.2
Pod undocumented
22 169 my($param) = @_;
23 169 ▶ if (my $ns = delete $param->{Namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = delete $param->{'Namespace'}) { } 55 114 169
24 55 $thing->{ns} = $ns;
25 55 $thing->{version} = $NS_VERSION{$ns};
26 } else {
27 114 ▶ my $version = delete $param->{Version} || $XML::Atom::DefaultVersion;
Condition66%2/3
delete $param->{'Version'} || $XML::Atom::DefaultVersion
l!l&&r!l&&!r
20940
MC/DC0%0/2
delete $param->{'Version'} || $XML::Atom::DefaultVersion
delete $param->{'Version'} $XML::Atom::DefaultVersion
Truth table66%2/3
A || B
A= delete $param->{'Version'}B= $XML::Atom::DefaultVersion
ABresult
1X1
011
000
28 114 ▶ $version = '1.0' if $version == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 1 15 99 114
29 114 ▶ my $ns = $NS_MAP{$version} or $thing->error("Unknown version: $version");
Branch: 1 branch
BranchTrueFalseTotal
my $ns = $XML::Atom::Util::NS_MAP{$version} or $thing->error("Unknown version: $version") 0 114 114
30 114 $thing->{ns} = $ns;
31 114 $thing->{version} = $version;
32 }
33 }
34  
35 sub ns_to_version {
36 68 ▶ my $ns = shift;
Subroutine ns_to_version: called CC=1 SCAR=0.0
Pod undocumented
37 68 $NS_VERSION{$ns};
38 }
39  
40 sub first {
41 0 ▶ my @nodes = nodelist(@_);
Subroutine first: not called CC=2 SCAR=17.9
Pod undocumented
42 0 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 0 0
43 0 return $nodes[0];
44 }
45  
46 sub nodelist {
47 0 ▶ if (LIBXML) {
Subroutine nodelist: not called CC=2 SCAR=17.9
Pod undocumented
48 0 ▶ return $_[1] ? $_[0]->getElementsByTagNameNS($_[1], $_[2]) :
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
49 $_[0]->getElementsByTagName($_[2]);
50 } else {
51 my $set = $_[1] ?
52 $_[0]->find("descendant::*[local-name()='$_[2]' and namespace-uri()='$_[1]']") :
53 $_[0]->find("descendant::$_[2]");
54 return unless $set && $set->isa('XML::XPath::NodeSet');
55 return $set->get_nodelist;
56 }
57 }
58  
59 sub childlist {
60 118 ▶ if (LIBXML) {
Subroutine childlist: called CC=2 SCAR=7.2
Pod undocumented
61 118 ▶ return $_[1] ? $_[0]->getChildrenByTagNameNS($_[1], $_[2]) :
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 118 0 118
62 $_[0]->getChildrenByTagName($_[2]);
63 } else {
64 my $set = $_[1] ?
65 $_[0]->find("*[local-name()='$_[2]' and namespace-uri()='$_[1]']") :
66 $_[0]->find($_[2]);
67 return unless $set && $set->isa('XML::XPath::NodeSet');
68 return $set->get_nodelist;
69 }
70 }
71  
72 sub textValue {
73 0 ▶ my $node = first(@_) or return;
Subroutine textValue: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
my $node = first(@_) or return 0 0 0
74 0 LIBXML ? $node->textContent : $node->string_value;
75 }
76  
77 sub iso2dt {
78 12 ▶ my($iso) = @_;
Subroutine iso2dt: called CC=9 SCAR=22.3
Pod documented
79 12 ▶ return unless $iso =~ /^(\d{4})(?:-?(\d{2})(?:-?(\d\d?)(?:T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|([+-]\d{2}:\d{2}))?)?)?)?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $iso =~ /^(\d{4})(?:-?(\d{2})(?:-?(\d\d?)(?:T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|([+-]\d{2}:\d{2}))?)?)?)?/ 0 12 12
80 12 ▶ my($y, $mo, $d, $h, $m, $s, $zone) =
Condition80%8/10
$2 || 1
l!l
120
$3 || 1
l!l
120
$4 || 0
l!l
102
$5 || 0
l!l
102
$6 || 0
l!l
102
MC/DC0%0/1
$2 || 1
$2
MC/DC0%0/1
$3 || 1
$3
MC/DC100%1/1
$4 || 0
$4
MC/DC100%1/1
$5 || 0
$5
MC/DC100%1/1
$6 || 0
$6
Truth table50%1/2
A
A= $2
Aresult
11
00
Truth table50%1/2
A
A= $3
Aresult
11
00
Truth table100%2/2
A
A= $4
Aresult
11
00
Truth table100%2/2
A
A= $5
Aresult
11
00
Truth table100%2/2
A
A= $6
Aresult
11
00
81 ($1, $2 || 1, $3 || 1, $4 || 0, $5 || 0, $6 || 0, $7);
82 12 require DateTime;
83 12 my $dt = DateTime->new(
84 year => $y,
85 month => $mo,
86 day => $d,
87 hour => $h,
88 minute => $m,
89 second => $s,
90 time_zone => 'UTC',
91 );
92 12 ▶ if ($zone && $zone ne 'Z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone and $zone ne "Z") 8 4 12
Condition66%2/3
$zone and $zone ne "Z"
!ll&&!rl&&r
408
MC/DC50%1/2
$zone and $zone ne "Z"
$zone $zone ne "Z"
Truth table66%2/3
A and B
A= $zoneB= $zone ne "Z"
ABresult
0X0
100
111
93 8 my $seconds = DateTime::TimeZone::offset_as_seconds($zone);
94 8 $dt->subtract(seconds => $seconds);
95 }
96 12 $dt;
97 }
98  
99 22 my %Map = ('&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;',
100 '\'' => '&apos;');
101 22 my $RE = join '|', keys %Map;
102  
103 sub encode_xml {
104 0 ▶ my($str) = @_;
Subroutine encode_xml: not called CC=1 SCAR=6.9
Pod documented
105 0 $str =~ s!($RE)!$Map{$1}!g;
106 0 $str;
107 }
108  
109 sub create_element {
110 33 ▶ my($ns, $name) = @_;
Subroutine create_element: called CC=3 SCAR=11.0
Pod undocumented
111 33 my($ns_uri, $ns_prefix);
112 33 ▶ if (ref $ns eq 'XML::Atom::Namespace') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ns eq 'XML::Atom::Namespace') { } 3 30 33
113 3 $ns_uri = $ns->{uri};
114 3 $ns_prefix = $ns->{prefix};
115 } else {
116 30 $ns_uri = $ns;
117 }
118 33 my $elem;
119 33 if (LIBXML) {
120 33 $elem = XML::LibXML::Element->new($name);
121 33 ▶ $elem->setNamespace($ns_uri, $ns_prefix ? $ns_prefix : ());
Branch: 1 branch
BranchTrueFalseTotal
$ns_prefix ? : 3 30 33
122 } else {
123 $ns_prefix ||= '#default';
124 $elem = XML::XPath::Node::Element->new($name);
125 my $ns = XML::XPath::Node::Namespace->new($ns_prefix => $ns_uri);
126 $elem->appendNamespace($ns);
127 }
128 33 return $elem;
129 }
130  
131 22 1;
132 __END__
133  
134 =head1 NAME
135  
136 XML::Atom::Util - Utility functions
137  
138 =head1 SYNOPSIS
139  
140 use XML::Atom::Util qw( iso2dt );
141 my $dt = iso2dt($entry->issued);
142  
143 =head1 USAGE
144  
145 =head2 iso2dt($iso)
146  
147 Transforms the ISO-8601 date I<$iso> into a I<DateTime> object and returns
148 the I<DateTime> object.
149  
150 =head2 encode_xml($str)
151  
152 Encodes characters with special meaning in XML into entities and returns
153 the encoded string.
154  
155 =head1 AUTHOR & COPYRIGHT
156  
157 Please see the I<XML::Atom> manpage for author, copyright, and license
158 information.
159  
160 =cut
« blib/lib/XML/Atom/Content.pm ↑ Summary blib/lib/XML/Atom/ErrorHandler.pm »