line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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.032 |
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
|
|
|
3
|
|
10385
|
use v5.14; |
|
3
|
|
|
|
|
15
|
|
36
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
154
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use Moo; |
39
|
3
|
|
|
3
|
|
17
|
use Types::Standard qw(Str ArrayRef); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
30
|
|
40
|
3
|
|
|
3
|
|
1161
|
use Encode qw(encode encode_utf8); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
47
|
|
41
|
3
|
|
|
3
|
|
2205
|
use Scalar::Util qw(blessed); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
206
|
|
42
|
3
|
|
|
3
|
|
18
|
use Attean::ListIterator; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
112
|
|
43
|
3
|
|
|
3
|
|
17
|
use List::MoreUtils qw(any); |
|
3
|
|
|
|
|
15
|
|
|
3
|
|
|
|
|
83
|
|
44
|
3
|
|
|
3
|
|
17
|
use namespace::clean; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
29
|
|
45
|
3
|
|
|
3
|
|
2874
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
29
|
|
46
|
|
|
|
|
|
|
has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/sparql-results+xml'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item C<< media_types >> |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Returns a list of media types that identify the format produced by this serializer. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return [qw(application/sparql-results+xml)]; |
55
|
|
|
|
|
|
|
} |
56
|
9
|
|
|
9
|
1
|
15
|
|
57
|
|
|
|
|
|
|
=item C<< file_extensions >> |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Returns a list of file extensions associated with the serialized format. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item C<< serialize_iter_to_io( $fh, $iterator ) >> |
65
|
5
|
|
|
5
|
1
|
23
|
|
66
|
|
|
|
|
|
|
Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the |
67
|
|
|
|
|
|
|
L<IO::Handle> object C<< $fh >>. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $self = shift; |
72
|
|
|
|
|
|
|
my $fh = shift; |
73
|
|
|
|
|
|
|
my $iter = shift; |
74
|
|
|
|
|
|
|
print {$fh} <<"END"; |
75
|
|
|
|
|
|
|
<?xml version="1.0" encoding="utf-8"?> |
76
|
|
|
|
|
|
|
<sparql xmlns="http://www.w3.org/2005/sparql-results#"> |
77
|
|
|
|
|
|
|
<head> |
78
|
|
|
|
|
|
|
END |
79
|
|
|
|
|
|
|
my @vars = @{ $iter->variables }; |
80
|
|
|
|
|
|
|
if ($iter->does('Attean::API::ResultIterator')) { |
81
|
|
|
|
|
|
|
foreach my $v (@vars) { |
82
|
|
|
|
|
|
|
print $fh qq(\t<variable name="$v"/>\n); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
print {$fh} <<"END"; |
87
|
|
|
|
|
|
|
</head> |
88
|
|
|
|
|
|
|
<results> |
89
|
|
|
|
|
|
|
END |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
while (my $t = $iter->next()) { |
92
|
|
|
|
|
|
|
print $fh "\t\t<result>\n"; |
93
|
|
|
|
|
|
|
foreach my $name (@vars) { |
94
|
|
|
|
|
|
|
my $term = $t->value($name); |
95
|
|
|
|
|
|
|
if (blessed($term)) { |
96
|
|
|
|
|
|
|
if ($term->does('Attean::API::IRI')) { |
97
|
|
|
|
|
|
|
my $label = $term->value; |
98
|
|
|
|
|
|
|
$label =~ s/&/&/g; |
99
|
|
|
|
|
|
|
$label =~ s/</</g; |
100
|
|
|
|
|
|
|
$label =~ s/"/"/g; |
101
|
|
|
|
|
|
|
$label = encode_utf8($label); |
102
|
|
|
|
|
|
|
print $fh qq(\t\t\t<binding name="${name}"><uri>${label}</uri></binding>\n); |
103
|
|
|
|
|
|
|
} elsif ($term->does('Attean::API::Literal')) { |
104
|
|
|
|
|
|
|
my $label = $term->value; |
105
|
|
|
|
|
|
|
$label =~ s/&/&/g; |
106
|
|
|
|
|
|
|
$label =~ s/</</g; |
107
|
|
|
|
|
|
|
$label =~ s/"/"/g; |
108
|
|
|
|
|
|
|
$label = encode_utf8($label); |
109
|
|
|
|
|
|
|
if (my $lang = $term->language) { |
110
|
|
|
|
|
|
|
$label = qq(<literal xml:lang="${lang}">${label}</literal>); |
111
|
|
|
|
|
|
|
} elsif (my $dt = $term->datatype) { |
112
|
|
|
|
|
|
|
$label = qq(<literal datatype=") . $dt->value . qq(">${label}</literal>); |
113
|
|
|
|
|
|
|
} else { |
114
|
|
|
|
|
|
|
$label = qq(<literal>${label}</literal>); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
print $fh qq(\t\t\t<binding name="${name}">${label}</binding>\n); |
117
|
|
|
|
|
|
|
} elsif ($term->does('Attean::API::Blank')) { |
118
|
|
|
|
|
|
|
my $label = $term->value; |
119
|
|
|
|
|
|
|
$label =~ s/&/&/g; |
120
|
|
|
|
|
|
|
$label =~ s/</</g; |
121
|
|
|
|
|
|
|
$label =~ s/"/"/g; |
122
|
|
|
|
|
|
|
$label = encode_utf8($label); |
123
|
|
|
|
|
|
|
print $fh qq(\t\t\t<binding name="${name}"><bnode>${label}</bnode></binding>\n); |
124
|
|
|
|
|
|
|
} else { |
125
|
|
|
|
|
|
|
die "Term object has an unrecognized type: " . ref($term); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
print $fh "\t\t</result>\n"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
print {$fh} "</results>\n"; |
132
|
|
|
|
|
|
|
print {$fh} "</sparql>\n"; |
133
|
|
|
|
|
|
|
return; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item C<< serialize_iter_to_bytes( $iterator ) >> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Serializes the L<Attean::API::Binding> objects from C<< $iterator >> |
139
|
|
|
|
|
|
|
and returns the serialization as a UTF-8 encoded byte string. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $self = shift; |
144
|
|
|
|
|
|
|
my $iter = shift; |
145
|
|
|
|
|
|
|
my $data = encode('UTF-8', ''); |
146
|
|
|
|
|
|
|
open(my $fh, '>', \$data); |
147
|
|
|
|
|
|
|
$self->serialize_iter_to_io($fh, $iter); |
148
|
|
|
|
|
|
|
close($fh); |
149
|
|
|
|
|
|
|
return $data; |
150
|
|
|
|
|
|
|
} |
151
|
1
|
|
|
1
|
|
7
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
152
|
|
|
|
|
|
|
with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer'; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=back |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 BUGS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Please report any bugs or feature requests to through the GitHub web interface |
163
|
|
|
|
|
|
|
at L<https://github.com/kasei/perlrdf/issues>. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHOR |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Gregory Todd Williams C<< <gwilliams@cpan.org> >> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Copyright (c) 2014--2022 Gregory Todd Williams. This |
172
|
|
|
|
|
|
|
program is free software; you can redistribute it and/or modify it under |
173
|
|
|
|
|
|
|
the same terms as Perl itself. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |