line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Copyright (c) 2006 IBM Corporation. |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# All rights reserved. This program and the accompanying materials |
5
|
|
|
|
|
|
|
# are made available under the terms of the Eclipse Public License v1.0 |
6
|
|
|
|
|
|
|
# which accompanies this distribution, and is available at |
7
|
|
|
|
|
|
|
# http://www.eclipse.org/legal/epl-v10.html |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# File: $Source: /var/lib/cvs/ODO/lib/ODO/Parser/NTriples.pm,v $ |
10
|
|
|
|
|
|
|
# Created by: Stephen Evanchik( evanchik@us.ibm.com ) |
11
|
|
|
|
|
|
|
# Created on: 10/01/2006 |
12
|
|
|
|
|
|
|
# Revision: $Id: NTriples.pm,v 1.2 2009-11-25 17:54:26 ubuntu Exp $ |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# Contributors: |
15
|
|
|
|
|
|
|
# IBM Corporation - initial API and implementation |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
package ODO::Parser::NTriples; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
42398
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
37
|
|
20
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
554
|
use ODO::Exception; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
65
|
|
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
1
|
|
700
|
use ODO::Node; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
54
|
|
25
|
1
|
|
|
1
|
|
574
|
use ODO::Statement; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
50
|
|
26
|
1
|
|
|
1
|
|
6
|
use vars qw /$VERSION/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
75
|
|
27
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /: (\d+)\.(\d+)/; |
28
|
1
|
|
|
1
|
|
6
|
use base qw/ODO::Parser/; |
|
1
|
|
|
|
|
1370
|
|
|
1
|
|
|
|
|
520
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/base_uri statements/); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 NAME |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
ODO::Parser::NTriples - Parser for statements serialized in NTriples format. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 SYNOPSIS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use ODO::Parser::NTriples; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $statements = ODO::Parser::NTriples->parse_file('some/path/to/data.ntriples'); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $rdf = ' ... ntriples format here ... '; |
43
|
|
|
|
|
|
|
my $other_statements = ODO::Parser::NTriples->parse(\$rdf); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 METHODS |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item parse( $rdf, [ base_uri=> $base_uri ] ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub parse { |
58
|
1
|
|
|
1
|
1
|
105
|
my ( $self, $rdf, %parameters ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
1
|
50
|
|
|
|
20
|
$self = ODO::Parser::NTriples->new(%parameters) |
61
|
|
|
|
|
|
|
unless(ref $self); |
62
|
|
|
|
|
|
|
|
63
|
1
|
50
|
|
|
|
21
|
$rdf = $self->_split_text($rdf) |
64
|
|
|
|
|
|
|
unless(UNIVERSAL::isa($rdf, 'ARRAY')); |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
3
|
foreach my $line (@{ $rdf }) { |
|
1
|
|
|
|
|
3
|
|
67
|
13
|
|
|
|
|
91
|
my $statement = $self->_make_statement($line); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
next |
70
|
13
|
50
|
|
|
|
28
|
unless($statement); |
71
|
|
|
|
|
|
|
|
72
|
13
|
|
|
|
|
33
|
$self->add_statement($statement); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
1
|
|
|
|
|
11
|
return $self->statements(); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item parse_file( $filename, [ base_uri=> $base_uri ] ) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub parse_file { |
84
|
1
|
|
|
1
|
1
|
1363
|
my ($self, $filename, %parameters) = @_; |
85
|
|
|
|
|
|
|
|
86
|
1
|
50
|
|
|
|
9
|
$self = ODO::Parser::NTriples->new(%parameters) |
87
|
|
|
|
|
|
|
unless(ref $self); |
88
|
|
|
|
|
|
|
|
89
|
1
|
50
|
|
|
|
36
|
throw ODO::Exception::File::Missing(error=> "Could not locate file: $filename") |
90
|
|
|
|
|
|
|
unless(-e $filename); |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
40
|
open(RDF_FILE, $filename); |
93
|
1
|
|
|
|
|
38
|
foreach my $line () { |
94
|
|
|
|
|
|
|
|
95
|
13
|
|
|
|
|
128
|
$line =~ s/\n|\r$//; |
96
|
13
|
|
|
|
|
34
|
my $statement = $self->_make_statement($line); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
next |
99
|
13
|
50
|
|
|
|
31
|
unless($statement); |
100
|
|
|
|
|
|
|
|
101
|
13
|
|
|
|
|
29
|
$self->add_statement($statement); |
102
|
|
|
|
|
|
|
} |
103
|
1
|
|
|
|
|
27
|
close(RDF_FILE); |
104
|
|
|
|
|
|
|
|
105
|
1
|
|
|
|
|
4
|
return $self->statements(); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub init { |
111
|
2
|
|
|
2
|
1
|
64
|
my ($self, $config) = @_; |
112
|
2
|
|
|
|
|
12
|
$self->base_uri( $config->{'base_uri'} ); |
113
|
2
|
|
|
|
|
35
|
$self->statements( [] ); |
114
|
2
|
|
|
|
|
13
|
return $self; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _split_text { |
119
|
1
|
|
|
1
|
|
2
|
shift; |
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
|
|
4
|
my $rdf_text = shift; |
122
|
1
|
|
|
|
|
27
|
my @statements = split(/\r\n|\n|\r/, $rdf_text); |
123
|
|
|
|
|
|
|
|
124
|
1
|
|
|
|
|
4
|
return \@statements; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub _make_statement { |
128
|
26
|
|
|
26
|
|
45
|
my ($self, $line) = @_; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
return undef |
131
|
26
|
50
|
33
|
|
|
220
|
unless($line && $line !~ /^#/ && $line !~ /^[\ \t]+$/); |
|
|
|
33
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
26
|
50
|
|
|
|
129
|
throw ODO::Exception::Parameter::Invalid(error=> "Line does not end with a '.': '$line'") |
134
|
|
|
|
|
|
|
unless($line =~ m/.*[\ \t]+\.[\ \t]*$/); |
135
|
|
|
|
|
|
|
|
136
|
26
|
|
|
|
|
134
|
my ($s, $p, $o ) = split(/[\ \t]+/, $line); |
137
|
|
|
|
|
|
|
|
138
|
26
|
50
|
33
|
|
|
158
|
throw ODO::Exception::Parameter::Invalid(error=> 'Could not split line in to components of a statement') |
|
|
|
33
|
|
|
|
|
139
|
|
|
|
|
|
|
unless($s && $p && $o); |
140
|
|
|
|
|
|
|
|
141
|
26
|
|
|
|
|
64
|
$s = $self->make_node($s); |
142
|
26
|
|
|
|
|
186
|
$p = $self->make_node($p); |
143
|
26
|
|
|
|
|
187
|
$o = $self->make_node($o); |
144
|
|
|
|
|
|
|
|
145
|
26
|
50
|
33
|
|
|
358
|
throw ODO::Exception::Parameter::Invalid(error=> "Could not create ODO::Node's for the raw text") |
|
|
|
33
|
|
|
|
|
146
|
|
|
|
|
|
|
unless( UNIVERSAL::isa($s, 'ODO::Node') |
147
|
|
|
|
|
|
|
&& UNIVERSAL::isa($p, 'ODO::Node') |
148
|
|
|
|
|
|
|
&& UNIVERSAL::isa($o, 'ODO::Node') |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
|
151
|
26
|
|
|
|
|
96
|
my $t = ODO::Statement->new('s'=> $s, 'p'=> $p, 'o'=> $o); |
152
|
|
|
|
|
|
|
|
153
|
26
|
50
|
|
|
|
202
|
throw ODO::Exception::Parameters::Invalid("Could not create statement for: $s, $p, $o") |
154
|
|
|
|
|
|
|
unless(UNIVERSAL::isa($t, 'ODO::Statement')); |
155
|
|
|
|
|
|
|
|
156
|
26
|
|
|
|
|
56
|
return $t; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub add_statement { |
160
|
26
|
|
|
26
|
0
|
39
|
my ($self, $statement) = @_; |
161
|
26
|
|
|
|
|
31
|
push @{ $self->statements() }, $statement; |
|
26
|
|
|
|
|
97
|
|
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub make_node { |
165
|
78
|
|
|
78
|
0
|
115
|
my ($self, $node) = @_; |
166
|
|
|
|
|
|
|
|
167
|
78
|
100
|
|
|
|
376
|
if($node =~ /^_:(\w+|\d+)/) { |
|
|
100
|
|
|
|
|
|
168
|
2
|
|
|
|
|
13
|
return ODO::Node::Blank->new($1); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
elsif($node =~ /^<(.*)>$/) { |
171
|
74
|
|
|
|
|
237
|
return ODO::Node::Resource->new($1); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
else { |
174
|
2
|
|
|
|
|
8
|
return $self->make_literal($node); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub make_literal { |
180
|
2
|
|
|
2
|
0
|
3
|
my ($self, $raw) = @_; |
181
|
2
|
|
|
|
|
12
|
my $literal = ODO::Node::Literal->new(); |
182
|
|
|
|
|
|
|
|
183
|
2
|
|
|
|
|
20
|
my $value; |
184
|
|
|
|
|
|
|
my $datatype; |
185
|
0
|
|
|
|
|
0
|
my $language; |
186
|
|
|
|
|
|
|
|
187
|
2
|
50
|
|
|
|
12
|
if( ( ( $value, $datatype ) = split('^^', $raw)) ) { |
|
|
0
|
|
|
|
|
|
188
|
2
|
50
|
|
|
|
8
|
if($value =~ /"(.*)"/) { |
189
|
0
|
|
|
|
|
0
|
$value = $1; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
2
|
50
|
|
|
|
6
|
$literal->datatype($datatype) |
193
|
|
|
|
|
|
|
if($datatype); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
elsif( ( ( $value, $language ) = split('@', $raw)) ) { |
196
|
0
|
0
|
|
|
|
0
|
if($value =~ /"(.*)"/) { |
197
|
0
|
|
|
|
|
0
|
$value = $1; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
0
|
0
|
|
|
|
0
|
$literal->language($language) |
201
|
|
|
|
|
|
|
if($language); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
else { |
204
|
0
|
0
|
|
|
|
0
|
if($raw=~ /^\w*"(.*)"\w*$/) { |
205
|
0
|
|
|
|
|
0
|
$value = $1; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
else { |
208
|
0
|
|
|
|
|
0
|
return undef; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
2
|
|
|
|
|
21
|
$literal->value($value); |
213
|
|
|
|
|
|
|
|
214
|
2
|
|
|
|
|
16
|
return $literal; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=back |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 COPYRIGHT |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Copyright (c) 2006 IBM Corporation. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials |
225
|
|
|
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0 |
226
|
|
|
|
|
|
|
which accompanies this distribution, and is available at |
227
|
|
|
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=cut |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
1; |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
__END__ |