File Coverage

blib/lib/RDF/Trine/Parser/NQuads.pm
Criterion Covered Total %
statement 75 78 96.1
branch 9 14 64.2
condition 2 6 33.3
subroutine 17 17 100.0
pod 1 1 100.0
total 104 116 89.6


line stmt bran cond sub pod time code
1             # RDF::Trine::Parser::NQuads
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Trine::Parser::NQuads - N-Quads Parser
7              
8             =head1 VERSION
9              
10             This document describes RDF::Trine::Parser::NQuads version 1.017
11              
12             =head1 SYNOPSIS
13              
14             use RDF::Trine::Parser;
15             my $parser = RDF::Trine::Parser->new( 'nquads' );
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::NQuads;
32              
33 68     68   434 use strict;
  68         163  
  68         1737  
34 68     68   333 use warnings;
  68         211  
  68         1470  
35 68     68   344 use utf8;
  68         162  
  68         353  
36              
37 68     68   1550 use base qw(RDF::Trine::Parser::NTriples);
  68         150  
  68         4281  
38              
39 68     68   404 use Carp;
  68         164  
  68         3592  
40 68     68   406 use Encode qw(decode);
  68         153  
  68         2475  
41 68     68   385 use Data::Dumper;
  68         190  
  68         2512  
42 68     68   381 use Log::Log4perl;
  68         146  
  68         464  
43 68     68   3948 use Scalar::Util qw(blessed reftype);
  68         164  
  68         3043  
44              
45 68     68   419 use RDF::Trine qw(literal);
  68         151  
  68         2210  
46 68     68   376 use RDF::Trine::Node;
  68         170  
  68         1988  
47 68     68   387 use RDF::Trine::Statement;
  68         205  
  68         1518  
48 68     68   340 use RDF::Trine::Error qw(:try);
  68         148  
  68         435  
49              
50             ######################################################################
51              
52             our ($VERSION);
53             BEGIN {
54 68     68   13426 $VERSION = '1.017';
55 68         238 $RDF::Trine::Parser::parser_names{ 'nquads' } = __PACKAGE__;
56 68         159 $RDF::Trine::Parser::format_uris{ 'http://sw.deri.org/2008/07/n-quads/#n-quads' } = __PACKAGE__;
57 68         226 foreach my $ext (qw(nq)) {
58 68         208 $RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__;
59             }
60 68         206 my $class = __PACKAGE__;
61 68         199 $RDF::Trine::Parser::canonical_media_types{ $class } = 'text/x-nquads';
62 68         171 foreach my $type (qw(text/x-nquads)) {
63 68         20530 $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__;
64             }
65             }
66              
67             ######################################################################
68              
69             =item C<< parse_into_model ( $base_uri, $data, $model ) >>
70              
71             Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. For each RDF triple
72             or quad parsed, will call C<< $model->add_statement( $statement ) >>.
73              
74             =cut
75              
76             sub parse_into_model {
77 6     6 1 33 my $proto = shift;
78 6 50       21 my $self = blessed($proto) ? $proto : $proto->new();
79 6         12 my $uri = shift;
80 6 50 33     21 if (blessed($uri) and $uri->isa('RDF::Trine::Node::Resource')) {
81 0         0 $uri = $uri->uri_value;
82             }
83 6         9 my $input = shift;
84 6         11 my $model = shift;
85 6         11 my %args = @_;
86            
87 6 50       15 if (my $context = $args{'context'}) {
88 0         0 throw RDF::Trine::Error::ParserError -text => "Cannot pass a context node to N-Quads parse_into_model method";
89             }
90            
91             my $handler = sub {
92 8     8   10 my $st = shift;
93 8         32 $model->add_statement( $st );
94 6         19 };
95 6         25 return $self->parse( $uri, $input, $handler );
96             }
97              
98             sub _emit_statement {
99 8     8   14 my $self = shift;
100 8         14 my $handler = shift;
101 8         13 my $nodes = shift;
102 8         13 my $lineno = shift;
103 8         13 my $st;
104            
105 8 100       26 if ($self->{canonicalize}) {
106 5 50 33     34 if ($nodes->[2]->isa('RDF::Trine::Node::Literal') and $nodes->[2]->has_datatype) {
107 5         16 my $value = $nodes->[2]->literal_value;
108 5         13 my $dt = $nodes->[2]->literal_datatype;
109 5         17 my $canon = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
110 5         21 $nodes->[2] = literal( $canon, undef, $dt );
111             }
112             }
113              
114 8 100       33 if (scalar(@$nodes) == 3) {
    50          
115 1         8 $st = RDF::Trine::Statement->new( @$nodes );
116             } elsif (scalar(@$nodes) == 4) {
117 7         37 $st = RDF::Trine::Statement::Quad->new( @$nodes );
118             } else {
119             # warn Dumper($nodes);
120 0         0 throw RDF::Trine::Error::ParserError -text => qq[Not valid N-Quads data at line $lineno];
121             }
122            
123 8         20 $handler->( $st );
124             }
125              
126              
127             1;
128              
129             __END__
130              
131             =back
132              
133             =head1 BUGS
134              
135             Please report any bugs or feature requests to through the GitHub web interface
136             at L<https://github.com/kasei/perlrdf/issues>.
137              
138             =head1 AUTHOR
139              
140             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
141              
142             =head1 COPYRIGHT
143              
144             Copyright (c) 2006-2012 Gregory Todd Williams. This
145             program is free software; you can redistribute it and/or modify it under
146             the same terms as Perl itself.
147              
148             =cut