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