File Coverage

blib/lib/RDF/Trine/Parser/Redland.pm
Criterion Covered Total %
statement 93 143 65.0
branch 4 28 14.2
condition 0 15 0.0
subroutine 28 35 80.0
pod 2 2 100.0
total 127 223 56.9


line stmt bran cond sub pod time code
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.018
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     1   416 use strict;
  1         2  
  1         23  
37 1     1   4 use warnings;
  1         2  
  1         22  
38              
39 1     1   4 use base qw(RDF::Trine::Parser);
  1         3  
  1         58  
40              
41 1     1   5 use Carp;
  1         3  
  1         47  
42 1     1   5 use Data::Dumper;
  1         3  
  1         35  
43 1     1   5 use Log::Log4perl;
  1         2  
  1         7  
44 1     1   69 use Scalar::Util qw(blessed reftype);
  1         2  
  1         45  
45              
46 1     1   5 use RDF::Trine qw(literal);
  1         2  
  1         31  
47 1     1   5 use RDF::Trine::Node;
  1         4  
  1         25  
48 1     1   5 use RDF::Trine::Statement;
  1         11  
  1         18  
49 1     1   5 use RDF::Trine::Error qw(:try);
  1         2  
  1         7  
50              
51             ######################################################################
52              
53             our ($VERSION, $HAVE_REDLAND_PARSER, %FORMATS);
54             BEGIN {
55 1     1   405 %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         3 $VERSION = '1.018';
89 1         3 for my $format (keys %FORMATS) {
90 5         11 $RDF::Trine::Parser::parser_names{$format} = $FORMATS{$format}[0];
91             $RDF::Trine::Parser::format_uris{ $FORMATS{$format}[1] } = $FORMATS{$format}[0]
92 5 100       15 if defined $FORMATS{$format}[1];
93 5         11 map { $RDF::Trine::Parser::media_types{$_} = $FORMATS{$format}[0] }
94 5         7 (@{$FORMATS{$format}[2]});
  5         9  
95 6         14 map { $RDF::Trine::Parser::file_extensions{$_} = $FORMATS{$format}[0] }
96 5         7 (@{$FORMATS{$format}[3]});
  5         9  
97             }
98            
99 1 50       5 unless ($ENV{RDFTRINE_NO_REDLAND}) {
100             ## no critic
101 1     1   61 eval "use RDF::Redland 1.000701;";
  1         172  
  0         0  
  0         0  
102 1 50       405 unless ($@) {
103 0         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             sub new {
119 0     0 1   my $class = shift;
120 0           my %args = @_;
121 0 0         unless ($HAVE_REDLAND_PARSER) {
122 0           throw RDF::Trine::Error
123             -text => "Failed to load RDF::Redland >= 1.0.7.1";
124             }
125 0 0         unless (defined $args{name}) {
126 0           throw RDF::Trine::Error
127             -text => "Redland parser needs to know which format it's parsing!";
128             }
129 0 0         unless ($FORMATS{$args{name}}) {
130 0           throw RDF::Trine::Error
131             -text => "Unrecognized format name $args{name} for Redland parser";
132             }
133            
134 0 0         my $parser = RDF::Redland::Parser->new($args{name}) or
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             sub parse {
156 0     0 1   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 0 0       if ($base and blessed($base) and $base->isa('URI')) {
      0        
165 0           $base = $base->as_string;
166             }
167 0 0         $base = RDF::Redland::URI->new(defined $base ? $base : $null_base);
168 0           my $stream = eval {
169 0           $parser->parse_string_as_stream($string, $base)
170             };
171 0 0         if ($@) {
172 0           throw RDF::Trine::Error::ParserError -text => $@;
173             }
174            
175 0   0       while ($stream and !$stream->end) {
176             #my $context = $stream->context;
177             #warn $context;
178 0           my $stmt = RDF::Trine::Statement->from_redland($stream->current);
179 0 0         if ($self->{canonicalize}) {
180 0           my $o = $stmt->object;
181             # basically copied from RDF::Trine::Parser::Turtle
182 0 0 0       if ($o->isa('RDF::Trine::Node::Literal') and $o->has_datatype) {
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 0 0       $handler->($stmt) if ($handler and reftype($handler) eq 'CODE');
194              
195 0           $stream->next;
196             }
197 0           undef $stream;
198            
199 0 0         if (my $map = $self->{ namespaces }) {
200 0           my %seen = $parser->namespaces_seen;
201 0           while (my ($ns, $uri) = each(%seen)) {
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     1   7 use strict;
  1         2  
  1         17  
210 1     1   4 use warnings;
  1         2  
  1         27  
211 1     1   5 use base qw(RDF::Trine::Parser::Redland);
  1         2  
  1         83  
212 0     0     sub new { shift->SUPER::new( @_, name => 'rdfxml' ) }
213              
214             package RDF::Trine::Parser::Redland::NTriples;
215 1     1   6 use strict;
  1         2  
  1         22  
216 1     1   4 use warnings;
  1         2  
  1         20  
217 1     1   4 use base qw(RDF::Trine::Parser::Redland);
  1         2  
  1         79  
218 0     0     sub new { shift->SUPER::new( @_, name => 'ntriples' ) }
219              
220             package RDF::Trine::Parser::Redland::Turtle;
221 1     1   6 use strict;
  1         2  
  1         14  
222 1     1   4 use warnings;
  1         3  
  1         22  
223 1     1   5 use base qw(RDF::Trine::Parser::Redland);
  1         1  
  1         76  
224 0     0     sub new { shift->SUPER::new( @_, name => 'turtle' ) }
225              
226             package RDF::Trine::Parser::Redland::Trig;
227 1     1   5 use strict;
  1         2  
  1         16  
228 1     1   5 use warnings;
  1         2  
  1         23  
229 1     1   4 use base qw(RDF::Trine::Parser::Redland);
  1         2  
  1         71  
230 0     0     sub new { shift->SUPER::new( @_, name => 'trig' ) }
231              
232             package RDF::Trine::Parser::Redland::RDFa;
233 1     1   6 use strict;
  1         3  
  1         16  
234 1     1   5 use warnings;
  1         2  
  1         23  
235 1     1   4 use base qw(RDF::Trine::Parser::Redland);
  1         2  
  1         66  
236 0     0     sub new { shift->SUPER::new( @_, name => 'librdfa' ) }
237              
238              
239             1;
240              
241             __END__
242              
243             =back
244              
245             =head1 ENVIRONMENT VARIABLES
246              
247             Set C<RDFTRINE_NO_REDLAND> to something true to disable the Redland parsers.
248              
249             =head1 BUGS
250              
251             Please report any bugs or feature requests to through the GitHub web interface
252             at L<https://github.com/kasei/perlrdf/issues>.
253              
254             =head1 AUTHOR
255              
256             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
257              
258             =head1 COPYRIGHT
259              
260             Copyright (c) 2006-2012 Gregory Todd Williams. This
261             program is free software; you can redistribute it and/or modify it under
262             the same terms as Perl itself.
263              
264             =cut