RDF/Trine/Serializer.pm

filter: Statementstmt 94.1% 144 / 153 Branchbran 92.8% 26 / 28 Conditioncond 71.4% 10 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 93.9% 31 / 33 Podpod 100% 7 / 7 totaltotal 91.3% 223 / 244 CC 21 SCAR 17.9
CC
21
Cov
92%
negotiate
16.0
serialize_iterator_to_file
6.0
media_types
4.0
CRAP 6.0
« RDF/Trine/Graph.pm ↑ Summary RDF/Trine/Node.pm »
1 # RDF::Trine::Serializer
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer - RDF Serializer class
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Serializer version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Serializer;
15  
16 =head1 DESCRIPTION
17  
18 The RDF::Trine::Serializer class provides an API for serializing RDF graphs
19 (via both model objects and graph iterators) to strings and files.
20  
21 =cut
22  
23 package RDF::Trine::Serializer;
24  
25 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 68 ▶use HTTP::Negotiate qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 68 our ($VERSION);
33 68 our %serializer_names;
34 68 our %format_uris;
35 68 our %media_types;
36 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 $VERSION = '1.019';
38 }
39  
40 68 ▶use RDF::Trine::Serializer::NQuads;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use RDF::Trine::Serializer::NTriples;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use RDF::Trine::Serializer::NTriples::Canonical;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use RDF::Trine::Serializer::RDFXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use RDF::Trine::Serializer::RDFJSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use RDF::Trine::Serializer::Turtle;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 68 ▶use RDF::Trine::Serializer::TriG;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 68 ▶use RDF::Trine::Serializer::RDFPatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 68 ▶use RDF::Trine::Serializer::TSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 =head1 METHODS
51  
52 =over 4
53  
54 =item C<< serializer_names >>
55  
56 Returns a list of valid serializer names for use as arguments to the serializer constructor.
57  
58 =cut
59  
60 0 ▶sub serializer_names {
Subroutine serializer_names: not called CC=1 SCAR=6.9
Pod documented
61 0 return keys %serializer_names;
62 }
63  
64 =item C<< new ( $serializer_name, %options ) >>
65  
66 Returns a new RDF::Trine::Serializer object for the serializer with the
67 specified name (e.g. "rdfxml" or "turtle"). If no serializer with the specified
68 name is found, throws a RDF::Trine::Error::SerializationError exception.
69  
70 The valid key-values used in C<< %options >> are specific to a particular
71 serializer implementation. For serializers that support namespace declarations
72 (to allow more concise serialization), use C<< namespaces => \%namespaces >> in
73 C<< %options >>, where the keys of C<< %namespaces >> are namespace names and
74 the values are (partial) URIs. For serializers that support base URI declarations,
75 use C<< base_uri => $base_uri >> .
76  
77 =cut
78  
79 9 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
80 9 my $class = shift;
81 9 my $name = shift;
82 9 my $key = lc($name);
83 9 $key =~ s/[^-a-z]//g;
84
85 9 ▶ if (my $class = $serializer_names{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $RDF::Trine::Serializer::serializer_names{$key}) { } 8 1 9
86 8 return $class->new( @_ );
87 } else {
88 1 throw RDF::Trine::Error::SerializationError -text => "No serializer known named $name";
89 }
90 }
91  
92 =item C<< negotiate ( request_headers => $request_headers, %options ) >>
93  
94 Returns a two-element list containing an appropriate media type and
95 RDF::Trine::Serializer object as decided by L<HTTP::Negotiate>. If
96 the C<< 'request_headers' >> key-value is supplied, the C<<
97 $request_headers >> is passed to C<< HTTP::Negotiate::choose >>. The
98 option C<< 'restrict' >>, set to a list of serializer names, can be
99 used to limit the serializers to choose from. Finally, an C<<'extend' >>
100 option can be set to a hashref that contains MIME-types
101 as keys and a custom variant as value. This will enable the user to
102 use this negotiator to return a type that isn't supported by any
103 serializers. The subsequent code will have to find out how to return a
104 representation. The rest of C<< %options >> is passed through to the
105 serializer constructor.
106  
107 =cut
108  
109 14 ▶sub negotiate {
Subroutine negotiate: called CC=16 SCAR=27.7
Pod documented
110 14 my $class = shift;
111 14 my %options = @_;
112 14 my $headers = delete $options{ 'request_headers' };
113 14 my $restrict = delete $options{ 'restrict' };
114 14 ▶ my $extend = delete $options{ 'extend' } || {};
Condition100%2/2
delete $options{'extend'} || {}
l!l
212
MC/DC100%1/1
delete $options{'extend'} || {}
delete $options{'extend'}
Truth table100%2/2
A
A= delete $options{'extend'}
Aresult
11
00
115 14 my %sclasses;
116 14 ▶ if (ref($restrict) && ref($restrict) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $restrict and ref $restrict eq 'ARRAY') { } 3 11 14
Condition66%2/3
ref $restrict and ref $restrict eq "ARRAY"
!ll&&!rl&&r
1103
MC/DC50%1/2
ref $restrict and ref $restrict eq "ARRAY"
ref $restrict ref $restrict eq "ARRAY"
Truth table66%2/3
A and B
A= ref $restrictB= ref $restrict eq "ARRAY"
ABresult
0X0
100
111
117 3 $sclasses{ $serializer_names{$_} } = 1 for @$restrict;
118 } else {
119 11 %sclasses = reverse %serializer_names;
120 }
121 14 my @default_variants;
122 14 ▶ while (my($type, $sclass) = each(%media_types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %RDF::Trine::Serializer::media_types) 154 14 168
123 154 ▶ next unless $sclasses{$sclass};
Branch: 1 branch
BranchTrueFalseTotal
unless $sclasses{$sclass} 19 135 154
124 135 my $qv;
125 # slightly prefer turtle as a readable format to others
126 # try hard to avoid using ntriples as 'text/plain' isn't very useful for conneg
127 135 ▶ if ($type eq 'text/turtle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'text/turtle') { } 14 121 135
elsif ($type eq 'text/plain') { } 11 110 121
128 14 $qv = 1.0;
129 } elsif ($type eq 'text/plain') {
130 11 $qv = 0.2;
131 } else {
132 110 $qv = 0.99;
133 }
134 135 ▶ $qv -= 0.01 if ($type =~ m#/x-#); # prefer non experimental media types
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[/x-] 37 98 135
135 135 ▶ $qv -= 0.01 if ($type =~ m#^application/(?!rdf[+]xml)#); # prefer standard rdf/xml to other application/* formats
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^application/(?!rdf[+]xml)] 63 72 135
136 135 push(@default_variants, [$type, $qv, $type]);
137 }
138
139 14 my %custom_thunks;
140 my @custom_variants;
141 14 ▶ while (my($type,$thunk) = each(%$extend)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extend) 3 14 17
142 3 push(@custom_variants, [$thunk, 1.0, $type]);
143 3 $custom_thunks{ $thunk } = [$type, $thunk];
144 }
145
146 # remove variants with media types that are in custom_variants from @variants
147 14 my @variants = grep { not exists $extend->{ $_->[2] } } @default_variants;
148 14 push(@variants, @custom_variants);
149
150 14 my $stype = choose( \@variants, $headers );
151 14 ▶ if (defined($stype) and $custom_thunks{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and $custom_thunks{$stype}) 2 12 14
Condition100%3/3
defined $stype and $custom_thunks{$stype}
!ll&&!rl&&r
2102
MC/DC100%2/2
defined $stype and $custom_thunks{$stype}
defined $stype $custom_thunks{$stype}
Truth table100%3/3
A and B
A= defined $stypeB= $custom_thunks{$stype}
ABresult
0X0
100
111
152 2 my $thunk = $stype;
153 2 my $type = $custom_thunks{ $stype }[0];
154 2 return ($type, $thunk);
155 }
156
157 12 ▶ if (defined($stype) and my $sclass = $media_types{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and my $sclass = $RDF::Trine::Serializer::media_types{$stype}) { } 10 2 12
Condition66%2/3
defined $stype and my $sclass = $RDF::Trine::Serializer::media_types{$stype}
!ll&&!rl&&r
2010
MC/DC50%1/2
defined $stype and my $sclass = $RDF::Trine::Serializer::media_types{$stype}
defined $stype my $sclass = $RDF::Trine::Serializer::media_types{$stype}
Truth table66%2/3
A and B
A= defined $stypeB= my $sclass = $RDF::Trine::Serializer::media_types{$stype}
ABresult
0X0
100
111
158 10 return ($stype, $sclass->new( %options ));
159 } else {
160 2 throw RDF::Trine::Error::SerializationError -text => "No appropriate serializer found for content-negotiation";
161 }
162 }
163  
164 =item C<< media_types >>
165  
166 Returns a list of media types appropriate for the format of the serializer.
167  
168 =cut
169  
170 6 ▶sub media_types {
Subroutine media_types: called CC=4 SCAR=14.0
Pod documented
171 6 my $self = shift;
172 6 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
173 6 my @list;
174 6 ▶ while (my($type, $sclass) = each(%media_types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %RDF::Trine::Serializer::media_types) 66 6 72
175 66 ▶ push(@list, $type) if ($sclass eq $class);
Branch: 1 branch
BranchTrueFalseTotal
if $sclass eq $class 9 57 66
176 }
177 6 my @types = sort @list;
178 6 return @types;
179 }
180  
181 =item C<< serialize_model_to_file ( $fh, $model ) >>
182  
183 Serializes the C<< $model >>, printing the results to the supplied filehandle
184 C<<$fh>>.
185  
186 =item C<< serialize_model_to_string ( $model ) >>
187  
188 Serializes the C<< $model >>, returning the result as a string.
189  
190 =cut
191  
192 13 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: called CC=1 SCAR=0.0
Pod documented
193 13 my $self = shift;
194 13 my $model = shift;
195 13 my $string = '';
196 13 open( my $fh, '>:encoding(UTF-8)', \$string );
197 13 $self->serialize_model_to_file( $fh, $model );
198 11 close($fh);
199 11 return $string;
200 }
201  
202 =item C<< serialize_iterator_to_file ( $file, $iterator ) >>
203  
204 Serializes the statement objects produced by C<< $iterator >>, printing the
205 results to the supplied filehandle C<<$fh>>.
206  
207 Note that some serializers may not support the use of this method, or may
208 require the full materialization of the iterator in order to serialize it.
209 If materialization is required, available memeory may constrain the iterators
210 that can be serialized.
211  
212 =cut
213  
214 0 ▶sub serialize_iterator_to_file {
Subroutine serialize_iterator_to_file: not called CC=2 SCAR=17.9
Pod documented
215 0 my $self = shift;
216 0 my $fh = shift;
217 0 my $iter = shift;
218 0 my %args = @_;
219 0 my $model = RDF::Trine::Model->temporary_model;
220 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
221 0 $model->add_statement( $st );
222 }
223 0 return $self->serialize_model_to_file( $fh, $model );
224 }
225  
226  
227 =item C<< serialize_iterator_to_string ( $iterator ) >>
228  
229 Serializes the statement objects produced by C<< $iterator >>, returning the
230 result as a string. Note that the same constraints apply to this method as to
231 C<< serialize_iterator_to_file >>.
232  
233 =cut
234  
235 7 ▶sub serialize_iterator_to_string {
Subroutine serialize_iterator_to_string: called CC=1 SCAR=0.0
Pod documented
236 7 my $self = shift;
237 7 my $iter = shift;
238 7 my $string = '';
239 7 open( my $fh, '>', \$string );
240 7 $self->serialize_iterator_to_file( $fh, $iter );
241 7 close($fh);
242 7 return $string;
243 }
244  
245  
246  
247  
248 =back
249  
250 =cut
251  
252 package RDF::Trine::Serializer::FileSink;
253  
254 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 =begin private
258  
259 =head1 NAME
260  
261 RDF::Trine::Serializer::FileSink
262  
263 =head1 METHODS
264  
265 =over 4
266  
267 =cut
268  
269 =item C<< new ( $fh ) >>
270  
271 Returns a new serializer sink object backed by a filehandle.
272  
273 =cut
274  
275 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
276 1 my $class = shift;
277 1 my $fh = shift;
278 1 return bless([$fh],$class);
279 }
280  
281 =item C<< emit ( $data ) >>
282  
283 Write the C<< $data >> to the sink.
284  
285 =cut
286  
287 16 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
288 16 my $self = shift;
289 16 my $data = shift;
290 16 print {$self->[0]} $data;
291 }
292  
293 =back
294  
295 =cut
296  
297 package RDF::Trine::Serializer::StringSink;
298  
299 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
301 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
302  
303 =head1 NAME
304  
305 RDF::Trine::Serializer::StringSink
306  
307 =head1 METHODS
308  
309 =over 4
310  
311 =cut
312  
313 =item C<< new () >>
314  
315 Returns a new serializer sink object backed by a string.
316  
317 =cut
318  
319 42 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
320 42 my $class = shift;
321 42 my $string = decode_utf8("");
322 42 return bless(\$string,$class);
323 }
324  
325 =item C<< emit ( $data ) >>
326  
327 Write the C<< $data >> to the sink.
328  
329 =cut
330  
331 767 ▶sub emit {
Subroutine emit: called CC=1 SCAR=0.0
332 767 my $self = shift;
333 767 my $data = shift;
334 767 $$self .= $data;
335 }
336  
337 =item C<< prepend ( $data ) >>
338  
339 Prepend the C<< $data >> to the underlying string.
340  
341 =cut
342  
343 10 ▶sub prepend {
Subroutine prepend: called CC=1 SCAR=0.0
344 10 my $self = shift;
345 10 my $data = shift;
346 10 $$self = $data . $$self;
347 }
348  
349 =item C<< string () >>
350  
351 Returns the string value of all data written to the sink.
352  
353 =cut
354  
355 47 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
356 47 my $self = shift;
357 47 return $$self;
358 }
359  
360 =back
361  
362 =end private
363  
364 =cut
365  
366 68 1;
367  
368 __END__
« RDF/Trine/Graph.pm ↑ Summary RDF/Trine/Node.pm »