line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
22548
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
93
|
|
2
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
128
|
|
3
|
|
|
|
|
|
|
package RDF::Dumper; |
4
|
|
|
|
|
|
|
{ |
5
|
|
|
|
|
|
|
$RDF::Dumper::VERSION = '0.3'; |
6
|
|
|
|
|
|
|
} |
7
|
|
|
|
|
|
|
# ABSTRACT: Dump RDF data objects |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
2320
|
use RDF::Trine::Serializer; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Scalar::Util 'blessed'; |
11
|
|
|
|
|
|
|
use Carp 'croak'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $GLOBAL_SERIALIZER; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Sub::Exporter -setup => { |
16
|
|
|
|
|
|
|
exports => [ |
17
|
|
|
|
|
|
|
rdfdump => \&_build, |
18
|
|
|
|
|
|
|
Dumper => \&_build, |
19
|
|
|
|
|
|
|
], |
20
|
|
|
|
|
|
|
groups => { |
21
|
|
|
|
|
|
|
default => [ qw(rdfdump) ], |
22
|
|
|
|
|
|
|
dd => [ qw(Dumper) ], |
23
|
|
|
|
|
|
|
# "all" is created automatically |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build { |
28
|
|
|
|
|
|
|
my ($class, $name, $arg) = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $fallback = delete($arg->{fallback_sub}); |
31
|
|
|
|
|
|
|
if ($name eq 'Dumper') { |
32
|
|
|
|
|
|
|
$fallback ||= sub { |
33
|
|
|
|
|
|
|
require Data::Dumper; |
34
|
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
35
|
|
|
|
|
|
|
Data::Dumper::Dumper(@_); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $format = delete($arg->{format}) || 'Turtle'; |
40
|
|
|
|
|
|
|
my $default_serializer = RDF::Trine::Serializer->new($format, %$arg); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
return sub { |
43
|
|
|
|
|
|
|
my $serializer = (blessed $_[0] and $_[0]->isa('RDF::Trine::Serializer')) |
44
|
|
|
|
|
|
|
? shift |
45
|
|
|
|
|
|
|
: ($GLOBAL_SERIALIZER || $default_serializer); |
46
|
|
|
|
|
|
|
my @serialized = map { _rdfdump($serializer, $_, $fallback) } @_; |
47
|
|
|
|
|
|
|
return join "\n", grep { defined $_ } @serialized; |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# In case people want to call fully-qualified RDF::Dumper::rdfdump($thing) |
52
|
|
|
|
|
|
|
*rdfdump = __PACKAGE__->_build(rdfdump => {}); |
53
|
|
|
|
|
|
|
*Dumper = __PACKAGE__->_build(Dumper => {}); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _rdfdump { |
56
|
|
|
|
|
|
|
my ($ser, $rdf, $fallback) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
if ( blessed $rdf ) { |
59
|
|
|
|
|
|
|
# RDF::Trine::Serializer should have a more general serialize_ method |
60
|
|
|
|
|
|
|
if ( $rdf->isa('RDF::Trine::Model') ) { |
61
|
|
|
|
|
|
|
return $ser->serialize_model_to_string( $rdf ); |
62
|
|
|
|
|
|
|
} elsif ( $rdf->isa('RDF::Trine::Iterator') ) { |
63
|
|
|
|
|
|
|
return $ser->serialize_iterator_to_string( $rdf ); |
64
|
|
|
|
|
|
|
} elsif ( $rdf->isa('RDF::Trine::Statement') ) { |
65
|
|
|
|
|
|
|
my $model = RDF::Trine::Model->temporary_model; |
66
|
|
|
|
|
|
|
$model->add_statement( $rdf ); |
67
|
|
|
|
|
|
|
return $ser->serialize_model_to_string( $model ); |
68
|
|
|
|
|
|
|
} elsif ( $rdf->isa('RDF::Trine::Store') or |
69
|
|
|
|
|
|
|
$rdf->isa('RDF::Trine::Graph') ) { |
70
|
|
|
|
|
|
|
$rdf = $rdf->get_statements; |
71
|
|
|
|
|
|
|
return $ser->serialize_iterator_to_string( $rdf ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
# TODO: serialize patterns (in Notation3) and single nodes? |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
if ( $fallback ) { |
77
|
|
|
|
|
|
|
return $fallback->($rdf); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Sorry, this was no RDF object... |
81
|
|
|
|
|
|
|
if ( ref $rdf ) { |
82
|
|
|
|
|
|
|
$rdf = "$rdf"; |
83
|
|
|
|
|
|
|
} elsif ( not defined $rdf ) { |
84
|
|
|
|
|
|
|
$rdf = 'undef'; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
croak "expected Model/Iterator/Store/Statement/Graph but got $rdf"; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=pod |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=encoding UTF-8 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 NAME |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
RDF::Dumper - Dump RDF data objects |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 VERSION |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
version 0.3 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
use RDF::Dumper; |
111
|
|
|
|
|
|
|
print rdfdump( $rdf_object ); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Data::Dumper-compatible version |
114
|
|
|
|
|
|
|
use RDF::Dumper qw(Dumper); # just like rdfdump, but falls back |
115
|
|
|
|
|
|
|
print Dumper($object); # to Data::Dumper for non-RDF things |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Custom serializer |
118
|
|
|
|
|
|
|
use RDF::Dumper rdfdump => { format => 'rdfxml', namespaces => \%ns }; |
119
|
|
|
|
|
|
|
print rdfdump( $rdf ); # use serializer created on import |
120
|
|
|
|
|
|
|
print rdfdump( $serializer, $rdf ); # use another serializer |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Multiple imports |
123
|
|
|
|
|
|
|
use RDF::Dumper |
124
|
|
|
|
|
|
|
rdfdump => { -as => 'dump_nt', format => 'ntriples' }, |
125
|
|
|
|
|
|
|
rdfdump => { -as => 'dump_xml', format => 'rdfxml', namespaces => \%ns }; |
126
|
|
|
|
|
|
|
print dump_nt( $rdf ); |
127
|
|
|
|
|
|
|
print dump_xml( $rdf ); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 DESCRIPTION |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Exports function 'rdfdump' to serialize RDF data objects given as instances of |
132
|
|
|
|
|
|
|
L<RDF::Trine::Model>, L<RDF::Trine::Iterator>, L<RDF::Trine::Statement>, |
133
|
|
|
|
|
|
|
L<RDF::Trine::Store>, or L<RDF::Trine::Graph>. See L<RDF::Trine::Serializer> |
134
|
|
|
|
|
|
|
for details on RDF serializers. By default RDF is serialized as RDF/Turtle. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Jakob Voà |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Jakob VoÃ. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
145
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |