| 1 | =head1 NAME | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | AtteanX::Serializer::SPARQLXML - SPARQL Results XML Serializer | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 VERSION | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | This document describes AtteanX::Serializer::SPARQLXML version 0.041 | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | use Attean; | ||||||||||||||||||
| 12 | my $s = Attean->get_serializer('SPARQLXML')->new(); | ||||||||||||||||||
| 13 | $s->serialize_iter_to_io( $fh, $iter ); | ||||||||||||||||||
| 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<< file_extensions >> | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | =back | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | =head1 METHODS | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =over 4 | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | =item C<< canonical_iri >> | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | Returns the canonical IRI of this format. | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | =cut | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 3 | ▶ | use v5.14; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 40 | 3 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 2 | package AtteanX::Serializer::SPARQLXML 0.041 { | |||||||||||||||||
| 43 | 3 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 44 | 3 | ▶ | use Types::Standard qw(Str ArrayRef); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 45 | 3 | ▶ | use Encode qw(encode encode_utf8); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 46 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 47 | 3 | ▶ | use Attean::ListIterator; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 48 | 3 | ▶ | use List::Util qw(any); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 49 | 3 | ▶ | use namespace::clean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 0 | ▶ | sub canonical_iri { return 'http://www.w3.org/ns/formats/SPARQL_Results_XML' } | ||||||||||||||||
|
Subroutine
canonical_iri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 52 | 2 | has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/sparql-results+xml'); | |||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | =item C<< media_types >> | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | Returns a list of media types that identify the format produced by this serializer. | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =cut | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 12 | ▶ | sub media_types { | ||||||||||||||||
|
Subroutine
media_types: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 61 | 12 | return [qw(application/sparql-results+xml)]; | |||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =item C<< file_extensions >> | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | Returns a list of file extensions associated with the serialized format. | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | =cut | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 4 | ▶ | sub file_extensions { return [qw(srx xml)] }; | ||||||||||||||||
|
Subroutine
file_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | =item C<< serialize_iter_to_io( $fh, $iterator ) >> | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the | ||||||||||||||||||
| 75 | L<IO::Handle> object C<< $fh >>. | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | =cut | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 5 | ▶ | sub serialize_iter_to_io { | ||||||||||||||||
|
Subroutine
serialize_iter_to_io: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||
| 80 | 5 | my $self = shift; | |||||||||||||||||
| 81 | 5 | my $fh = shift; | |||||||||||||||||
| 82 | 5 | my $iter = shift; | |||||||||||||||||
| 83 | 5 | print {$fh} <<"END"; | |||||||||||||||||
| 84 | <?xml version="1.0" encoding="utf-8"?> | ||||||||||||||||||
| 85 | <sparql xmlns="http://www.w3.org/2005/sparql-results#"> | ||||||||||||||||||
| 86 | <head> | ||||||||||||||||||
| 87 | END | ||||||||||||||||||
| 88 | 5 | my @vars; | |||||||||||||||||
| 89 | 5 | ▶ | if ($iter->does('Attean::API::ResultIterator')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | 3 | @vars = @{ $iter->variables }; | |||||||||||||||||
| 91 | 3 | foreach my $v (@vars) { | |||||||||||||||||
| 92 | 9 | print $fh qq(\t<variable name="$v"/>\n); | |||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 5 | print {$fh} qq[</head>\n]; | |||||||||||||||||
| 97 | 5 | ▶ | if ($iter->does('Attean::API::TermIterator')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | 2 | my $v = $iter->get_boolean; | |||||||||||||||||
| 99 | 2 | ▶ | my $lex = ($v) ? 'true' : 'false'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | 2 | print $fh qq[<boolean>$lex</boolean>\n]; | |||||||||||||||||
| 101 | } else { | ||||||||||||||||||
| 102 | 3 | print $fh qq[<results>\n]; | |||||||||||||||||
| 103 | 3 | ▶ | while (my $t = $iter->next()) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 9 | print $fh "\t<result>\n"; | |||||||||||||||||
| 105 | 9 | foreach my $name (@vars) { | |||||||||||||||||
| 106 | 27 | my $term = $t->value($name); | |||||||||||||||||
| 107 | 27 | ▶ | if (blessed($term)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 21 | ▶ | if ($term->does('Attean::API::IRI')) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 109 | 9 | my $label = $term->value; | |||||||||||||||||
| 110 | 9 | $label =~ s/&/&/g; | |||||||||||||||||
| 111 | 9 | $label =~ s/</</g; | |||||||||||||||||
| 112 | 9 | $label =~ s/"/"/g; | |||||||||||||||||
| 113 | 9 | $label = encode_utf8($label); | |||||||||||||||||
| 114 | 9 | print $fh qq(\t\t<binding name="${name}"><uri>${label}</uri></binding>\n); | |||||||||||||||||
| 115 | } elsif ($term->does('Attean::API::Literal')) { | ||||||||||||||||||
| 116 | 6 | my $label = $term->value; | |||||||||||||||||
| 117 | 6 | $label =~ s/&/&/g; | |||||||||||||||||
| 118 | 6 | $label =~ s/</</g; | |||||||||||||||||
| 119 | 6 | $label =~ s/"/"/g; | |||||||||||||||||
| 120 | 6 | $label = encode_utf8($label); | |||||||||||||||||
| 121 | 6 | ▶ | if (my $lang = $term->language) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 122 | 3 | $label = qq(<literal xml:lang="${lang}">${label}</literal>); | |||||||||||||||||
| 123 | } elsif (my $dt = $term->datatype) { | ||||||||||||||||||
| 124 | 3 | $label = qq(<literal datatype=") . $dt->value . qq(">${label}</literal>); | |||||||||||||||||
| 125 | } else { | ||||||||||||||||||
| 126 | 0 | $label = qq(<literal>${label}</literal>); | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | 6 | print $fh qq(\t\t<binding name="${name}">${label}</binding>\n); | |||||||||||||||||
| 129 | } elsif ($term->does('Attean::API::Blank')) { | ||||||||||||||||||
| 130 | 6 | my $label = $term->value; | |||||||||||||||||
| 131 | 6 | $label =~ s/&/&/g; | |||||||||||||||||
| 132 | 6 | $label =~ s/</</g; | |||||||||||||||||
| 133 | 6 | $label =~ s/"/"/g; | |||||||||||||||||
| 134 | 6 | $label = encode_utf8($label); | |||||||||||||||||
| 135 | 6 | print $fh qq(\t\t<binding name="${name}"><bnode>${label}</bnode></binding>\n); | |||||||||||||||||
| 136 | } else { | ||||||||||||||||||
| 137 | 0 | die "Term object has an unrecognized type: " . ref($term); | |||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | 9 | print $fh "\t</result>\n"; | |||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | 3 | print {$fh} "</results>\n"; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | 5 | print {$fh} "</sparql>\n"; | |||||||||||||||||
| 146 | 5 | return; | |||||||||||||||||
| 147 | } | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | =item C<< serialize_iter_to_bytes( $iterator ) >> | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | Serializes the L<Attean::API::Binding> objects from C<< $iterator >> | ||||||||||||||||||
| 152 | and returns the serialization as a UTF-8 encoded byte string. | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | =cut | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | 2 | ▶ | sub serialize_iter_to_bytes { | ||||||||||||||||
|
Subroutine
serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 157 | 2 | my $self = shift; | |||||||||||||||||
| 158 | 2 | my $iter = shift; | |||||||||||||||||
| 159 | 2 | my $data = encode('UTF-8', ''); | |||||||||||||||||
| 160 | 2 | open(my $fh, '>', \$data); | |||||||||||||||||
| 161 | 2 | $self->serialize_iter_to_io($fh, $iter); | |||||||||||||||||
| 162 | 2 | close($fh); | |||||||||||||||||
| 163 | 2 | return $data; | |||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 2 | with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer'; | |||||||||||||||||
| 167 | } | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | 2 | 1; | |||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||