AtteanX/Serializer/RDFXML.pm

filter: Statementstmt 89.5% 172 / 192 Branchbran 72.0% 36 / 50 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 85.0% 233 / 274 CC 35 SCAR 27.3
CC
35
Cov
85%
_statements_same_subject_as_string
30.7
_top_xmlns
7.1
serialize_iter_to_io
6.0
CRAP 15.3
« Attean/API.pm ↑ Summary Attean/API/QueryPlanner.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::RDFXML - RDF/XML Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::RDFXML version 0.040
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $s = Attean->get_serializer('RDFXML')->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<< scoped_namespaces >>
26  
27 =item C<< file_extensions >>
28  
29 =item C<< file_extensions >>
30  
31 =item C<< blank_nodes >>
32  
33 =back
34  
35 =head1 METHODS
36  
37 =over 4
38  
39 =cut
40  
41 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 package AtteanX::Serializer::RDFXML 0.040 {
45 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶ use Types::Standard qw(Bool HashRef ArrayRef HashRef Str Maybe InstanceOf ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶ use Attean::API::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 4 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 4 has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/rdf+xml');
55 7 ▶ has '_rev' => (is => 'rw', isa => HashRef, init_arg => undef, default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
56 4 has 'scoped_namespaces' => (is => 'rw', init_arg => undef);
57 7 ▶ has 'blank_nodes' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Blank']], default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 =item C<< file_extensions >>
60  
61 Returns a list of file extensions associated with the serialized format.
62  
63 =cut
64  
65 14 ▶ sub file_extensions { return [qw(rdf xml)] }
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
66
67 =item C<< media_types >>
68  
69 Returns a list of media types that identify the format produced by this serializer.
70  
71 =cut
72  
73 14 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
74 14 return [qw(application/rdf+xml)];
75 }
76
77 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
78  
79 Serializes the L<Attean::API::Triple> objects from C<< $iterator >> to the
80 L<IO::Handle> object C<< $fh >> (which SHOULD be open with the UTF-8 encoding).
81  
82 =cut
83  
84 10 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=6 SCAR=17.9
Pod documented
85 10 my $self = shift;
86 10 my $io = shift;
87 10 my $iter = shift;
88
89 10 my $ns = $self->_top_xmlns();
90 10 my $base_uri = '';
91 10 ▶ if ($self->{base_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"base_uri"}) 0 10 10
92 0 $base_uri = "xml:base=\"$self->{base_uri}\" ";
93 }
94 10 print {$io} qq[<?xml version="1.0" encoding="utf-8"?>\n<rdf:RDF ${base_uri}xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"$ns>\n];
95
96 10 my $st = $iter->next;
97 10 my @statements;
98 10 ▶ push(@statements, $st) if blessed($st);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($st) 10 0 10
99 10 ▶ while (@statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@statements) 12 10 22
100 12 my $st = shift(@statements);
101 12 my @samesubj;
102 12 push(@samesubj, $st);
103 12 my $subj = $st->subject;
104 12 ▶ while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 14 10 24
105 14 ▶ if ($row->subject->equals( $subj )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->subject->equals($subj)) { } 12 2 14
106 12 push(@samesubj, $row);
107 } else {
108 2 push(@statements, $row);
109 2 last;
110 }
111 }
112
113 12 print {$io} $self->_statements_same_subject_as_string( @samesubj );
114 }
115
116 10 print {$io} qq[</rdf:RDF>\n];
117 10 return;
118 }
119
120 =item C<< serialize_iter_to_bytes( $iterator ) >>
121  
122 Serializes the L<Attean::API::Triple> objects from C<< $iterator >>
123 and returns the serialization as a UTF-8 encoded byte string.
124  
125 =cut
126  
127 8 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod documented
128 8 my $self = shift;
129 8 my $iter = shift;
130 8 my $data = '';
131 8 open(my $fh, '>:utf8', \$data);
132 8 $self->serialize_iter_to_io($fh, $iter);
133 8 close($fh);
134 8 return $data;
135 }
136  
137 12 ▶ sub _statements_same_subject_as_string {
Subroutine _statements_same_subject_as_string: called CC=23 SCAR=34.2
138 12 my $self = shift;
139 12 my @statements = @_;
140 12 my $s = $statements[0]->subject;
141
142 12 my $id;
143 12 ▶ if ($s->does('Attean::API::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->does('Attean::API::Blank')) { } 10 2 12
144 10 my $b;
145 10 ▶ if (my $bb = $self->blank_nodes->{$s->value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bb = $self->blank_nodes->{$s->value}) { } 5 5 10
146 5 $b = $bb->value;
147 } else {
148 5 my $label = 'b' . $s->value;
149 5 $self->blank_nodes->{$s->value} = Attean::Blank->new( value => $label );
150 5 $b = $label;
151 }
152 # my $b = 'b' . $s->value;
153 10 $id = qq[rdf:nodeID="$b"];
154 } else {
155 2 my $i = $s->abs;
156 2 for ($i) {
157 2 s/&/&amp;/g;
158 2 s/</&lt;/g;
159 2 s/"/&quot;/g;
160 }
161 2 $id = qq[rdf:about="$i"];
162 }
163
164 12 my $counter = 1;
165 12 my %namespaces = %{ $self->_rev };
166 12 my $string = '';
167 12 foreach my $st (@statements) {
168 24 my (undef, $p, $o) = $st->values;
169 24 my %used_namespaces;
170 24 my ($ns, $ln);
171 24 eval {
172 24 ($ns,$ln) = $self->_qname($p);
173 };
174 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
175 0 my $uri = $p->abs;
176 0 die "Can't turn predicate $uri into a QName.";
177 }
178 24 $used_namespaces{ $ns }++;
179 24 ▶ unless (exists $namespaces{ $ns }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $namespaces{$ns}) 11 13 24
180 11 $namespaces{ $ns } = 'ns' . $counter++;
181 }
182
183 24 my $prefix = $namespaces{ $ns };
184 24 my $nsdecl = '';
185 24 ▶ if ($self->scoped_namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scoped_namespaces) 0 24 24
186 0 $nsdecl = qq[ xmlns:$prefix="$ns"];
187 }
188 24 ▶ if ($o->does('Attean::API::Literal')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($o->does('Attean::API::Literal')) { } 22 2 24
elsif ($o->does('Attean::API::Blank')) { } 0 2 2
189 {
190 22 my $dt = $o->datatype();
191 22 ▶ if (blessed($dt) and ($dt->value eq 'http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map' or $dt->value eq 'http://w3id.org/awslabs/neptune/SPARQL-CDTs/List')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($dt) and $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map" || $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List") 0 22 22
Condition33%2/6
builtin::blessed($dt) and $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map" || $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List"
!ll&&!rl&&r
0220
$dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map" or $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List"
l!l&&r!l&&!r
0022
MC/DC0%0/3
builtin::blessed($dt) and $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map" || $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List"
builtin::blessed($dt) $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map" $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List"
Truth table25%1/4
A and B or C
A= builtin::blessed($dt)B= $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/Map"C= $dt->value eq "http://w3id.org/awslabs/neptune/SPARQL-CDTs/List"
ABCresult
0XX0
1000
11X1
1011
192 0 my $parse_id = '1';
193 0 $o = AtteanX::Functions::CompositeLists::rewrite_lexical($o, $self->blank_nodes, $parse_id);
194 }
195 }
196
197 22 my $lv = $o->value;
198 22 for ($lv) {
199 22 s/&/&amp;/g;
200 22 s/</&lt;/g;
201 22 s/"/&quot;/g;
202 }
203 22 my $lang = $o->language;
204 22 my $dt = $o->datatype->value;
205 22 my $tag = join(':', $prefix, $ln);
206
207 22 ▶ if ($lang) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang) { } 2 20 22
elsif ($dt) { } 20 0 20
208 2 $string .= qq[\t<${tag}${nsdecl} xml:lang="${lang}">${lv}</${tag}>\n];
209 } elsif ($dt) {
210 20 ▶ if ($dt eq 'http://www.w3.org/2001/XMLSchema#string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt eq 'http://www.w3.org/2001/XMLSchema#string') { } 4 16 20
211 4 $string .= qq[\t<${tag}${nsdecl}>${lv}</${tag}>\n];
212 } else {
213 16 $string .= qq[\t<${tag}${nsdecl} rdf:datatype="${dt}">${lv}</${tag}>\n];
214 }
215 } else {
216 0 $string .= qq[\t<${tag}${nsdecl}>${lv}</${tag}>\n];
217 }
218 } elsif ($o->does('Attean::API::Blank')) {
219 0 my $b;
220 0 ▶ if (my $bb = $self->blank_nodes->{$s->value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bb = $self->blank_nodes->{$s->value}) { } 0 0 0
221 0 $b = $bb->value;
222 } else {
223 0 my $label = 'b' . $s->value;
224 0 $self->blank_nodes->{$s->value} = Attean::Blank->new( value => $label );
225 0 $b = $label;
226 }
227 # my $b = 'b' . $o->value;
228 0 for ($b) {
229 0 s/&/&amp;/g;
230 0 s/</&lt;/g;
231 0 s/"/&quot;/g;
232 }
233 0 $string .= qq[\t<${prefix}:$ln${nsdecl} rdf:nodeID="$b"/>\n];
234 } else {
235 2 my $u = $o->abs;
236 2 for ($u) {
237 2 s/&/&amp;/g;
238 2 s/</&lt;/g;
239 2 s/"/&quot;/g;
240 }
241 2 $string .= qq[\t<${prefix}:$ln${nsdecl} rdf:resource="$u"/>\n];
242 }
243 }
244
245 12 $string .= qq[</rdf:Description>\n];
246
247 # rdf namespace is already defined in the <rdf:RDF> tag, so ignore it here
248 12 my %seen = %{ $self->_rev };
249 12 my @ns;
250 0 foreach my $uri (sort { $namespaces{$a} cmp $namespaces{$b} } grep { not($seen{$_}) } (keys %namespaces)) {
251 11 my $ns = $namespaces{$uri};
252 11 ▶ my $str = ($ns eq '') ? qq[xmlns="$uri"] : qq[xmlns:${ns}="$uri"];
Branch: 1 branch
BranchTrueFalseTotal
$ns eq '' ? : 0 11 11
253 11 push(@ns, $str);
254 }
255 12 my $ns = join(' ', @ns);
256 12 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) { } 11 1 12
257 11 return qq[<rdf:Description ${ns} $id>\n] . $string;
258 } else {
259 1 return qq[<rdf:Description $id>\n] . $string;
260 }
261 }
262  
263 24 ▶ sub _qname {
Subroutine _qname: called CC=2 SCAR=7.0
264 24 my $self = shift;
265 24 my $p = shift;
266 24 my $uri = $p->abs;
267  
268 24 state $r_PN_CHARS_BASE = qr/([A-Z]|[a-z]|[\x{00C0}-\x{00D6}]|[\x{00D8}-\x{00F6}]|[\x{00F8}-\x{02FF}]|[\x{0370}-\x{037D}]|[\x{037F}-\x{1FFF}]|[\x{200C}-\x{200D}]|[\x{2070}-\x{218F}]|[\x{2C00}-\x{2FEF}]|[\x{3001}-\x{D7FF}]|[\x{F900}-\x{FDCF}]|[\x{FDF0}-\x{FFFD}]|[\x{10000}-\x{EFFFF}])/o;
269 24 state $r_PN_CHARS_U = qr/(_|${r_PN_CHARS_BASE})/o;
270 24 state $r_PN_CHARS = qr/${r_PN_CHARS_U}|-|[0-9]|\x{00B7}|[\x{0300}-\x{036F}]|[\x{203F}-\x{2040}]/o;
271 24 state $r_PN_LOCAL = qr/((${r_PN_CHARS_U})((${r_PN_CHARS}|[.])*${r_PN_CHARS})?)/o;
272 24 ▶ if ($uri =~ m/${r_PN_LOCAL}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /$r_PN_LOCAL$/ou) { } 24 0 24
273 24 my $ln = $1;
274 24 my $ns = substr($uri, 0, length($uri)-length($ln));
275 24 return ($ns, $ln);
276 } else {
277 0 die "Can't turn IRI $uri into a QName.";
278 }
279 }
280  
281 10 ▶ sub _top_xmlns {
Subroutine _top_xmlns: called CC=7 SCAR=19.6
282 10 my $self = shift;
283 10 my $namespaces = $self->namespaces;
284 10 ▶ return '' if ($self->scoped_namespaces);
Branch: 1 branch
BranchTrueFalseTotal
if $self->scoped_namespaces 0 10 10
285
286 10 my @ns;
287 10 ▶ my @prefixes = $namespaces ? $namespaces->list_prefixes : ();
Branch: 1 branch
BranchTrueFalseTotal
$namespaces ? : 4 6 10
288 10 foreach my $k (sort { $a cmp $b } @prefixes) {
289 5 my $v = $namespaces->namespace_uri($k)->as_string;
290 5 $self->_rev->{$v} = $k;
291 5 ▶ next if ($v eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#" 0 5 5
292 5 ▶ my $str = ($k eq '') ? qq[xmlns="$v"] : qq[xmlns:$k="$v"];
Branch: 1 branch
BranchTrueFalseTotal
$k eq '' ? : 0 5 5
293 5 push(@ns, $str);
294 }
295 10 my $ns = join(' ', @ns);
296 10 ▶ if (length($ns)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ns) 4 6 10
297 4 $ns = " $ns";
298 }
299 10 return $ns;
300 }
301  
302 4 with 'Attean::API::TripleSerializer';
303 4 with 'Attean::API::AbbreviatingSerializer';
304 }
305  
306 4 1;
307  
308 __END__
« Attean/API.pm ↑ Summary Attean/API/QueryPlanner.pm »