| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | AtteanX::Serializer::Turtle - Turtle Serializer | ||||||||||
| 4 | |||||||||||
| 5 | =head1 VERSION | ||||||||||
| 6 | |||||||||||
| 7 | This document describes AtteanX::Serializer::Turtle version 0.041 | ||||||||||
| 8 | |||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||
| 10 | |||||||||||
| 11 | use Attean; | ||||||||||
| 12 | my $serializer = Attean->get_serializer('Turtle')->new(); | ||||||||||
| 13 | $serializer->serialize_iter_to_io( $io, $fh ); | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | ... | ||||||||||
| 18 | |||||||||||
| 19 | =head1 ATTRIBUTES | ||||||||||
| 20 | |||||||||||
| 21 | =over 4 | ||||||||||
| 22 | |||||||||||
| 23 | =item C<< canonical_media_type >> | ||||||||||
| 24 | |||||||||||
| 25 | =item C<< media_types >> | ||||||||||
| 26 | |||||||||||
| 27 | =item C<< handled_type >> | ||||||||||
| 28 | |||||||||||
| 29 | =item C<< file_extensions >> | ||||||||||
| 30 | |||||||||||
| 31 | =back | ||||||||||
| 32 | |||||||||||
| 33 | =head1 METHODS | ||||||||||
| 34 | |||||||||||
| 35 | =over 4 | ||||||||||
| 36 | |||||||||||
| 37 | =item C<< canonical_iri >> | ||||||||||
| 38 | |||||||||||
| 39 | Returns the canonical IRI of this format. | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 2 | package AtteanX::Serializer::Turtle 0.041 { | |||||||||
| 47 | 3 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 3 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 3 | ▶ | use Encode qw(encode); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 3 | ▶ | use Attean::ListIterator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 3 | ▶ | use List::Util qw(any); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 3 | ▶ | use AtteanX::Parser::Turtle::Constants; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 3 | ▶ | use AtteanX::Parser::Turtle::Token; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 3 | ▶ | use AtteanX::Serializer::TurtleTokens; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 3 | ▶ | use Types::Standard qw(InstanceOf HashRef ArrayRef Bool Str); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 3 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 2 | with 'Attean::API::AbbreviatingSerializer'; | |||||||||
| 58 | 2 | with 'Attean::API::AppendableSerializer'; | |||||||||
| 59 | 2 | with 'Attean::API::TripleSerializer'; | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub canonical_iri { return 'http://www.w3.org/ns/formats/Turtle' } | ||||||||
|
Subroutine
canonical_iri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 1 | ▶ | sub canonical_media_type { return "text/turtle" } | ||||||||
|
Subroutine
canonical_media_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | |||||||||||
| 64 | 13 | ▶ | sub media_types { | ||||||||
|
Subroutine
media_types: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 65 | 13 | return [qw(text/turtle)]; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | sub handled_type { | ||||||||
|
Subroutine
handled_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 5 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Triple'); | |||||||||
| 70 | 5 | return $ITEM_TYPE; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | =item C<< file_extensions >> | ||||||||||
| 74 | |||||||||||
| 75 | Returns a list of file extensions associated with the serialized format. | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 2 | ▶ | sub file_extensions { return [qw(ttl)] } | ||||||||
|
Subroutine
file_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | |||||||||||
| 81 | 2 | has 'serializer' => (is => 'rw', isa => InstanceOf['AtteanX::Serializer::TurtleTokens']); | |||||||||
| 82 | |||||||||||
| 83 | 9 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 84 | 9 | my $self = shift; | |||||||||
| 85 | 9 | my $s = $self->serializer; | |||||||||
| 86 | 9 | ▶ | unless ($s) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 9 | my @args; | |||||||||
| 88 | 9 | ▶ | if (my $map = $self->namespaces) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 4 | push(@args, namespaces => $map); | |||||||||
| 90 | } | ||||||||||
| 91 | 9 | $s = AtteanX::Serializer::TurtleTokens->new( @args ); | |||||||||
| 92 | 9 | $self->serializer($s); | |||||||||
| 93 | } | ||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =item C<< serialize_iter_to_io( $fh, $iterator ) >> | ||||||||||
| 97 | |||||||||||
| 98 | Serializes the Turtle token objects from C<< $iterator >> to the | ||||||||||
| 99 | L<IO::Handle> object C<< $fh >>. | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||
| 102 | |||||||||||
| 103 | 12 | ▶ | sub serialize_iter_to_io { | ||||||||
|
Subroutine
serialize_iter_to_io: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 104 | 12 | my $self = shift; | |||||||||
| 105 | 12 | my $io = shift; | |||||||||
| 106 | 12 | my $iter = shift; | |||||||||
| 107 | 12 | my @buffer; | |||||||||
| 108 | |||||||||||
| 109 | # TODO: look for shared subject-predicate in repeated triples, and emit COMMA syntax | ||||||||||
| 110 | # TODO: look for shared subject in repeated triples, and emit SEMICOLON syntax | ||||||||||
| 111 | |||||||||||
| 112 | 12 | my $dot = AtteanX::Parser::Turtle::Token->dot; | |||||||||
| 113 | 12 | my $comma = AtteanX::Parser::Turtle::Token->comma; | |||||||||
| 114 | 12 | my $semi = AtteanX::Parser::Turtle::Token->semicolon; | |||||||||
| 115 | 12 | ▶ | if (my $map = $self->namespaces) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 4 | my $prefix = AtteanX::Parser::Turtle::Token->prefix; | |||||||||
| 117 | 4 | foreach my $ns (sort $map->list_prefixes) { | |||||||||
| 118 | 5 | my $uri = Attean::IRI->new( value => $map->namespace_uri($ns)->as_string ); | |||||||||
| 119 | 5 | my $name = AtteanX::Parser::Turtle::Token->fast_constructor( PREFIXNAME, -1, -1, -1, -1, ["${ns}:"] ); | |||||||||
| 120 | 5 | my $iri = AtteanX::Parser::Turtle::Token->fast_constructor( IRI, -1, -1, -1, -1, [$uri->value] ); | |||||||||
| 121 | 5 | push(@buffer, $prefix); | |||||||||
| 122 | 5 | push(@buffer, $name); | |||||||||
| 123 | 5 | push(@buffer, $iri); | |||||||||
| 124 | 5 | push(@buffer, $dot); | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 12 | my $last_subj; | |||||||||
| 129 | my $last_pred; | ||||||||||
| 130 | my $sub = sub { | ||||||||||
| 131 | if (scalar(@buffer)) { | ||||||||||
| 132 | return shift(@buffer); | ||||||||||
| 133 | } | ||||||||||
| 134 | if (my $t = $iter->next) { | ||||||||||
| 135 | my ($subj, $pred, $obj) = $t->values; | ||||||||||
| 136 | if (defined($last_subj) and $subj->equals($last_subj)) { | ||||||||||
| 137 | if (defined($last_pred) and $pred->equals($last_pred)) { | ||||||||||
| 138 | push(@buffer, $comma); | ||||||||||
| 139 | push(@buffer, $obj->sparql_tokens->elements); | ||||||||||
| 140 | } else { | ||||||||||
| 141 | push(@buffer, $semi); | ||||||||||
| 142 | push(@buffer, $pred->sparql_tokens->elements); | ||||||||||
| 143 | push(@buffer, $obj->sparql_tokens->elements); | ||||||||||
| 144 | } | ||||||||||
| 145 | } else { | ||||||||||
| 146 | if (defined($last_pred)) { | ||||||||||
| 147 | push(@buffer, $dot); | ||||||||||
| 148 | } | ||||||||||
| 149 | foreach my $term ($subj, $pred, $obj) { | ||||||||||
| 150 | push(@buffer, $term->sparql_tokens->elements); | ||||||||||
| 151 | } | ||||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | $last_subj = $subj; | ||||||||||
| 155 | $last_pred = $pred; | ||||||||||
| 156 | return shift(@buffer); | ||||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | if (defined($last_subj)) { | ||||||||||
| 160 | push(@buffer, $dot); | ||||||||||
| 161 | $last_subj = undef; | ||||||||||
| 162 | $last_pred = undef; | ||||||||||
| 163 | return shift(@buffer); | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | return; | ||||||||||
| 167 | 12 | }; | |||||||||
| 168 | |||||||||||
| 169 | 12 | my $titer = Attean::CodeIterator->new( generator => $sub, item_type => 'AtteanX::Parser::Turtle::Token' ); | |||||||||
| 170 | 12 | return $self->serializer->serialize_iter_to_io($io, $titer); | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | =item C<< serialize_iter_to_bytes( $iterator ) >> | ||||||||||
| 174 | |||||||||||
| 175 | Serializes the Turtle token objects from C<< $iterator >> | ||||||||||
| 176 | and returns the serialization as a UTF-8 encoded byte string. | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||
| 179 | |||||||||||
| 180 | 10 | ▶ | sub serialize_iter_to_bytes { | ||||||||
|
Subroutine
serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 181 | 10 | my $self = shift; | |||||||||
| 182 | 10 | my $iter = shift; | |||||||||
| 183 | 10 | my $data = encode('UTF-8', ''); | |||||||||
| 184 | 10 | open(my $fh, '>', \$data); | |||||||||
| 185 | 10 | $self->serialize_iter_to_io($fh, $iter); | |||||||||
| 186 | 10 | close($fh); | |||||||||
| 187 | 10 | return $data; | |||||||||
| 188 | } | ||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | 2 | 1; | |||||||||
| 192 | |||||||||||
| 193 | __END__ | ||||||||||