summaryrefslogtreecommitdiffstats
path: root/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php')
-rw-r--r--core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php50
1 files changed, 31 insertions, 19 deletions
diff --git a/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php b/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php
index af3503e..1392b5b 100644
--- a/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php
+++ b/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php
@@ -5,7 +5,7 @@
*
* LICENSE
*
- * Copyright (c) 2009-2012 Nicholas J Humfrey. All rights reserved.
+ * Copyright (c) 2009-2013 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:
@@ -31,16 +31,15 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
- * @copyright Copyright (c) 2009-2012 Nicholas J Humfrey
+ * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
- * @version $Id$
*/
/**
* A pure-php class to parse N-Triples with no dependancies.
*
* @package EasyRdf
- * @copyright Copyright (c) 2009-2012 Nicholas J Humfrey
+ * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
@@ -78,21 +77,22 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
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) {
+ if ($no < 128) { // 0x80
$char = chr($no);
- } elseif ($no < 2048) {
+ } elseif ($no < 2048) { // 0x800
$char = chr(($no >> 6) + 192) .
chr(($no & 63) + 128);
- } elseif ($no < 65536) {
+ } elseif ($no < 65536) { // 0x10000
$char = chr(($no >> 12) + 224) .
chr((($no >> 6) & 63) + 128) .
chr(($no & 63) + 128);
- } elseif ($no < 2097152) {
+ } 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);
@@ -103,7 +103,7 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
/**
* @ignore
*/
- protected function parseNtriplesSubject($sub)
+ protected function parseNtriplesSubject($sub, $lineNum)
{
if (preg_match('/<([^<>]+)>/', $sub, $matches)) {
return $this->unescapeString($matches[1]);
@@ -115,8 +115,9 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
return $this->remapBnode($nodeid);
}
} else {
- throw new EasyRdf_Exception(
- "Failed to parse subject: $sub"
+ throw new EasyRdf_Parser_Exception(
+ "Failed to parse subject: $sub",
+ $lineNum
);
}
}
@@ -124,7 +125,7 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
/**
* @ignore
*/
- protected function parseNtriplesObject($obj)
+ protected function parseNtriplesObject($obj, $lineNum)
{
if (preg_match('/"(.+)"\^\^<([^<>]+)>/', $obj, $matches)) {
return array(
@@ -156,8 +157,9 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
);
}
} else {
- throw new EasyRdf_Exception(
- "Failed to parse object: $obj"
+ throw new EasyRdf_Parser_Exception(
+ "Failed to parse object: $obj",
+ $lineNum
);
}
}
@@ -181,15 +183,25 @@ class EasyRdf_Parser_Ntriples extends EasyRdf_Parser
);
}
- $lines = preg_split("/[\r\n]+/", strval($data));
- foreach ($lines as $line) {
+ $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*\./", $line, $matches)) {
+ } elseif (preg_match("/^\s*(.+?)\s+<([^<>]+?)>\s+(.+?)\s*\.\s*$/", $line, $matches)) {
$this->addTriple(
- $this->parseNtriplesSubject($matches[1]),
+ $this->parseNtriplesSubject($matches[1], $lineNum),
$this->unescapeString($matches[2]),
- $this->parseNtriplesObject($matches[3])
+ $this->parseNtriplesObject($matches[3], $lineNum)
+ );
+ } elseif (preg_match("/^\s*$/", $line)) {
+ # Blank line
+ continue;
+ } else {
+ throw new EasyRdf_Parser_Exception(
+ "Failed to parse statement",
+ $lineNum
);
}
}