File Coverage

blib/lib/RDF/Trine/Serializer/NTriples.pm
Criterion Covered Total %
statement 68 96 70.8
branch 0 4 0.0
condition 0 2 0.0
subroutine 16 19 84.2
pod 7 7 100.0
total 91 128 71.0


line stmt bran cond sub pod time code
1             # RDF::Trine::Serializer::NTriples
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Trine::Serializer::NTriples - N-Triples Serializer
7              
8             =head1 VERSION
9              
10             This document describes RDF::Trine::Serializer::NTriples version 1.018
11              
12             =head1 SYNOPSIS
13              
14             use RDF::Trine::Serializer::NTriples;
15             my $serializer = RDF::Trine::Serializer::NTriples->new();
16              
17             =head1 DESCRIPTION
18              
19             The RDF::Trine::Serializer::NTriples class provides an API for serializing RDF
20             graphs to the N-Triples 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::NTriples;
32              
33 68     68   16752 use strict;
  68         151  
  68         1665  
34 68     68   307 use warnings;
  68         153  
  68         1607  
35 68     68   359 use base qw(RDF::Trine::Serializer);
  68         147  
  68         4182  
36              
37 68     68   804 use URI;
  68         3735  
  68         1192  
38 68     68   329 use Carp;
  68         194  
  68         3084  
39 68     68   387 use Data::Dumper;
  68         157  
  68         2524  
40 68     68   363 use Scalar::Util qw(blessed);
  68         147  
  68         2574  
41              
42 68     68   1073 use RDF::Trine::Node;
  68         151  
  68         1918  
43 68     68   680 use RDF::Trine::Statement;
  68         172  
  68         1460  
44 68     68   324 use RDF::Trine::Error qw(:try);
  68         151  
  68         385  
45              
46             ######################################################################
47              
48             our ($VERSION);
49             BEGIN {
50 68     68   10792 $VERSION = '1.018';
51 68         232 $RDF::Trine::Serializer::serializer_names{ 'ntriples' } = __PACKAGE__;
52 68         149 $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/N-Triples' } = __PACKAGE__;
53 68         174 foreach my $type (qw(text/plain)) {
54 68         34763 $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
55             }
56             }
57              
58             ######################################################################
59              
60             =item C<< new >>
61              
62             Returns a new N-Triples serializer object.
63              
64             =cut
65              
66             sub new {
67 7     7 1 8123 my $class = shift;
68 7         20 my %args = @_;
69 7         20 my $self = bless( {}, $class);
70 7         32 return $self;
71             }
72              
73             =item C<< serialize_model_to_file ( $fh, $model ) >>
74              
75             Serializes the C<$model> to N-Triples, printing the results to the supplied
76             filehandle C<<$fh>>.
77              
78             =cut
79              
80             sub serialize_model_to_file {
81 1     1 1 6 my $self = shift;
82 1         3 my $file = shift;
83 1         2 my $model = shift;
84 1         4 my $st = RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
  3         17  
85 1         11 my $pat = RDF::Trine::Pattern->new( $st );
86 1         9 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
87 1         10 my $iter = $stream->as_statements( qw(s p o) );
88 1         5 while (my $st = $iter->next) {
89 4         7 print {$file} $self->statement_as_string( $st );
  4         14  
90             }
91             }
92              
93             =item C<< serialize_model_to_string ( $model ) >>
94              
95             Serializes the C<$model> to N-Triples, returning the result as a string.
96              
97             =cut
98              
99             sub serialize_model_to_string {
100 0     0 1 0 my $self = shift;
101 0         0 my $model = shift;
102 0         0 my $st = RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
  0         0  
103 0         0 my $pat = RDF::Trine::Pattern->new( $st );
104 0         0 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
105 0         0 my $iter = $stream->as_statements( qw(s p o) );
106            
107 0         0 my $string = '';
108 0         0 while (my $st = $iter->next) {
109 0         0 my @nodes = $st->nodes;
110 0         0 $string .= $self->statement_as_string( $st );
111             }
112 0         0 return $string;
113             }
114              
115             =item C<< serialize_iterator_to_file ( $file, $iter ) >>
116              
117             Serializes the iterator to N-Triples, printing the results to the supplied
118             filehandle C<<$fh>>.
119              
120             =cut
121              
122             sub serialize_iterator_to_file {
123 1     1 1 7 my $self = shift;
124 1         3 my $file = shift;
125 1         2 my $iter = shift;
126 1         5 while (my $st = $iter->next) {
127 2         5 print {$file} $self->statement_as_string( $st );
  2         6  
128             }
129             }
130              
131             =item C<< serialize_iterator_to_string ( $iter ) >>
132              
133             Serializes the iterator to N-Triples, returning the result as a string.
134              
135             =cut
136              
137             sub serialize_iterator_to_string {
138 1     1 1 4 my $self = shift;
139 1         2 my $iter = shift;
140 1         3 my $string = '';
141 1         4 while (my $st = $iter->next) {
142 2         7 my @nodes = $st->nodes;
143 2         6 $string .= $self->statement_as_string( $st );
144             }
145 1         3 return $string;
146             }
147              
148             sub _serialize_bounded_description {
149 0     0   0 my $self = shift;
150 0         0 my $model = shift;
151 0         0 my $node = shift;
152 0   0     0 my $seen = shift || {};
153 0 0       0 return '' if ($seen->{ $node->sse }++);
154 0         0 my $iter = $model->get_statements( $node, undef, undef );
155 0         0 my $string = '';
156 0         0 while (my $st = $iter->next) {
157 0         0 my @nodes = $st->nodes;
158 0         0 $string .= $self->statement_as_string( $st );
159 0 0       0 if ($nodes[2]->isa('RDF::Trine::Node::Blank')) {
160 0         0 $string .= $self->_serialize_bounded_description( $model, $nodes[2], $seen );
161             }
162             }
163 0         0 return $string;
164             }
165              
166             =item C<< statement_as_string ( $st ) >>
167              
168             Returns a string with the supplied RDF::Trine::Statement object serialized as
169             N-Triples, ending in a DOT and newline.
170              
171             =cut
172              
173             sub statement_as_string {
174 8     8 1 13 my $self = shift;
175 8         14 my $st = shift;
176 8         20 my @nodes = $st->nodes;
177 8         28 return join(' ', map { $_->as_ntriples } @nodes[0..2]) . " .\n";
  24         68  
178             }
179              
180             =item C<< serialize_node ( $node ) >>
181              
182             Returns a string containing the N-Triples serialization of C<< $node >>.
183              
184             =cut
185              
186             sub serialize_node {
187 0     0 1   my $self = shift;
188 0           my $node = shift;
189 0           return $node->as_ntriples;
190             }
191              
192             1;
193              
194             __END__
195              
196             =back
197              
198             =head1 NOTES
199              
200             As described in L<RDF::Trine::Node::Resource/as_ntriples>, N-Triples serialization will
201             decode any L<punycode|http://www.ietf.org/rfc/rfc3492.txt> that is included in the IRI,
202             and serialize it using unicode codepoint escapes.
203              
204             =head1 BUGS
205              
206             Please report any bugs or feature requests to through the GitHub web interface
207             at L<https://github.com/kasei/perlrdf/issues>.
208              
209             =head1 SEE ALSO
210              
211             L<http://www.w3.org/TR/rdf-testcases/#ntriples>
212              
213             =head1 AUTHOR
214              
215             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
216              
217             =head1 COPYRIGHT
218              
219             Copyright (c) 2006-2012 Gregory Todd Williams. This
220             program is free software; you can redistribute it and/or modify it under
221             the same terms as Perl itself.
222              
223             =cut