Add escaping to N-Quads.

This commit is contained in:
Dave Longley 2012-05-13 16:31:11 -04:00
parent 00c18e853e
commit 5149bf383c

View file

@ -903,15 +903,15 @@ class JsonLdProcessor {
// parse quad // parse quad
if(!preg_match($quad, $line, $match)) { if(!preg_match($quad, $line, $match)) {
throw new JsonLdException( throw new JsonLdException(
'Error while parsing N-Quads; invalid quad.', 'Error while parsing N-Quads; invalid quad.',
'jsonld.ParseError', array('line' => $line_number)); 'jsonld.ParseError', array('line' => $line_number));
} }
// create RDF statement // create RDF statement
$s = (object)array( $s = (object)array(
'subject' => new stdClass(), 'subject' => new stdClass(),
'property' => new stdClass(), 'property' => new stdClass(),
'object' => new stdClass()); 'object' => new stdClass());
// get subject // get subject
if($match[1] !== '') { if($match[1] !== '') {
@ -936,12 +936,16 @@ class JsonLdProcessor {
$s->object->nominalValue = $match[5]; $s->object->nominalValue = $match[5];
$s->object->interfaceName = 'BlankNode'; $s->object->interfaceName = 'BlankNode';
} }
else { else {
$s->object->nominalValue = $match[6]; $unescaped = str_replace(
array('\"', '\t', '\n', '\r', '\\\\'),
array('"', "\t", "\n", "\r", '\\'),
$match[6]);
$s->object->nominalValue = $unescaped;
$s->object->interfaceName = 'LiteralNode'; $s->object->interfaceName = 'LiteralNode';
if(isset($match[7]) && $match[7] !== '') { if(isset($match[7]) && $match[7] !== '') {
$s->object->datatype = (object)array( $s->object->datatype = (object)array(
'nominalValue' => $match[7], 'interfaceName' => 'IRI'); 'nominalValue' => $match[7], 'interfaceName' => 'IRI');
} }
else if(isset($match[8]) && $match[8] !== '') { else if(isset($match[8]) && $match[8] !== '') {
$s->object->language = $match[8]; $s->object->language = $match[8];
@ -951,11 +955,11 @@ class JsonLdProcessor {
// get graph // get graph
if(isset($match[9]) && $match[9] !== '') { if(isset($match[9]) && $match[9] !== '') {
$s->name = (object)array( $s->name = (object)array(
'nominalValue' => $match[9], 'interfaceName' => 'IRI'); 'nominalValue' => $match[9], 'interfaceName' => 'IRI');
} }
else if(isset($match[10]) && $match[10] !== '') { else if(isset($match[10]) && $match[10] !== '') {
$s->name = (object)array( $s->name = (object)array(
'nominalValue' => $match[10], 'interfaceName' => 'BlankNode'); 'nominalValue' => $match[10], 'interfaceName' => 'BlankNode');
} }
// add statement // add statement
@ -1012,8 +1016,12 @@ class JsonLdProcessor {
$quad .= $o->nominalValue; $quad .= $o->nominalValue;
} }
} }
else { else {
$quad .= '"' . $o->nominalValue . '"'; $escaped = str_replace(
array('\\', "\t", "\n", "\r", '"'),
array('\\\\', '\t', '\n', '\r', '\"'),
$o->nominalValue);
$quad .= '"' . $escaped . '"';
if(property_exists($o, 'datatype')) { if(property_exists($o, 'datatype')) {
$quad .= "^^<{$o->datatype->nominalValue}>"; $quad .= "^^<{$o->datatype->nominalValue}>";
} }