| 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.019 | ||||||||||||
| 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 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 68 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | 68 | ▶ | use base qw(RDF::Trine::Serializer); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | |||||||||||||
| 37 | 68 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | 68 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 68 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 68 | ▶ | use Scalar::Util qw(blessed); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | |||||||||||||
| 42 | 68 | ▶ | use RDF::Trine::Node; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 68 | ▶ | use RDF::Trine::Statement; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 68 | ▶ | use RDF::Trine::Error qw(:try); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | |||||||||||||
| 46 | ###################################################################### | ||||||||||||
| 47 | |||||||||||||
| 48 | 68 | our ($VERSION); | |||||||||||
| 49 | 68 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||
| 50 | 68 | $VERSION = '1.019'; | |||||||||||
| 51 | 68 | $RDF::Trine::Serializer::serializer_names{ 'nquads' } = __PACKAGE__; | |||||||||||
| 52 | 68 | $RDF::Trine::Serializer::format_uris{ 'http://sw.deri.org/2008/07/n-quads/#n-quads' } = __PACKAGE__; | |||||||||||
| 53 | 68 | foreach my $type (qw(text/x-nquads)) { | |||||||||||
| 54 | 68 | $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 | 5 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 67 | 5 | my $class = shift; | |||||||||||
| 68 | 5 | my %args = @_; | |||||||||||
| 69 | 5 | my $self = bless( {}, $class); | |||||||||||
| 70 | 5 | 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 | 1 | ▶ | sub serialize_model_to_file { | ||||||||||
|
Subroutine
serialize_model_to_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 81 | 1 | my $self = shift; | |||||||||||
| 82 | 1 | my $file = shift; | |||||||||||
| 83 | 1 | my $model = shift; | |||||||||||
| 84 | 1 | my $iter = $model->as_stream; | |||||||||||
| 85 | 1 | ▶ | while (my $st = $iter->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 4 | print {$file} $self->_statement_as_string( $st ); | |||||||||||
| 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 | 0 | ▶ | sub serialize_model_to_string { | ||||||||||
|
Subroutine
serialize_model_to_string: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||
| 98 | 0 | my $model = shift; | |||||||||||
| 99 | 0 | my $iter = $model->as_stream; | |||||||||||
| 100 | 0 | my $string = ''; | |||||||||||
| 101 | 0 | ▶ | while (my $st = $iter->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 0 | my @nodes = $st->nodes; | |||||||||||
| 103 | 0 | $string .= $self->_statement_as_string( $st ); | |||||||||||
| 104 | } | ||||||||||||
| 105 | 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 | 2 | ▶ | sub serialize_iterator_to_file { | ||||||||||
|
Subroutine
serialize_iterator_to_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 116 | 2 | my $self = shift; | |||||||||||
| 117 | 2 | my $file = shift; | |||||||||||
| 118 | 2 | my $iter = shift; | |||||||||||
| 119 | 2 | ▶ | while (my $st = $iter->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 4 | print {$file} $self->_statement_as_string( $st ); | |||||||||||
| 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 | 1 | ▶ | sub serialize_iterator_to_string { | ||||||||||
|
Subroutine
serialize_iterator_to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 131 | 1 | my $self = shift; | |||||||||||
| 132 | 1 | my $iter = shift; | |||||||||||
| 133 | 1 | my $string = ''; | |||||||||||
| 134 | 1 | ▶ | while (my $st = $iter->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 2 | my @nodes = $st->nodes; | |||||||||||
| 136 | 2 | $string .= $self->_statement_as_string( $st ); | |||||||||||
| 137 | } | ||||||||||||
| 138 | 1 | return $string; | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 0 | ▶ | sub _serialize_bounded_description { | ||||||||||
|
Subroutine
_serialize_bounded_description: not called CC=5 SCAR=34.0
| |||||||||||||
| 142 | 0 | my $self = shift; | |||||||||||
| 143 | 0 | my $model = shift; | |||||||||||
| 144 | 0 | my $node = shift; | |||||||||||
| 145 | 0 | ▶ | my $seen = shift || {}; | ||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 146 | 0 | ▶ | return '' if ($seen->{ $node->sse }++); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | 0 | my $iter = $model->get_statements( $node, undef, undef ); | |||||||||||
| 148 | 0 | my $string = ''; | |||||||||||
| 149 | 0 | ▶ | while (my $st = $iter->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 0 | my @nodes = $st->nodes; | |||||||||||
| 151 | 0 | $string .= $self->_statement_as_string( $st ); | |||||||||||
| 152 | 0 | ▶ | if ($nodes[2]->isa('RDF::Trine::Node::Blank')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 153 | 0 | $string .= $self->_serialize_bounded_description( $model, $nodes[2], $seen ); | |||||||||||
| 154 | } | ||||||||||||
| 155 | } | ||||||||||||
| 156 | 0 | return $string; | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | 10 | ▶ | sub _statement_as_string { | ||||||||||
|
Subroutine
_statement_as_string: called CC=3 SCAR=11.0
| |||||||||||||
| 160 | 10 | my $self = shift; | |||||||||||
| 161 | 10 | my $st = shift; | |||||||||||
| 162 | 10 | my @nodes; | |||||||||||
| 163 | 10 | ▶ | if ($st->type eq 'TRIPLE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 2 | @nodes = $st->nodes; | |||||||||||
| 165 | } else { | ||||||||||||
| 166 | 8 | my $g = $st->context; | |||||||||||
| 167 | 8 | ▶ | if ($g->is_nil) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 4 | @nodes = ($st->nodes)[0..2]; | |||||||||||
| 169 | } else { | ||||||||||||
| 170 | 4 | @nodes = $st->nodes; | |||||||||||
| 171 | } | ||||||||||||
| 172 | } | ||||||||||||
| 173 | 10 | return join(' ', map { $_->as_ntriples } @nodes) . " .\n"; | |||||||||||
| 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 | 0 | ▶ | sub statement_as_string { | ||||||||||
|
Subroutine
statement_as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 185 | 0 | 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"; | |||||||||||
| 189 | } | ||||||||||||
| 190 | |||||||||||||
| 191 | 68 | 1; | |||||||||||
| 192 | |||||||||||||
| 193 | __END__ | ||||||||||||