line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
# $Id: Parser.pm,v 1.14 2010-12-02 23:41:29 Martin Exp $ |
3
|
|
|
|
|
|
|
|
4
|
8
|
|
|
8
|
|
471474
|
use strict; |
|
8
|
|
|
|
|
70
|
|
|
8
|
|
|
|
|
240
|
|
5
|
8
|
|
|
8
|
|
40
|
use warnings; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
426
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
RDF::Simple::Parser - convert RDF string to bucket of triples |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 DESCRIPTION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
A simple RDF/XML parser - |
14
|
|
|
|
|
|
|
reads a string containing RDF in XML |
15
|
|
|
|
|
|
|
returns a 'bucket-o-triples' (array of arrays) |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $uri = 'http://www.zooleika.org.uk/bio/foaf.rdf'; |
20
|
|
|
|
|
|
|
my $rdf = LWP::Simple::get($uri); |
21
|
|
|
|
|
|
|
my $parser = RDF::Simple::Parser->new(base => $uri) |
22
|
|
|
|
|
|
|
my @triples = $parser->parse_rdf($rdf); |
23
|
|
|
|
|
|
|
# Returns an array of array references which are triples |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 METHODS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=over |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
package RDF::Simple::Parser; |
32
|
|
|
|
|
|
|
|
33
|
8
|
|
|
8
|
|
47
|
use constant DEBUG => 0; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
738
|
|
34
|
|
|
|
|
|
|
|
35
|
8
|
|
|
8
|
|
4577
|
use File::Slurp; |
|
8
|
|
|
|
|
267634
|
|
|
8
|
|
|
|
|
523
|
|
36
|
8
|
|
|
8
|
|
5605
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
366990
|
|
|
8
|
|
|
|
|
330
|
|
37
|
8
|
|
|
8
|
|
4385
|
use RDF::Simple::Parser::Handler; |
|
8
|
|
|
|
|
27
|
|
|
8
|
|
|
|
|
305
|
|
38
|
8
|
|
|
8
|
|
4191
|
use XML::SAX qw(Namespaces Validation); |
|
8
|
|
|
|
|
32352
|
|
|
8
|
|
|
|
|
723
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
our |
41
|
|
|
|
|
|
|
$VERSION = 1.16; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Use a hash to implement objects of this type: |
44
|
|
|
|
|
|
|
use Class::MethodMaker [ |
45
|
8
|
|
|
|
|
61
|
new => 'new', |
46
|
|
|
|
|
|
|
scalar => [ qw/ base http_proxy /, ], |
47
|
8
|
|
|
8
|
|
74
|
]; |
|
8
|
|
|
|
|
29
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item new( [ base => 'http://example.com/foo.rdf' ]) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Create a new RDF::Simple::Parser object. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
'base' supplies a base URI for relative URIs found in the document |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
'http_proxy' optionally supplies the address of an http proxy server. |
56
|
|
|
|
|
|
|
If this is not given, it will try to use the default environment settings. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item parse_rdf($rdf) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Accepts a string which is an RDF/XML document |
63
|
|
|
|
|
|
|
(complete XML, with headers) |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Returns an array of array references which are RDF triples. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub parse_rdf |
70
|
|
|
|
|
|
|
{ |
71
|
12
|
|
|
12
|
1
|
5640
|
my ($self, $rdf) = @_; |
72
|
12
|
|
|
|
|
25
|
DEBUG && print STDERR " DDD Parser::parse_rdf()\n"; |
73
|
12
|
|
|
|
|
399
|
my $handler = new RDF::Simple::Parser::Handler(q{deprecated argument}, |
74
|
|
|
|
|
|
|
qnames => 1, |
75
|
|
|
|
|
|
|
base => $self->base, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
# Save (a reference to) our handler for future reference: |
78
|
12
|
|
|
|
|
76
|
$self->{_handler_} = $handler; |
79
|
12
|
|
|
|
|
87
|
my $factory = new XML::SAX::ParserFactory; |
80
|
12
|
|
|
|
|
2482
|
$factory->require_feature(Namespaces); |
81
|
12
|
|
|
|
|
137
|
my $parser = $factory->parser(Handler => $handler); |
82
|
12
|
|
|
|
|
418541
|
$parser->parse_string($rdf); |
83
|
12
|
|
|
|
|
3052
|
my $res = $handler->result; |
84
|
12
|
100
|
|
|
|
413
|
return $res ? @$res : $res; |
85
|
|
|
|
|
|
|
} # parse_rdf |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item parse_file($sFname) |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Takes one argument, a string which is a fully qualified filename. |
91
|
|
|
|
|
|
|
Reads the contents of that file, |
92
|
|
|
|
|
|
|
parses it as RDF, |
93
|
|
|
|
|
|
|
and returns the same thing as parse_rdf(). |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub parse_file |
98
|
|
|
|
|
|
|
{ |
99
|
5
|
|
|
5
|
1
|
5264
|
my $self = shift; |
100
|
5
|
|
100
|
|
|
22
|
my $sFname = shift || return; |
101
|
3
|
|
50
|
|
|
18
|
my $sRDF = read_file($sFname) || return; |
102
|
3
|
|
|
|
|
474
|
return $self->parse_rdf($sRDF); |
103
|
|
|
|
|
|
|
} # parse_file |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item parse_uri($uri) |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Accepts a string which is a fully qualified http:// uri |
109
|
|
|
|
|
|
|
at which some valid RDF lives. |
110
|
|
|
|
|
|
|
Fetches the remote file and returns the same thing as parse_rdf(). |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub parse_uri |
115
|
|
|
|
|
|
|
{ |
116
|
2
|
|
|
2
|
1
|
446
|
my $self = shift; |
117
|
2
|
|
50
|
|
|
6
|
my $uri = shift || return; |
118
|
0
|
|
|
|
|
0
|
my $rdf; |
119
|
|
|
|
|
|
|
eval |
120
|
0
|
|
|
|
|
0
|
{ |
121
|
|
|
|
|
|
|
# TODO: Just use LWP::Simple->get() and tell user if that's not |
122
|
|
|
|
|
|
|
# sufficient, do it themselves |
123
|
0
|
|
|
|
|
0
|
$rdf = $self->ua->get($uri)->content; |
124
|
|
|
|
|
|
|
}; |
125
|
0
|
0
|
|
|
|
0
|
warn ($@) if $@; |
126
|
0
|
0
|
0
|
|
|
0
|
if ($rdf && ($rdf ne q{})) |
127
|
|
|
|
|
|
|
{ |
128
|
|
|
|
|
|
|
# print STDERR " DDD will parse_rdf(===$rdf===)\n"; |
129
|
0
|
|
|
|
|
0
|
$self->base($uri); |
130
|
0
|
|
|
|
|
0
|
return $self->parse_rdf($rdf); |
131
|
|
|
|
|
|
|
} # if |
132
|
|
|
|
|
|
|
} # parse_uri |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item getns |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Returns a hashref of all namespaces found in the document. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub getns |
141
|
|
|
|
|
|
|
{ |
142
|
3
|
50
|
|
3
|
1
|
2206
|
my $self = shift or return; |
143
|
3
|
50
|
|
|
|
15
|
my $handler = $self->{_handler_} or return; |
144
|
3
|
50
|
|
|
|
11
|
my $ns = $handler->ns or return; |
145
|
3
|
|
|
|
|
21
|
return $ns->{_lookup}; |
146
|
|
|
|
|
|
|
} # getns |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# TODO: get rid of this! Just use LWP |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub ua |
151
|
|
|
|
|
|
|
{ |
152
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
153
|
0
|
0
|
|
|
|
|
unless ($self->{_ua}) { |
154
|
0
|
|
|
|
|
|
$self->{_ua} = LWP::UserAgent->new(timeout => 30); |
155
|
0
|
0
|
|
|
|
|
if ($self->http_proxy) { |
156
|
0
|
|
|
|
|
|
$self->{_ua}->proxy('http',$self->http_proxy); |
157
|
|
|
|
|
|
|
} else { |
158
|
0
|
|
|
|
|
|
$self->{_ua}->env_proxy; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
0
|
|
|
|
|
|
return $self->{_ua}; |
162
|
|
|
|
|
|
|
} # ua |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 BUGS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Please report bugs via the RT web site L |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Jo Walsh |
173
|
|
|
|
|
|
|
Currently maintained by Martin Thurn |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 LICENSE |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This module is available under the same terms as perl itself |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
__END__ |