blib/lib/RDF/Trine/Node/Literal/XML.pm

filter: Statementstmt 93.9% 77 / 82 Branchbran 84.3% 27 / 32 Conditioncond 83.3% 15 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 86.3% 133 / 154 CC 26 SCAR 23.7
CC
26
Cov
90%
new
18.4
_check_type
7.0
xml_element
2.1
CRAP 10.7
↑ Summary
1 # RDF::Trine::Node::Literal::XML
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Node::Literal::XML - RDF Node class for XMLLiterals
7  
8 =cut
9  
10 package RDF::Trine::Node::Literal::XML;
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use base qw(RDF::Trine::Node::Literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use XML::LibXML qw(:ns);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ######################################################################
22  
23 3 our ( $VERSION, %XML_FRAGMENTS );
24  
25 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 $VERSION = '0.16';
27 }
28  
29 ######################################################################
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item C<< new ( $node ) >>
36  
37 =item C<< new ( $string [ , $lang, $datatype ] ) >>
38  
39 Returns a new XML Literal object. This method can be used in two different ways:
40 It can either be passed a string or an XML::LibXML node.
41  
42 In the case of passing a string, this method follows the same API as the
43 RDF::Trine::Node::Literal constructor, but:
44  
45 =over
46  
47 =item * $string must be a well-balanced XML fragment
48 =item * $lang is optional, but if a language code is present it will be used as the value of C<< xml:lang >> attribute(s) on the root XML element(s) of the literal. If the element already has an C<< xml:lang >> attribute it will be overwritten. For the node types that doesn't support adding a language, text and CData, a warning will be issued.
49 =item * $datatype will be ignored and set to 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'
50  
51 =back
52  
53 In the case of using a XML::LibXML node C<< $node >>,
54 the Node may be one of these types or a subclass thereof:
55  
56 =over
57  
58 =item * XML::LibXML::Document
59 =item * XML::LibXML::DocumentFragment
60 =item * XML::LibXML::Element
61 =item * XML::LibXML::CDATASection
62 =item * XML::LibXML::NodeList
63 =item * XML::LibXML::Text
64  
65 =back
66  
67 If the string is not a valid XML fragment, and the C<< $node >> is not
68 of one of the above types, this method throws a RDF::Trine::Error exception.
69  
70 =cut
71  
72 49 ▶sub new {
Subroutine new: called CC=18 SCAR=29.1
Pod documented
73 49 my $class = shift;
74 49 my $input = shift;
75 49 my $lang = shift;
76  
77 49 my $typeok = _check_type($input); # First check if we have a valid node
78 49 ▶ if ($typeok) { # Then use it
Branch: 1 branch
BranchTrueFalseTotal
if ($typeok) 22 27 49
79 22 my $literal;
80 22 ▶ if ( $input->isa('XML::LibXML::NodeList') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->isa('XML::LibXML::NodeList')) { } 8 14 22
81 8 foreach my $context ( $input->get_nodelist ) {
82 16 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) 8 8 16
83 8 $context->setAttributeNS( XML_XML_NS, 'lang', $lang );
84 }
85 16 ▶ if ( $context->ownerDocument ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->ownerDocument) { } 16 0 16
86 16 $literal .= $context->toStringEC14N;
87 }
88 else {
89 0 $literal .= $context->toString;
90 }
91 }
92 }
93 else {
94 14 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) 7 7 14
95 7 ▶ if ( $input->isa('XML::LibXML::Element') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($input->isa('XML::LibXML::Element')) { } 1 6 7
elsif ($input->isa('XML::LibXML::Document')) { } 3 3 6
elsif ($input->isa('XML::LibXML::DocumentFragment')) { } 3 0 3
96 1 $input->setAttributeNS( XML_XML_NS, 'lang', $lang );
97 }
98 elsif ( $input->isa('XML::LibXML::Document') ) {
99 3 my $root = $input->documentElement();
100 3 $root->setAttributeNS( XML_XML_NS, 'lang', $lang );
101 }
102 elsif ( $input->isa('XML::LibXML::DocumentFragment') ) {
103 3 foreach my $context ( $input->childNodes ) {
104 3 $context->setAttributeNS( XML_XML_NS, 'lang', $lang );
105 }
106 }
107 else {
108 0 carp ref($input) . " doesn't support xml:lang attributes";
109 }
110 }
111 14 ▶ if ( $input->ownerDocument ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->ownerDocument) { } 5 9 14
112 5 $literal = $input->toStringEC14N;
113 }
114 else {
115 9 $literal = $input->toString;
116 }
117 }
118 22 my $self = $class->SUPER::new( $literal, undef,
119 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
120 22 $XML_FRAGMENTS{ refaddr($self) } = $input;
121 22 return $self;
122 }
123  
124 27 ▶ if ( ref($input) && ( !$typeok ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input and not $typeok) 0 27 27
Condition33%1/3
ref $input and not $typeok
!ll&&!rl&&r
2700
MC/DC0%0/2
ref $input and not $typeok
ref $input not $typeok
Truth table33%1/3
A and not(B)
A= ref $inputB= not $typeok
ABresult
0X0
100
111
125 { # Then it is neither a string nor a good type
126 0 throw RDF::Trine::Error -text => ref($input) . " is not a valid type.";
127 }
128  
129 # If we have an empty string, just create an empty text node
130 27 ▶ if ( length($input) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input == 0) 1 26 27
131 1 my $self = $class->SUPER::_new( $input, undef,
132 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
133  
134 1 $XML_FRAGMENTS{ refaddr($self) } = XML::LibXML::Text->new($input);
135 1 return $self;
136 }
137  
138 # Last chance is that it is a string with valid XML
139 26 my $parser = XML::LibXML->new();
140 26 my $doc = eval { $parser->parse_balanced_chunk($input) };
141 26 ▶ if ($@) { # Didn't parse, so invalid XML string
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 11 15 26
142 11 throw RDF::Trine::Error -text => "$@";
143 }
144  
145 15 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) 2 13 15
146 2 foreach my $context ( $doc->childNodes ) {
147 2 $context->setAttributeNS( XML_XML_NS, 'lang', $lang );
148 }
149 2 $input = $doc->toString;
150 }
151  
152 15 my $self = $class->SUPER::_new( $input, undef,
153 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
154  
155 15 $XML_FRAGMENTS{ refaddr($self) } = $doc;
156 15 return $self;
157 }
158  
159 =item C<< xml_element >>
160  
161 Returns the XML::LibXML node for the XML Literal.
162  
163 =cut
164  
165 14 ▶sub xml_element {
Subroutine xml_element: called CC=2 SCAR=7.4
Pod documented
166 14 my $self = shift;
167 14 my $node = $XML_FRAGMENTS{ refaddr($self) };
168 14 ▶ unless ( blessed($node) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($node)) 0 14 14
169 0 throw RDF::Trine::Error -text => "No XML element found for object";
170 }
171 14 return $node;
172 }
173  
174 # Check if we have an acceptable type
175 49 ▶sub _check_type {
Subroutine _check_type: called CC=7 SCAR=19.5
176 49 my $type = shift;
177 49 ▶ return 0 unless blessed($type);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($type) 27 22 49
178 22 ▶ return ( $type->isa('XML::LibXML::Document')
Condition93%14/15
$type->isa('XML::LibXML::Document') || $type->isa('XML::LibXML::DocumentFragment') || $type->isa('XML::LibXML::Element') || $type->isa('XML::LibXML::CDATASection') || $type->isa('XML::LibXML::Text') || $type->isa('XML::LibXML::NodeList')
l!l&&r!l&&!r
1480
$type->isa("XML::LibXML::Document") or $type->isa("XML::LibXML::DocumentFragment") or $type->isa("XML::LibXML::Element") or $type->isa("XML::LibXML::CDATASection") or $type->isa("XML::LibXML::Text")
l!l&&r!l&&!r
1318
$type->isa("XML::LibXML::Document") or $type->isa("XML::LibXML::DocumentFragment") or $type->isa("XML::LibXML::Element") or $type->isa("XML::LibXML::CDATASection")
l!l&&r!l&&!r
1219
$type->isa("XML::LibXML::Document") or $type->isa("XML::LibXML::DocumentFragment") or $type->isa("XML::LibXML::Element")
l!l&&r!l&&!r
9310
$type->isa("XML::LibXML::Document") or $type->isa("XML::LibXML::DocumentFragment")
l!l&&r!l&&!r
5413
MC/DC0%0/6
$type->isa('XML::LibXML::Document') || $type->isa('XML::LibXML::DocumentFragment') || $type->isa('XML::LibXML::Element') || $type->isa('XML::LibXML::CDATASection') || $type->isa('XML::LibXML::Text') || $type->isa('XML::LibXML::NodeList')
$type->isa("XML::LibXML::Document") $type->isa("XML::LibXML::DocumentFragment") $type->isa("XML::LibXML::Element") $type->isa("XML::LibXML::CDATASection") $type->isa("XML::LibXML::Text") $type->isa('XML::LibXML::NodeList')
Truth table85%6/7
A or B or C or D or E || F
A= $type->isa("XML::LibXML::Document")B= $type->isa("XML::LibXML::DocumentFragment")C= $type->isa("XML::LibXML::Element")D= $type->isa("XML::LibXML::CDATASection")E= $type->isa("XML::LibXML::Text")F= $type->isa('XML::LibXML::NodeList')
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
179 || $type->isa('XML::LibXML::DocumentFragment')
180 || $type->isa('XML::LibXML::Element')
181 || $type->isa('XML::LibXML::CDATASection')
182 || $type->isa('XML::LibXML::Text')
183 || $type->isa('XML::LibXML::NodeList') );
184 }
185  
186 38 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.4
187 38 my $self = shift;
188 38 delete $XML_FRAGMENTS{ refaddr($self) };
189  
190 38 ▶ if ( $self->can('SUPER::DESTROY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("SUPER::DESTROY")) 0 38 38
191 0 $self->SUPER::DESTROY();
192 }
193 38 return;
194 }
195  
196 3 1;
197  
198 __END__
↑ Summary