RDF/Trine/Parser/RDFXML.pm

filter: Statementstmt 92.5% 572 / 618 Branchbran 78.7% 156 / 198 Conditioncond 62.0% 36 / 58 MC/DCmcdc 31.4% 11 / 35 Subroutinesub 100% 59 / 59 Podpod 100% 4 / 4 totaltotal 86.2% 838 / 972 CC 129 SCAR 25.8
CC
129
Cov
87%
start_element
38.6
end_element
16.2
parse
15.5
CRAP 13.2
« RDF/Trine/Model/Dataset.pm ↑ Summary RDF/Trine/Serializer/Turtle.pm »
1 # RDF::Trine::Parser::RDFXML
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::RDFXML - RDF/XML Parser
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::RDFXML version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15 my $parser = RDF::Trine::Parser->new( 'rdfxml' );
16 $parser->parse_into_model( $base_uri, $data, $model );
17  
18 =head1 DESCRIPTION
19  
20 ...
21  
22 =head1 METHODS
23  
24 Beyond the methods documented below, this class inherits methods from the
25 L<RDF::Trine::Parser> class.
26  
27 =over 4
28  
29 =cut
30  
31 package RDF::Trine::Parser::RDFXML;
32  
33 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 68 ▶use base qw(RDF::Trine::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 68 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 68 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use XML::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 68 ▶use RDF::Trine qw(literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 ######################################################################
53  
54 68 our ($VERSION, $HAS_XML_LIBXML);
55 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
56 68 $VERSION = '1.019';
57 68 $RDF::Trine::Parser::parser_names{ 'rdfxml' } = __PACKAGE__;
58 68 foreach my $ext (qw(rdf xrdf rdfx)) {
59 204 $RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__;
60 }
61 68 my $class = __PACKAGE__;
62 68 $RDF::Trine::Parser::canonical_media_types{ $class } = 'application/rdf+xml';
63 68 foreach my $type (qw(application/rdf+xml application/octet-stream)) {
64 136 $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__;
65 }
66 68 $RDF::Trine::Parser::format_uris{ 'http://www.w3.org/ns/formats/RDF_XML' } = __PACKAGE__;
67
68 68 $HAS_XML_LIBXML = can_load( modules => {
69 'XML::LibXML' => 1.70,
70 } );
71  
72 }
73  
74 ######################################################################
75  
76 =item C<< new >>
77  
78 =cut
79  
80 263 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
81 263 my $class = shift;
82 263 my %args = @_;
83 263 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
02630
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
84  
85 263 my $prefix = '';
86 263 ▶ if (defined($args{ BNodePrefix })) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'BNodePrefix'}) { } 256 7 263
elsif (defined $args{'bnode_prefix'}) { } 0 7 7
87 256 $prefix = delete $args{ BNodePrefix };
88 } elsif (defined($args{ bnode_prefix })) {
89 0 $prefix = delete $args{ bnode_prefix };
90 } else {
91 7 $prefix = $class->new_bnode_prefix();
92 }
93
94 263 my $saxhandler = RDF::Trine::Parser::RDFXML::SAXHandler->new( %args, bnode_prefix => $prefix );
95 263 my $p = XML::SAX::ParserFactory->parser(Handler => $saxhandler);
96
97 263 my $self = bless( {
98 saxhandler => $saxhandler,
99 parser => $p,
100 %args,
101 }, $class);
102 263 return $self;
103 }
104  
105 =item C<< parse_into_model ( $base_uri, $data, $model [, context => $context] ) >>
106  
107 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. For each RDF
108 statement parsed, will call C<< $model->add_statement( $statement ) >>.
109  
110 =cut
111  
112 136 ▶sub parse_into_model {
Subroutine parse_into_model: called CC=4 SCAR=15.0
Pod documented
113 136 my $proto = shift;
114 136 ▶ my $self = blessed($proto) ? $proto : $proto->new();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proto) ? : 136 0 136
115 136 my $uri = shift;
116 136 ▶ if (blessed($uri) and $uri->isa('RDF::Trine::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")) 0 136 136
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
13600
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
builtin::blessed($uri) $uri->isa("RDF::Trine::Node::Resource")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
117 0 $uri = $uri->uri_value;
118 }
119 136 my $input = shift;
120 136 my $model = shift;
121 136 my %args = @_;
122 136 my $context = $args{'context'};
123
124 242 ▶ my $handler = sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
125 242 my $st = shift;
126 242 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 242 242
127 0 my $quad = RDF::Trine::Statement::Quad->new( $st->nodes, $context );
128 0 $model->add_statement( $quad );
129 } else {
130 242 $model->add_statement( $st );
131 }
132 136 };
133 136 $self->{saxhandler}->set_handler( $handler );
134 136 return $self->parse( $uri, $input, $handler );
135 }
136  
137 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
138  
139 =cut
140  
141 136 ▶sub parse {
Subroutine parse: called CC=11 SCAR=27.4
Pod documented
142 136 my $self = shift;
143 136 my $base = shift;
144 136 my $string = shift;
145 136 my $handler = shift;
146 136 ▶ unless ($string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string) 4 132 136
147 4 throw RDF::Trine::Error::ParserError -text => "No RDF/XML content supplied to parser.";
148 }
149 132 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 132 0 132
150 132 ▶ unless (blessed($base)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($base)) 132 0 132
151 132 $base = RDF::Trine::Node::Resource->new( $base );
152 }
153 132 $self->{saxhandler}->push_base( $base );
154 }
155
156 132 ▶ if ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 132 0 132
157 132 $self->{saxhandler}->set_handler( $handler );
158 }
159
160 132 eval {
161 132 ▶ if (ref($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) { } 0 132 132
162 0 $self->{parser}->parse_file( $string );
163 } else {
164 132 $string = encode('UTF-8', $string, Encode::FB_CROAK);
165 132 $self->{parser}->parse_string( $string );
166 }
167 };
168 132 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 132 132
169 0 throw RDF::Trine::Error::ParserError -text => "$@";
170 }
171
172 132 my $nodes = $self->{saxhandler}{nodes};
173 132 ▶ if ($nodes and scalar(@$nodes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes and scalar @$nodes) 0 132 132
Condition50%1/2
$nodes and scalar @$nodes
!ll
0132
MC/DC0%0/1
$nodes and scalar @$nodes
$nodes
Truth table50%1/2
A
A= $nodes
Aresult
00
11
174 0 warn Dumper($nodes);
175 0 throw RDF::Trine::Error::ParserError -text => "node stack isn't empty after parse";
176 }
177 132 my $expect = $self->{saxhandler}{expect};
178 132 ▶ if ($expect and scalar(@$expect) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect and scalar @$expect > 2) 0 132 132
Condition33%1/3
$expect and scalar @$expect > 2
!ll&&!rl&&r
01320
MC/DC0%0/2
$expect and scalar @$expect > 2
$expect scalar @$expect > 2
Truth table33%1/3
A and B
A= $expectB= scalar @$expect > 2
ABresult
0X0
100
111
179 0 warn Dumper($expect);
180 0 throw RDF::Trine::Error::ParserError -text => "expect stack isn't empty after parse";
181 }
182 }
183  
184 =item C<< parse_file ( $base_uri, $fh, \&handler ) >>
185  
186 Parses all data read from the filehandle C<< $fh >>, using the given
187 C<< $base_uri >>. For each RDF statement parsed, C<< $handler->( $st ) >> is called.
188  
189 Note: The filehandle should NOT be opened with the ":encoding(UTF-8)" IO layer,
190 as this is known to cause problems for XML::SAX.
191  
192 =cut
193  
194 130 ▶sub parse_file {
Subroutine parse_file: called CC=11 SCAR=27.0
Pod documented
195 130 my $self = shift;
196 130 my $base = shift;
197 130 my $fh = shift;
198 130 my $handler = shift;
199
200 130 ▶ unless (ref($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 130 0 130
201 130 my $filename = $fh;
202 130 undef $fh;
203 130 ▶ open( $fh, '<', $filename ) or throw RDF::Trine::Error::ParserError -text => $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or "RDF::Trine::Error::ParserError"->throw("-text", $!) 130 0 130
204 }
205 130 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 128 2 130
206 128 ▶ unless (blessed($base)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($base)) 128 0 128
207 128 $base = RDF::Trine::Node::Resource->new( $base );
208 }
209 128 $self->{saxhandler}->push_base( $base );
210 }
211
212 130 ▶ if ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 130 0 130
213 130 $self->{saxhandler}->set_handler( $handler );
214 }
215
216 130 eval {
217 130 $self->{parser}->parse_file( $fh );
218 };
219 130 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 130 130
220 0 throw RDF::Trine::Error::ParserError -text => "$@";
221 }
222
223 130 my $nodes = $self->{saxhandler}{nodes};
224 130 ▶ if ($nodes and scalar(@$nodes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes and scalar @$nodes) 0 130 130
Condition50%1/2
$nodes and scalar @$nodes
!ll
0130
MC/DC0%0/1
$nodes and scalar @$nodes
$nodes
Truth table50%1/2
A
A= $nodes
Aresult
00
11
225 0 warn Dumper($nodes);
226 0 throw RDF::Trine::Error::ParserError -text => "node stack isn't empty after parse";
227 }
228 130 my $expect = $self->{saxhandler}{expect};
229 130 ▶ if ($expect and scalar(@$expect) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect and scalar @$expect > 2) 0 130 130
Condition33%1/3
$expect and scalar @$expect > 2
!ll&&!rl&&r
01300
MC/DC0%0/2
$expect and scalar @$expect > 2
$expect scalar @$expect > 2
Truth table33%1/3
A and B
A= $expectB= scalar @$expect > 2
ABresult
0X0
100
111
230 0 warn Dumper($expect);
231 0 throw RDF::Trine::Error::ParserError -text => "expect stack isn't empty after parse";
232 }
233 }
234  
235  
236 package RDF::Trine::Parser::RDFXML::SAXHandler;
237  
238 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 68 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
244 68 ▶use RDF::Trine::Namespace qw(rdf);
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 68 ▶use constant NIL => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 68 ▶use constant SUBJECT => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
248 68 ▶use constant PREDICATE => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 68 ▶use constant OBJECT => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 68 ▶use constant LITERAL => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 68 ▶use constant COLLECTION => 0x16;
Subroutine BEGIN: called CC=1 SCAR=0.0
252  
253 263 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
254 263 my $class = shift;
255 263 my %args = @_;
256 263 my $prefix = '';
257 263 ▶ if (defined($args{ BNodePrefix })) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'BNodePrefix'}) { } 0 263 263
elsif (defined $args{'bnode_prefix'}) { } 263 0 263
258 0 $prefix = $args{ BNodePrefix };
259 } elsif (defined($args{ bnode_prefix })) {
260 263 $prefix = $args{ bnode_prefix };
261 }
262 263 my $self = bless( {
263 expect => [ SUBJECT, NIL ],
264 base => [],
265 depth => 0,
266 characters => '',
267 prefix => $prefix,
268 counter => 0,
269 nodes => [],
270 chars_ok => 0,
271 }, $class );
272 263 ▶ if (my $ns = $args{ namespaces }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $args{"namespaces"}) 1 262 263
273 1 $self->{namespaces} = $ns;
274 }
275 263 ▶ if (my $base = $args{ base }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $args{"base"}) 0 263 263
276 0 $self->push_base( $base );
277 }
278 263 return $self;
279 }
280  
281 525 ▶sub new_expect {
Subroutine new_expect: called CC=1 SCAR=0.0
282 525 my $self = shift;
283 525 my $new = shift;
284 525 unshift( @{ $self->{expect} }, $new );
285 }
286  
287 785 ▶sub old_expect {
Subroutine old_expect: called CC=1 SCAR=0.0
288 785 my $self = shift;
289 785 shift( @{ $self->{expect} } );
290 }
291  
292 5047 ▶sub expect {
Subroutine expect: called CC=2 SCAR=7.4
293 5047 my $self = shift;
294 5047 ▶ if (scalar(@{ $self->{expect} }) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"expect"};} == 0) 0 5047 5047
295 0 Carp::cluck '********* expect stack is empty';
296 }
297 5047 return $self->{expect}[0];
298 }
299  
300 288 ▶sub peek_expect {
Subroutine peek_expect: called CC=1 SCAR=0.0
301 288 my $self = shift;
302 288 return $self->{expect}[1];
303 }
304  
305  
306 =begin private
307  
308 =item C<< start_element >>
309  
310 =cut
311  
312 807 ▶sub start_element {
Subroutine start_element: called CC=37 SCAR=36.5
313 807 my $self = shift;
314 807 my $el = shift;
315 807 my $l = Log::Log4perl->get_logger("rdf.trine.parser.rdfxml");
316
317 807 $l->trace('start_element ' . $el->{Name});
318
319 807 $self->{depth}++;
320 807 ▶ unless ($self->expect == LITERAL) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->expect == 8) 783 24 807
321 783 $self->handle_scoped_values( $el );
322 }
323 807 ▶ if ($self->{depth} == 1 and $el->{NamespaceURI} eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' and $el->{LocalName} eq 'RDF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'depth'} == 1 and $el->{'NamespaceURI'} eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' and $el->{'LocalName'} eq 'RDF') { } 260 547 807
Condition83%5/6
$self->{"depth"} == 1 and $el->{"NamespaceURI"} eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#" and $el->{"LocalName"} eq "RDF"
!ll&&!rl&&r
5470260
$self->{"depth"} == 1 and $el->{"NamespaceURI"} eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
!ll&&!rl&&r
5452260
MC/DC66%2/3
$self->{"depth"} == 1 and $el->{"NamespaceURI"} eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#" and $el->{"LocalName"} eq "RDF"
$self->{"depth"} == 1 $el->{"NamespaceURI"} eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#" $el->{"LocalName"} eq "RDF"
Truth table75%3/4
A and B and C
A= $self->{"depth"} == 1B= $el->{"NamespaceURI"} eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#"C= $el->{"LocalName"} eq "RDF"
ABCresult
0XX0
10X0
1100
1111
324 # ignore the wrapping rdf:RDF element
325 } else {
326 547 my $prefix = $el->{Prefix};
327 547 my $expect = $self->expect;
328
329 547 ▶ if ($expect == NIL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect == 0) 0 547 547
330 0 $self->new_expect( $expect = SUBJECT );
331 }
332
333 547 ▶ if ($expect == SUBJECT or $expect == OBJECT) {
Branch: 4 branches
BranchTrueFalseTotal
if ($expect == 1 or $expect == 4) { } 288 259 547
elsif ($self->expect == 22) { } 0 259 259
elsif ($self->expect == 2) { } 235 24 259
elsif ($self->expect == 8) { } 24 0 24
Condition100%3/3
$expect == 1 or $expect == 4
l!l&&r!l&&!r
2808259
MC/DC100%2/2
$expect == 1 or $expect == 4
$expect == 1 $expect == 4
Truth table100%3/3
A or B
A= $expect == 1B= $expect == 4
ABresult
1X1
011
000
334 288 my $ns = $self->get_namespace( $prefix );
335 288 my $local = $el->{LocalName};
336 288 my $uri = join('', $ns, $local);
337 288 my $node = $self->new_resource( $uri );
338 288 $l->trace("-> expect SUBJECT or OBJECT");
339 288 ▶ if ($self->expect == OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expect == 4) 8 280 288
340 8 ▶ if (defined($self->{characters}) and length(my $string = $self->{characters})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"characters"} and length(my $string = $self->{"characters"})) 4 4 8
Condition100%3/3
defined $self->{"characters"} and length(my $string = $self->{"characters"})
!ll&&!rl&&r
224
MC/DC100%2/2
defined $self->{"characters"} and length(my $string = $self->{"characters"})
defined $self->{"characters"} length(my $string = $self->{"characters"})
Truth table100%3/3
A and B
A= defined $self->{"characters"}B= length(my $string = $self->{"characters"})
ABresult
0X0
100
111
341 4 ▶ if ($string =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\S/) 0 4 4
342 0 die "character data found before object element";
343 }
344 }
345 8 delete($self->{characters}); # get rid of any whitespace we saw before the element
346 }
347 288 my $node_id = $self->node_id( $el );
348
349 288 ▶ if ($self->peek_expect == COLLECTION) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->peek_expect == 22) { } 4 284 288
elsif ($self->expect == 4) { } 4 280 284
350 4 my $list = $self->new_bnode;
351 4 $l->trace("adding an OBJECT to a COLLECTION " . $list->sse . "\n");
352 4 ▶ if (my $last = $self->{ collection_last }[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->{"collection_last"}[0]) 2 2 4
353 2 my $st = RDF::Trine::Statement->new( $last, $rdf->rest, $list );
354 2 $self->assert( $st );
355 }
356 4 $self->{ collection_last }[0] = $list;
357 4 my $st = RDF::Trine::Statement->new( $list, $rdf->first, $node_id );
358 4 $self->assert( $st );
359 4 ▶ $self->{ collection_head }[0] ||= $list;
Condition66%2/3
$self->{"collection_head"}[0] ||= $list
l!l&&r!l&&!r
220
MC/DC0%0/2
$self->{"collection_head"}[0] ||= $list
$self->{"collection_head"}[0] $list
Truth table66%2/3
A ||= B
A= $self->{"collection_head"}[0]B= $list
ABresult
1X1
011
000
360 } elsif ($self->expect == OBJECT) {
361 4 my $nodes = $self->{nodes};
362 4 my $st = RDF::Trine::Statement->new( @{ $nodes }[ $#{$nodes} - 1, $#{$nodes} ], $node_id );
363 4 $self->assert( $st );
364 }
365
366 288 ▶ if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Description') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Description') { } 202 86 288
367 202 $l->trace("got rdf:Description of " . $node_id->as_string);
368 } else {
369 86 my $type = $node;
370 86 $l->trace("got object node " . $node_id->as_string . " of type " . $node->as_string);
371 # emit an rdf:type statement
372 86 my $st = RDF::Trine::Statement->new( $node_id, $rdf->type, $node );
373 86 $self->assert( $st );
374 }
375 288 push( @{ $self->{nodes} }, $node_id );
376
377 288 $self->parse_literal_property_attributes( $el, $node_id );
378 288 $self->new_expect( PREDICATE );
379 288 unshift(@{ $self->{seqs} }, 0);
380 288 $l->trace('unshifting seq counter: ' . Dumper($self->{seqs}));
381 } elsif ($self->expect == COLLECTION) {
382 0 $l->logdie("-> expect COLLECTION");
383 } elsif ($self->expect == PREDICATE) {
384 235 my $ns = $self->get_namespace( $prefix );
385 235 my $local = $el->{LocalName};
386 235 my $uri = join('', $ns, $local);
387 235 my $node = $self->new_resource( $uri );
388 235 $l->trace("-> expect PREDICATE");
389
390 235 ▶ if ($node->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#li') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#li") 44 191 235
391 44 my $id = ++(${ $self }{seqs}[0]);
392 44 $node = $self->new_resource( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_' . $id );
393 }
394
395 235 push( @{ $self->{nodes} }, $node );
396
397 235 ▶ if (my $data = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}datatype'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $el->{"Attributes"}{"{http://www.w3.org/1999/02/22-rdf-syntax-ns#}datatype"}) 12 223 235
398 12 $self->{datatype} = $data->{Value};
399 }
400
401 235 ▶ if (my $data = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID'}) { } 22 213 235
402 22 my $id = $data->{Value};
403 22 unshift(@{ $self->{reify_id} }, $id);
404 } else {
405 213 unshift(@{ $self->{reify_id} }, undef);
406 }
407
408 235 ▶ if (my $pt = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}parseType'}) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $pt = $el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}parseType'}) { } 34 201 235
elsif (my $data = $el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource'}) { } 69 132 201
elsif (my $ndata = $el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}nodeID'}) { } 10 122 132
elsif (my $node = $self->parse_literal_property_attributes($el)) { } 10 112 122
409 34 ▶ if ($pt->{Value} eq 'Resource') {
Branch: 3 branches
BranchTrueFalseTotal
if ($pt->{'Value'} eq 'Resource') { } 14 20 34
elsif ($pt->{'Value'} eq 'Literal') { } 18 2 20
elsif ($pt->{'Value'} eq 'Collection') { } 2 0 2
410 # fake an enclosing object scope
411 14 my $node = $self->new_bnode;
412 14 my $nodes = $self->{nodes};
413 14 push( @$nodes, $node );
414 14 my $st = RDF::Trine::Statement->new( @{ $nodes }[ $#{$nodes} - 2 .. $#{$nodes} ] );
415 14 $self->assert( $st );
416
417 14 $self->new_expect( PREDICATE );
418 } elsif ($pt->{Value} eq 'Literal') {
419 18 $self->{datatype} = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral';
420 18 my $depth = $self->{depth};
421 18 $self->{literal_depth} = $depth - 1;
422 18 $self->new_expect( LITERAL );
423 } elsif ($pt->{Value} eq 'Collection') {
424 2 my $depth = $self->{depth};
425
426 2 unshift( @{ $self->{ collection_head } }, undef );
427 2 unshift( @{ $self->{ collection_last } }, undef );
428 2 $self->new_expect( COLLECTION );
429 2 $self->new_expect( OBJECT );
430 }
431 } elsif (my $data = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource'}) {
432 # stash the uri away so that we can use it when we get the end_element call for this predicate
433 69 my $uri = $self->new_resource( $data->{Value} );
434 69 $self->parse_literal_property_attributes( $el, $uri );
435 69 $self->{'rdf:resource'} = $uri;
436 69 $self->new_expect( OBJECT );
437 69 $self->{chars_ok} = 1;
438 } elsif (my $ndata = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}nodeID'}) {
439 10 my $node_name = $ndata->{Value};
440 # stash the bnode away so that we can use it when we get the end_element call for this predicate
441 10 my $bnode = $self->get_named_bnode( $node_name );
442 10 $self->parse_literal_property_attributes( $el, $uri );
443 10 $self->{'rdf:resource'} = $bnode; # the key 'rdf:resource' is a bit misused here, but both rdf:resource and rdf:nodeID use it for the same purpose, so...
444 10 $self->new_expect( OBJECT );
445 10 $self->{chars_ok} = 1;
446 } elsif (my $node = $self->parse_literal_property_attributes( $el )) {
447 # fake an enclosing object scope
448 10 my $nodes = $self->{nodes};
449 10 push( @$nodes, $node );
450 10 my $st = RDF::Trine::Statement->new( @{ $nodes }[ $#{$nodes} - 2 .. $#{$nodes} ] );
451 10 $self->assert( $st );
452
453 10 $self->new_expect( PREDICATE );
454 } else {
455 112 $self->new_expect( OBJECT );
456 112 $self->{chars_ok} = 1;
457 }
458 } elsif ($self->expect == LITERAL) {
459 24 my $tag;
460 24 ▶ if ($el->{Prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->{'Prefix'}) { } 4 20 24
461 4 $tag = join(':', @{ $el }{qw(Prefix LocalName)});
462 } else {
463 20 $tag = $el->{LocalName};
464 }
465 24 $self->{characters} .= '<' . $tag;
466 24 my $attr = $el->{Attributes};
467
468 24 ▶ if (my $ns = $el->{NamespaceURI}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $el->{"NamespaceURI"}) 22 2 24
469 22 my $abbr = $el->{Prefix};
470 22 ▶ unless ($self->{defined_literal_namespaces}{$abbr}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"defined_literal_namespaces"}{$abbr}{$ns}) 10 12 22
471 10 $self->{characters} .= ' xmlns';
472 10 ▶ if (length($abbr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $abbr) 4 6 10
473 4 $self->{characters} .= ':' . $abbr;
474 }
475 10 $self->{characters} .= '="' . $ns . '"';
476 10 $self->{defined_literal_namespaces}{$abbr}{$ns}++;
477 }
478 }
479 24 ▶ if (%$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$attr) 0 24 24
480 0 foreach my $k (keys %$attr) {
481 0 $self->{characters} .= ' ';
482 0 my $el = $attr->{ $k };
483 0 my $prop;
484 0 ▶ if ($el->{Prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->{'Prefix'}) { } 0 0 0
485 0 $prop = join(':', @{ $el }{qw(Prefix LocalName)});
486 } else {
487 0 $prop = $el->{LocalName};
488 }
489 0 $self->{characters} .= $prop . '="' . $el->{Value} . '"';
490 }
491 }
492 24 $self->{characters} .= '>';
493 } else {
494 0 die "not sure what type of token is expected";
495 }
496 # warn "GOT: $uri\n";
497
498 # warn 'start_element: ' . Dumper($el);
499 # warn 'namespaces: ' . Dumper($self->{_namespaces});
500 }
501 }
502  
503 =item C<< end_element >>
504  
505 =cut
506  
507 807 ▶sub end_element {
Subroutine end_element: called CC=16 SCAR=27.8
508 807 my $self = shift;
509 807 my $el = shift;
510 807 $self->{depth}--;
511 807 my $l = Log::Log4perl->get_logger("rdf.trine.parser.rdfxml");
512 807 $l->trace("($self->{depth}) end_element " . $el->{Name});
513
514 807 my $cleanup = 0;
515 807 my $expect = $self->expect;
516 807 ▶ if ($expect == SUBJECT) {
Branch: 5 branches
BranchTrueFalseTotal
if ($expect == 1) { } 260 547 807
elsif ($expect == 2) { } 312 235 547
elsif ($expect == 4 or $expect == 8 and $self->{'literal_depth'} == $self->{'depth'}) { } 211 24 235
elsif ($expect == 22) { } 0 24 24
elsif ($expect == 8) { } 24 0 24
517 260 $l->trace("-> expect SUBJECT");
518 260 $self->old_expect;
519 260 $cleanup = 1;
520 260 $self->{chars_ok} = 0;
521 260 shift(@{ $self->{reify_id} });
522 } elsif ($expect == PREDICATE) {
523 312 $l->trace("-> expect PREDICATE");
524 312 $self->old_expect;
525 312 ▶ if ($self->expect == PREDICATE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expect == 2) { } 24 288 312
526 # we're closing a parseType=Resource block, so take off the extra implicit node.
527 24 pop( @{ $self->{nodes} } );
528 } else {
529 288 $l->trace('shifting seq counter: ' . Dumper($self->{seqs}));
530 288 shift(@{ $self->{seqs} });
531 }
532 312 $cleanup = 1;
533 312 $self->{chars_ok} = 0;
534 ▶ } elsif ($expect == OBJECT or ($expect == LITERAL and $self->{literal_depth} == $self->{depth})) {
Condition83%5/6
$expect == 4 or $expect == 8 and $self->{"literal_depth"} == $self->{"depth"}
l!l&&r!l&&!r
1931824
$expect == 8 and $self->{"literal_depth"} == $self->{"depth"}
!ll&&!rl&&r
02418
MC/DC66%2/3
$expect == 4 or $expect == 8 and $self->{"literal_depth"} == $self->{"depth"}
$expect == 4 $expect == 8 $self->{"literal_depth"} == $self->{"depth"}
Truth table75%3/4
A or B and C
A= $expect == 4B= $expect == 8C= $self->{"literal_depth"} == $self->{"depth"}
ABCresult
1XX1
0111
00X0
0100
535 211 ▶ if (exists $self->{'rdf:resource'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"rdf:resource"}) 79 132 211
536 79 $l->trace("-> predicate used rdf:resource or rdf:nodeID\n");
537 79 my $uri = delete $self->{'rdf:resource'};
538 79 my $nodes = $self->{nodes};
539 79 my $st = RDF::Trine::Statement->new( @{ $nodes }[ $#{$nodes} - 1, $#{$nodes} ], $uri );
540 79 delete $self->{characters};
541 79 $self->assert( $st );
542 }
543
544 211 $l->trace("-> expect OBJECT");
545 211 $self->old_expect;
546 211 ▶ if (defined($self->{characters})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"characters"}) 126 85 211
547 126 my $string = $self->{characters};
548 126 my $literal = $self->new_literal( $string );
549 126 $l->trace('node stack: ' . Dumper($self->{nodes}));
550 126 my $nodes = $self->{nodes};
551 126 my $st = RDF::Trine::Statement->new( @{ $nodes }[ $#{$nodes} - 1, $#{$nodes} ], $literal );
552 126 $self->assert( $st );
553 126 delete($self->{characters});
554 126 delete $self->{datatype};
555 126 delete $self->{defined_literal_namespaces};
556 }
557
558 211 ▶ if ($self->expect == COLLECTION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expect == 22) 2 209 211
559 # We were expecting an object, but got an end_element instead.
560 # after poping the OBJECT expectation, we see we were expecting objects in a COLLECTION.
561 # so we're ending the COLLECTION here:
562 2 $self->old_expect;
563 2 my $nodes = $self->{nodes};
564 2 ▶ my $head = $self->{ collection_head }[0] || $rdf->nil;
Condition33%1/3
$self->{'collection_head'}[0] || $RDF::Trine::Parser::RDFXML::SAXHandler::rdf->nil
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{'collection_head'}[0] || $RDF::Trine::Parser::RDFXML::SAXHandler::rdf->nil
$self->{'collection_head'}[0] $RDF::Trine::Parser::RDFXML::SAXHandler::rdf->nil
Truth table33%1/3
A || B
A= $self->{'collection_head'}[0]B= $RDF::Trine::Parser::RDFXML::SAXHandler::rdf->nil
ABresult
1X1
011
000
565 2 my @nodes = (@{ $nodes }[ $#{$nodes} - 1, $#{$nodes} ], $head);
566 2 my $st = RDF::Trine::Statement->new( @nodes );
567 2 $self->assert( $st );
568
569 2 ▶ if (my $last = $self->{ collection_last }[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->{"collection_last"}[0]) 2 0 2
570 2 my @nodes = ( $last, $rdf->rest, $rdf->nil );
571 2 my $st = RDF::Trine::Statement->new( @nodes );
572 2 $self->assert( $st );
573 }
574
575 2 shift( @{ $self->{ collection_last } } );
576 2 shift( @{ $self->{ collection_head } } );
577 }
578
579 211 $cleanup = 1;
580 211 $self->{chars_ok} = 0;
581 211 shift(@{ $self->{reify_id} });
582 } elsif ($expect == COLLECTION) {
583 0 shift( @{ $self->{collections} } );
584 0 $self->old_expect;
585 0 $l->trace("-> expect COLLECTION");
586 } elsif ($expect == LITERAL) {
587 24 my $tag;
588 24 ▶ if ($el->{Prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->{'Prefix'}) { } 4 20 24
589 4 $tag = join(':', @{ $el }{qw(Prefix LocalName)});
590 } else {
591 20 $tag = $el->{LocalName};
592 }
593 24 $self->{characters} .= '</' . $tag . '>';
594 24 $cleanup = 0;
595 } else {
596 0 die "how did we get here?";
597 }
598
599 807 ▶ if ($cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup) 783 24 807
600 783 pop( @{ $self->{nodes} } );
601 783 $self->pop_namespace_pad();
602 783 $self->pop_language();
603 783 $self->pop_base();
604 }
605 }
606  
607 1249 ▶sub characters {
Subroutine characters: called CC=4 SCAR=13.9
608 1249 my $self = shift;
609 1249 my $data = shift;
610 1249 my $expect = $self->expect;
611 1249 my $l = Log::Log4perl->get_logger("rdf.trine.parser.rdfxml");
612 1249 ▶ if ($expect == LITERAL or ($expect == OBJECT and $self->{chars_ok})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect == 8 or $expect == 4 and $self->{"chars_ok"}) 158 1091 1249
Condition100%6/6
$expect == 8 or $expect == 4 and $self->{"chars_ok"}
l!l&&r!l&&!r
521061091
$expect == 4 and $self->{"chars_ok"}
!ll&&!rl&&r
108110106
MC/DC100%3/3
$expect == 8 or $expect == 4 and $self->{"chars_ok"}
$expect == 8 $expect == 4 $self->{"chars_ok"}
Truth table100%4/4
A or B and C
A= $expect == 8B= $expect == 4C= $self->{"chars_ok"}
ABCresult
1XX1
0111
00X0
0100
613 158 $l->trace("got character data ($expect): <<$data->{Data}>>\n");
614 158 my $chars = $data->{Data};
615 158 $self->{characters} .= $chars;
616 }
617 }
618  
619 489 ▶sub parse_literal_property_attributes {
Subroutine parse_literal_property_attributes: called CC=9 SCAR=22.0
620 489 my $self = shift;
621 489 my $el = shift;
622 489 ▶ my $node_id = shift || $self->new_bnode;
Condition66%2/3
shift() || $self->new_bnode
l!l&&r!l&&!r
3671220
MC/DC0%0/2
shift() || $self->new_bnode
shift() $self->new_bnode
Truth table66%2/3
A || B
A= shift()B= $self->new_bnode
ABresult
1X1
011
000
623 72 my @keys = grep { not(m<[{][}](xmlns|about)>) }
624 423 grep { not(m<[{]http://www.w3.org/1999/02/22-rdf-syntax-ns#[}](resource|about|ID|datatype|nodeID)>) }
625 433 grep { not(m<[{]http://www.w3.org/XML/1998/namespace[}](base|lang)>) }
626 489 keys %{ $el->{Attributes} };
627 489 my $asserted = 0;
628
629 489 unshift(@{ $self->{reify_id} }, undef); # don't reify any of these triples
630 489 foreach my $k (@keys) {
631 68 my $data = $el->{Attributes}{ $k };
632 68 my $ns = $data->{NamespaceURI};
633 68 ▶ unless ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns) 2 66 68
634 2 my $prefix = $data->{Prefix};
635 2 ▶ next unless (length($ns));
Branch: 1 branch
BranchTrueFalseTotal
unless length $ns 2 0 2
636 0 $ns = $self->get_namespace( $prefix );
637 }
638 66 ▶ next if ($ns eq 'http://www.w3.org/XML/1998/namespace');
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "http://www.w3.org/XML/1998/namespace" 4 62 66
639 62 ▶ next if ($ns eq 'http://www.w3.org/2000/xmlns/');
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "http://www.w3.org/2000/xmlns/" 2 60 62
640 60 my $local = $data->{LocalName};
641 60 my $uri = join('', $ns, $local);
642 60 my $value = $data->{Value};
643 60 my $pred = $self->new_resource( $uri );
644 60 ▶ if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') { } 2 58 60
645 # rdf:type is a special case -- it produces a resource instead of a literal
646 2 my $res = $self->new_resource( $value );
647 2 my $st = RDF::Trine::Statement->new( $node_id, $pred, $res );
648 2 $self->assert( $st );
649 } else {
650 58 my $lit = $self->new_literal( $value );
651 58 my $st = RDF::Trine::Statement->new( $node_id, $pred, $lit );
652 58 $self->assert( $st );
653 }
654 60 $asserted++;
655 }
656 489 shift(@{ $self->{reify_id} });
657 489 ▶ return ($asserted ? $node_id : 0);
Branch: 1 branch
BranchTrueFalseTotal
$asserted ? : 56 433 489
658 }
659  
660 398 ▶sub set_handler {
Subroutine set_handler: called CC=1 SCAR=0.0
661 398 my $self = shift;
662 398 my $handler = shift;
663 398 $self->{sthandler} = $handler;
664 }
665  
666 389 ▶sub assert {
Subroutine assert: called CC=7 SCAR=23.4
667 389 my $self = shift;
668 389 my $st = shift;
669 389 my $l = Log::Log4perl->get_logger("rdf.trine.parser.rdfxml");
670 389 $l->debug('[rdfxml parser] ' . $st->as_string);
671
672 389 ▶ if ($self->{sthandler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sthandler"}) 389 0 389
673 389 ▶ if ($self->{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"canonicalize"}) 0 389 389
674 0 my $o = $st->object;
675 0 ▶ if ($o->isa('RDF::Trine::Node::Literal') and $o->has_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->isa("RDF::Trine::Node::Literal") and $o->has_datatype) 0 0 0
Condition0%0/3
$o->isa("RDF::Trine::Node::Literal") and $o->has_datatype
!ll&&!rl&&r
000
MC/DC0%0/2
$o->isa("RDF::Trine::Node::Literal") and $o->has_datatype
$o->isa("RDF::Trine::Node::Literal") $o->has_datatype
Truth table0%0/3
A and B
A= $o->isa("RDF::Trine::Node::Literal")B= $o->has_datatype
ABresult
0X0
100
111
676 0 my $value = $o->literal_value;
677 0 my $dt = $o->literal_datatype;
678 0 my $canon = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
679 0 $o = literal( $canon, undef, $dt );
680 0 $st->object( $o );
681 }
682 }
683
684 389 $self->{sthandler}->( $st );
685 389 ▶ if (defined(my $id = $self->{reify_id}[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $id = $self->{"reify_id"}[0])) 22 367 389
686 22 my $stid = $self->new_resource( "#$id" );
687
688 22 my $tst = RDF::Trine::Statement->new( $stid, $rdf->type, $rdf->Statement );
689 22 my $sst = RDF::Trine::Statement->new( $stid, $rdf->subject, $st->subject );
690 22 my $pst = RDF::Trine::Statement->new( $stid, $rdf->predicate, $st->predicate );
691 22 my $ost = RDF::Trine::Statement->new( $stid, $rdf->object, $st->object );
692 22 foreach ($tst, $sst, $pst, $ost) {
693 88 $self->{sthandler}->( $_ );
694 }
695 22 $self->{reify_id}[0] = undef; # now that we've used this reify ID, get rid of it (because we don't want it used again)
696 }
697 }
698 }
699  
700 288 ▶sub node_id {
Subroutine node_id: called CC=4 SCAR=13.9
701 288 my $self = shift;
702 288 my $el = shift;
703
704 288 ▶ if ($el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about'}) { } 204 84 288
elsif ($el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID'}) { } 30 54 84
elsif ($el->{'Attributes'}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}nodeID'}) { } 8 46 54
705 204 my $uri = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about'}{Value};
706 204 return $self->new_resource( $uri );
707 } elsif ($el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID'}) {
708 30 my $uri = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID'}{Value};
709 30 return $self->new_resource( '#' . $uri );
710 } elsif ($el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}nodeID'}) {
711 8 my $name = $el->{Attributes}{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}nodeID'}{Value};
712 8 return $self->get_named_bnode( $name );
713 } else {
714 46 return $self->new_bnode;
715 }
716 }
717  
718 783 ▶sub handle_scoped_values {
Subroutine handle_scoped_values: called CC=7 SCAR=19.5
719 783 my $self = shift;
720 783 my $el = shift;
721 783 my %new;
722
723 {
724 # xml:base
725 783 my $base = '';
726 783 ▶ if (exists($el->{Attributes}{'{http://www.w3.org/XML/1998/namespace}base'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $el->{"Attributes"}{"{http://www.w3.org/XML/1998/namespace}base"}) 26 757 783
727 26 my $uri = $el->{Attributes}{'{http://www.w3.org/XML/1998/namespace}base'}{Value};
728 26 $base = $self->new_resource( $uri );
729 }
730 783 $self->push_base( $base );
731 }
732
733 {
734 # language
735 783 my $lang = '';
736 783 ▶ if (exists($el->{Attributes}{'{http://www.w3.org/XML/1998/namespace}lang'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $el->{"Attributes"}{"{http://www.w3.org/XML/1998/namespace}lang"}) 8 775 783
737 8 $lang = $el->{Attributes}{'{http://www.w3.org/XML/1998/namespace}lang'}{Value};
738 }
739 783 $self->push_language( $lang );
740 }
741
742 {
743 # namespaces
744 783 my @ns = grep { m<^[{]http://www.w3.org/2000/xmlns/[}]> } (keys %{ $el->{Attributes} });
745 783 foreach my $n (@ns) {
746 434 my ($prefix) = substr($n, 31);
747 434 my $value = $el->{Attributes}{$n}{Value};
748 434 $new{ $prefix } = $value;
749 434 ▶ if (blessed(my $ns = $self->{namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $ns = $self->{"namespaces"})) 2 432 434
750 2 ▶ unless ($ns->namespace_uri($prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns->namespace_uri($prefix)) 2 0 2
751 2 $ns->add_mapping( $prefix => $value );
752 }
753 }
754 }
755
756 783 ▶ if (exists($el->{Attributes}{'{}xmlns'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $el->{"Attributes"}{"{}xmlns"}) 19 764 783
757 19 my $value = $el->{Attributes}{'{}xmlns'}{Value};
758 19 $new{ '' } = $value;
759 }
760
761 783 $self->push_namespace_pad( \%new );
762 }
763 }
764  
765 1043 ▶sub push_base {
Subroutine push_base: called CC=4 SCAR=14.2
766 1043 my $self = shift;
767 1043 my $base = shift;
768 1043 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 286 757 1043
769 286 ▶ my $uri = (blessed($base) and $base->isa('URI')) ? $base : new URI ($base->uri_value );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($base) && $base->isa('URI') ? : 0 286 286
Condition33%1/3
builtin::blessed($base) and $base->isa("URI")
!ll&&!rl&&r
02860
MC/DC0%0/2
builtin::blessed($base) and $base->isa("URI")
builtin::blessed($base) $base->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($base)B= $base->isa("URI")
ABresult
0X0
100
111
770 286 $uri->fragment( undef );
771 286 $base = RDF::Trine::Node::Resource->new( "$uri" );
772 }
773 1043 unshift( @{ $self->{base} }, $base );
774 }
775  
776 783 ▶sub pop_base {
Subroutine pop_base: called CC=1 SCAR=0.0
777 783 my $self = shift;
778 783 shift( @{ $self->{base} } );
779 }
780  
781 980 ▶sub get_base {
Subroutine get_base: called CC=3 SCAR=11.0
782 980 my $self = shift;
783 980 foreach my $level (0 .. $#{ $self->{base} }) {
784 3244 my $base = $self->{base}[ $level ];
785 3244 ▶ if (length($base)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $base) 974 2270 3244
786 974 return $base;
787 }
788 }
789 6 return ();
790 }
791  
792 783 ▶sub push_language {
Subroutine push_language: called CC=1 SCAR=0.0
793 783 my $self = shift;
794 783 my $lang = shift;
795 783 unshift( @{ $self->{language} }, $lang );
796 }
797  
798 783 ▶sub pop_language {
Subroutine pop_language: called CC=1 SCAR=0.0
799 783 my $self = shift;
800 783 shift( @{ $self->{language} } );
801 }
802  
803 154 ▶sub get_language {
Subroutine get_language: called CC=3 SCAR=11.0
804 154 my $self = shift;
805 154 foreach my $level (0 .. $#{ $self->{language} }) {
806 422 my $lang = $self->{language}[ $level ];
807 422 ▶ if (length($lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $lang) 4 418 422
808 4 return $lang;
809 }
810 }
811 150 return '';
812 }
813  
814 783 ▶sub push_namespace_pad {
Subroutine push_namespace_pad: called CC=1 SCAR=0.0
815 783 my $self = shift;
816 783 my $pad = shift;
817 783 unshift( @{ $self->{_namespaces} }, $pad );
818 }
819  
820 783 ▶sub pop_namespace_pad {
Subroutine pop_namespace_pad: called CC=1 SCAR=0.0
821 783 my $self = shift;
822 783 shift( @{ $self->{_namespaces} } );
823 }
824  
825 523 ▶sub get_namespace {
Subroutine get_namespace: called CC=3 SCAR=11.0
826 523 my $self = shift;
827 523 my $prefix = shift;
828 523 foreach my $level (0 .. $#{ $self->{_namespaces} }) {
829 1311 my $pad = $self->{_namespaces}[ $level ];
830 1311 ▶ if (exists($pad->{ $prefix })) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pad->{$prefix}) 523 788 1311
831 523 my $uri = $pad->{ $prefix };
832 523 return $uri;
833 }
834 }
835 0 throw RDF::Trine::Error::ParserError -text => "Unknown namespace: $prefix";
836 }
837  
838 196 ▶sub new_bnode {
Subroutine new_bnode: called CC=2 SCAR=6.9
839 196 my $self = shift;
840 196 ▶ if (my $prefix = $self->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefix = $self->{'prefix'}) { } 188 8 196
841 188 my $id = $prefix . ++$self->{counter};
842 188 return RDF::Trine::Node::Blank->new( $id );
843 } else {
844 8 return RDF::Trine::Node::Blank->new();
845 }
846 }
847  
848 184 ▶sub new_literal {
Subroutine new_literal: called CC=7 SCAR=19.5
849 184 my $self = shift;
850 184 my $string = shift;
851 184 my @args = (undef, undef);
852 184 ▶ if (my $dt = $self->{datatype}) { # datatype
Branch: 2 branches
BranchTrueFalseTotal
if (my $dt = $self->{'datatype'}) { } 30 154 184
elsif (my $lang = $self->get_language) { } 4 150 154
853 30 $args[1] = $dt;
854 30 ▶ if ($dt eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") 18 12 30
855 18 ▶ if ($HAS_XML_LIBXML) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RDF::Trine::Parser::RDFXML::HAS_XML_LIBXML) 18 0 18
856 18 eval {
857 18 ▶ if ($string =~ m/^</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^</) 2 16 18
858 2 my $doc = XML::LibXML->load_xml(string => $string);
859 2 my $canon = $doc->toStringEC14N(1);
860 2 $string = $canon;
861 }
862 };
863 18 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 18 18
864 0 warn "Cannot canonicalize XMLLiteral: $@" . Dumper($string);
865 }
866 }
867 }
868 } elsif (my $lang = $self->get_language) {
869 4 $args[0] = $lang;
870 }
871 184 my $literal = RDF::Trine::Node::Literal->new( $string, @args );
872 }
873  
874 980 ▶sub new_resource {
Subroutine new_resource: called CC=1 SCAR=0.0
875 980 my $self = shift;
876 980 my $uri = shift;
877 980 my @base = $self->get_base;
878 980 my $res = RDF::Trine::Node::Resource->new( $uri, @base );
879 980 return $res;
880 }
881  
882 18 ▶sub get_named_bnode {
Subroutine get_named_bnode: called CC=2 SCAR=7.0
883 18 my $self = shift;
884 18 my $name = shift;
885 18 ▶ return ($self->{named_bnodes}{ $name } ||= $self->new_bnode);
Condition66%2/3
$self->{"named_bnodes"}{$name} ||= $self->new_bnode
l!l&&r!l&&!r
8100
MC/DC0%0/2
$self->{"named_bnodes"}{$name} ||= $self->new_bnode
$self->{"named_bnodes"}{$name} $self->new_bnode
Truth table66%2/3
A ||= B
A= $self->{"named_bnodes"}{$name}B= $self->new_bnode
ABresult
1X1
011
000
886 }
887  
888 68 1;
889  
890 __END__
« RDF/Trine/Model/Dataset.pm ↑ Summary RDF/Trine/Serializer/Turtle.pm »