RDF/Trine/Parser/NTriples.pm

filter: Statementstmt 85.0% 171 / 201 Branchbran 76.0% 76 / 100 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 95.6% 22 / 23 Podpod 100% 4 / 4 totaltotal 82.8% 284 / 343 CC 56 SCAR 29.2
CC
56
Cov
82%
_unescape
41.0
_eat_node
23.6
parse_file
10.5
CRAP 18.5
« RDF/Trine/Store/DBI.pm ↑ Summary RDF/Trine/Iterator/Graph.pm »
1 # RDF::Trine::Parser::NTriples
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::NTriples - N-Triples Parser
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::NTriples version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15 my $parser = RDF::Trine::Parser->new( 'ntriples' );
16 $parser->parse_into_model( $base_uri, $data, $model );
17  
18 =head1 DESCRIPTION
19  
20 ...
21  
22 =head1 METHODS
23  
24 Beyond the methods documented below, this class inherits methods from the
25 L<RDF::Trine::Parser> class.
26  
27 =over 4
28  
29 =cut
30  
31 package RDF::Trine::Parser::NTriples;
32  
33 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 68 ▶use base qw(RDF::Trine::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 68 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 68 ▶use RDF::Trine qw(literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 ######################################################################
51  
52 68 our ($VERSION);
53 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
54 68 $VERSION = '1.019';
55 68 $RDF::Trine::Parser::parser_names{ 'ntriples' } = __PACKAGE__;
56 68 foreach my $ext (qw(nt)) {
57 68 $RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__;
58 }
59 68 my $class = __PACKAGE__;
60 68 $RDF::Trine::Parser::canonical_media_types{ $class } = 'text/plain';
61 68 foreach my $type (qw(text/plain)) {
62 68 $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__;
63 }
64 68 $RDF::Trine::Parser::format_uris{ 'http://www.w3.org/ns/formats/N-Triples' } = __PACKAGE__;
65 }
66  
67 ######################################################################
68  
69 =item C<< new >>
70  
71 =cut
72  
73 394 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 394 my $class = shift;
75 394 my $self = bless( {@_}, $class);
76 394 return $self;
77 }
78  
79 =item C<< parse_into_model ( $base_uri, $data, $model [, context => $context] ) >>
80  
81 Parses the bytes in C<< $data >>.
82 For each RDF statement parsed, will call C<< $model->add_statement( $statement ) >>.
83  
84 =item C<< parse_file_into_model ( $base_uri, $fh, $model [, context => $context] ) >>
85  
86 Parses all data read from the filehandle C<< $fh >>.
87 For each RDF statement parsed, will call C<< $model->add_statement( $statement ) >>.
88  
89 =cut
90  
91  
92 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
93  
94 =cut
95  
96 24 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
97 24 my $self = shift;
98 24 my $base = shift;
99 24 my $string = shift;
100 24 my $handler = shift;
101 24 open( my $fh, '<:encoding(UTF-8)', \$string );
102 24 return $self->parse_file( $base, $fh, $handler );
103 }
104  
105 =item C<< parse_node ( $string, $base ) >>
106  
107 Returns the RDF::Trine::Node object corresponding to the node whose N-Triples
108 serialization is found at the beginning of C<< $string >>.
109  
110 =cut
111  
112 0 ▶sub parse_node {
Subroutine parse_node: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 my $string = shift;
115 0 my $uri = shift;
116 0 my $n = $self->_eat_node( $uri, 0, $string );
117 0 return $n;
118 }
119  
120 =item C<< parse_file ( $base, $fh, \&handler ) >>
121  
122 =cut
123  
124 412 ▶sub parse_file {
Subroutine parse_file: called CC=10 SCAR=23.5
Pod documented
125 412 my $self = shift;
126 412 my $base = shift;
127 412 my $fh = shift;
128 412 my $handler = shift;
129
130 412 ▶ unless (ref($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 412 412
131 0 my $filename = $fh;
132 0 undef $fh;
133 0 ▶ open( $fh, '<:encoding(UTF-8)', $filename ) or throw RDF::Trine::Error::ParserError -text => $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(UTF-8)", $filename or "RDF::Trine::Error::ParserError"->throw("-text", $!) 0 0 0
134 }
135
136 412 my $lineno = 0;
137 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
138 412 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3120 412 3532
139 3870 LINE:
140 ($line, my @extra) = split(/\r\n|\r|\n/, $line, 2);
141 3870 $lineno++;
142
143 3870 ▶ next unless (defined($line) and length($line));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 851 3019 3870
Condition100%3/3
defined $line and length $line
!ll&&!rl&&r
7501013019
MC/DC100%2/2
defined $line and length $line
defined $line length $line
Truth table100%3/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
144 3019 ▶ next unless ($line =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 2 3017 3019
145 3017 chomp($line);
146 3017 $line =~ s/^\s*//;
147 3017 $line =~ s/\s*$//;
148 3017 ▶ next if ($line =~ /^#/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 2267 750 3017
149
150 750 my @nodes = ();
151 750 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
152 750 ▶ while (my $n = $self->_eat_node( $base, $lineno, $line )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $self->_eat_node($base, $lineno, $line)) 2257 750 3007
153 2257 push(@nodes, $n);
154 2257 $line =~ s/^\s*//;
155 }
156 750 };
157 750 $line =~ s/^\s//g;
158 750 ▶ unless ($line eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line eq ".") 0 750 750
159 # Carp::cluck 'N-Triples parser failed: ' . Dumper(\@nodes, $line);
160 0 throw RDF::Trine::Error::ParserError -text => "Missing expected '.' at line $lineno";
161 }
162
163 750 $self->_emit_statement( $handler, \@nodes, $lineno );
164 750 ▶ if (@extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) 750 0 750
165 750 $line = shift(@extra);
166 750 goto LINE;
167 }
168 }
169 }
170  
171 742 ▶sub _emit_statement {
Subroutine _emit_statement: called CC=5 SCAR=16.2
172 742 my $self = shift;
173 742 my $handler = shift;
174 742 my $nodes = shift;
175 742 my $lineno = shift;
176 742 my $st;
177 742 ▶ if (scalar(@$nodes) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$nodes == 3) { } 742 0 742
178 742 ▶ if ($self->{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"canonicalize"}) 6 736 742
179 6 ▶ if ($nodes->[2]->isa('RDF::Trine::Node::Literal') and $nodes->[2]->has_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->[2]->isa("RDF::Trine::Node::Literal") and $nodes->[2]->has_datatype) 5 1 6
Condition66%2/3
$nodes->[2]->isa("RDF::Trine::Node::Literal") and $nodes->[2]->has_datatype
!ll&&!rl&&r
015
MC/DC50%1/2
$nodes->[2]->isa("RDF::Trine::Node::Literal") and $nodes->[2]->has_datatype
$nodes->[2]->isa("RDF::Trine::Node::Literal") $nodes->[2]->has_datatype
Truth table66%2/3
A and B
A= $nodes->[2]->isa("RDF::Trine::Node::Literal")B= $nodes->[2]->has_datatype
ABresult
0X0
100
111
180 5 my $value = $nodes->[2]->literal_value;
181 5 my $dt = $nodes->[2]->literal_datatype;
182 5 my $canon = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
183 5 $nodes->[2] = literal( $canon, undef, $dt );
184 }
185 }
186 742 $st = RDF::Trine::Statement->new( @$nodes );
187 # } elsif (scalar(@$nodes) == 4) {
188 # $st = RDF::Trine::Statement::Quad->new( @$nodes );
189 } else {
190 # warn Dumper($nodes);
191 0 throw RDF::Trine::Error::ParserError -text => qq[Not valid N-Triples data at line $lineno];
192 }
193 742 $handler->( $st );
194 }
195  
196 3007 ▶sub _eat_node {
Subroutine _eat_node: called CC=23 SCAR=31.6
197 3007 my $self = shift;
198 3007 my $base = shift;
199 3007 my $lineno = shift;
200 3007 $_[0] =~ s/^\s*//;
201 3007 ▶ return unless length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 0 3007 3007
202 3007 my $char = substr($_[0], 0, 1);
203 3007 ▶ return if ($char eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "." 750 2257 3007
204
205 2257 ▶ if ($char eq '<') {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '<') { } 1649 608 2257
elsif ($char eq '_') { } 282 326 608
elsif ($char eq '"') { } 326 0 326
206 1649 my ($uri) = $_[0] =~ m/^<([^>]*)>/;
207 1649 substr($_[0], 0, length($uri)+2) = '';
208 1649 return RDF::Trine::Node::Resource->new( _unescape($uri, $lineno) );
209 } elsif ($char eq '_') {
210 282 my ($name) = $_[0] =~ m/^_:([A-Za-z][A-Za-z0-9]*)/;
211 282 substr($_[0], 0, length($name)+2) = '';
212 282 return RDF::Trine::Node::Blank->new( $name );
213 } elsif ($char eq '"') {
214 326 substr($_[0], 0, 1) = '';
215 326 my $value = decode('utf8', '');
216 326 ▶ while (length($_[0]) and substr($_[0], 0, 1) ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] and substr($_[0], 0, 1) ne "\"") 379 326 705
Condition66%2/3
length $_[0] and substr($_[0], 0, 1) ne "\""
!ll&&!rl&&r
0326379
MC/DC50%1/2
length $_[0] and substr($_[0], 0, 1) ne "\""
length $_[0] substr($_[0], 0, 1) ne "\""
Truth table66%2/3
A and B
A= length $_[0]B= substr($_[0], 0, 1) ne "\""
ABresult
0X0
100
111
217 379 ▶ while ($_[0] =~ m/^([^"\\]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([^"\\]+)/) 350 379 729
218 350 $value .= $1;
219 350 substr($_[0],0,length($1)) = '';
220 }
221 379 ▶ if (substr($_[0],0,1) eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "\\") 98 281 379
222 98 ▶ while ($_[0] =~ m/^\\(.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\\(.)/) 211 98 309
223 211 ▶ if ($1 eq 't') {
Branch: 7 branches
BranchTrueFalseTotal
if ($1 eq 't') { } 15 196 211
elsif ($1 eq 'r') { } 4 192 196
elsif ($1 eq 'n') { } 44 148 192
elsif ($1 eq '"') { } 19 129 148
elsif ($1 eq '\\') { } 3 126 129
elsif ($1 eq 'u') { } 99 27 126
elsif ($1 eq 'U') { } 27 0 27
224 15 $value .= "\t";
225 15 substr($_[0],0,2) = '';
226 } elsif ($1 eq 'r') {
227 4 $value .= "\r";
228 4 substr($_[0],0,2) = '';
229 } elsif ($1 eq 'n') {
230 44 $value .= "\n";
231 44 substr($_[0],0,2) = '';
232 } elsif ($1 eq '"') {
233 19 $value .= '"';
234 19 substr($_[0],0,2) = '';
235 } elsif ($1 eq '\\') {
236 3 $value .= "\\";
237 3 substr($_[0],0,2) = '';
238 } elsif ($1 eq 'u') {
239 99 ▶ $_[0] =~ m/^\\u([0-9A-Fa-f]{4})/ or throw RDF::Trine::Error::ParserError -text => qq[Bad N-Triples \\u escape at line $lineno, near "$_[0]"];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\\u([0-9A-Fa-f]{4})/ or "RDF::Trine::Error::ParserError"->throw("-text", qq[Bad N-Triples \\u escape at line $lineno, near "$_[0]"]) 99 0 99
240 99 $value .= chr(oct('0x' . $1));
241 99 substr($_[0],0,6) = '';
242 } elsif ($1 eq 'U') {
243 27 ▶ $_[0] =~ m/^\\U([0-9A-Fa-f]{8})/ or throw RDF::Trine::Error::ParserError -text => qq[Bad N-Triples \\U escape at line $lineno, near "$_[0]"];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\\U([0-9A-Fa-f]{8})/ or "RDF::Trine::Error::ParserError"->throw("-text", qq[Bad N-Triples \\U escape at line $lineno, near "$_[0]"]) 27 0 27
244 27 $value .= chr(oct('0x' . $1));
245 27 substr($_[0],0,10) = '';
246 } else {
247 0 throw RDF::Trine::Error::ParserError -text => qq[Not valid N-Triples escape character '\\$1' at line $lineno, near "$_[0]"];
248 }
249 }
250 }
251 }
252 326 ▶ if (substr($_[0],0,1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq '"') { } 326 0 326
253 326 substr($_[0],0,1) = '';
254 } else {
255 0 throw RDF::Trine::Error::ParserError -text => qq[Ending double quote not found at line $lineno];
256 }
257
258 326 ▶ if ($_[0] =~ m/^@([a-z]+(-[a-zA-Z0-9]+)*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^@([a-z]+(-[a-zA-Z0-9]+)*)/) { } 20 306 326
elsif (substr($_[0], 0, 3) eq '^^<') { } 89 217 306
259 20 my $lang = $1;
260 20 substr($_[0],0,1+length($lang)) = '';
261 20 return RDF::Trine::Node::Literal->new($value, $lang);
262 } elsif (substr($_[0],0,3) eq '^^<') {
263 89 substr($_[0],0,3) = '';
264 89 my ($uri) = $_[0] =~ m/^([^>]*)>/;
265 89 substr($_[0], 0, length($uri)+1) = '';
266 89 return RDF::Trine::Node::Literal->new($value, undef, $uri);
267 } else {
268 217 return RDF::Trine::Node::Literal->new($value);
269 }
270 } else {
271 0 throw RDF::Trine::Error::ParserError -text => qq[Not valid N-Triples node start character '$char' at line $lineno, near "$_[0]"];
272 }
273 }
274  
275 1649 ▶sub _unescape {
Subroutine _unescape: called CC=18 SCAR=37.1
276 1649 my $string = shift;
277 1649 my $lineno = shift;
278 1649 my $value = '';
279 1649 ▶ while (length($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 1653 1649 3302
280 1653 ▶ while ($string =~ m/^([^\\]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([^\\]+)/) 1653 1653 3306
281 1653 $value .= $1;
282 1653 substr($string,0,length($1)) = '';
283 }
284 1653 ▶ if (length($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 11 1642 1653
285 11 ▶ if ($string eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "\\") 0 11 11
286 0 throw RDF::Trine::Error::ParserError -text => qq[Backslash in N-Triples node without escaped character at line $lineno];
287 }
288 11 ▶ if ($string =~ m/^\\([tbnrf"'uU])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\\([tbnrf"'uU])/) { } 11 0 11
289 11 ▶ while ($string =~ m/^\\([tbnrf"'uU])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\\([tbnrf"'uU])/) 78 11 89
290 78 ▶ if ($1 eq 't') {
Branch: 9 branches
BranchTrueFalseTotal
if ($1 eq 't') { } 0 78 78
elsif ($1 eq 'b') { } 0 78 78
elsif ($1 eq 'n') { } 0 78 78
elsif ($1 eq 'r') { } 0 78 78
elsif ($1 eq 'f') { } 0 78 78
elsif ($1 eq '"') { } 0 78 78
elsif ($1 eq '\\') { } 0 78 78
elsif ($1 eq 'u') { } 74 4 78
elsif ($1 eq 'U') { } 4 0 4
291 0 $value .= "\t";
292 0 substr($string,0,2) = '';
293 } elsif ($1 eq 'b') {
294 0 $value .= "\b";
295 0 substr($string,0,2) = '';
296 } elsif ($1 eq 'n') {
297 0 $value .= "\n";
298 0 substr($string,0,2) = '';
299 } elsif ($1 eq 'r') {
300 0 $value .= "\r";
301 0 substr($string,0,2) = '';
302 } elsif ($1 eq 'f') {
303 0 $value .= "\f";
304 0 substr($string,0,2) = '';
305 } elsif ($1 eq '"') {
306 0 $value .= '"';
307 0 substr($string,0,2) = '';
308 } elsif ($1 eq '\\') {
309 0 $value .= "\\";
310 0 substr($string,0,2) = '';
311 } elsif ($1 eq 'u') {
312 74 ▶ $string =~ m/^\\u([0-9A-F]{4})/ or throw RDF::Trine::Error::ParserError -text => qq[Bad N-Triples \\u escape at line $lineno, near "$string"];
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\\u([0-9A-F]{4})/ or "RDF::Trine::Error::ParserError"->throw("-text", qq[Bad N-Triples \\u escape at line $lineno, near "$string"]) 74 0 74
313 74 $value .= chr(oct('0x' . $1));
314 74 substr($string,0,6) = '';
315 } elsif ($1 eq 'U') {
316 4 ▶ $string =~ m/^\\U([0-9A-F]{8})/ or throw RDF::Trine::Error::ParserError -text => qq[Bad N-Triples \\U escape at line $lineno, near "$string"];
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\\U([0-9A-F]{8})/ or "RDF::Trine::Error::ParserError"->throw("-text", qq[Bad N-Triples \\U escape at line $lineno, near "$string"]) 4 0 4
317 4 $value .= chr(oct('0x' . $1));
318 4 substr($string,0,10) = '';
319 }
320 }
321 } else {
322 0 my $esc = substr($string, 0, 2);
323 0 throw RDF::Trine::Error::ParserError -text => qq[Not a valid N-Triples escape sequence '$esc' at line $lineno, near "$string"];
324 }
325 }
326 }
327 1649 return $value;
328 }
329  
330 68 1;
331  
332 __END__
« RDF/Trine/Store/DBI.pm ↑ Summary RDF/Trine/Iterator/Graph.pm »