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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
<?php
namespace EasyRdf\Parser;
/**
* EasyRdf
*
* LICENSE
*
* Copyright (c) 2009-2020 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2020 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
use EasyRdf\Graph;
use EasyRdf\Parser;
/**
* A pure-php class to parse N-Triples with no dependencies.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2020 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
class Ntriples extends Parser
{
/**
* Decodes an encoded N-Triples string. Any \-escape sequences are substituted
* with their decoded value.
*
* @param string $str An encoded N-Triples string.
*
* @return string The unencoded string.
**/
protected function unescapeString($str)
{
if (strpos($str, '\\') === false) {
return $str;
}
$mappings = array(
't' => chr(0x09),
'b' => chr(0x08),
'n' => chr(0x0A),
'r' => chr(0x0D),
'f' => chr(0x0C),
'\"' => chr(0x22),
'\'' => chr(0x27)
);
foreach ($mappings as $in => $out) {
$str = preg_replace('/\x5c([' . $in . '])/', $out, $str);
}
if (stripos($str, '\u') === false) {
return $str;
}
while (preg_match('/\\\(U)([0-9A-F]{8})/', $str, $matches) ||
preg_match('/\\\(u)([0-9A-F]{4})/', $str, $matches)) {
$no = hexdec($matches[2]);
if ($no < 128) { // 0x80
$char = chr($no);
} elseif ($no < 2048) { // 0x800
$char = chr(($no >> 6) + 192) .
chr(($no & 63) + 128);
} elseif ($no < 65536) { // 0x10000
$char = chr(($no >> 12) + 224) .
chr((($no >> 6) & 63) + 128) .
chr(($no & 63) + 128);
} elseif ($no < 2097152) { // 0x200000
$char = chr(($no >> 18) + 240) .
chr((($no >> 12) & 63) + 128) .
chr((($no >> 6) & 63) + 128) .
chr(($no & 63) + 128);
} else {
# FIXME: throw an exception instead?
$char = '';
}
$str = str_replace('\\' . $matches[1] . $matches[2], $char, $str);
}
return $str;
}
/**
* @ignore
*/
protected function parseNtriplesSubject($sub, $lineNum)
{
if (preg_match('/<([^<>]+)>/', $sub, $matches)) {
return $this->unescapeString($matches[1]);
} elseif (preg_match('/_:([A-Za-z0-9]*)/', $sub, $matches)) {
if (empty($matches[1])) {
return $this->graph->newBNodeId();
} else {
$nodeid = $this->unescapeString($matches[1]);
return $this->remapBnode($nodeid);
}
} else {
throw new Exception(
"Failed to parse subject: $sub",
$lineNum
);
}
}
/**
* @ignore
*/
protected function parseNtriplesObject($obj, $lineNum)
{
if (preg_match('/"(.+)"\^\^<([^<>]+)>/', $obj, $matches)) {
return array(
'type' => 'literal',
'value' => $this->unescapeString($matches[1]),
'datatype' => $this->unescapeString($matches[2])
);
} elseif (preg_match('/"(.+)"@([\w\-]+)/', $obj, $matches)) {
return array(
'type' => 'literal',
'value' => $this->unescapeString($matches[1]),
'lang' => $this->unescapeString($matches[2])
);
} elseif (preg_match('/"(.*)"/', $obj, $matches)) {
return array('type' => 'literal', 'value' => $this->unescapeString($matches[1]));
} elseif (preg_match('/<([^<>]+)>/', $obj, $matches)) {
return array('type' => 'uri', 'value' => $this->unescapeString($matches[1]));
} elseif (preg_match('/_:([A-Za-z0-9]*)/', $obj, $matches)) {
if (empty($matches[1])) {
return array(
'type' => 'bnode',
'value' => $this->graph->newBNodeId()
);
} else {
$nodeid = $this->unescapeString($matches[1]);
return array(
'type' => 'bnode',
'value' => $this->remapBnode($nodeid)
);
}
} else {
throw new Exception(
"Failed to parse object: $obj",
$lineNum
);
}
}
/**
* Parse an N-Triples document into an EasyRdf\Graph
*
* @param Graph $graph the graph to load the data into
* @param string $data the RDF document data
* @param string $format the format of the input data
* @param string $baseUri the base URI of the data being parsed
*
* @throws Exception
* @throws \EasyRdf\Exception
* @return integer The number of triples added to the graph
*/
public function parse($graph, $data, $format, $baseUri)
{
parent::checkParseParams($graph, $data, $format, $baseUri);
if ($format != 'ntriples') {
throw new \EasyRdf\Exception(
"EasyRdf\\Parser\\Ntriples does not support: $format"
);
}
$lines = preg_split('/\x0D?\x0A/', strval($data));
foreach ($lines as $index => $line) {
$lineNum = $index + 1;
if (preg_match('/^\s*#/', $line)) {
# Comment
continue;
} elseif (preg_match('/^\s*(.+?)\s+<([^<>]+?)>\s+(.+?)\s*\.\s*$/', $line, $matches)) {
$this->addTriple(
$this->parseNtriplesSubject($matches[1], $lineNum),
$this->unescapeString($matches[2]),
$this->parseNtriplesObject($matches[3], $lineNum)
);
} elseif (preg_match('/^\s*$/', $line)) {
# Blank line
continue;
} else {
throw new Exception(
"Failed to parse statement",
$lineNum
);
}
}
return $this->tripleCount;
}
}
|