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