AtteanX/Serializer/SPARQL.pm

filter: Statementstmt 82.9% 136 / 164 Branchbran 86.7% 59 / 68 Conditioncond 81.4% 22 / 27 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 84.0% 248 / 295 CC 46 SCAR 38.5
CC
46
Cov
84%
serialize_iter_to_io
61.8
BEGIN
1.0
BEGIN
1.0
CRAP 46.8
« AtteanX/Parser/SPARQL.pm ↑ Summary AtteanX/Parser/NTuples.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::SPARQL - SPARQL Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::SPARQL version 0.040
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $serializer = Attean->get_serializer('SPARQL')->new();
13 $serializer->serialize_iter_to_io( $io, $fh );
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<< media_types >>
26  
27 =item C<< handled_type >>
28  
29 =item C<< file_extensions >>
30  
31 =back
32  
33 =head1 METHODS
34  
35 =over 4
36  
37 =cut
38  
39 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 46 package AtteanX::Serializer::SPARQL 0.040 {
43 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 50 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 50 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 50 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 50 ▶ use AtteanX::SPARQL::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 46 with 'Attean::API::AbbreviatingSerializer';
52
53 1 ▶ sub canonical_media_type { return "application/sparql-query" }
Subroutine canonical_media_type: called CC=1 SCAR=0.0
Pod documented
54  
55 14 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
56 14 return [qw(application/sparql-query)];
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 12 ▶ sub file_extensions { return [qw(rq ru)] };
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
66
67 1 ▶ sub handled_type {
Subroutine handled_type: called CC=1 SCAR=0.0
Pod documented
68 1 state $ITEM_TYPE = Type::Tiny::Role->new(role => 'AtteanX::SPARQL::Token');
69 1 return $ITEM_TYPE;
70 }
71  
72 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
73  
74 Serializes the SPARQL token objects from C<< $iterator >> to the
75 L<IO::Handle> object C<< $fh >>.
76  
77 =cut
78  
79 56 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=46 SCAR=41.2
Pod documented
80 56 my $self = shift;
81 56 my $io = shift;
82 56 my $iter = shift;
83 56 my $indent = 0;
84 56 my $newline = 1;
85 56 my $semicolon = 0;
86 56 my $need_space = 0;
87 56 my $ns = $self->namespaces;
88 56 my $parser = Attean->get_parser('SPARQLLex')->new();
89
90 56 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) 2 54 56
91 2 NSLOOP: foreach my $p (sort $ns->list_prefixes) {
92 3 my $prefix = $ns->namespace_uri($p)->as_string;
93 3 $io->print("PREFIX $p: <$prefix>\n");
94 }
95 }
96
97 56 my $last;
98
99 56 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 594 56 650
100 594 my $type = $t->type;
101
102 594 ▶ if ($type == LANG or $type == HATHAT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 15 or $type == 12) 3 591 594
Condition100%3/3
$type == 15 or $type == 12
l!l&&r!l&&!r
21591
MC/DC100%2/2
$type == 15 or $type == 12
$type == 15 $type == 12
Truth table100%3/3
A or B
A= $type == 15B= $type == 12
ABresult
1X1
011
000
103 3 $need_space= 0;
104 }
105
106 594 ▶ unless ($newline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newline) 435 159 594
107 435 ▶ if ($type == RBRACE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 22) { } 4 431 435
elsif ($type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/u) { } 17 414 431
elsif ($type == 47 and $t->value eq 'WHERE' and builtin::blessed($last) and $last->type == 21 || $last->type == 14) { } 8 797 805
108 4 $io->print("\n");
109 4 $newline = 1;
110 ▶ } elsif ($type == KEYWORD and $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/) {
Condition100%3/3
$type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/u
!ll&&!rl&&r
3674717
MC/DC100%2/2
$type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/u
$type == 47 $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/u
Truth table100%3/3
A and B
A= $type == 47B= $t->value =~ /^(BASE|PREFIX|SELECT|ASK|CONSTRUCT|DESCRIBE|USING|FROM)$/u
ABresult
0X0
100
111
111 17 $io->print("\n");
112 17 $newline = 1;
113 ▶ } elsif ($type == KEYWORD and $t->value eq 'WHERE' and blessed($last) and ($last->type == PREFIXNAME or $last->type == IRI)) {
Condition83%10/12
$type == 47 and $t->value eq "WHERE" and builtin::blessed($last) and $last->type == 21 || $last->type == 14
!ll&&!rl&&r
391158
$type == 47 and $t->value eq "WHERE" and builtin::blessed($last)
!ll&&!rl&&r
391023
$type == 47 and $t->value eq "WHERE"
!ll&&!rl&&r
3672423
$last->type == 21 or $last->type == 14
l!l&&r!l&&!r
0815
MC/DC60%3/5
$type == 47 and $t->value eq "WHERE" and builtin::blessed($last) and $last->type == 21 || $last->type == 14
$type == 47 $t->value eq "WHERE" builtin::blessed($last) $last->type == 21 $last->type == 14
Truth table66%4/6
A and B and C and D or E
A= $type == 47B= $t->value eq "WHERE"C= builtin::blessed($last)D= $last->type == 21E= $last->type == 14
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
114 # this captures "USING <g> WHERE" and "USING NAMED <g> WHERE", forcing a newline before the "WHERE"
115 8 $io->print("\n");
116 8 $newline = 1;
117 }
118 }
119
120 594 ▶ if ($type == RBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 22) 49 545 594
121 49 $indent--;
122 }
123
124 594 ▶ if ($semicolon and $type == KEYWORD and $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($semicolon and $type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/u) 1 593 594
Condition66%4/6
$semicolon and $type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/u
!ll&&!rl&&r
59301
$semicolon and $type == 47
!ll&&!rl&&r
59301
MC/DC33%1/3
$semicolon and $type == 47 and $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/u
$semicolon $type == 47 $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/u
Truth table50%2/4
A and B and C
A= $semicolonB= $type == 47C= $t->value =~ /^(BASE|PREFIX|SELECT|ADD|COPY|MOVE|USING|LOAD|DELETE|INSERT|WITH|CLEAR|DROP|CREATE)$/u
ABCresult
0XX0
10X0
1100
1111
125 # SPARQL Update use of a semicolon is different from its use in a Query;
126 # In queries, semicolon affects indentation. In updates, it's just a separator.
127 # So back out the indentation if it's being used as a separator here.
128 1 $semicolon = 0;
129 1 $indent--;
130 }
131
132 594 ▶ if ($newline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($newline) { } 188 406 594
elsif ($need_space) { } 402 4 406
133 188 $io->print(' ' x $indent);
134 188 $newline = 0;
135 } elsif ($need_space) {
136 402 $io->print(' ');
137 402 $need_space = 0;
138 }
139
140 594 ▶ if ($type == KEYWORD) {
Branch: 14 branches
BranchTrueFalseTotal
if ($type == 47) { } 97 497 594
elsif ($type == 14) { } 143 354 497
elsif ($type == 21) { } 0 354 354
elsif ($type == 4) { } 3 351 354
elsif ($type == 15) { } 2 349 351
elsif ($type == 27) { } 0 349 349
elsif ($type == 26) { } 29 320 349
elsif ($type == 29) { } 0 320 320
elsif ($type == 28) { } 0 320 320
elsif ($type == 59) { } 93 227 320
elsif ($type == 1) { } 0 227 227
elsif ($type == 41) { } 0 227 227
elsif ($type == 7) { } 0 227 227
elsif ($type == 12) { } 1 226 227
141 97 $io->print($t->value);
142 97 $need_space++;
143 } elsif ($type == IRI) {
144 143 my $value = $t->value;
145 143 my $ser = '<' . $value . '>';
146 143 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) 10 133 143
147 10 NSLOOP: foreach my $p ($ns->list_prefixes) {
148 13 my $prefix = $ns->namespace_uri($p)->as_string;
149 13 ▶ if (substr($value, 0, length($prefix)) eq $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($value, 0, length $prefix) eq $prefix) 6 7 13
150 # now verify that the prefixname is valid SPARQL syntax by re-parsing it
151 6 my $pname = join(':', $p, substr($value, length($prefix)));
152 6 my $b = $pname;
153 6 $b = encode('UTF-8', $b, Encode::FB_CROAK);
154 6 my ($pnt) = eval { $parser->parse_list_from_bytes($b) };
155 6 ▶ if (blessed($pnt) and $pnt->type == PREFIXNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($pnt) and $pnt->type == 21) 4 2 6
Condition66%2/3
builtin::blessed($pnt) and $pnt->type == 21
!ll&&!rl&&r
204
MC/DC50%1/2
builtin::blessed($pnt) and $pnt->type == 21
builtin::blessed($pnt) $pnt->type == 21
Truth table66%2/3
A and B
A= builtin::blessed($pnt)B= $pnt->type == 21
ABresult
0X0
100
111
156 4 $ser = $pname;
157 }
158 6 last NSLOOP;
159 }
160 }
161 }
162
163 # TODO: escape
164 143 $io->print($ser);
165 143 $need_space++;
166 } elsif ($type == PREFIXNAME) {
167 0 my $args = $t->args;
168 0 $io->print(join('', @$args));
169 0 $need_space++;
170 } elsif ($type == BNODE) {
171 3 $io->print('_:');
172 3 $io->print($t->value);
173 3 $need_space++;
174 } elsif ($type == LANG) {
175 2 $io->print('@');
176 2 $io->print($t->value);
177 2 $need_space++;
178 } elsif ($type == STRING1S) {
179 0 my $value = $t->value;
180 0 $value =~ s/'/\\'/g;
181 0 $io->print("'");
182 0 $io->print($value);
183 0 $io->print("'");
184 0 $need_space++;
185 } elsif ($type == STRING1D) {
186 29 my $value = $t->value;
187 29 $value =~ s/"/\\"/g;
188 29 $io->print('"');
189 29 $io->print($value);
190 29 $io->print('"');
191 29 $need_space++;
192 } elsif ($type == STRING3S) {
193 0 my $value = $t->value;
194 0 $value =~ s/'''/''\\'/g;
195 0 $io->print("'''");
196 0 $io->print($value);
197 0 $io->print("'''");
198 0 $need_space++;
199 } elsif ($type == STRING3D) {
200 0 my $value = $t->value;
201 0 $value =~ s/"""/""\\"/g;
202 0 $io->print('"""');
203 0 $io->print($value);
204 0 $io->print('"""');
205 0 $need_space++;
206 } elsif ($type == VAR) {
207 93 $io->print('?');
208 93 $io->print($t->value);
209 93 $need_space++;
210 } elsif ($type == A) {
211 0 $io->print('a');
212 0 $need_space++;
213 } elsif ($type == WS) {
214 } elsif ($type == COMMENT) {
215 0 $io->print('# ');
216 0 $io->print($t->value);
217 0 $io->print("\n");
218 } elsif ($type == HATHAT) {
219 1 $io->print($t->value);
220 } else {
221 226 $io->print($t->value);
222 226 $need_space++;
223 }
224
225 594 ▶ if ($type == DOT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 9) { } 60 534 594
elsif ($type == 16) { } 49 485 534
elsif ($type == 25) { } 1 484 485
226 60 ▶ if ($semicolon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($semicolon) 0 60 60
227 0 $indent--;
228 0 $semicolon = 0;
229 }
230 60 $need_space = 0;
231 60 $io->print("\n");
232 60 $newline = 1;
233 } elsif ($type == LBRACE) {
234 49 $io->print("\n");
235 49 $need_space = 0;
236 49 $newline = 1;
237 49 $indent++;
238 } elsif ($type == SEMICOLON) {
239 1 $io->print("\n");
240 1 $need_space = 0;
241 1 $newline = 1;
242 1 ▶ unless ($semicolon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($semicolon) 1 0 1
243 1 $indent++;
244 }
245 1 $semicolon = 1;
246 }
247 594 $last = $t;
248 }
249 56 ▶ unless ($newline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newline) 49 7 56
250 49 $io->print("\n");
251 }
252 56 return;
253 }
254
255 =item C<< serialize_iter_to_bytes( $iterator ) >>
256  
257 Serializes the SPARQL token objects from C<< $iterator >>
258 and returns the serialization as a UTF-8 encoded byte string.
259  
260 =cut
261  
262 56 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod documented
263 56 my $self = shift;
264 56 my $iter = shift;
265 56 my $data = '';
266 56 open(my $fh, '>:utf8', \$data);
267 56 $self->serialize_iter_to_io($fh, $iter);
268 56 close($fh);
269 56 return $data;
270 }
271 }
272  
273 46 1;
274  
275 __END__
« AtteanX/Parser/SPARQL.pm ↑ Summary AtteanX/Parser/NTuples.pm »