AtteanX/Parser/SPARQLXML/SAXHandler.pm

filter: Statementstmt 85.1% 109 / 128 Branchbran 70.8% 34 / 48 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 79.4% 162 / 204 CC 28 SCAR 33.0
CC
28
Cov
79%
end_element
45.9
start_element
3.1
new
3.0
CRAP 27.2
« AtteanX/Parser/SPARQLLex.pm ↑ Summary AtteanX/Parser/Turtle/Token.pm »
1 # AtteanX::Parser::SPARQLXML::SAXHandler
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 AtteanX::Parser::SPARQLXML::SAXHandler - XML parser for SPARQL XML Results format
7  
8 =head1 VERSION
9  
10 This document describes AtteanX::Parser::SPARQLXML::SAXHandler version 0.040
11  
12 =head1 STATUS
13  
14 This module's API and functionality should be considered unstable.
15 In the future, this module may change in backwards-incompatible ways,
16 or be removed entirely.
17  
18 =head1 SYNOPSIS
19  
20 use AtteanX::Parser::SPARQLXML::SAXHandler;
21  
22 =head1 METHODS
23  
24 =over 4
25  
26 =cut
27  
28 package AtteanX::Parser::SPARQLXML::SAXHandler 0.040;
29  
30 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 4 ▶use Attean;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use Attean;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 4 my %strings;
40 my %tagstack;
41 4 my %results;
42 4 my %values;
43 4 my %bindings;
44 4 my %booleans;
45 4 my %variables;
46 4 my %has_head;
47 4 my %has_end;
48 4 my %result_count;
49 4 my %result_handlers;
50 4 my %config;
51 4 my %triples;
52  
53 4 my %expecting_string = map { $_ => 1 } qw(boolean bnode uri literal);
54  
55 =item C<< new ( [ \&handler ] ) >>
56  
57 Returns a new XML::SAX handler object. If C<< &handler >> is supplied, it will
58 be called with a variable bindings object as each is parsed, bypassing the
59 normal process of collecting the results for retrieval via an iterator object.
60  
61 =cut
62  
63 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
64 3 my $class = shift;
65 3 my $self = $class->SUPER::new();
66 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
67 3 my $addr = refaddr( $self );
68 3 my $code = shift;
69 3 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
03
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
70 3 $result_handlers{ $addr } = $code;
71 3 $config{ $addr } = { %$args };
72 }
73 3 return $self;
74 }
75  
76 =begin private
77  
78 =item C<< start_element >>
79  
80 =cut
81  
82 55 ▶sub start_element {
Subroutine start_element: called CC=3 SCAR=11.2
Pod documented
83 55 my $self = shift;
84 55 my $el = shift;
85 55 my $tag = $el->{LocalName};
86 55 my $addr = refaddr( $self );
87
88 55 unshift( @{ $tagstack{ $addr } }, [$tag, $el] );
89 55 ▶ if ($expecting_string{ $tag }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting_string{$tag}) 15 40 55
90 15 $strings{ $addr } = '';
91 }
92
93 55 ▶ if ($tag eq 'triple') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "triple") 0 55 55
94 0 push(@{ $triples{ $addr } }, {});
95 }
96 }
97  
98 =item C<< end_element >>
99  
100 =cut
101  
102 55 ▶sub end_element {
Subroutine end_element: called CC=23 SCAR=38.3
Pod documented
103 55 my $self = shift;
104 55 my $class = ref($self);
105 55 my $eel = shift;
106 55 my $addr = refaddr( $self );
107 55 my $string = $strings{ $addr };
108 55 my $taginfo = shift( @{ $tagstack{ $addr } } );
109 55 my ($tag, $el) = @$taginfo;
110
111 55 ▶ if ($tag eq 'head') {
Branch: 13 branches
BranchTrueFalseTotal
if ($tag eq 'head') { } 3 52 55
elsif ($tag eq 'sparql') { } 3 49 52
elsif ($tag eq 'variable') { } 11 38 49
elsif ($tag eq 'boolean') { } 0 38 38
elsif ($tag eq 'binding') { } 15 23 38
elsif ($tag eq 'result') { } 5 18 23
elsif ($tag eq 'bnode') { } 2 16 18
elsif ($tag eq 'uri') { } 7 9 16
elsif ($tag eq 'literal') { } 6 3 9
elsif ($tag eq 'subject') { } 0 3 3
elsif ($tag eq 'predicate') { } 0 3 3
elsif ($tag eq 'object') { } 0 3 3
elsif ($tag eq 'triple') { } 0 3 3
112 3 $has_head{ $addr } = 1;
113 3 ▶ if (my $code = $result_handlers{ $addr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $result_handlers{$addr}) 3 0 3
114 3 ▶ if ($config{ $addr }{ variables }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{$addr}{"variables"}) 0 3 3
115 0 $code->( $variables{ $addr } );
116 }
117 }
118 } elsif ($tag eq 'sparql') {
119 3 $has_end{ $addr } = 1;
120 } elsif ($tag eq 'variable') {
121 11 push( @{ $variables{ $addr } }, $el->{Attributes}{'{}name'}{Value});
122 } elsif ($tag eq 'boolean') {
123 0 ▶ $booleans{ $addr } = ($string eq 'true') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$string eq 'true' ? : 0 0 0
124 0 ▶ if ($string =~ /^(?:true|false)$/ and my $code = $result_handlers{ $addr }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(?:true|false)$/u and my $code = $result_handlers{$addr}) 0 0 0
Condition0%0/3
$string =~ /^(?:true|false)$/u and my $code = $result_handlers{$addr}
!ll&&!rl&&r
000
MC/DC0%0/2
$string =~ /^(?:true|false)$/u and my $code = $result_handlers{$addr}
$string =~ /^(?:true|false)$/u my $code = $result_handlers{$addr}
Truth table0%0/3
A and B
A= $string =~ /^(?:true|false)$/uB= my $code = $result_handlers{$addr}
ABresult
0X0
100
111
125 0 $code->( Attean::Literal->$string() );
126 }
127 } elsif ($tag eq 'binding') {
128 15 my $name = $el->{Attributes}{'{}name'}{Value};
129 15 my $value = delete( $values{ $addr } );
130 15 $bindings{ $addr }{ $name } = $value;
131 } elsif ($tag eq 'result') {
132 5 ▶ my $result = delete( $bindings{ $addr } ) || {};
Condition50%1/2
delete $bindings{$addr} || {}
l!l
50
MC/DC0%0/1
delete $bindings{$addr} || {}
delete $bindings{$addr}
Truth table50%1/2
A
A= delete $bindings{$addr}
Aresult
11
00
133 5 $result_count{ $addr }++;
134 5 my $vb = Attean::Result->new( bindings => $result );
135 5 ▶ if (my $code = $result_handlers{ $addr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $result_handlers{$addr}) { } 5 0 5
136 5 $code->( $vb );
137 } else {
138 0 push( @{ $results{ $addr } }, $vb );
139 }
140 } elsif ($tag eq 'bnode') {
141 2 $values{ $addr } = Attean::Blank->new( $string );
142 } elsif ($tag eq 'uri') {
143 7 $values{ $addr } = Attean::IRI->new( $string );
144 } elsif ($tag eq 'literal') {
145 6 my ($lang, $dt);
146 6 ▶ if (my $dtinf = $el->{Attributes}{'{}datatype'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $dtinf = $el->{'Attributes'}{'{}datatype'}) { } 3 3 6
elsif (my $langinf = $el->{'Attributes'}{'{http://www.w3.org/XML/1998/namespace}lang'}) { } 2 1 3
147 3 $dt = $dtinf->{Value};
148 3 $values{ $addr } = Attean::Literal->new( value => $string, datatype => $dt );
149 } elsif (my $langinf = $el->{Attributes}{'{http://www.w3.org/XML/1998/namespace}lang'}) {
150 2 $lang = $langinf->{Value};
151 2 $values{ $addr } = Attean::Literal->new( value => $string, language => $lang );
152 } else {
153 1 $values{ $addr } = Attean::Literal->new( value => $string );
154 }
155 } elsif ($tag eq 'subject') {
156 0 my $value = delete( $values{ $addr } );
157 0 $triples{ $addr }[-1]{$tag} = $value;
158 } elsif ($tag eq 'predicate') {
159 0 my $value = delete( $values{ $addr } );
160 0 $triples{ $addr }[-1]{$tag} = $value;
161 } elsif ($tag eq 'object') {
162 0 my $value = delete( $values{ $addr } );
163 0 $triples{ $addr }[-1]{$tag} = $value;
164 } elsif ($tag eq 'triple') {
165 0 my $data = pop(@{ $triples{ $addr } });
166 0 my $t = Attean::Triple->new( %{ $data } );
167 0 $values{ $addr } = $t;
168 }
169 }
170  
171 =item C<< characters >>
172  
173 =cut
174  
175 76 ▶sub characters {
Subroutine characters: called CC=2 SCAR=6.9
Pod documented
176 76 my $self = shift;
177 76 my $data = shift;
178 76 my $addr = refaddr( $self );
179
180 76 my $tag = $self->_current_tag;
181 76 ▶ if ($expecting_string{ $tag }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expecting_string{$tag}) 15 61 76
182 15 my $chars = $data->{Data};
183 15 $strings{ $addr } .= $chars;
184 }
185 }
186  
187 76 ▶sub _current_tag {
Subroutine _current_tag: called CC=1 SCAR=0.0
188 76 my $self = shift;
189 76 my $addr = refaddr( $self );
190 76 return $tagstack{ $addr }[0][0];
191 }
192  
193 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
194 3 my $self = shift;
195 3 my $addr = refaddr( $self );
196 3 delete $strings{ $addr };
197 3 delete $results{ $addr };
198 3 delete $tagstack{ $addr };
199 3 delete $values{ $addr };
200 3 delete $bindings{ $addr };
201 3 delete $booleans{ $addr };
202 3 delete $variables{ $addr };
203 3 delete $has_head{ $addr };
204 3 delete $has_end{ $addr };
205 3 delete $result_count{ $addr };
206 3 delete $result_handlers{ $addr };
207 3 delete $config{ $addr };
208 }
209  
210  
211 4 1;
212  
213 __END__
« AtteanX/Parser/SPARQLLex.pm ↑ Summary AtteanX/Parser/Turtle/Token.pm »