AtteanX/Serializer/SPARQLHTML.pm

filter: Statementstmt 100% 92 / 92 Branchbran 87.5% 14 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 96.9% 126 / 130 CC 15 SCAR 16.3
CC
15
Cov
97%
node_as_html
9.0
serialize_iter_to_io
7.0
BEGIN
1.0
CRAP 5.1
« Attean/API/Iterator.pm ↑ Summary Attean/Algebra.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::SPARQLHTML - SPARQL Results HTML Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::SPARQLHTML version 0.040
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $s = Attean->get_serializer('SPARQLHTML')->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 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 package AtteanX::Serializer::SPARQLHTML 0.040 {
39 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶ use Types::Standard qw(Str Bool ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 4 has 'full_document' => (is => 'rw', isa => Bool, default => 1);
48 4 has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'text/html');
49  
50 =item C<< media_types >>
51  
52 Returns a list of media types that identify the format produced by this serializer.
53  
54 =cut
55  
56 13 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
57 13 return [qw(text/html)];
58 }
59  
60 =item C<< file_extensions >>
61  
62 Returns a list of file extensions associated with the serialized format.
63  
64 =cut
65  
66 8 ▶ sub file_extensions { return [qw(html)] };
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
67
68 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
69  
70 Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the
71 L<IO::Handle> object C<< $fh >>.
72  
73 =cut
74  
75 2 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=7 SCAR=19.5
Pod documented
76 2 my $self = shift;
77 2 my $io = shift;
78 2 my $iter = shift;
79 2 ▶ if ($self->full_document) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->full_document) 2 0 2
80 2 $io->print(<<"END");
81 <!DOCTYPE html>
82 <html><head><title>SPARQL Results</title></head><body>
83 <div id="result">
84 <h2>Results</h2>
85 END
86 }
87 2 my @names = @{ $iter->variables };
88 2 my $count = 0;
89 2 my $first = 1;
90 2 $io->print("<table class='sparqlresults'>\n<thead><tr>\n");
91 2 foreach my $name (@names) {
92 6 $io->print("\t<th>" . $name . "</th>\n");
93 }
94 2 $io->print("</tr></thead>\n");
95 2 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 6 2 8
96 6 $count++;
97
98 6 $io->print("<tr>\n");
99 6 foreach my $k (@names) {
100 18 my $term = $t->value($k);
101 18 ▶ my $value = $self->node_as_html($term) // '';
Condition50%1/2
$self->node_as_html($term) // ""
l!l
180
MC/DC0%0/1
$self->node_as_html($term) // ""
$self->node_as_html($term)
Truth table50%1/2
A
A= $self->node_as_html($term)
Aresult
11
00
102 18 $io->print("\t<td>$value</td>\n");
103 }
104 6 $io->print("</tr>\n");
105 }
106 2 my $columns = scalar(@names);
107 2 $io->print("<tfoot><tr><th colspan=\"$columns\">Total: $count</th></tr></tfoot>\n</table>\n");
108 2 ▶ if ($self->full_document) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->full_document) 2 0 2
109 2 $io->print("</div>\n</body></html>\n");
110 }
111 2 return;
112 }
113
114 =item C<< serialize_iter_to_bytes( $iterator ) >>
115  
116 Serializes the L<Attean::API::Binding> objects from C<< $iterator >>
117 and returns the serialization as a UTF-8 encoded byte string.
118  
119 =cut
120  
121 1 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod documented
122 1 my $self = shift;
123 1 my $iter = shift;
124 1 my $data = '';
125 1 open(my $fh, '>:utf8', \$data);
126 1 $self->serialize_iter_to_io($fh, $iter);
127 1 close($fh);
128 1 return $data;
129 }
130  
131 =item C<< node_as_html($node) >>
132  
133 Serializes the L<Attean::API::Term> object as HTML.
134  
135 =cut
136  
137 22 ▶ sub node_as_html {
Subroutine node_as_html: called CC=9 SCAR=22.0
Pod documented
138 22 my $self = shift;
139 22 my $node = shift;
140 22 ▶ return '' unless (blessed($node));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) 4 18 22
141 18 ▶ if ($node->does('Attean::API::IRI')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->does('Attean::API::IRI')) { } 10 8 18
elsif ($node->does('Attean::API::Literal')) { } 4 4 8
142 10 my $uri = $node->value;
143 10 for ($uri) {
144 10 s/&/&amp;/g;
145 10 s/</&lt;/g;
146 }
147 10 my $html = qq[<a href="${uri}">$uri</a>];
148  
149 10 ▶ if (my $map = $self->namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->namespaces) { } 2 8 10
150 2 my $abr = $map->abbreviate($uri);
151  
152 2 ▶ if ($abr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abr) { } 1 1 2
153 1 return qq[<a href="${uri}">$abr</a>];
154 } else {
155 1 return $html;
156 }
157  
158 } else {
159
160 8 return $html;
161 }
162  
163  
164 # if ($link) {
165 # $html = qq[<a href="${uri}">$html</a>];
166 # }
167 } elsif ($node->does('Attean::API::Literal')) {
168 4 my $html = $node->value;
169 4 for ($html) {
170 4 s/&/&amp;/g;
171 4 s/</&lt;/g;
172 }
173 4 return $html;
174 } else {
175 4 my $html = $node->value;
176 4 for ($html) {
177 4 s/&/&amp;/g;
178 4 s/</&lt;/g;
179 }
180 4 return $html;
181 }
182 }
183 4 with 'Attean::API::ResultSerializer';
184 4 with 'Attean::API::AbbreviatingSerializer';
185 }
186  
187 4 1;
188  
189 __END__
« Attean/API/Iterator.pm ↑ Summary Attean/Algebra.pm »