RDF/Trine/Parser.pm

filter: Statementstmt 48.8% 152 / 311 Branchbran 25.4% 30 / 118 Conditioncond 19.5% 8 / 41 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 63.4% 26 / 41 Podpod 100% 12 / 12 totaltotal 41.4% 228 / 550 CC 77 SCAR 42.6
CC
77
Cov
40%
__ANON__
182.0
parse_url
182.0
__ANON__
182.0
CRAP 70.8
« RDF/Trine/Iterator/JSONHandler.pm ↑ Summary RDF/Trine/Store/SPARQL.pm »
1 # RDF::Trine::Parser
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser - RDF Parser class
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15
16 RDF::Trine::Parser->parse_url_into_model( $url, $model );
17
18 my $parser = RDF::Trine::Parser->new( 'turtle' );
19 $parser->parse_into_model( $base_uri, $rdf, $model );
20
21 $parser->parse_file_into_model( $base_uri, 'data.ttl', $model );
22  
23 =head1 DESCRIPTION
24  
25 RDF::Trine::Parser is a base class for RDF parsers. It may be used as a factory
26 class for constructing parser objects by name or media type with the C<< new >>
27 method, or used to abstract away the logic of choosing a parser based on the
28 media type of RDF content retrieved over the network with the
29 C<< parse_url_into_model >> method.
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =cut
36  
37 package RDF::Trine::Parser;
38  
39 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use LWP::MediaTypes;
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 our ($VERSION);
48 68 our %file_extensions;
49 68 our %parser_names;
50 68 our %canonical_media_types;
51 68 our %media_types;
52 68 our %format_uris;
53 68 our %encodings;
54  
55 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 68 $VERSION = '1.019';
57 68 can_load( modules => {
58 'Data::UUID' => undef,
59 'UUID::Tiny' => undef,
60 } );
61 }
62  
63 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 68 ▶use RDF::Trine::Parser::NTriples;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 68 ▶use RDF::Trine::Parser::NQuads;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 68 ▶use RDF::Trine::Parser::RDFXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 68 ▶use RDF::Trine::Parser::RDFJSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 68 ▶use RDF::Trine::Parser::RDFa;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 =item C<< media_type >>
73  
74 Returns the canonical media type associated with this parser.
75  
76 =cut
77  
78 0 ▶sub media_type {
Subroutine media_type: not called CC=2 SCAR=17.9
Pod documented
79 0 my $self = shift;
80 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
81 0 return $canonical_media_types{ $class };
82 }
83  
84 =item C<< media_types >>
85  
86 Returns the media types associated with this parser.
87  
88 =cut
89  
90 0 ▶sub media_types {
Subroutine media_types: not called CC=3 SCAR=24.8
Pod documented
91 0 my $self = shift;
92 0 my @types;
93 0 foreach my $type (keys %media_types) {
94 0 my $class = $media_types{ $type };
95 0 ▶ push(@types, $type) if ($self->isa($class));
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa($class) 0 0 0
96 }
97 0 return @types;
98 }
99  
100 =item C<< parser_by_media_type ( $media_type ) >>
101  
102 Returns the parser class appropriate for parsing content of the specified media type.
103 Returns undef if not appropriate parser is found.
104  
105 =cut
106  
107 0 ▶sub parser_by_media_type {
Subroutine parser_by_media_type: not called CC=1 SCAR=6.9
Pod documented
108 0 my $proto = shift;
109 0 my $type = shift;
110 0 my $class = $media_types{ $type };
111 0 return $class;
112 }
113  
114 =item C<< guess_parser_by_filename ( $filename ) >>
115  
116 Returns the best-guess parser class to parse a file with the given filename.
117 Defaults to L<RDF::Trine::Parser::RDFXML> if not appropriate parser is found.
118  
119 =cut
120  
121 2 ▶sub guess_parser_by_filename {
Subroutine guess_parser_by_filename: called CC=4 SCAR=16.4
Pod documented
122 2 my $class = shift;
123 2 my $file = shift;
124 2 ▶ if ($file =~ m/[.](\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /[.](\w+)$/) 2 0 2
125 2 my $ext = $1;
126 2 ▶ return $file_extensions{ $ext } if exists $file_extensions{ $ext };
Branch: 1 branch
BranchTrueFalseTotal
if exists $RDF::Trine::Parser::file_extensions{$ext} 2 0 2
127 }
128 0 ▶ return $class->parser_by_media_type( 'application/rdf+xml' ) || 'RDF::Trine::Parser::RDFXML';
Condition0%0/2
$class->parser_by_media_type('application/rdf+xml') || 'RDF::Trine::Parser::RDFXML'
l!l
00
MC/DC0%0/1
$class->parser_by_media_type('application/rdf+xml') || 'RDF::Trine::Parser::RDFXML'
$class->parser_by_media_type('application/rdf+xml')
Truth table0%0/2
A
A= $class->parser_by_media_type('application/rdf+xml')
Aresult
11
00
129 }
130  
131 =item C<< new ( $parser_name, @args ) >>
132  
133 Returns a new RDF::Trine::Parser object for the parser with the specified name
134 (e.g. "rdfxml" or "turtle"). If no parser with the specified name is found,
135 throws a RDF::Trine::Error::ParserError exception.
136  
137 Any C<< @args >> will be passed through to the format-specific parser
138 constructor.
139  
140 If C<< @args >> contains the key-value pair C<< (canonicalize => 1) >>, literal
141 value canonicalization will be attempted during parsing with warnings being
142 emitted for invalid lexical forms for recognized datatypes.
143  
144 =cut
145  
146 50 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
147 50 my $class = shift;
148 50 my $name = shift;
149 50 my $key = lc($name);
150 50 $key =~ s/[^a-z]//g;
151  
152 50 ▶ if ($name eq 'guess') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'guess') { } 1 49 50
elsif (my $class = $RDF::Trine::Parser::parser_names{$key}) { } 48 1 49
153 1 throw RDF::Trine::Error::UnimplementedError -text => "guess parser heuristics are not implemented yet";
154 } elsif (my $class = $parser_names{ $key }) {
155 # re-add name for multiformat (e.g. Redland) parsers
156 48 return $class->new( name => $key, @_ );
157 } else {
158 1 throw RDF::Trine::Error::ParserError -text => "No parser known named $name";
159 }
160 }
161  
162 =item C<< parse_url_into_model ( $url, $model [, %args] ) >>
163  
164 Retrieves the content from C<< $url >> and attempts to parse the resulting RDF
165 into C<< $model >> using a parser chosen by the associated content media type.
166  
167 If C<< %args >> contains a C<< 'content_cb' >> key with a CODE reference value,
168 that callback function will be called after a successful response as:
169  
170 $content_cb->( $url, $content, $http_response_object )
171  
172 If C<< %args >> contains a C<< 'useragent' >> key with a LWP::UserAgent object value,
173 that object is used to retrieve the requested URL without any configuration (such as
174 setting the Accept: header) which would ordinarily take place. Otherwise, the default
175 user agent (L<RDF::Trine/default_useragent>) is cloned and configured to retrieve
176 content that will be acceptable to any available parser.
177  
178 =cut
179  
180 1 ▶sub parse_url_into_model {
Subroutine parse_url_into_model: called CC=13 SCAR=42.4
Pod documented
181 1 my $class = shift;
182 1 my $url = shift;
183 1 my $model = shift;
184 1 my %args = @_;
185
186 1 my $base = $url;
187 1 ▶ if (defined($args{base})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"base"}) 0 1 1
188 0 $base = $args{base};
189 }
190
191 1 my $ua;
192 1 ▶ if (defined($args{useragent})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'useragent'}) { } 0 1 1
193 0 $ua = $args{useragent};
194 } else {
195 1 $ua = RDF::Trine->default_useragent->clone;
196 1 my $accept = $class->default_accept_header;
197 1 $ua->default_headers->push_header( 'Accept' => $accept );
198 }
199
200 1 my $resp = $ua->get( $url );
201 1 ▶ if ($url =~ /^file:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^file:/) 1 0 1
202 1 my $type = guess_media_type($url);
203 1 $resp->header('Content-Type', $type);
204 }
205
206 1 ▶ unless ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
207 0 throw RDF::Trine::Error::ParserError -text => $resp->status_line;
208 }
209
210 1 my $content = $resp->content;
211 1 ▶ if (my $cb = $args{content_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $args{"content_cb"}) 0 1 1
212 0 $cb->( $url, $content, $resp );
213 }
214
215 1 my $type = $resp->header('content-type');
216 1 $type =~ s/^([^\s;]+).*/$1/;
217 1 my $pclass = $media_types{ $type };
218 1 ▶ if ($pclass and $pclass->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass and $pclass->can("new")) 1 0 1
Condition33%1/3
$pclass and $pclass->can("new")
!ll&&!rl&&r
001
MC/DC0%0/2
$pclass and $pclass->can("new")
$pclass $pclass->can("new")
Truth table33%1/3
A and B
A= $pclassB= $pclass->can("new")
ABresult
0X0
100
111
219 1 my $data = $content;
220 1 ▶ if (my $e = $encodings{ $pclass }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $RDF::Trine::Parser::encodings{$pclass}) 1 0 1
221 1 $data = decode( $e, $content );
222 }
223
224 # pass %args in here too so the constructor can take its pick
225 1 my $parser = $pclass->new(%args);
226 1 my $ok = 0;
227 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 1 $parser->parse_into_model( $base, $data, $model, %args );
229 1 $ok = 1;
230 1 ▶ } catch RDF::Trine::Error with {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
231 1 ▶ return 1 if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
232 }
233
234 ### FALLBACK
235 0 my %options;
236 0 ▶ if (defined $args{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"canonicalize"}) 0 0 0
237 0 $options{ canonicalize } = $args{canonicalize};
238 }
239
240 0 my $ok = 0;
241 0 ▶ try {
Subroutine __ANON__: not called CC=13 SCAR=52.0
242 0 ▶ if ($url =~ /[.](x?rdf|owl)$/ or $content =~ m/\x{FEFF}?<[?]xml /smo) {
Branch: 7 branches
BranchTrueFalseTotal
if ($url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos) { } 0 0 0
elsif ($url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos) { } 0 0 0
elsif ($url =~ /[.]trig$/) { } 0 0 0
elsif ($url =~ /[.]nt$/) { } 0 0 0
elsif ($url =~ /[.]nq$/) { } 0 0 0
elsif ($url =~ /[.]js(?:on)?$/) { } 0 0 0
elsif ($url =~ /[.]x?html?$/) { } 0 0 0
Condition0%0/3
$url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos
$url =~ /[.](x?rdf|owl)$/ $content =~ /\x{FEFF}?<[?]xml /mos
Truth table0%0/3
A or B
A= $url =~ /[.](x?rdf|owl)$/B= $content =~ /\x{FEFF}?<[?]xml /mos
ABresult
1X1
011
000
243 0 my $parser = RDF::Trine::Parser::RDFXML->new(%options);
244 0 $parser->parse_into_model( $base, $content, $model, %args );
245 0 $ok = 1;;
246 ▶ } elsif ($url =~ /[.]ttl$/ or $content =~ m/@(prefix|base)/smo) {
Condition0%0/3
$url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos
$url =~ /[.]ttl$/ $content =~ /@(prefix|base)/mos
Truth table0%0/3
A or B
A= $url =~ /[.]ttl$/B= $content =~ /@(prefix|base)/mos
ABresult
1X1
011
000
247 0 my $parser = RDF::Trine::Parser::Turtle->new(%options);
248 0 my $data = decode('utf8', $content);
249 0 $parser->parse_into_model( $base, $data, $model, %args );
250 0 $ok = 1;;
251 } elsif ($url =~ /[.]trig$/) {
252 0 my $parser = RDF::Trine::Parser::Trig->new(%options);
253 0 my $data = decode('utf8', $content);
254 0 $parser->parse_into_model( $base, $data, $model, %args );
255 0 $ok = 1;;
256 } elsif ($url =~ /[.]nt$/) {
257 0 my $parser = RDF::Trine::Parser::NTriples->new(%options);
258 0 $parser->parse_into_model( $base, $content, $model, %args );
259 0 $ok = 1;;
260 } elsif ($url =~ /[.]nq$/) {
261 0 my $parser = RDF::Trine::Parser::NQuads->new(%options);
262 0 $parser->parse_into_model( $base, $content, $model, %args );
263 0 $ok = 1;;
264 } elsif ($url =~ /[.]js(?:on)?$/) {
265 0 my $parser = RDF::Trine::Parser::RDFJSON->new(%options);
266 0 $parser->parse_into_model( $base, $content, $model, %args );
267 0 $ok = 1;;
268 } elsif ($url =~ /[.]x?html?$/) {
269 0 my $parser = RDF::Trine::Parser::RDFa->new(%options);
270 0 $parser->parse_into_model( $base, $content, $model, %args );
271 0 $ok = 1;;
272 } else {
273 0 my @types = keys %{ { map { $_ => 1 } values %media_types } };
274 0 foreach my $pclass (@types) {
275 0 my $data = $content;
276 0 ▶ if (my $e = $encodings{ $pclass }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $RDF::Trine::Parser::encodings{$pclass}) 0 0 0
277 0 $data = decode( $e, $content );
278 }
279 0 my $parser = $pclass->new(%options);
280 0 my $ok = 0;
281 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 0 $parser->parse_into_model( $base, $data, $model, %args );
283 0 $ok = 1;
284 0 ▶ } catch RDF::Trine::Error::ParserError with {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 ▶ last if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
286 }
287 }
288 0 ▶ } catch RDF::Trine::Error with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 my $e = shift;
290 0 };
291 0 ▶ return 1 if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
292
293 0 ▶ if ($pclass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass) { } 0 0 0
294 0 throw RDF::Trine::Error::ParserError -text => "Failed to parse data of type $type from $url";
295 } else {
296 0 throw RDF::Trine::Error::ParserError -text => "Failed to parse data from $url";
297 }
298 }
299  
300 =item C<< parse_url ( $url, \&handler [, %args] ) >>
301  
302 Retrieves the content from C<< $url >> and attempts to parse the resulting RDF.
303 For each parsed RDF triple that is parsed, C<&handler> will be called with the
304 triple as an argument. Otherwise, this method acts just like
305 C<parse_url_into_model>.
306  
307 =cut
308  
309 0 ▶sub parse_url {
Subroutine parse_url: not called CC=13 SCAR=52.0
Pod documented
310 0 my $class = shift;
311 0 my $url = shift;
312 0 my $handler = shift;
313 0 my %args = @_;
314
315 0 my $base = $url;
316 0 ▶ if (defined($args{base})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"base"}) 0 0 0
317 0 $base = $args{base};
318 }
319
320 0 my $ua;
321 0 ▶ if (defined($args{useragent})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'useragent'}) { } 0 0 0
322 0 $ua = $args{useragent};
323 } else {
324 0 $ua = RDF::Trine->default_useragent->clone;
325 0 my $accept = $class->default_accept_header;
326 0 $ua->default_headers->push_header( 'Accept' => $accept );
327 }
328
329 0 my $resp = $ua->get( $url );
330 0 ▶ if ($url =~ /^file:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^file:/) 0 0 0
331 0 my $type = guess_media_type($url);
332 0 $resp->header('Content-Type', $type);
333 }
334
335 0 ▶ unless ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
336 0 throw RDF::Trine::Error::ParserError -text => $resp->status_line;
337 }
338
339 0 my $content = $resp->content;
340 0 ▶ if (my $cb = $args{content_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $args{"content_cb"}) 0 0 0
341 0 $cb->( $url, $content, $resp );
342 }
343
344 0 my $type = $resp->header('content-type');
345 0 $type =~ s/^([^\s;]+).*/$1/;
346 0 my $pclass = $media_types{ $type };
347 0 ▶ if ($pclass and $pclass->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass and $pclass->can("new")) 0 0 0
Condition0%0/3
$pclass and $pclass->can("new")
!ll&&!rl&&r
000
MC/DC0%0/2
$pclass and $pclass->can("new")
$pclass $pclass->can("new")
Truth table0%0/3
A and B
A= $pclassB= $pclass->can("new")
ABresult
0X0
100
111
348 0 my $data = $content;
349 0 ▶ if (my $e = $encodings{ $pclass }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $RDF::Trine::Parser::encodings{$pclass}) 0 0 0
350 0 $data = decode( $e, $content );
351 }
352
353 # pass %args in here too so the constructor can take its pick
354 0 my $parser = $pclass->new(%args);
355 0 my $ok = 0;
356 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 0 $parser->parse( $base, $data, $handler );
358 0 $ok = 1;
359 0 ▶ } catch RDF::Trine::Error with {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 0 ▶ return 1 if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
361 }
362
363 ### FALLBACK
364 0 my %options;
365 0 ▶ if (defined $args{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"canonicalize"}) 0 0 0
366 0 $options{ canonicalize } = $args{canonicalize};
367 }
368
369 0 my $ok = 0;
370 0 ▶ try {
Subroutine __ANON__: not called CC=13 SCAR=52.0
371 0 ▶ if ($url =~ /[.](x?rdf|owl)$/ or $content =~ m/\x{FEFF}?<[?]xml /smo) {
Branch: 7 branches
BranchTrueFalseTotal
if ($url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos) { } 0 0 0
elsif ($url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos) { } 0 0 0
elsif ($url =~ /[.]trig$/) { } 0 0 0
elsif ($url =~ /[.]nt$/) { } 0 0 0
elsif ($url =~ /[.]nq$/) { } 0 0 0
elsif ($url =~ /[.]js(?:on)?$/) { } 0 0 0
elsif ($url =~ /[.]x?html?$/) { } 0 0 0
Condition0%0/3
$url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /[.](x?rdf|owl)$/ or $content =~ /\x{FEFF}?<[?]xml /mos
$url =~ /[.](x?rdf|owl)$/ $content =~ /\x{FEFF}?<[?]xml /mos
Truth table0%0/3
A or B
A= $url =~ /[.](x?rdf|owl)$/B= $content =~ /\x{FEFF}?<[?]xml /mos
ABresult
1X1
011
000
372 0 my $parser = RDF::Trine::Parser::RDFXML->new(%options);
373 0 $parser->parse( $base, $content, $handler, %args );
374 0 $ok = 1;;
375 ▶ } elsif ($url =~ /[.]ttl$/ or $content =~ m/@(prefix|base)/smo) {
Condition0%0/3
$url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /[.]ttl$/ or $content =~ /@(prefix|base)/mos
$url =~ /[.]ttl$/ $content =~ /@(prefix|base)/mos
Truth table0%0/3
A or B
A= $url =~ /[.]ttl$/B= $content =~ /@(prefix|base)/mos
ABresult
1X1
011
000
376 0 my $parser = RDF::Trine::Parser::Turtle->new(%options);
377 0 my $data = decode('utf8', $content);
378 0 $parser->parse( $base, $data, $handler, %args );
379 0 $ok = 1;;
380 } elsif ($url =~ /[.]trig$/) {
381 0 my $parser = RDF::Trine::Parser::Trig->new(%options);
382 0 my $data = decode('utf8', $content);
383 0 $parser->parse( $base, $data, $handler, %args );
384 0 $ok = 1;;
385 } elsif ($url =~ /[.]nt$/) {
386 0 my $parser = RDF::Trine::Parser::NTriples->new(%options);
387 0 $parser->parse( $base, $content, $handler, %args );
388 0 $ok = 1;;
389 } elsif ($url =~ /[.]nq$/) {
390 0 my $parser = RDF::Trine::Parser::NQuads->new(%options);
391 0 $parser->parse( $base, $content, $handler, %args );
392 0 $ok = 1;;
393 } elsif ($url =~ /[.]js(?:on)?$/) {
394 0 my $parser = RDF::Trine::Parser::RDFJSON->new(%options);
395 0 $parser->parse( $base, $content, $handler, %args );
396 0 $ok = 1;;
397 } elsif ($url =~ /[.]x?html?$/) {
398 0 my $parser = RDF::Trine::Parser::RDFa->new(%options);
399 0 $parser->parse( $base, $content, $handler, %args );
400 0 $ok = 1;;
401 } else {
402 0 my @types = keys %{ { map { $_ => 1 } values %media_types } };
403 0 foreach my $pclass (@types) {
404 0 my $data = $content;
405 0 ▶ if (my $e = $encodings{ $pclass }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $RDF::Trine::Parser::encodings{$pclass}) 0 0 0
406 0 $data = decode( $e, $content );
407 }
408 0 my $parser = $pclass->new(%options);
409 0 my $ok = 0;
410 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
411 0 $parser->parse( $base, $data, $handler, %args );
412 0 $ok = 1;
413 0 ▶ } catch RDF::Trine::Error::ParserError with {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
414 0 ▶ last if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
415 }
416 }
417 0 ▶ } catch RDF::Trine::Error with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
418 0 my $e = shift;
419 0 };
420 0 ▶ return 1 if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
421
422 0 ▶ if ($pclass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass) { } 0 0 0
423 0 throw RDF::Trine::Error::ParserError -text => "Failed to parse data of type $type from $url";
424 } else {
425 0 throw RDF::Trine::Error::ParserError -text => "Failed to parse data from $url";
426 }
427 }
428  
429 =item C<< parse_into_model ( $base_uri, $data, $model [, context => $context] ) >>
430  
431 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. For each RDF
432 statement parsed, will call C<< $model->add_statement( $statement ) >>.
433  
434 =cut
435  
436 68 ▶sub parse_into_model {
Subroutine parse_into_model: called CC=4 SCAR=14.8
Pod documented
437 68 my $proto = shift;
438 68 ▶ my $self = blessed($proto) ? $proto : $proto->new();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proto) ? : 68 0 68
439 68 my $uri = shift;
440 68 ▶ 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 68 68
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
6800
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
441 0 $uri = $uri->uri_value;
442 }
443 68 my $input = shift;
444 68 my $model = shift;
445 68 my %args = @_;
446 68 my $context = $args{'context'};
447
448 2447 ▶ my $handler = sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
449 2447 my $st = shift;
450 2447 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 2447 2447
451 0 my $quad = RDF::Trine::Statement::Quad->new( $st->nodes, $context );
452 0 $model->add_statement( $quad );
453 } else {
454 2447 $model->add_statement( $st );
455 }
456 68 };
457
458 68 $model->begin_bulk_ops();
459 68 my $s = $self->parse( $uri, $input, $handler );
460 68 $model->end_bulk_ops();
461 68 return $s;
462 }
463  
464 =item C<< parse_file_into_model ( $base_uri, $fh, $model [, context => $context] ) >>
465  
466 Parses all data read from the filehandle or file C<< $fh >>, using the
467 given C<< $base_uri >>. For each RDF statement parsed, will call
468 C<< $model->add_statement( $statement ) >>.
469  
470 =cut
471  
472 656 ▶sub parse_file_into_model {
Subroutine parse_file_into_model: called CC=5 SCAR=16.5
Pod documented
473 656 my $proto = shift;
474 656 ▶ my $self = (blessed($proto) or $proto eq __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proto) || $proto eq __PACKAGE__ ? : 656 0 656
Condition66%2/3
builtin::blessed($proto) or $proto eq __PACKAGE__
l!l&&r!l&&!r
65510
MC/DC0%0/2
builtin::blessed($proto) or $proto eq __PACKAGE__
builtin::blessed($proto) $proto eq __PACKAGE__
Truth table66%2/3
A or B
A= builtin::blessed($proto)B= $proto eq __PACKAGE__
ABresult
1X1
011
000
475 ? $proto : $proto->new();
476 656 my $uri = shift;
477 656 ▶ 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 656 656
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
65600
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
478 0 $uri = $uri->uri_value;
479 }
480 656 my $fh = shift;
481 656 my $model = shift;
482 656 my %args = @_;
483 656 my $context = $args{'context'};
484
485 1168 ▶ my $handler = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
486 1168 my $st = shift;
487 1168 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 1 1167 1168
488 1 my $quad = RDF::Trine::Statement::Quad->new( $st->nodes, $context );
489 1 $model->add_statement( $quad );
490 } else {
491 1167 $model->add_statement( $st );
492 }
493 656 };
494
495 656 $model->begin_bulk_ops();
496 656 my $s = $self->parse_file( $uri, $fh, $handler );
497 655 $model->end_bulk_ops();
498 655 return $s;
499 }
500  
501 =item C<< parse_file ( $base_uri, $fh, $handler ) >>
502  
503 Parses all data read from the filehandle or file C<< $fh >>, using the given
504 C<< $base_uri >>. If C<< $fh >> is a filename, this method can guess the
505 associated parse. For each RDF statement parsed, C<< $handler->( $st ) >> is called.
506  
507 =cut
508  
509 2 ▶sub parse_file {
Subroutine parse_file: called CC=8 SCAR=22.8
Pod documented
510 2 my $self = shift;
511 2 my $base = shift;
512 2 my $fh = shift;
513 2 my $handler = shift;
514  
515 2 ▶ unless (ref($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 2 0 2
516 2 my $filename = $fh;
517 2 undef $fh;
518 2 ▶ unless ($self->can('parse')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("parse")) 2 0 2
519 2 my $pclass = $self->guess_parser_by_filename( $filename );
520 2 ▶ $self = $pclass->new() if ($pclass and $pclass->can('new'));
Branch: 1 branch
BranchTrueFalseTotal
if $pclass and $pclass->can("new") 2 0 2
Condition33%1/3
$pclass and $pclass->can("new")
!ll&&!rl&&r
002
MC/DC0%0/2
$pclass and $pclass->can("new")
$pclass $pclass->can("new")
Truth table33%1/3
A and B
A= $pclassB= $pclass->can("new")
ABresult
0X0
100
111
521 }
522 2 ▶ open( $fh, '<:encoding(UTF-8)', $filename ) or throw RDF::Trine::Error::ParserError -text => $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(UTF-8)", $filename or "RDF::Trine::Error::ParserError"->throw("-text", $!) 2 0 2
523 }
524  
525 2 ▶ if ($self and $self->can('parse')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and $self->can('parse')) { } 2 0 2
Condition33%1/3
$self and $self->can("parse")
!ll&&!rl&&r
002
MC/DC0%0/2
$self and $self->can("parse")
$self $self->can("parse")
Truth table33%1/3
A and B
A= $selfB= $self->can("parse")
ABresult
0X0
100
111
526 2 my $content = do { local($/) = undef; <$fh> };
527 2 return $self->parse( $base, $content, $handler, @_ );
528 } else {
529 0 throw RDF::Trine::Error::ParserError -text => "Cannot parse unknown serialization";
530 }
531 }
532  
533 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
534  
535 =cut
536  
537  
538 =item C<< new_bnode_prefix () >>
539  
540 Returns a new prefix to be used in the construction of blank node identifiers.
541 If either Data::UUID or UUID::Tiny are available, they are used to construct
542 a globally unique bnode prefix. Otherwise, an empty string is returned.
543  
544 =cut
545  
546 9 ▶sub new_bnode_prefix {
Subroutine new_bnode_prefix: called CC=4 SCAR=21.2
Pod documented
547 9 my $class = shift;
548 9 ▶ if (defined($Data::UUID::VERSION)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Data::UUID::VERSION) { } 0 9 9
elsif (defined $UUID::Tiny::VERSION and $] < 5.01) { } 0 9 9
549 0 my $ug = new Data::UUID;
550 0 my $uuid = $ug->to_string( $ug->create() );
551 0 $uuid =~ s/-//g;
552 0 return 'b' . $uuid;
553 ▶ } elsif (defined($UUID::Tiny::VERSION) && ($] < 5.010000)) { # UUID::Tiny 1.03 isn't working nice with thread support in Perl 5.14. When this is fixed, this may be removed and dep added.
Condition33%1/3
defined $UUID::Tiny::VERSION and $] < "5.01"
!ll&&!rl&&r
900
MC/DC0%0/2
defined $UUID::Tiny::VERSION and $] < "5.01"
defined $UUID::Tiny::VERSION $] < "5.01"
Truth table33%1/3
A and B
A= defined $UUID::Tiny::VERSIONB= $] < "5.01"
ABresult
0X0
100
111
554 0 my $uuid = UUID::Tiny::create_UUID_as_string(UUID::Tiny::UUID_V1());
555 0 $uuid =~ s/-//g;
556 0 return 'b' . $uuid;
557 } else {
558 9 return '';
559 }
560 }
561  
562 =item C<< default_accept_header >>
563  
564 Returns the default HTTP Accept header value used in requesting RDF content (e.g. in
565 L</parse_url_into_model>) that may be parsed by one of the available RDF::Trine::Parser
566 subclasses.
567  
568 By default, RDF/XML and Turtle are preferred over other media types.
569  
570 =cut
571  
572 1 ▶sub default_accept_header {
Subroutine default_accept_header: called CC=2 SCAR=6.9
Pod documented
573 # prefer RDF/XML or Turtle, then anything else that we've got a parser for.
574 1 ▶ my $accept = join(',', map { /(turtle|rdf[+]xml)/ ? "$_;q=1.0" : "$_;q=0.9" } keys %media_types);
Branch: 1 branch
BranchTrueFalseTotal
/(turtle|rdf[+]xml)/ ? : 4 5 9
575 1 return $accept;
576 }
577  
578 68 1;
579  
580 __END__
« RDF/Trine/Iterator/JSONHandler.pm ↑ Summary RDF/Trine/Store/SPARQL.pm »