File Coverage

blib/lib/RDF/Trine/Serializer/NQuads.pm
Criterion Covered Total %
statement 70 96 72.9
branch 4 8 50.0
condition 0 2 0.0
subroutine 16 19 84.2
pod 6 6 100.0
total 96 131 73.2


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.017
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   14736 use strict;
  68         174  
  68         1711  
34 68     68   321 use warnings;
  68         144  
  68         1703  
35 68     68   371 use base qw(RDF::Trine::Serializer);
  68         147  
  68         5033  
36              
37 68     68   1270 use URI;
  68         7392  
  68         1363  
38 68     68   321 use Carp;
  68         149  
  68         3258  
39 68     68   391 use Data::Dumper;
  68         158  
  68         2569  
40 68     68   361 use Scalar::Util qw(blessed);
  68         151  
  68         2645  
41              
42 68     68   1340 use RDF::Trine::Node;
  68         151  
  68         1999  
43 68     68   1022 use RDF::Trine::Statement;
  68         164  
  68         1695  
44 68     68   361 use RDF::Trine::Error qw(:try);
  68         150  
  68         574  
45              
46             ######################################################################
47              
48             our ($VERSION);
49             BEGIN {
50 68     68   13224 $VERSION = '1.017';
51 68         200 $RDF::Trine::Serializer::serializer_names{ 'nquads' } = __PACKAGE__;
52 68         165 $RDF::Trine::Serializer::format_uris{ 'http://sw.deri.org/2008/07/n-quads/#n-quads' } = __PACKAGE__;
53 68         198 foreach my $type (qw(text/x-nquads)) {
54 68         34907 $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 1439 my $class = shift;
68 5         17 my %args = @_;
69 5         17 my $self = bless( {}, $class);
70 5         16 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 11 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         8 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 14 my $self = shift;
117 2         4 my $file = shift;
118 2         4 my $iter = shift;
119 2         9 while (my $st = $iter->next) {
120 4         9 print {$file} $self->_statement_as_string( $st );
  4         13  
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 5 my $self = shift;
132 1         4 my $iter = shift;
133 1         4 my $string = '';
134 1         7 while (my $st = $iter->next) {
135 2         12 my @nodes = $st->nodes;
136 2         9 $string .= $self->_statement_as_string( $st );
137             }
138 1         5 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   22 my $self = shift;
161 10         15 my $st = shift;
162 10         21 my @nodes;
163 10 100       32 if ($st->type eq 'TRIPLE') {
164 2         8 @nodes = $st->nodes;
165             } else {
166 8         26 my $g = $st->context;
167 8 100       41 if ($g->is_nil) {
168 4         15 @nodes = ($st->nodes)[0..2];
169             } else {
170 4         15 @nodes = $st->nodes;
171             }
172             }
173 10         31 return join(' ', map { $_->as_ntriples } @nodes) . " .\n";
  34         113  
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