File Coverage

blib/lib/Catmandu/Exporter/RDF.pm
Criterion Covered Total %
statement 29 29 100.0
branch 3 6 50.0
condition 5 7 71.4
subroutine 10 10 100.0
pod n/a
total 47 52 90.3


line stmt bran cond sub pod time code
1             package Catmandu::Exporter::RDF;
2              
3 5     5   150548 use namespace::clean;
  5         32530  
  5         37  
4 5     5   1644 use Catmandu::Sane;
  5         331344  
  5         45  
5 5     5   1343 use Moo;
  5         14  
  5         36  
6 5     5   7030 use RDF::Trine::Serializer;
  5         3006943  
  5         134  
7 5     5   36 use RDF::Trine::Model;
  5         9  
  5         103  
8 5     5   736 use RDF::aREF;
  5         39980  
  5         2251  
9              
10             our $VERSION = '0.32';
11              
12             with 'Catmandu::RDF';
13             with 'Catmandu::Exporter';
14              
15             # internal attributes
16             has decoder => (
17             is => 'lazy'
18             );
19              
20             has serializer => (
21             is => 'lazy'
22             );
23              
24             has model => (
25             is => 'lazy'
26             );
27              
28             sub _build_decoder {
29 13 0 33 13   322 RDF::aREF::Decoder->new(
30             ns => $_[0]->ns // ($_[0]->ns eq 0 ? { } : RDF::NS->new),
31             callback => $_[0]->model
32             );
33             }
34              
35             sub _build_serializer {
36 13   100 13   5576 RDF::Trine::Serializer->new($_[0]->type // 'ntriples');
37             }
38              
39             sub _build_model {
40 13     13   105 my $self = shift;
41              
42             # Streaming output when we have type => NTriples
43 13 100 100     88 if (lc($self->type // 'ntriples') eq 'ntriples') {
44             sub {
45 12     12   8931 require RDF::Trine::Statement;
46 12         129 eval {
47 12         46 my $st = RDF::aREF::Decoder::trine_statement(@_);
48 12         1347 $self->fh->print($self->serializer->statement_as_string($st));
49             };
50 12 50       12982 $self->decoder->error($@) if $@;
51 3         65 };
52             }
53             else {
54 10         94 RDF::Trine::Model->new;
55             }
56             }
57              
58             sub add {
59             my ($self, $aref) = @_;
60             $self->decoder->decode($aref, keep_bnode_map => 1);
61             }
62              
63             sub commit {
64             my ($self) = @_;
65              
66             if (ref $self->model eq 'RDF::Trine::Model') {
67             $self->model->end_bulk_ops;
68             $self->decoder->clean_bnodes;
69             $self->serializer->serialize_model_to_file( $self->fh, $self->model );
70             }
71             }
72              
73             1;
74             __END__
75              
76             =head1 NAME
77              
78             Catmandu::Exporter::RDF - serialize RDF data
79              
80             =head1 SYNOPSIS
81              
82             In Perl code:
83            
84             use Catmandu -all;
85              
86             my $exporter = exporter('RDF',
87             file => 'export.rdf',
88             type => 'XML',
89             fix => 'rdf.fix'
90             );
91              
92             $exporter->add( $aref ); # pass RDF data in aREF encoding
93              
94             $exporter->commit;
95              
96             =head1 DESCRIPTION
97              
98             This L<Catmandu::Exporter> exports RDF data in different RDF serializations.
99              
100             =head1 CONFIGURATION
101              
102             =over
103              
104             =item file
105              
106             =item fh
107              
108             =item encoding
109              
110             =item fix
111              
112             Default configuration options of L<Catmandu::Exporter>. The option C<fix> is
113             supported as derived from L<Catmandu::Fixable>. For every C<add> or for every
114             item in C<add_many> the given fixes will be applied first.
115              
116             =item type
117              
118             A serialization form can be set with option C<type> with default value
119             C<NTriples>. The option must refer to a subclass of L<RDF::Trine::Serializer>,
120             for instance C<Turtle> for RDF/Turtle with L<RDF::Trine::Serializer::Turtle>.
121             The first letter is transformed uppercase, so C<< format => 'turtle' >> will
122             work as well. In addition there are aliases C<ttl> for C<Turtle>, C<n3> for
123             C<Notation3>, C<xml> and C<XML> for C<RDFXML>, C<json> for C<RDFJSON>.
124              
125             When the option C<type> is set to 'NTriples' the export can be streamed in all
126             other cases the results are exported in bulk after C<commit()>.
127              
128             =item ns
129              
130             The option C<ns> can refer to an instance of or to a constructor argument of
131             L<RDF::NS>. Use a fixed date, such as "C<20130816>" to make sure your URI
132             namespace prefixes are stable.
133              
134             =back
135              
136             =head1 METHODS
137              
138             See also L<Catmandu::Exporter>.
139              
140             =head2 add( ... )
141              
142             RDF data is added given in B<another RDF Encoding Form (aREF)> as
143             implemented with L<RDF::aREF> and defined at L<http://github.com/gbv/aref>.
144              
145             =head2 count
146              
147             Returns the number of times C<add> has been called. In contrast to other
148             Catmandu exporters, this does not reflect the number of exporter records
149             because RDF data is always merged to one RDF graph.
150              
151             =head2 uri( $uri )
152              
153             Expand and abbreviated with L<RDF::NS>. For instance "C<dc:title>" is expanded
154             to "C<http://purl.org/dc/elements/1.1/title>".
155              
156             =cut
157              
158             =head1 SEE ALSO
159              
160             Serialization is based on L<RDF::Trine::Serializer>.
161              
162             =encoding utf8
163              
164             =cut