RDF/Trine/Node.pm

filter: Statementstmt 88.5% 116 / 131 Branchbran 77.7% 42 / 54 Conditioncond 55.5% 10 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 87.5% 21 / 24 Podpod 100% 13 / 13 totaltotal 81.7% 206 / 252 CC 34 SCAR 17.1
CC
34
Cov
83%
from_sse
13.1
_unicode_escape
8.0
as_hashref
6.1
CRAP 5.6
« RDF/Trine/Serializer.pm ↑ Summary RDF/Trine/Store/LanguagePreference.pm »
1 # RDF::Trine::Node
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Node - Base class for RDF Nodes
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Node version 1.019
11  
12 =cut
13  
14 package RDF::Trine::Node;
15  
16 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 68 our ($VERSION, @ISA, @EXPORT_OK);
21 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 $VERSION = '1.019';
23
24 68 require Exporter;
25 68 @ISA = qw(Exporter);
26 68 @EXPORT_OK = qw(ntriples_escape);
27 }
28  
29 68 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 68 ▶use RDF::Trine::Node::Nil;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 68 ▶use RDF::Trine::Node::Blank;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 68 ▶use RDF::Trine::Node::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use RDF::Trine::Node::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use RDF::Trine::Node::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 =head1 FUNCTIONS
39  
40 =over 4
41  
42 =item C<< ntriples_escape ( $value ) >>
43  
44 Returns the passed string value with special characters (control characters,
45 Unicode, etc.) escaped, suitable for printing inside an N-Triples or Turtle
46 encoded literal.
47  
48 =cut
49  
50 0 ▶sub ntriples_escape {
Subroutine ntriples_escape: not called CC=1 SCAR=6.9
Pod documented
51 0 my $class = __PACKAGE__;
52 0 return $class->_unicode_escape( @_ );
53 }
54  
55 =back
56  
57 =head1 METHODS
58  
59 =over 4
60  
61 =item C<< is_node >>
62  
63 Returns true if this object is a RDF node, false otherwise.
64  
65 =cut
66  
67 1 ▶sub is_node {
Subroutine is_node: called CC=2 SCAR=8.1
Pod documented
68 1 my $self = shift;
69 1 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node'));
Condition33%1/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node')
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node')
builtin::blessed($self) $self->isa('RDF::Trine::Node')
Truth table33%1/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node')
ABresult
0X0
100
111
70 }
71  
72 =item C<< is_nil >>
73  
74 Returns true if this object is the nil-valued node.
75  
76 =cut
77  
78 3411 ▶sub is_nil {
Subroutine is_nil: called CC=2 SCAR=8.1
Pod documented
79 3411 my $self = shift;
80 3411 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node::Nil'));
Condition33%1/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Nil')
!ll&&!rl&&r
034110
MC/DC0%0/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Nil')
builtin::blessed($self) $self->isa('RDF::Trine::Node::Nil')
Truth table33%1/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node::Nil')
ABresult
0X0
100
111
81 }
82  
83 =item C<< is_blank >>
84  
85 Returns true if this RDF node is a blank node, false otherwise.
86  
87 =cut
88  
89 1098 ▶sub is_blank {
Subroutine is_blank: called CC=2 SCAR=7.1
Pod documented
90 1098 my $self = shift;
91 1098 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node::Blank'));
Condition66%2/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Blank')
!ll&&!rl&&r
0107919
MC/DC50%1/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Blank')
builtin::blessed($self) $self->isa('RDF::Trine::Node::Blank')
Truth table66%2/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node::Blank')
ABresult
0X0
100
111
92 }
93  
94 =item C<< is_resource >>
95  
96 Returns true if this RDF node is a resource, false otherwise.
97  
98 =cut
99  
100 1111 ▶sub is_resource {
Subroutine is_resource: called CC=2 SCAR=7.1
Pod documented
101 1111 my $self = shift;
102 1111 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node::Resource'));
Condition66%2/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Resource')
!ll&&!rl&&r
0481063
MC/DC50%1/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Resource')
builtin::blessed($self) $self->isa('RDF::Trine::Node::Resource')
Truth table66%2/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node::Resource')
ABresult
0X0
100
111
103 }
104  
105 =item C<< is_literal >>
106  
107 Returns true if this RDF node is a literal, false otherwise.
108  
109 =cut
110  
111 292 ▶sub is_literal {
Subroutine is_literal: called CC=2 SCAR=7.1
Pod documented
112 292 my $self = shift;
113 292 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node::Literal'));
Condition66%2/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Literal')
!ll&&!rl&&r
020785
MC/DC50%1/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Literal')
builtin::blessed($self) $self->isa('RDF::Trine::Node::Literal')
Truth table66%2/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node::Literal')
ABresult
0X0
100
111
114 }
115  
116 =item C<< is_variable >>
117  
118 Returns true if this RDF node is a variable, false otherwise.
119  
120 =cut
121  
122 12012 ▶sub is_variable {
Subroutine is_variable: called CC=2 SCAR=7.1
Pod documented
123 12012 my $self = shift;
124 12012 ▶ return (blessed($self) and $self->isa('RDF::Trine::Node::Variable'));
Condition66%2/3
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Variable')
!ll&&!rl&&r
065595453
MC/DC50%1/2
builtin::blessed($self) && $self->isa('RDF::Trine::Node::Variable')
builtin::blessed($self) $self->isa('RDF::Trine::Node::Variable')
Truth table66%2/3
A && B
A= builtin::blessed($self)B= $self->isa('RDF::Trine::Node::Variable')
ABresult
0X0
100
111
125 }
126  
127 =item C<< as_string >>
128  
129 Returns the node in a string form.
130  
131 =cut
132  
133 9728 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.1
Pod documented
134 9728 my $self = shift;
135 9728 ▶ Carp::confess unless ($self->can('sse'));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("sse") 0 9728 9728
136 9728 return $self->sse;
137 }
138  
139 =item C<< as_ntriples >>
140  
141 Returns the node in a string form suitable for NTriples serialization.
142  
143 =cut
144  
145 0 ▶sub as_ntriples {
Subroutine as_ntriples: not called CC=1 SCAR=6.9
Pod documented
146 0 return $_[0]->sse;
147 }
148  
149 =item C<< sse >>
150  
151 Returns the SSE serialization of the node.
152  
153 =cut
154  
155 =item C<< equal ( $node ) >>
156  
157 Returns true if the two nodes are equal, false otherwise.
158  
159 =cut
160  
161 0 ▶sub equal {
Subroutine equal: not called CC=2 SCAR=17.9
Pod documented
162 0 my $self = shift;
163 0 my $node = shift;
164 0 ▶ return 0 unless (blessed($node));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) 0 0 0
165 0 return (refaddr($self) == refaddr($node));
166 }
167  
168 =item C<< compare ( $node_a, $node_b ) >>
169  
170 Returns -1, 0, or 1 if $node_a sorts less than, equal to, or greater than
171 $node_b in the defined SPARQL ordering, respectively. This function may be
172 used as the function argument to C<<sort>>.
173  
174 =cut
175  
176 68 my %order = (
177 NIL => 0,
178 BLANK => 1,
179 URI => 2,
180 LITERAL => 3,
181 );
182 2367 ▶sub compare {
Subroutine compare: called CC=4 SCAR=13.9
Pod documented
183 2367 my $a = shift;
184 2367 my $b = shift;
185 2367 ▶ return -1 unless blessed($a);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($a) 1 2366 2367
186 2366 ▶ return 1 unless blessed($b);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($b) 0 2366 2366
187
188 # (Lowest) no value assigned to the variable or expression in this solution.
189 # Blank nodes
190 # IRIs
191 # RDF literals (plain < xsd:string)
192 2366 my $at = $a->type;
193 2366 my $bt = $b->type;
194 2366 ▶ if ($a->type ne $b->type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->type ne $b->type) { } 255 2111 2366
195 255 my $an = $order{ $at };
196 255 my $bn = $order{ $bt };
197 255 return ($an <=> $bn);
198 } else {
199 2111 return $a->_compare( $b );
200 }
201 }
202  
203 =item C<< as_hashref >>
204  
205 Returns a hashref representing the node in an RDF/JSON-like manner.
206  
207 See C<< as_hashref >> at L<RDF::Trine::Model> for full documentation of the
208 hashref format.
209  
210 =cut
211  
212 6 ▶sub as_hashref {
Subroutine as_hashref: called CC=6 SCAR=18.1
Pod documented
213 6 my $self = shift;
214 6 my $o = {};
215 6 ▶ if ($self->isa('RDF::Trine::Node::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('RDF::Trine::Node::Literal')) { } 4 2 6
216 4 $o->{'type'} = 'literal';
217 4 $o->{'value'} = $self->literal_value;
218 4 ▶ $o->{'lang'} = $self->literal_value_language
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_language 2 2 4
219 if $self->has_language;
220 4 ▶ $o->{'datatype'} = $self->literal_datatype
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_datatype 0 4 4
221 if $self->has_datatype;
222 } else {
223 2 ▶ $o->{'type'} = $self->isa('RDF::Trine::Node::Blank') ? 'bnode' : 'uri';
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('RDF::Trine::Node::Blank') ? : 0 2 2
224 2 ▶ $o->{'value'} = $self->isa('RDF::Trine::Node::Blank') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('RDF::Trine::Node::Blank') ? : 0 2 2
225 ('_:'.$self->blank_identifier) :
226 $self->uri ;
227 }
228 6 return $o;
229 }
230  
231 =item C<< from_sse ( $string, $context ) >>
232  
233 Parses the supplied SSE-encoded string and returns a RDF::Trine::Node object.
234  
235 =cut
236  
237 68 my $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}])/;
238 68 my $r_PN_CHARS_U = qr/(_|${r_PN_CHARS_BASE})/;
239 68 my $r_VARNAME = qr/((${r_PN_CHARS_U}|[0-9])(${r_PN_CHARS_U}|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}])*)/;
240 10 ▶sub from_sse {
Subroutine from_sse: called CC=11 SCAR=25.7
Pod documented
241 10 my $class = shift;
242 10 my $context = $_[1];
243 10 $_ = $_[0];
244 10 ▶ if (my ($iri) = m/^<([^>]+)>/o) {
Branch: 6 branches
BranchTrueFalseTotal
if (my($iri) = /^<([^>]+)>/o) { } 5 5 10
elsif (my($lit) = /^"(([^"\\]+|\\([\\"nt]))+)"/o) { } 3 2 5
elsif (my($id1) = /^[(]([^)]+)[)]/) { } 1 1 2
elsif (my($id2) = /^_:(\S+)/) { } 0 1 1
elsif (my($v) = /^[?]($r_VARNAME)/) { } 0 1 1
elsif (() = /^(\S*):(\S*)/o) { } 1 0 1
245 5 s/^<([^>]+)>\s*//;
246 5 return RDF::Trine::Node::Resource->new( $iri );
247 } elsif (my ($lit) = m/^"(([^"\\]+|\\([\\"nt]))+)"/o) {
248 3 my @args;
249 3 s/^"(([^"\\]+|\\([\\"nt]))+)"//;
250 3 ▶ if (my ($lang) = m/[@](\S+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($lang) = /[\@](\S+)/) { } 1 2 3
elsif (/^\^\^/) { } 1 1 2
251 1 s/[@](\S+)\s*//;
252 1 $args[0] = $lang;
253 } elsif (m/^\Q^^\E/) {
254 1 s/^\Q^^\E//;
255 1 my ($dt) = $class->from_sse( $_, $context );
256 1 $args[1] = $dt->uri_value;
257 }
258 0 $lit =~ s/\\(.)/eval "\"\\$1\""/ge;
259 3 return RDF::Trine::Node::Literal->new( $lit, @args );
260 } elsif (my ($id1) = m/^[(]([^)]+)[)]/) {
261 1 s/^[(]([^)]+)[)]\s*//;
262 1 return RDF::Trine::Node::Blank->new( $id1 );
263 } elsif (my ($id2) = m/^_:(\S+)/) {
264 0 s/^_:(\S+)\s*//;
265 0 return RDF::Trine::Node::Blank->new( $id2 );
266 } elsif (my ($v) = m/^[?](${r_VARNAME})/) {
267 0 s/^[?](${r_VARNAME})\s*//;
268 0 return RDF::Trine::Node::Variable->new( $v );
269 } elsif (my ($pn, $ln) = m/^(\S*):(\S*)/o) {
270 1 ▶ if ($pn eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pn eq "") 0 1 1
271 0 $pn = '__DEFAULT__';
272 }
273 1 ▶ if (my $ns = $context->{namespaces}{ $pn }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $context->{'namespaces'}{$pn}) { } 1 0 1
274 1 s/^(\S+):(\S+)\s*//;
275 1 return RDF::Trine::Node::Resource->new( join('', $ns, $ln) );
276 } else {
277 0 throw RDF::Trine::Error -text => "No such namespace '$pn' while parsing SSE QName: >>$_<<";
278 }
279 } else {
280 0 throw RDF::Trine::Error -text => "Cannot parse SSE node from SSE string: >>$_<<";
281 }
282 }
283  
284 4110 ▶sub _unicode_escape {
Subroutine _unicode_escape: called CC=8 SCAR=20.8
285 4110 my $self = shift;
286 4110 my $str = shift;
287
288 4110 ▶ if ($str =~ /\A[^\\\n\t\r"\x{10000}-\x{10ffff}\x{7f}-\x{ffff}\x{00}-\x{08}\x{0b}-\x{0c}\x{0e}-\x{1f}]*\z/sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\A[^\\\n\t\r"\x{10000}-\x{10ffff}\x{7f}-\x{ffff}\x{00}-\x{08}\x{0b}-\x{0c}\x{0e}-\x{1f}]*\z/ms) { } 3782 328 4110
289 # hot path - no special characters to escape, just printable ascii
290 3782 return $str;
291 } else {
292 # slow path - escape all the special characters
293 328 my $rslt = '';
294 328 ▶ while (length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 4857 328 5185
295 4857 ▶ if (my ($ascii) = $str =~ /^([A-Za-z0-9 \t]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ascii) = $str =~ /^([A-Za-z0-9 \t]+)/) { } 1469 3388 4857
296 1469 $rslt .= $ascii;
297 1469 substr($str, 0, length($ascii)) = '';
298 } else {
299 3388 my $utf8 = substr($str,0,1,'');
300 3388 ▶ if ($utf8 eq '\\') {
Branch: 4 branches
BranchTrueFalseTotal
if ($utf8 eq '\\') { } 62 3326 3388
elsif ($utf8 =~ /^[\x{10000}-\x{10ffff}]$/) { } 184 3142 3326
elsif ($utf8 =~ /^[\x7f-\x{ffff}]$/) { } 523 2619 3142
elsif ($utf8 =~ /^[\x00-\x08\x0b-\x0c\x0e-\x1f]$/) { } 303 2316 2619
301 62 $rslt .= '\\\\';
302 } elsif ($utf8 =~ /^[\x{10000}-\x{10ffff}]$/) {
303 184 $rslt .= sprintf('\\U%08X', ord($utf8));
304 } elsif ($utf8 =~ /^[\x7f-\x{ffff}]$/) {
305 # $rslt = '\\u'.uc(unpack('H4', $uchar->utf16be)) . $rslt;
306 523 $rslt .= sprintf('\\u%04X', ord($utf8));
307 } elsif ($utf8 =~ /^[\x00-\x08\x0b-\x0c\x0e-\x1f]$/) {
308 303 $rslt .= sprintf('\\u%04X', ord($utf8));
309 } else {
310 2316 $rslt .= $utf8;
311 }
312 }
313 }
314 # $rslt =~ s/\\/\\\\/g;
315 328 $rslt =~ s/\n/\\n/g;
316 328 $rslt =~ s/\t/\\t/g;
317 328 $rslt =~ s/\r/\\r/g;
318 328 $rslt =~ s/"/\\"/g;
319 328 return $rslt;
320 }
321 }
322  
323 68 1;
324  
325 __END__
« RDF/Trine/Serializer.pm ↑ Summary RDF/Trine/Store/LanguagePreference.pm »