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 CC 19 SCAR 16.5
CC
19
Cov
78%
iso2dt
9.3
first
6.0
nodelist
6.0
CRAP 5.2
« Atom/Server.pm ↑ Summary Atom/Base.pm »
1 # $Id$
2  
3 package XML::Atom::Util;
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use vars qw( @EXPORT_OK @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 @EXPORT_OK = qw( set_ns first nodelist childlist textValue iso2dt encode_xml create_element );
11 23 @ISA = qw( Exporter );
12  
13 23 our %NS_MAP = (
14 '0.3' => 'http://purl.org/atom/ns#',
15 '1.0' => 'http://www.w3.org/2005/Atom',
16 );
17  
18 23 our %NS_VERSION = reverse %NS_MAP;
19  
20 215 ▶sub set_ns {
Subroutine set_ns: called CC=5 SCAR=16.2
Pod undocumented
21 215 my $thing = shift;
22 215 my($param) = @_;
23 215 ▶ if (my $ns = delete $param->{Namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = delete $param->{'Namespace'}) { } 87 128 215
24 87 $thing->{ns} = $ns;
25 87 $thing->{version} = $NS_VERSION{$ns};
26 } else {
27 128 ▶ my $version = delete $param->{Version} || $XML::Atom::DefaultVersion;
Condition66%2/3
delete $param->{'Version'} || $XML::Atom::DefaultVersion
l!l&&r!l&&!r
221060
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 128 ▶ $version = '1.0' if $version == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 1 15 113 128
29 128 ▶ 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") 128 0 128
30 128 $thing->{ns} = $ns;
31 128 $thing->{version} = $version;
32 }
33 }
34  
35 81 ▶sub ns_to_version {
Subroutine ns_to_version: called CC=1 SCAR=0.0
Pod undocumented
36 81 my $ns = shift;
37 81 $NS_VERSION{$ns};
38 }
39  
40 0 ▶sub first {
Subroutine first: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my @nodes = nodelist(@_);
42 0 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 0 0
43 0 return $nodes[0];
44 }
45  
46 0 ▶sub nodelist {
Subroutine nodelist: not called CC=2 SCAR=17.9
Pod undocumented
47 0 if (LIBXML) {
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 180 ▶sub childlist {
Subroutine childlist: called CC=2 SCAR=7.2
Pod undocumented
60 180 if (LIBXML) {
61 180 ▶ return $_[1] ? $_[0]->getChildrenByTagNameNS($_[1], $_[2]) :
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 180 0 180
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 0 ▶sub textValue {
Subroutine textValue: not called CC=2 SCAR=17.9
Pod undocumented
73 0 ▶ my $node = first(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = first(@_) or return 0 0 0
74 0 LIBXML ? $node->textContent : $node->string_value;
75 }
76  
77 12 ▶sub iso2dt {
Subroutine iso2dt: called CC=9 SCAR=22.3
Pod documented
78 12 my($iso) = @_;
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 23 my %Map = ('&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;',
100 '\'' => '&apos;');
101 23 my $RE = join '|', keys %Map;
102  
103 0 ▶sub encode_xml {
Subroutine encode_xml: not called CC=1 SCAR=6.9
Pod documented
104 0 my($str) = @_;
105 0 $str =~ s!($RE)!$Map{$1}!g;
106 0 $str;
107 }
108  
109 47 ▶sub create_element {
Subroutine create_element: called CC=3 SCAR=11.0
Pod undocumented
110 47 my($ns, $name) = @_;
111 47 my($ns_uri, $ns_prefix);
112 47 ▶ if (ref $ns eq 'XML::Atom::Namespace') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ns eq 'XML::Atom::Namespace') { } 7 40 47
113 7 $ns_uri = $ns->{uri};
114 7 $ns_prefix = $ns->{prefix};
115 } else {
116 40 $ns_uri = $ns;
117 }
118 47 my $elem;
119 47 if (LIBXML) {
120 47 $elem = XML::LibXML::Element->new($name);
121 47 ▶ $elem->setNamespace($ns_uri, $ns_prefix ? $ns_prefix : ());
Branch: 1 branch
BranchTrueFalseTotal
$ns_prefix ? : 7 40 47
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 47 return $elem;
129 }
130  
131 23 1;
132 __END__
« Atom/Server.pm ↑ Summary Atom/Base.pm »