AtteanX/Serializer/RDFXML.pm

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