RDF/Trine/Parser/RDFPatch.pm

filter: Statementstmt 73.7% 115 / 156 Branchbran 55.2% 21 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 22 / 26 Podpod 100% 6 / 6 totaltotal 72.5% 164 / 226 CC 21 SCAR 19.9
CC
21
Cov
70%
parse_file
30.0
parse_line
11.3
handle_op
6.0
CRAP 7.3
« RDF/Trine/Statement.pm ↑ Summary RDF/Trine/Store/Memory.pm »
1 # RDF::Trine::Parser::RDFPatch
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::RDFPatch - RDF-Patch Parser
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::RDFPatch version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser::RDFPatch;
15 my $serializer = RDF::Trine::Parser::RDFPatch->new();
16  
17 =head1 DESCRIPTION
18  
19 The RDF::Trine::Parser::RDFPatch class provides an API for serializing RDF
20 graphs to the RDF-Patch syntax.
21  
22 =head1 METHODS
23  
24 =over 4
25  
26 =cut
27  
28 package RDF::Trine::Parser::RDFPatch;
29  
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use RDF::Trine::Parser::Turtle;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use RDF::Trine::Parser::Turtle::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 ######################################################################
46  
47 1 our ($VERSION);
48 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 $VERSION = '1.019';
50 }
51  
52 ######################################################################
53  
54 =item C<< new ( ) >>
55  
56 Returns a new RDF-Patch Parser object.
57  
58 =cut
59  
60 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
61 2 my $class = shift;
62 2 my $self = bless( {
63 last => [],
64 namespaces => RDF::Trine::NamespaceMap->new(),
65 }, $class );
66 2 return $self;
67 }
68  
69 =item C<< namespace_map >>
70  
71 Returns the RDF::Trine::NamespaceMap object used in parsing.
72  
73 =cut
74  
75 0 ▶sub namespace_map {
Subroutine namespace_map: not called CC=1 SCAR=6.9
Pod documented
76 0 my $self = shift;
77 0 return $self->{namespaces};
78 }
79  
80 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
81  
82 =cut
83  
84 0 ▶sub parse {
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 0 my $base = shift;
87 0 my $string = shift;
88 0 my $handler = shift;
89 0 open( my $fh, '<:encoding(UTF-8)', \$string );
90 0 return $self->parse_file( $base, $fh, $handler );
91 }
92  
93 =item C<< parse_file ( $base, $fh, \&handler ) >>
94  
95 =cut
96  
97 0 ▶sub parse_file {
Subroutine parse_file: not called CC=5 SCAR=34.0
Pod documented
98 0 my $self = shift;
99 0 my $base = shift;
100 0 my $fh = shift;
101 0 my $handler = shift;
102
103 0 ▶ unless (ref($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 0 0
104 0 my $filename = $fh;
105 0 undef $fh;
106 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
107 }
108
109 0 my $lineno = 0;
110 0 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
111 0 $lineno++;
112 0 my $op = $self->parse_line( $line, $base );
113 0 ▶ last unless blessed($op);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($op) 0 0 0
114 0 $self->handle_op( $op, $handler, $lineno );
115 }
116 }
117  
118 =item C<< handle_op ( $op, $handler, $lineno ) >>
119  
120 Handles the RDF::Trine::Parser::RDFPatch::Op operation object.
121 For 'A'dd operations, the C<< $handler >> callback is called with the RDF statement.
122 Otherwise an exception is thrown.
123  
124 =cut
125  
126 0 ▶sub handle_op {
Subroutine handle_op: not called CC=2 SCAR=17.9
Pod documented
127 0 my $self = shift;
128 0 my $op = shift;
129 0 my $handler = shift;
130 0 my $lineno = shift;
131 0 my $opid = $op->op;
132 0 ▶ if ($opid eq 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opid eq 'A') { } 0 0 0
133 0 my ($st) = $op->args;
134 0 $handler->( $st );
135 } else {
136 0 my $col = 0;
137 0 throw RDF::Trine::Error::ParserError::Positioned (
138 -text => "Cannot handle RDF Patch operation type '$opid' during RDF parsing at $lineno:$col",
139 -value => [$lineno, $col],
140 );
141 }
142 }
143  
144 =item C<< parse_line ( $line, $base ) >>
145  
146 Returns an operation object.
147  
148 =cut
149  
150 4 ▶sub _get_token_type {
Subroutine _get_token_type: called CC=3 SCAR=12.3
151 4 my $self = shift;
152 4 my $l = shift;
153 4 my $type = shift;
154 4 my $t = $l->get_token;
155 4 ▶ unless ($t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 0 4 4
156 0 $l->_throw_error(sprintf("Expecting %s but got EOF", decrypt_constant($type)));
157 0 return;
158 }
159 4 ▶ unless ($t->type eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->type eq $type) 0 4 4
160 0 $self->_throw_error(sprintf("Expecting %s but got %s", decrypt_constant($type), decrypt_constant($t->type)), $t, $l);
161 }
162 4 return $t;
163 }
164  
165 11 ▶sub parse_line {
Subroutine parse_line: called CC=11 SCAR=24.3
Pod documented
166 11 my $self = shift;
167 11 my $line = shift;
168 11 my $base = shift;
169 11 ▶ return if ($line =~ /^#/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 11 11
170 11 ▶ if (substr($line, 0, 7) eq '@prefix') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 7) eq "\@prefix") 1 10 11
171 1 open( my $fh, '<:encoding(UTF-8)', \$line );
172 1 my $l = RDF::Trine::Parser::Turtle::Lexer->new($fh);
173 1 $self->_get_token_type($l, PREFIX);
174 1 my $t = $self->_get_token_type($l, PREFIXNAME);
175 1 my $name = $t->value;
176 1 $name =~ s/:$//;
177 1 $t = $self->_get_token_type($l, IRI);
178 1 my $r = RDF::Trine::Node::Resource->new($t->value, $base);
179 1 my $iri = $r->uri_value;
180 1 $t = $self->_get_token_type($l, DOT);
181 1 $self->{namespaces}->add_mapping( $name => $iri );
182 1 return;
183 }
184
185 10 my ($op, $tail) = split(/ /, $line, 2);
186 10 ▶ unless ($op =~ /^[ADQ]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op =~ /^[ADQ]$/) 1 9 10
187 1 throw RDF::Trine::Error::ParserError -text => "Unknown RDF Patch operation ID '$op'";
188 }
189
190 9 my $p = RDF::Trine::Parser::Turtle->new( 'map' => $self->{namespaces} );
191 9 my @nodes;
192 9 foreach my $pos (1,2,3,4) {
193 34 ▶ if ($tail =~ /^\s*U\b/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tail =~ /^\s*U\b/) { } 2 32 34
elsif ($tail =~ /^\s*R\b/) { } 0 32 32
elsif ($tail =~ /^\s*[.]/) { } 8 24 32
194 2 substr($tail, 0, $+[0], '');
195 2 my $v = RDF::Trine::Node::Variable->new("v$pos");
196 2 $self->{last}[$pos] = $v;
197 2 push(@nodes, $v);
198 } elsif ($tail =~ /^\s*R\b/) {
199 0 substr($tail, 0, $+[0], '');
200 0 my $node = $self->{last}[$pos];
201 0 ▶ unless (blessed($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($node)) 0 0 0
202 0 throw RDF::Trine::Error -text => "Use of non-existent `R`epeated term";
203 }
204 0 push(@nodes, $node);
205 } elsif ($tail =~ /^\s*[.]/) {
206 8 last;
207 } else {
208 24 my $token;
209 24 my $n = $p->parse_node($tail, $base, token => \$token);
210 24 $self->{last}[$pos] = $n;
211 24 push(@nodes, $n);
212 24 my $len = $token->column;
213 24 substr($tail, 0, $len, '');
214 }
215 }
216
217 9 my $st;
218 9 ▶ if (scalar(@nodes) == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @nodes == 3) { } 7 2 9
elsif (scalar @nodes == 4) { } 1 1 2
219 7 $st = RDF::Trine::Statement->new(@nodes);
220 } elsif (scalar(@nodes) == 4) {
221 1 $st = RDF::Trine::Statement::Quad->new(@nodes);
222 } else {
223 1 my $arity = scalar(@nodes);
224 1 throw RDF::Trine::Error::ParserError -text => "RDFPatch operation '$op' has unexpected arity ($arity)";
225 }
226
227 8 return RDF::Trine::Parser::RDFPatch::Op->new( $op, $st );
228 }
229  
230  
231 package RDF::Trine::Parser::RDFPatch::Op;
232  
233 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
235  
236 =item C<< new ( $op, @args ) >>
237  
238 Returns a new RDF-Patch Parser operation object.
239  
240 =cut
241  
242 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
243 8 my $class = shift;
244 8 my $op = shift;
245 8 my @args = @_;
246 8 my $self = bless( { op => $op, args => \@args }, $class );
247 8 return $self;
248 }
249  
250 12 ▶sub op {
Subroutine op: called CC=1 SCAR=0.0
251 12 my $self = shift;
252 12 return $self->{op};
253 }
254  
255 11 ▶sub args {
Subroutine args: called CC=1 SCAR=0.0
256 11 my $self = shift;
257 11 return @{ $self->{args} };
258 }
259  
260 7 ▶sub execute {
Subroutine execute: called CC=4 SCAR=14.0
261 7 my $self = shift;
262 7 my $model = shift;
263 7 my $op = $self->op;
264 7 ▶ if ($op eq 'A') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'A') { } 4 3 7
elsif ($op eq 'D') { } 2 1 3
elsif ($op eq 'Q') { } 1 0 1
265 4 return $model->add_statement( $self->args );
266 } elsif ($op eq 'D') {
267 2 return $model->remove_statement( $self->args );
268 } elsif ($op eq 'Q') {
269 1 my ($st) = $self->args;
270 1 return $model->get_statements( $st->nodes );
271 } else {
272 0 throw RDF::Trine::Error -text => "Unexpected operation '$op' in RDF::Trine::Parser::RDFPatch::Op->execute";
273 }
274 }
275  
276 1 1;
277  
278 __END__
« RDF/Trine/Statement.pm ↑ Summary RDF/Trine/Store/Memory.pm »