This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDFXMLParser.php
203 lines (177 loc) · 8.29 KB
/
RDFXMLParser.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*
* This file is part of Saft.
*
* (c) Konrad Abicht <[email protected]>
* (c) Natanael Arndt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Saft\Data;
use Sabre\Xml\Service;
use Saft\Rdf\NodeFactory;
use Saft\Rdf\RdfHelpers;
use Saft\Rdf\StatementFactory;
use Saft\Rdf\StatementIteratorFactory;
/**
* A handy RDF/XML parser.
*/
class RDFXMLParser implements Parser
{
protected $nodeFactory;
protected $rdfHelpers;
protected $statementFactory;
protected $statementIteratorFactory;
/**
* @param NodeFactory $nodeFactory
* @param StatementFactory $statementFactory
* @param StatementIteratorFactory $statementIteratorFactory
* @param NodeUtils $rdfHelpers
*/
public function __construct(
NodeFactory $nodeFactory,
StatementFactory $statementFactory,
StatementIteratorFactory $statementIteratorFactory,
RdfHelpers $rdfHelpers
) {
$this->nodeFactory = $nodeFactory;
$this->rdfHelpers = $rdfHelpers;
$this->statementFactory = $statementFactory;
$this->statementIteratorFactory = $statementIteratorFactory;
}
/**
* Returns an array of prefixes which where found during the last parsing.
*
* @return array An associative array with a prefix mapping of the prefixes parsed so far. The key
* will be the prefix, while the values contains the according namespace URI.
*/
public function getCurrentPrefixList()
{
// TODO implement a way to get a list of all namespaces used in the last parsed datastring/file.
throw new \Exception('Not implemented yet.');
}
/**
* Parses a given string and returns an iterator containing Statement instances representing the read data.
*
* @param string $inputString data string containing RDF serialized data
* @param string $baseUri The base URI of the parsed content. If this URI is null the inputStreams URL
* is taken as base URI.
*
* @return StatementIterator StatementIterator instaince containing all the Statements parsed by the
* parser to far
*
* @throws \Exception if the base URI $baseUri is no valid URI
*/
public function parseStringToIterator($inputString, $baseUri = null)
{
// check $baseUri
if (null !== $baseUri && false == $this->rdfHelpers->simpleCheckURI($baseUri)) {
throw new \Exception('No base URI support for now. To continue, just leave $baseUri = null.');
}
$service = new Service();
$xmlArray = $service->parse($inputString);
$rdfAboutString = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about';
$rdfDatatypeString = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}datatype';
$rdfDescriptionString = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description';
$rdfResourceString = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource';
$xmlNamespaceLangString = '{http://www.w3.org/XML/1998/namespace}lang';
$statements = [];
// go through all rdf:Description elements
foreach ($xmlArray as $rdfDescription) {
// if its a rdf:Description element
if ($rdfDescription['name'] == $rdfDescriptionString) {
// create subject
$subject = $this->nodeFactory->createNamedNode(
$rdfDescription['attributes'][$rdfAboutString]
);
foreach ($rdfDescription['value'] as $value) {
// if object is a resource
if (isset($value['attributes'][$rdfResourceString])
&& $value['attributes'][$rdfResourceString]) {
// create predicate
$predicate = $this->nodeFactory->createNamedNode(
str_replace(['{', '}'], '', $value['name'])
);
// we know that the object can only be a named node, so add triple
// to statements and go the next entry
$statements[] = $this->statementFactory->createStatement(
$subject,
$predicate,
$this->nodeFactory->createNamedNode(
$value['attributes'][$rdfResourceString]
)
);
continue;
// at least one custom predicate is used
} elseif (isset($rdfDescription['attributes'][$rdfAboutString])
&& $rdfDescription['attributes'][$rdfAboutString]) {
foreach ($rdfDescription['value'] as $objectValue) {
$predicate = $this->nodeFactory->createNamedNode(
str_replace(['{', '}'], '', $objectValue['name'])
);
// object is URI
if (isset($objectValue['attributes'][$rdfResourceString])
&& $this->rdfHelpers->simpleCheckURI(
$objectValue['attributes'][$rdfResourceString])
) {
$object = $this->nodeFactory->createNamedNode(
$objectValue['attributes'][$rdfResourceString]
);
// object is blank node
} elseif ($this->rdfHelpers->simpleCheckBlankNodeId($objectValue['value'])) {
$object = $this->nodeFactory->createBlankNode($objectValue['value']);
// guess object is of type literal
} else {
// check for language
if (isset($objectValue['attributes'][$xmlNamespaceLangString])) {
$lang = $objectValue['attributes'][$xmlNamespaceLangString];
$datatype = null;
// check for datatype
} elseif (isset($objectValue['attributes'][$rdfDatatypeString])) {
$lang = null;
$datatype = $objectValue['attributes'][$rdfDatatypeString];
}
$object = $this->nodeFactory->createLiteral(
str_replace(['{', '}'], '', $objectValue['value']),
$datatype,
$lang
);
}
$statements[] = $this->statementFactory->createStatement(
$subject,
$predicate,
$object
);
continue;
}
}
}
}
}
return $this->statementIteratorFactory->createStatementIteratorFromArray($statements);
}
/**
* Parses a given stream and returns an iterator containing Statement instances
* representing the previously read data. The stream parses the data not as a whole but
* in chunks.
*
* @param string $inputStream filename of the stream to parse which contains RDF
* serialized data
* @param string $baseUri The base URI of the parsed content. If this URI is null,
* the inputStreams URL is taken as base URI. (optional)
*
* @return StatementIterator a StatementIterator containing all the Statements parsed by
* the parser to far
*
* @throws \Exception if the base URI $baseUri is no valid URI
*
* @api
*
* @since 0.1
*/
public function parseStreamToIterator($inputStream, $baseUri = null)
{
return $this->parseStringToIterator(file_get_contents($inputStream), $baseUri);
}
}