line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# RDF::Trine::Serializer::NQuads |
2
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
RDF::Trine::Serializer::NQuads - N-Quads Serializer |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
This document describes RDF::Trine::Serializer::NQuads version 1.018 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use RDF::Trine::Serializer::NQuads; |
15
|
|
|
|
|
|
|
my $serializer = RDF::Trine::Serializer::NQuads->new(); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
The RDF::Trine::Serializer::NQuads class provides an API for serializing RDF |
20
|
|
|
|
|
|
|
graphs to the N-Quads syntax. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 METHODS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Beyond the methods documented below, this class inherits methods from the |
25
|
|
|
|
|
|
|
L<RDF::Trine::Serializer> class. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=over 4 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
package RDF::Trine::Serializer::NQuads; |
32
|
|
|
|
|
|
|
|
33
|
68
|
|
|
68
|
|
16567
|
use strict; |
|
68
|
|
|
|
|
169
|
|
|
68
|
|
|
|
|
1676
|
|
34
|
68
|
|
|
68
|
|
322
|
use warnings; |
|
68
|
|
|
|
|
140
|
|
|
68
|
|
|
|
|
1704
|
|
35
|
68
|
|
|
68
|
|
330
|
use base qw(RDF::Trine::Serializer); |
|
68
|
|
|
|
|
160
|
|
|
68
|
|
|
|
|
5071
|
|
36
|
|
|
|
|
|
|
|
37
|
68
|
|
|
68
|
|
1241
|
use URI; |
|
68
|
|
|
|
|
6935
|
|
|
68
|
|
|
|
|
1339
|
|
38
|
68
|
|
|
68
|
|
328
|
use Carp; |
|
68
|
|
|
|
|
144
|
|
|
68
|
|
|
|
|
3242
|
|
39
|
68
|
|
|
68
|
|
379
|
use Data::Dumper; |
|
68
|
|
|
|
|
171
|
|
|
68
|
|
|
|
|
2515
|
|
40
|
68
|
|
|
68
|
|
383
|
use Scalar::Util qw(blessed); |
|
68
|
|
|
|
|
141
|
|
|
68
|
|
|
|
|
2881
|
|
41
|
|
|
|
|
|
|
|
42
|
68
|
|
|
68
|
|
1028
|
use RDF::Trine::Node; |
|
68
|
|
|
|
|
148
|
|
|
68
|
|
|
|
|
2063
|
|
43
|
68
|
|
|
68
|
|
1005
|
use RDF::Trine::Statement; |
|
68
|
|
|
|
|
165
|
|
|
68
|
|
|
|
|
1747
|
|
44
|
68
|
|
|
68
|
|
339
|
use RDF::Trine::Error qw(:try); |
|
68
|
|
|
|
|
143
|
|
|
68
|
|
|
|
|
643
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
###################################################################### |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
our ($VERSION); |
49
|
|
|
|
|
|
|
BEGIN { |
50
|
68
|
|
|
68
|
|
13029
|
$VERSION = '1.018'; |
51
|
68
|
|
|
|
|
209
|
$RDF::Trine::Serializer::serializer_names{ 'nquads' } = __PACKAGE__; |
52
|
68
|
|
|
|
|
180
|
$RDF::Trine::Serializer::format_uris{ 'http://sw.deri.org/2008/07/n-quads/#n-quads' } = __PACKAGE__; |
53
|
68
|
|
|
|
|
189
|
foreach my $type (qw(text/x-nquads)) { |
54
|
68
|
|
|
|
|
34712
|
$RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
###################################################################### |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item C<< new >> |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Returns a new N-Quads serializer object. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub new { |
67
|
5
|
|
|
5
|
1
|
830
|
my $class = shift; |
68
|
5
|
|
|
|
|
15
|
my %args = @_; |
69
|
5
|
|
|
|
|
15
|
my $self = bless( {}, $class); |
70
|
5
|
|
|
|
|
17
|
return $self; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item C<< serialize_model_to_file ( $fh, $model ) >> |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Serializes the C<$model> to N-Quads, printing the results to the supplied |
76
|
|
|
|
|
|
|
filehandle C<<$fh>>. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub serialize_model_to_file { |
81
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
82
|
1
|
|
|
|
|
2
|
my $file = shift; |
83
|
1
|
|
|
|
|
2
|
my $model = shift; |
84
|
1
|
|
|
|
|
6
|
my $iter = $model->as_stream; |
85
|
1
|
|
|
|
|
5
|
while (my $st = $iter->next) { |
86
|
4
|
|
|
|
|
7
|
print {$file} $self->_statement_as_string( $st ); |
|
4
|
|
|
|
|
12
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item C<< serialize_model_to_string ( $model ) >> |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Serializes the C<$model> to N-Quads, returning the result as a string. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub serialize_model_to_string { |
97
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
98
|
0
|
|
|
|
|
0
|
my $model = shift; |
99
|
0
|
|
|
|
|
0
|
my $iter = $model->as_stream; |
100
|
0
|
|
|
|
|
0
|
my $string = ''; |
101
|
0
|
|
|
|
|
0
|
while (my $st = $iter->next) { |
102
|
0
|
|
|
|
|
0
|
my @nodes = $st->nodes; |
103
|
0
|
|
|
|
|
0
|
$string .= $self->_statement_as_string( $st ); |
104
|
|
|
|
|
|
|
} |
105
|
0
|
|
|
|
|
0
|
return $string; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item C<< serialize_iterator_to_file ( $file, $iter ) >> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Serializes the iterator to N-Quads, printing the results to the supplied |
111
|
|
|
|
|
|
|
filehandle C<<$fh>>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub serialize_iterator_to_file { |
116
|
2
|
|
|
2
|
1
|
8
|
my $self = shift; |
117
|
2
|
|
|
|
|
4
|
my $file = shift; |
118
|
2
|
|
|
|
|
3
|
my $iter = shift; |
119
|
2
|
|
|
|
|
7
|
while (my $st = $iter->next) { |
120
|
4
|
|
|
|
|
8
|
print {$file} $self->_statement_as_string( $st ); |
|
4
|
|
|
|
|
10
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item C<< serialize_iterator_to_string ( $iter ) >> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Serializes the iterator to N-Quads, returning the result as a string. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub serialize_iterator_to_string { |
131
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
132
|
1
|
|
|
|
|
2
|
my $iter = shift; |
133
|
1
|
|
|
|
|
2
|
my $string = ''; |
134
|
1
|
|
|
|
|
5
|
while (my $st = $iter->next) { |
135
|
2
|
|
|
|
|
8
|
my @nodes = $st->nodes; |
136
|
2
|
|
|
|
|
8
|
$string .= $self->_statement_as_string( $st ); |
137
|
|
|
|
|
|
|
} |
138
|
1
|
|
|
|
|
4
|
return $string; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _serialize_bounded_description { |
142
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
143
|
0
|
|
|
|
|
0
|
my $model = shift; |
144
|
0
|
|
|
|
|
0
|
my $node = shift; |
145
|
0
|
|
0
|
|
|
0
|
my $seen = shift || {}; |
146
|
0
|
0
|
|
|
|
0
|
return '' if ($seen->{ $node->sse }++); |
147
|
0
|
|
|
|
|
0
|
my $iter = $model->get_statements( $node, undef, undef ); |
148
|
0
|
|
|
|
|
0
|
my $string = ''; |
149
|
0
|
|
|
|
|
0
|
while (my $st = $iter->next) { |
150
|
0
|
|
|
|
|
0
|
my @nodes = $st->nodes; |
151
|
0
|
|
|
|
|
0
|
$string .= $self->_statement_as_string( $st ); |
152
|
0
|
0
|
|
|
|
0
|
if ($nodes[2]->isa('RDF::Trine::Node::Blank')) { |
153
|
0
|
|
|
|
|
0
|
$string .= $self->_serialize_bounded_description( $model, $nodes[2], $seen ); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
0
|
|
|
|
|
0
|
return $string; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _statement_as_string { |
160
|
10
|
|
|
10
|
|
20
|
my $self = shift; |
161
|
10
|
|
|
|
|
13
|
my $st = shift; |
162
|
10
|
|
|
|
|
16
|
my @nodes; |
163
|
10
|
100
|
|
|
|
30
|
if ($st->type eq 'TRIPLE') { |
164
|
2
|
|
|
|
|
6
|
@nodes = $st->nodes; |
165
|
|
|
|
|
|
|
} else { |
166
|
8
|
|
|
|
|
24
|
my $g = $st->context; |
167
|
8
|
100
|
|
|
|
35
|
if ($g->is_nil) { |
168
|
4
|
|
|
|
|
13
|
@nodes = ($st->nodes)[0..2]; |
169
|
|
|
|
|
|
|
} else { |
170
|
4
|
|
|
|
|
15
|
@nodes = $st->nodes; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
10
|
|
|
|
|
25
|
return join(' ', map { $_->as_ntriples } @nodes) . " .\n"; |
|
34
|
|
|
|
|
97
|
|
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item C<< statement_as_string ( $st ) >> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Returns a string with the supplied RDF::Trine::Statement::Quad object serialized |
180
|
|
|
|
|
|
|
as N-Quads, ending in a DOT and newline. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub statement_as_string { |
185
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
186
|
0
|
|
|
|
|
|
my $st = shift; |
187
|
0
|
|
|
|
|
|
my @nodes = $st->nodes; |
188
|
0
|
|
|
|
|
|
return join(' ', map { $_->as_ntriples } @nodes[0..3]) . " .\n"; |
|
0
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
__END__ |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=back |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 BUGS |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Please report any bugs or feature requests to through the GitHub web interface |
200
|
|
|
|
|
|
|
at L<https://github.com/kasei/perlrdf/issues>. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 SEE ALSO |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L<http://sw.deri.org/2008/07/n-quads/> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Gregory Todd Williams C<< <gwilliams@cpan.org> >> |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Copyright (c) 2006-2012 Gregory Todd Williams. This |
213
|
|
|
|
|
|
|
program is free software; you can redistribute it and/or modify it under |
214
|
|
|
|
|
|
|
the same terms as Perl itself. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |