You are here

public function EasyRdf_Parser_Ntriples::parse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php \EasyRdf_Parser_Ntriples::parse()

Parse an N-Triples document into an EasyRdf_Graph

Parameters

object EasyRdf_Graph $graph the graph to load the data into:

string $data the RDF document data:

string $format the format of the input data:

string $baseUri the base URI of the data being parsed:

Return value

integer The number of triples added to the graph

Overrides EasyRdf_Parser::parse

1 method overrides EasyRdf_Parser_Ntriples::parse()
EasyRdf_Parser_Turtle::parse in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php
Parse Turtle into an EasyRdf_Graph

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Ntriples.php, line 176

Class

EasyRdf_Parser_Ntriples
A pure-php class to parse N-Triples with no dependancies.

Code

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 EasyRdf_Parser_Exception("Failed to parse statement", $lineNum);
    }
  }
  return $this->tripleCount;
}