RDF/Trine/Parser/RDFJSON.pm

filter: Statementstmt 88.6% 101 / 114 Branchbran 53.3% 16 / 30 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 19 / 19 Podpod 100% 3 / 3 totaltotal 76.7% 142 / 185 CC 25 SCAR 28.1
CC
25
Cov
77%
parse
36.9
parse_into_model
4.5
BEGIN
3.0
CRAP 16.6
« RDF/Trine/Store.pm ↑ Summary RDF/Trine/Iterator/Bindings.pm »
1 # RDF::Trine::Parser::RDFJSON
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::RDFJSON - RDF/JSON RDF Parser
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::RDFJSON version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15 my $parser = RDF::Trine::Parser->new( 'RDF/JSON' );
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::RDFJSON;
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 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 ▶use base qw(RDF::Trine::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 68 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 68 ▶use RDF::Trine qw(literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use RDF::Trine::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 68 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 68 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 68 our ($VERSION, $rdf, $xsd);
52 68 our ($r_boolean, $r_comment, $r_decimal, $r_double, $r_integer, $r_language, $r_lcharacters, $r_line, $r_nameChar_extra, $r_nameStartChar_minus_underscore, $r_scharacters, $r_ucharacters, $r_booltest, $r_nameStartChar, $r_nameChar, $r_prefixName, $r_qname, $r_resource_test, $r_nameChar_test);
53 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
54 68 $VERSION = '1.019';
55 68 $RDF::Trine::Parser::parser_names{ 'rdfjson' } = __PACKAGE__;
56 68 foreach my $ext (qw(json js)) {
57 136 $RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__;
58 }
59 68 my $class = __PACKAGE__;
60 68 $RDF::Trine::Parser::canonical_media_types{ $class } = 'application/json';
61 68 foreach my $type (qw(application/json application/x-rdf+json)) {
62 136 $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__;
63 }
64 }
65  
66 =item C<< new >>
67  
68 Returns a new RDFJSON parser.
69  
70 =cut
71  
72 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
73 2 my $class = shift;
74 2 my %args = @_;
75 2 my $prefix = '';
76 2 ▶ if (defined($args{ bnode_prefix })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'bnode_prefix'}) { } 0 2 2
77 0 $prefix = $args{ bnode_prefix };
78 } else {
79 2 $prefix = $class->new_bnode_prefix();
80 }
81 2 my $self = bless({
82 bindings => {},
83 bnode_id => 0,
84 bnode_prefix => $prefix,
85 @_
86 }, $class);
87 2 return $self;
88 }
89  
90 =item C<< parse_into_model ( $base_uri, $data, $model [, context => $context] ) >>
91  
92 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. For each RDF
93 statement parsed, will call C<< $model->add_statement( $statement ) >>.
94  
95 =cut
96  
97 2 ▶sub parse_into_model {
Subroutine parse_into_model: called CC=4 SCAR=15.0
Pod documented
98 2 my $proto = shift;
99 2 ▶ my $self = blessed($proto) ? $proto : $proto->new();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proto) ? : 2 0 2
100 2 my $uri = shift;
101 2 ▶ if (blessed($uri) and $uri->isa('RDF::Trine::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")) 0 2 2
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
builtin::blessed($uri) $uri->isa("RDF::Trine::Node::Resource")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
102 0 $uri = $uri->uri_value;
103 }
104 2 my $input = shift;
105 2 my $model = shift;
106 2 my %args = @_;
107 2 my $context = $args{'context'};
108 2 my $opts = $args{'json_opts'};
109
110 4 ▶ my $handler = sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
111 4 my $st = shift;
112 4 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 4 4
113 0 my $quad = RDF::Trine::Statement::Quad->new( $st->nodes, $context );
114 0 $model->add_statement( $quad );
115 } else {
116 4 $model->add_statement( $st );
117 }
118 2 };
119 2 return $self->parse( $uri, $input, $handler, $opts );
120 }
121  
122 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
123  
124 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. Calls the
125 C<< triple >> method for each RDF triple parsed. This method does nothing by
126 default, but can be set by using one of the default C<< parse_* >> methods.
127  
128 =cut
129  
130 2 ▶sub parse {
Subroutine parse: called CC=18 SCAR=36.1
Pod documented
131 2 my $self = shift;
132 2 my $uri = shift;
133 2 my $input = shift;
134 2 my $handler = shift;
135 2 my $opts = shift;
136
137 2 my $index = eval { from_json($input, $opts) };
138 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
139 0 throw RDF::Trine::Error::ParserError -text => "$@";
140 }
141
142 2 foreach my $s (keys %$index) {
143 my $ts = ( $s =~ /^_:(.*)$/ ) ?
144 2 ▶ RDF::Trine::Node::Blank->new($self->{bnode_prefix} . $1) :
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^_:(.*)$/ ? : 1 1 2
145 RDF::Trine::Node::Resource->new($s, $uri);
146
147 2 foreach my $p (keys %{ $index->{$s} }) {
148 2 my $tp = RDF::Trine::Node::Resource->new($p, $uri);
149
150 2 foreach my $O (@{ $index->{$s}->{$p} }) {
151 4 my $to;
152
153 # $O should be a hashref, but we can do a little error-correcting.
154 4 ▶ unless (ref $O) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $O) 1 3 4
155 1 ▶ if ($O =~ /^_:/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($O =~ /^_:/) { } 1 0 1
elsif ($O =~ /^[a-z0-9._\+-]{1,12}:\S+$/i) { } 0 0 0
elsif ($O =~ /^(.*)\@([a-z]{2})$/) { } 0 0 0
156 1 $O = { 'value'=>$O, 'type'=>'bnode' };
157 } elsif ($O =~ /^[a-z0-9._\+-]{1,12}:\S+$/i) {
158 0 $O = { 'value'=>$O, 'type'=>'uri' };
159 } elsif ($O =~ /^(.*)\@([a-z]{2})$/) {
160 0 $O = { 'value'=>$1, 'type'=>'literal', 'lang'=>$2 };
161 } else {
162 0 $O = { 'value'=>$O, 'type'=>'literal' };
163 }
164 }
165
166 4 ▶ if (lc $O->{'type'} eq 'literal') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $O->{'type'} eq 'literal') { } 2 2 4
167 $to = RDF::Trine::Node::Literal->new(
168 2 $O->{'value'}, $O->{'lang'}, $O->{'datatype'});
169 } else {
170 $to = ( $O->{'value'} =~ /^_:(.*)$/ ) ?
171 RDF::Trine::Node::Blank->new($self->{bnode_prefix} . $1) :
172 2 ▶ RDF::Trine::Node::Resource->new($O->{'value'}, $uri);
Branch: 1 branch
BranchTrueFalseTotal
$O->{'value'} =~ /^_:(.*)$/ ? : 1 1 2
173 }
174
175 4 ▶ if ( $ts && $tp && $to ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts and $tp and $to) 4 0 4
Condition33%2/6
$ts and $tp and $to
!ll&&!rl&&r
004
$ts and $tp
!ll&&!rl&&r
040
MC/DC0%0/3
$ts and $tp and $to
$ts $tp $to
Truth table0%0/4
A and B and C
A= $tsB= $tpC= $to
ABCresult
0XX0
10X0
1100
1111
176 4 ▶ if ($self->{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"canonicalize"}) 0 4 4
177 0 ▶ if ($to->isa('RDF::Trine::Node::Literal') and $to->has_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to->isa("RDF::Trine::Node::Literal") and $to->has_datatype) 0 0 0
Condition0%0/3
$to->isa("RDF::Trine::Node::Literal") and $to->has_datatype
!ll&&!rl&&r
000
MC/DC0%0/2
$to->isa("RDF::Trine::Node::Literal") and $to->has_datatype
$to->isa("RDF::Trine::Node::Literal") $to->has_datatype
Truth table0%0/3
A and B
A= $to->isa("RDF::Trine::Node::Literal")B= $to->has_datatype
ABresult
0X0
100
111
178 0 my $value = $to->literal_value;
179 0 my $dt = $to->literal_datatype;
180 0 my $canon = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
181 0 $to = literal( $canon, undef, $dt );
182 }
183 }
184 4 my $st = RDF::Trine::Statement->new($ts, $tp, $to);
185 4 $handler->($st);
186 }
187 }
188 }
189 }
190
191 2 return;
192 }
193  
194  
195 68 1;
196  
197 __END__
« RDF/Trine/Store.pm ↑ Summary RDF/Trine/Iterator/Bindings.pm »