RDF/Trine/Parser/Redland.pm

filter: Statementstmt 65.5% 95 / 145 Branchbran 12.5% 4 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 80.0% 28 / 35 Podpod 100% 2 / 2 totaltotal 54.2% 129 / 238 CC 23 SCAR 42.0
CC
23
Cov
52%
parse
240.0
new
30.0
BEGIN
5.2
CRAP 66.9
« RDF/Trine/Store/SPARQL.pm ↑ Summary RDF/Trine/Store/DBI/mysql.pm »
1 # RDF::Trine::Parser::Redland
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::Redland - RDF Parser using the Redland library
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::Redland version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15 use RDF::Trine::Parser::Redland; # to overwrite internal dispatcher
16  
17 # Redland does turtle, ntriples, trig and rdfa as well
18 my $parser = RDF::Trine::Parser->new( 'rdfxml' );
19 $parser->parse_into_model( $base_uri, $data, $model );
20  
21 =head1 DESCRIPTION
22  
23 ...
24  
25 =head1 METHODS
26  
27 Beyond the methods documented below, this class inherits methods from the
28 L<RDF::Trine::Parser> class.
29  
30 =over 4
31  
32 =cut
33  
34 package RDF::Trine::Parser::Redland;
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use base qw(RDF::Trine::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶use RDF::Trine qw(literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 ######################################################################
52  
53 1 our ($VERSION, $HAVE_REDLAND_PARSER, %FORMATS);
54 1 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=16.5
55 1 %FORMATS = (
56 rdfxml => [
57 'RDF::Trine::Parser::Redland::RDFXML',
58 'http://www.w3.org/ns/formats/RDF_XML',
59 [qw(application/rdf+xml)],
60 [qw(rdf xrdf rdfx)]
61 ],
62 ntriples => [
63 'RDF::Trine::Parser::Redland::NTriples',
64 'http://www.w3.org/ns/formats/data/N-Triples',
65 [qw(text/plain)],
66 [qw(nt)]
67 ],
68 turtle => [
69 'RDF::Trine::Parser::Redland::Turtle',
70 'http://www.w3.org/ns/formats/Turtle',
71 [qw(application/x-turtle application/turtle text/turtle)],
72 [qw(ttl)]
73 ],
74 trig => [
75 'RDF::Trine::Parser::Redland::Trig',
76 undef,
77 [],
78 [qw(trig)]
79 ],
80 librdfa => [
81 'RDF::Trine::Parser::Redland::RDFa',
82 'http://www.w3.org/ns/formats/data/RDFa',
83 [], #[qw(application/xhtml+xml)],
84 [], #[qw(html xhtml)]
85 ],
86 );
87
88 1 $VERSION = '1.019';
89 1 for my $format (keys %FORMATS) {
90 5 $RDF::Trine::Parser::parser_names{$format} = $FORMATS{$format}[0];
91 $RDF::Trine::Parser::format_uris{ $FORMATS{$format}[1] } = $FORMATS{$format}[0]
92 5 ▶ if defined $FORMATS{$format}[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $RDF::Trine::Parser::Redland::FORMATS{$format}[1] 4 1 5
93 5 map { $RDF::Trine::Parser::media_types{$_} = $FORMATS{$format}[0] }
94 5 (@{$FORMATS{$format}[2]});
95 6 map { $RDF::Trine::Parser::file_extensions{$_} = $FORMATS{$format}[0] }
96 5 (@{$FORMATS{$format}[3]});
97 }
98
99 1 ▶ unless ($ENV{RDFTRINE_NO_REDLAND}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"RDFTRINE_NO_REDLAND"}) 1 0 1
100 ## no critic
101 0 ▶ eval "use RDF::Redland 1.000701;";
Subroutine BEGIN: called CC=1 SCAR=1.2
102 1 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
103 0 $HAVE_REDLAND_PARSER = 1;
104 }
105 ## use critic
106 }
107 }
108  
109 ######################################################################
110  
111 =item C<< new ( options => \%options ) >>
112  
113 Returns a new Redland parser object with the supplied options. Use the
114 C<name> option to tell Redland which parser it should use.
115  
116 =cut
117  
118 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
119 0 my $class = shift;
120 0 my %args = @_;
121 0 ▶ unless ($HAVE_REDLAND_PARSER) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($RDF::Trine::Parser::Redland::HAVE_REDLAND_PARSER) 0 0 0
122 0 throw RDF::Trine::Error
123 -text => "Failed to load RDF::Redland >= 1.0.7.1";
124 }
125 0 ▶ unless (defined $args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"name"}) 0 0 0
126 0 throw RDF::Trine::Error
127 -text => "Redland parser needs to know which format it's parsing!";
128 }
129 0 ▶ unless ($FORMATS{$args{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($RDF::Trine::Parser::Redland::FORMATS{$args{"name"}}) 0 0 0
130 0 throw RDF::Trine::Error
131 -text => "Unrecognized format name $args{name} for Redland parser";
132 }
133
134 0 ▶ my $parser = RDF::Redland::Parser->new($args{name}) or
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "RDF::Redland::Parser"->new($args{"name"}) or "RDF::Trine::Error"->throw("-text", qq[Could not load a Redland $args{"name"} parser.]) 0 0 0
135 throw RDF::Trine::Error
136 -text => "Could not load a Redland $args{name} parser.";
137
138 #warn "sup dawgs";
139  
140 0 my $self = bless( { %args }, $class);
141 0 return $self;
142 }
143  
144 =item C<< parse_into_model ( $base_uri, $data, $model [, context => $context] ) >>
145  
146 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. For each RDF
147 statement parsed, will call C<< $model->add_statement( $statement ) >>.
148  
149 =cut
150  
151 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
152  
153 =cut
154  
155 0 ▶sub parse {
Subroutine parse: not called CC=15 SCAR=54.8
Pod documented
156 0 my $self = shift;
157 0 my $base = shift;
158 0 my $string = shift;
159 0 my $handler = shift;
160
161 0 my $parser = RDF::Redland::Parser->new($self->{name});
162
163 0 my $null_base = 'urn:uuid:1d1e755d-c622-4610-bae8-40261157687b';
164 0 ▶ if ($base and blessed($base) and $base->isa('URI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base and builtin::blessed($base) and $base->isa("URI")) 0 0 0
Condition0%0/6
$base and builtin::blessed($base) and $base->isa("URI")
!ll&&!rl&&r
000
$base and builtin::blessed($base)
!ll&&!rl&&r
000
MC/DC0%0/3
$base and builtin::blessed($base) and $base->isa("URI")
$base builtin::blessed($base) $base->isa("URI")
Truth table0%0/4
A and B and C
A= $baseB= builtin::blessed($base)C= $base->isa("URI")
ABCresult
0XX0
10X0
1100
1111
165 0 $base = $base->as_string;
166 }
167 0 ▶ $base = RDF::Redland::URI->new(defined $base ? $base : $null_base);
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 0 0 0
168 0 my $stream = eval {
169 0 $parser->parse_string_as_stream($string, $base)
170 };
171 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
172 0 throw RDF::Trine::Error::ParserError -text => $@;
173 }
174
175 0 ▶ while ($stream and !$stream->end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and not $stream->end) 0 0 0
Condition0%0/3
$stream and not $stream->end
!ll&&!rl&&r
000
MC/DC0%0/2
$stream and not $stream->end
$stream not $stream->end
Truth table0%0/3
A and not(B)
A= $streamB= not $stream->end
ABresult
0X0
100
111
176 #my $context = $stream->context;
177 #warn $context;
178 0 my $stmt = RDF::Trine::Statement->from_redland($stream->current);
179 0 ▶ if ($self->{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"canonicalize"}) 0 0 0
180 0 my $o = $stmt->object;
181 # basically copied from RDF::Trine::Parser::Turtle
182 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
183 0 my $value = $o->literal_value;
184 0 my $dt = $o->literal_datatype;
185 0 my $canon = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
186 0 $o = literal( $canon, undef, $dt );
187  
188 0 $stmt->object($o);
189 }
190 }
191  
192 # run handler
193 0 ▶ $handler->($stmt) if ($handler and reftype($handler) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $handler and builtin::reftype($handler) eq "CODE" 0 0 0
Condition0%0/3
$handler and builtin::reftype($handler) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$handler and builtin::reftype($handler) eq "CODE"
$handler builtin::reftype($handler) eq "CODE"
Truth table0%0/3
A and B
A= $handlerB= builtin::reftype($handler) eq "CODE"
ABresult
0X0
100
111
194  
195 0 $stream->next;
196 }
197 0 undef $stream;
198
199 0 ▶ if (my $map = $self->{ namespaces }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->{"namespaces"}) 0 0 0
200 0 my %seen = $parser->namespaces_seen;
201 0 ▶ while (my ($ns, $uri) = each(%seen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %seen) 0 0 0
202 0 $map->add_mapping( $ns => $uri->as_string );
203 }
204 }
205 0 return;
206 }
207  
208 package RDF::Trine::Parser::Redland::RDFXML;
209 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
210 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
211 1 ▶use base qw(RDF::Trine::Parser::Redland);
Subroutine BEGIN: called CC=1 SCAR=0.0
212 0 ▶sub new { shift->SUPER::new( @_, name => 'rdfxml' ) }
Subroutine new: not called CC=1 SCAR=6.9
213  
214 package RDF::Trine::Parser::Redland::NTriples;
215 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1 ▶use base qw(RDF::Trine::Parser::Redland);
Subroutine BEGIN: called CC=1 SCAR=0.0
218 0 ▶sub new { shift->SUPER::new( @_, name => 'ntriples' ) }
Subroutine new: not called CC=1 SCAR=6.9
219  
220 package RDF::Trine::Parser::Redland::Turtle;
221 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 1 ▶use base qw(RDF::Trine::Parser::Redland);
Subroutine BEGIN: called CC=1 SCAR=0.0
224 0 ▶sub new { shift->SUPER::new( @_, name => 'turtle' ) }
Subroutine new: not called CC=1 SCAR=6.9
225  
226 package RDF::Trine::Parser::Redland::Trig;
227 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 1 ▶use base qw(RDF::Trine::Parser::Redland);
Subroutine BEGIN: called CC=1 SCAR=0.0
230 0 ▶sub new { shift->SUPER::new( @_, name => 'trig' ) }
Subroutine new: not called CC=1 SCAR=6.9
231  
232 package RDF::Trine::Parser::Redland::RDFa;
233 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 1 ▶use base qw(RDF::Trine::Parser::Redland);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 0 ▶sub new { shift->SUPER::new( @_, name => 'librdfa' ) }
Subroutine new: not called CC=1 SCAR=6.9
237  
238  
239 1 1;
240  
241 __END__
« RDF/Trine/Store/SPARQL.pm ↑ Summary RDF/Trine/Store/DBI/mysql.pm »