-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.php
180 lines (166 loc) · 5.5 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace librdf;
/* $Id: Parser.php 171 2006-06-15 23:24:18Z das-svn $ */
/**
* Parser, a wrapper around parser.
*
* PHP version 5
*
* Copyright (C) 2006, David Shea <[email protected]>
*
* LICENSE: This package is Free Software and a derivative work of Redland
* http://librdf.org/. This package is not endorsed by Dave Beckett or the
* University of Bristol. It is licensed under the following three licenses as
* alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of the
* above three licenses.
*
* See LICENSE.txt at the top of this package for the complete terms and futher
* detail along with the license tests for the licenses in COPYING.LIB, COPYING
* and LICENSE-2.0.txt repectively.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
/**
*/
use librdf\exception\Error;
use librdf\URI;
use librdf\StreamIterator;
/**
* A wrapper around the parser datatype.
*
* Parsers can be used to particular type of serialized RDF into a
* {@link Model} or to simply convert the serialization into an
* iteration of {@link Statement} objects.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
class Parser
{
/**
* The underlying uri resource.
*
* @var resource
* @access private
*/
private $parser;
/**
* Create a new parser.
*
* Name is the type of parser. Common parsers are "rdfxml", "ntriples" and
* "turtle". Others include "grddl", "guess", "json", "rss-tag-soup" and "trig".
* If all arguments are NULL, any available parser for application/rdf+xml
* will be used. The "guess" type parser is a special parser that picks the
* actual parser to be used based on the content type.
*
* @param string $name The name of the parser to use
* @param string $mime_type The MIME type of the values to parse
* @param string $type_uri The URI of the RDF syntax to parse
* @return void
* @throws Error If unable to create a new parser
* @access public
* @see http://librdf.org/raptor/api/raptor-parsers.html
*/
public function __construct($name=NULL, $mime_type=NULL, $type_uri=NULL)
{
if ($type_uri) {
$type_uri = new URI($type_uri);
}
$this->parser = librdf_new_parser(librdf_php_get_world(),
$name, $mime_type, ($type_uri ? $type_uri->getURI() : $type_uri));
if (!$this->parser) {
throw new Error("Unable to create new parser");
}
}
/**
* Free the parser's resources.
*
* @return void
* @access public
*/
public function __destruct()
{
if ($this->parser) {
librdf_free_parser($this->parser);
}
}
/**
* Return the underlying parser resource.
*
* This function is intended for other LibRDF classes and shoult not
* be called.
*
* @return resource The wrapper parser resource
* @access public
*/
public function getParser()
{
return $this->parser;
}
/**
* Parse a string and return an iterable object over the statements.
*
* The object returned can be used in PHP foreach statements. It is not
* rewindable.
*
* @param string $string The data to parse
* @param string $base_uri The value to use for xml:base abbreviations
* @return StreamIterator An iterator over the Statements parsed from the string
* @throws Error If unable to parse the string
* @access public
*/
public function parseString($string, $base_uri=NULL)
{
if ($base_uri) {
$base_uri = new URI($base_uri);
} else {
$base_uri = new URI(URI::RDF_BASE_URI);
}
$stream = librdf_parser_parse_string_as_stream($this->parser,
$string, $base_uri->getURI());
if (!$stream) {
throw new Error("Unable to parse string");
}
return new StreamIterator($stream, $this);
}
/**
* Parse a URI and return an iterable object over the statements.
*
* The object returned can be used in PHP foreach statements. It is not
* rewindable.
*
* @param string $uri The URI to parse
* @param string $base_uri The value to use for the base URI if different from $uri
* @return StreamIterator An iterator over the Statements parsed from $uri
* @throws Error If unable to parse the URI
* @access public
*/
public function parseURI($uri, $base_uri=NULL)
{
$uri = new URI($uri);
if ($base_uri) {
$base_uri = new URI($base_uri);
}
$stream = librdf_parser_parse_as_stream($this->parser,
$uri->getURI(), ($base_uri ? $base_uri->getURI() : $base_uri));
if (!$stream) {
throw new Error("Unable to parse URI");
}
return new StreamIterator($stream, $this);
}
}
?>