You are here

public function EasyRdf_Parser_Redland::parse in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Redland.php \EasyRdf_Parser_Redland::parse()

Parse an RDF 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

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Redland.php, line 193

Class

EasyRdf_Parser_Redland
Class to parse RDF using Redland (librdf) C library.

Code

public function parse($graph, $data, $format, $baseUri) {
  parent::checkParseParams($graph, $data, $format, $baseUri);
  $parser = librdf_new_parser($this->world, $format, null, null);
  if (!$parser) {
    throw new EasyRdf_Exception("Failed to create librdf_parser of type: {$format}");
  }
  $rdfUri = librdf_new_uri($this->world, $baseUri);
  if (!$rdfUri) {
    throw new EasyRdf_Exception("Failed to create librdf_uri from: {$baseUri}");
  }
  $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri);
  if (!$stream) {
    throw new EasyRdf_Parser_Exception("Failed to parse RDF stream");
  }
  do {
    $statement = librdf_stream_get_object($stream);
    if ($statement) {
      $subject = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_subject($statement));
      $predicate = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_predicate($statement));
      $object = EasyRdf_Parser_Redland::nodeToRdfPhp(librdf_statement_get_object($statement));
      $this
        ->addTriple($subject, $predicate, $object);
    }
  } while (!librdf_stream_next($stream));
  $errorCount = $this
    ->parserErrorCount($parser);
  if ($errorCount) {
    throw new EasyRdf_Parser_Exception("{$errorCount} errors while parsing.");
  }
  librdf_free_uri($rdfUri);
  librdf_free_stream($stream);
  librdf_free_parser($parser);
  return $this->tripleCount;
}