RDF/Trine/Node/Resource.pm

filter: Statementstmt 91.6% 131 / 143 Branchbran 78.5% 44 / 56 Conditioncond 84.6% 11 / 13 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 26 / 26 Podpod 100% 10 / 10 totaltotal 89.0% 227 / 255 CC 36 SCAR 21.1
CC
36
Cov
88%
as_ntriples
22.1
new
7.4
sse
6.0
CRAP 8.3
« RDF/Trine/Parser/Turtle.pm ↑ Summary RDF/Trine/Statement/Quad.pm »
1 # RDF::Trine::Node::Resource
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Node::Resource - RDF Node class for IRI resources
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Node::Resource version 1.019
11  
12 =cut
13  
14 package RDF::Trine::Node::Resource;
15  
16 68 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 68 ▶use base qw(RDF::Trine::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 68 ▶use IRI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 68 ▶use URI 1.52;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 68 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 68 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30  
31 68 our ($VERSION, %sse, %ntriples);
32 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 68 $VERSION = '1.019';
34 }
35  
36 ######################################################################
37  
38 7428 ▶use overload '""' => sub { $_[0]->sse },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
39 68 ;
40  
41 =head1 METHODS
42  
43 Beyond the methods documented below, this class inherits methods from the
44 L<RDF::Trine::Node> class.
45  
46 =over 4
47  
48 =cut
49  
50 =item C<new ( $iri, [ $base_uri ] )>
51  
52 Returns a new Resource structure.
53  
54 =cut
55  
56 15844 ▶sub new {
Subroutine new: called CC=7 SCAR=20.0
Pod documented
57 15844 my $class = shift;
58 15844 my $uri = shift;
59 15844 my $base_uri = shift;
60
61 15844 ▶ unless (defined($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri) 0 15844 15844
62 0 throw RDF::Trine::Error::MethodInvocationError -text => "Resource constructor called with an undefined value";
63 }
64
65 15844 ▶ if (defined($base_uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base_uri) 2785 13059 15844
66 2785 ▶ if (blessed($base_uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($base_uri)) { } 1060 1725 2785
67 1060 ▶ if ($base_uri->isa('RDF::Trine::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_uri->isa("RDF::Trine::Node::Resource")) 975 85 1060
68 975 $base_uri = IRI->new( $base_uri->uri_value );
69 }
70 } else {
71 1725 $base_uri = IRI->new($base_uri);
72 }
73 2785 my $iri = IRI->new( value => $uri, base => $base_uri );
74 2785 $uri = $iri->abs;
75 }
76 15844 utf8::upgrade($uri);
77
78 15844 ▶ if ($uri eq &RDF::Trine::NIL_GRAPH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq &RDF::Trine::NIL_GRAPH) 0 15844 15844
79 0 return RDF::Trine::Node::Nil->new();
80 }
81
82 15844 ▶ if ($uri =~ /([<>" {}|\\^`])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /([<>" {}|\\^`])/) 0 15844 15844
83 0 throw RDF::Trine::Error -text => sprintf("Bad IRI character: '%s' (0x%x)", $1, ord($1));
84 }
85
86 15844 return bless( [ 'URI', $uri ], $class );
87 }
88  
89 =item C<< uri_value >>
90  
91 Returns the URI/IRI value of this resource.
92  
93 =cut
94  
95 42932 ▶sub uri_value {
Subroutine uri_value: called CC=1 SCAR=0.0
Pod documented
96 42932 my $self = shift;
97 42932 return $self->[1];
98 }
99  
100 =item C<< value >>
101  
102 Returns the URI/IRI value.
103  
104 =cut
105  
106 11 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
107 11 my $self = shift;
108 11 return $self->uri_value;
109 }
110  
111 =item C<< uri ( $uri ) >>
112  
113 Returns the URI value of this resource, optionally updating the URI.
114  
115 =cut
116  
117 687 ▶sub uri {
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
118 687 my $self = shift;
119 687 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 686 687
120 1 $self->[1] = shift;
121 1 delete $sse{ refaddr($self) };
122 1 delete $ntriples{ refaddr($self) };
123 }
124 687 return $self->[1];
125 }
126  
127 =item C<< sse >>
128  
129 Returns the SSE string for this resource.
130  
131 =cut
132  
133 22297 ▶sub sse {
Subroutine sse: called CC=6 SCAR=17.9
Pod documented
134 22297 my $self = shift;
135 22297 my $context = shift;
136
137 22297 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 2 22295 22297
138 2 my $uri = $self->uri_value;
139 2 ▶ my $ns = $context->{namespaces} || {};
Condition50%1/2
$context->{'namespaces'} || {}
l!l
20
MC/DC0%0/1
$context->{'namespaces'} || {}
$context->{'namespaces'}
Truth table50%1/2
A
A= $context->{'namespaces'}
Aresult
11
00
140 2 my %ns = %$ns;
141 2 foreach my $k (keys %ns) {
142 2 my $v = $ns{ $k };
143 2 ▶ if (index($uri, $v) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($uri, $v) == 0) 1 1 2
144 1 my $qname = join(':', $k, substr($uri, length($v)));
145 1 return $qname;
146 }
147 }
148 }
149
150 22296 my $ra = refaddr($self);
151 22296 ▶ if ($sse{ $ra }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RDF::Trine::Node::Resource::sse{$ra}) { } 13657 8639 22296
152 13657 return $sse{ $ra };
153 } else {
154 8639 my $string = URI->new( $self->uri_value )->canonical;
155 8639 my $sse = '<' . $string . '>';
156 8639 $sse{ $ra } = $sse;
157 8639 return $sse;
158 }
159
160 # my $string = $uri;
161 # my $escaped = $self->_unicode_escape( $string );
162 # return '<' . $escaped . '>';
163 }
164  
165 =item C<< as_string >>
166  
167 Returns a string representation of the node.
168  
169 =cut
170  
171 19779 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
172 19779 my $self = shift;
173 19779 return '<' . $self->uri_value . '>';
174 }
175  
176 =item C<< as_ntriples >>
177  
178 Returns the node in a string form suitable for NTriples serialization.
179 If the IRI contains punycode, it will be decoded and serialized as unicode codepoints.
180  
181 =cut
182  
183 3307 ▶sub as_ntriples {
Subroutine as_ntriples: called CC=15 SCAR=30.9
Pod documented
184 3307 my $self = shift;
185 3307 my $context = shift;
186 3307 my $ra = refaddr($self);
187 3307 ▶ if ($ntriples{ $ra }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RDF::Trine::Node::Resource::ntriples{$ra}) { } 207 3100 3307
188 207 return $ntriples{ $ra };
189 } else {
190 3100 my $uri = $self->uri_value;
191 3100 $uri = URI->new($uri)->as_iri;
192 3100 my @chars = split(//, $uri);
193 3100 my $string = '';
194 3100 ▶ while (scalar(@chars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @chars) 107571 3100 110671
195 107571 my $c = shift(@chars);
196 107571 my $o = ord($c);
197 107571 ▶ if ($o < 0x8) {
Branch: 12 branches
BranchTrueFalseTotal
if ($o < 8) { } 0 107571 107571
elsif ($o == 9) { } 0 107571 107571
elsif ($o == 10) { } 0 107571 107571
elsif ($o < 12) { } 0 107571 107571
elsif ($o == 13) { } 0 107571 107571
elsif ($o < 31) { } 0 107571 107571
elsif ($o < 33) { } 0 107571 107571
elsif ($o == 34) { } 0 107571 107571
elsif ($o < 91) { } 36690 70881 107571
elsif ($o == 92) { } 0 70881 70881
elsif ($o < 126) { } 70706 175 70881
elsif ($o < 65535) { } 167 8 175
198 0 $string .= sprintf("\\u%04X", $o);
199 } elsif ($o == 0x9) {
200 0 $string .= "\\t";
201 } elsif ($o == 0xA) {
202 0 $string .= "\\n";
203 } elsif ($o < 0xC) {
204 0 $string .= sprintf("\\u%04X", $o);
205 } elsif ($o == 0xD) {
206 0 $string .= "\\r";
207 } elsif ($o < 0x1F) {
208 0 $string .= sprintf("\\u%04X", $o);
209 } elsif ($o < 0x21) {
210 0 $string .= $c;
211 } elsif ($o == 0x22) {
212 0 $string .= "\"";
213 } elsif ($o < 0x5B) {
214 36690 $string .= $c;
215 } elsif ($o == 0x5C) {
216 0 $string .= "\\";
217 } elsif ($o < 0x7E) {
218 70706 $string .= $c;
219 } elsif ($o < 0xFFFF) {
220 167 $string .= sprintf("\\u%04X", $o);
221 } else {
222 8 $string .= sprintf("\\U%08X", $o);
223 }
224 }
225 3100 my $ntriples = '<' . $string . '>';
226 3100 $ntriples{ $ra } = $ntriples;
227 3100 return $ntriples;
228 }
229 }
230  
231 =item C<< type >>
232  
233 Returns the type string of this node.
234  
235 =cut
236  
237 6368 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
238 6368 return 'URI';
239 }
240  
241 =item C<< equal ( $node ) >>
242  
243 Returns true if the two nodes are equal, false otherwise.
244  
245 =cut
246  
247 1219 ▶sub equal {
Subroutine equal: called CC=5 SCAR=16.1
Pod documented
248 1219 my $self = shift;
249 1219 my $node = shift;
250 1219 ▶ return 0 unless defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 37 1182 1219
251 1182 ▶ return 1 if (refaddr($self) == refaddr($node));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($self) == builtin::refaddr($node) 54 1128 1182
252 1128 ▶ return 0 unless (blessed($node) and $node->isa('RDF::Trine::Node::Resource'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) and $node->isa("RDF::Trine::Node::Resource") 16 1112 1128
Condition66%2/3
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
0161112
MC/DC50%1/2
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Resource")
builtin::blessed($node) $node->isa("RDF::Trine::Node::Resource")
Truth table66%2/3
A and B
A= builtin::blessed($node)B= $node->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
253
254 1112 my $uri1 = URI->new($self->uri_value)->as_iri;
255 1112 my $uri2 = URI->new($node->uri_value)->as_iri;
256 1112 return ($uri1 eq $uri2);
257 }
258  
259 # called to compare two nodes of the same type
260 1464 ▶sub _compare {
Subroutine _compare: called CC=1 SCAR=0.0
261 1464 my $a = shift;
262 1464 my $b = shift;
263 1464 return ($a->uri_value cmp $b->uri_value);
264 }
265  
266 =item C<< qname >>
267  
268 If the IRI can be split into a namespace and local part for construction of a
269 QName, returns a list containing these two parts. Otherwise throws an exception.
270  
271 =cut
272  
273 263 ▶sub qname {
Subroutine qname: called CC=6 SCAR=17.9
Pod documented
274 263 my $p = shift;
275 263 my $uri = $p->uri_value;
276  
277 263 ▶ our $r_PN_CHARS_BASE ||= qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/;
Condition100%2/2
our $r_PN_CHARS_BASE ||= qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/
l!l
2558
MC/DC100%1/1
our $r_PN_CHARS_BASE ||= qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/
our $r_PN_CHARS_BASE
Truth table100%2/2
A
A= our $r_PN_CHARS_BASE
Aresult
11
00
278 263 ▶ our $r_PN_CHARS_U ||= qr/(_|${r_PN_CHARS_BASE})/;
Condition100%2/2
our $r_PN_CHARS_U ||= qr/(_|$RDF::Trine::Node::Resource::r_PN_CHARS_BASE)/
l!l
2558
MC/DC100%1/1
our $r_PN_CHARS_U ||= qr/(_|$RDF::Trine::Node::Resource::r_PN_CHARS_BASE)/
our $r_PN_CHARS_U
Truth table100%2/2
A
A= our $r_PN_CHARS_U
Aresult
11
00
279 263 ▶ our $r_PN_CHARS ||= qr/${r_PN_CHARS_U}|-|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}]/;
Condition100%2/2
our $r_PN_CHARS ||= qr/$RDF::Trine::Node::Resource::r_PN_CHARS_U|-|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}]/
l!l
2558
MC/DC100%1/1
our $r_PN_CHARS ||= qr/$RDF::Trine::Node::Resource::r_PN_CHARS_U|-|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}]/
our $r_PN_CHARS
Truth table100%2/2
A
A= our $r_PN_CHARS
Aresult
11
00
280 263 ▶ our $r_PN_LOCAL ||= qr/((${r_PN_CHARS_U})((${r_PN_CHARS}|[.])*${r_PN_CHARS})?)/;
Condition100%2/2
our $r_PN_LOCAL ||= qr/(($RDF::Trine::Node::Resource::r_PN_CHARS_U)(($RDF::Trine::Node::Resource::r_PN_CHARS|[.])*$RDF::Trine::Node::Resource::r_PN_CHARS)?)/
l!l
2558
MC/DC100%1/1
our $r_PN_LOCAL ||= qr/(($RDF::Trine::Node::Resource::r_PN_CHARS_U)(($RDF::Trine::Node::Resource::r_PN_CHARS|[.])*$RDF::Trine::Node::Resource::r_PN_CHARS)?)/
our $r_PN_LOCAL
Truth table100%2/2
A
A= our $r_PN_LOCAL
Aresult
11
00
281 263 ▶ if ($uri =~ m/${r_PN_LOCAL}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /$RDF::Trine::Node::Resource::r_PN_LOCAL$/) { } 252 11 263
282 252 my $ln = $1;
283 252 my $ns = substr($uri, 0, length($uri)-length($ln));
284 252 return ($ns, $ln);
285 } else {
286 11 throw RDF::Trine::Error -text => "Can't turn IRI $uri into a QName.";
287 }
288 }
289  
290 15799 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
291 15799 my $self = shift;
292 15799 delete $sse{ refaddr($self) };
293 15799 delete $ntriples{ refaddr($self) };
294 }
295  
296 68 1;
297  
298 __END__
« RDF/Trine/Parser/Turtle.pm ↑ Summary RDF/Trine/Statement/Quad.pm »