remapBnode( librdf_node_get_blank_identifier($node) ); } else { throw new EasyRdf_Exception("Unsupported type: ".$type); } } /** * Convert a node into an associate array * @ignore */ protected function nodeToRdfPhp($node) { $object = array(); $object['type'] = EasyRdf_Parser_Redland::nodeTypeString($node); if ($object['type'] == 'uri') { $object['value'] = EasyRdf_Parser_Redland::nodeUriString($node); } elseif ($object['type'] == 'bnode') { $object['value'] = '_:'.librdf_node_get_blank_identifier($node); } elseif ($object['type'] == 'literal') { $object['value'] = librdf_node_get_literal_value($node); $lang = librdf_node_get_literal_value_language($node); if ($lang) { $object['lang'] = $lang; } $datatype = librdf_node_get_literal_value_datatype_uri($node); if ($datatype) { $object['datatype'] = librdf_uri_to_string($datatype); } } else { throw new EasyRdf_Exception("Unsupported type: ".$object['type']); } return $object; } /** * Return the number of errors during parsing * @ignore */ protected function parserErrorCount($parser) { $errorUri = librdf_new_uri( $this->world, self::LIBRDF_PARSER_FEATURE_ERROR_COUNT ); $errorNode = librdf_parser_get_feature($parser, $errorUri); $errorCount = librdf_node_get_literal_value($errorNode); librdf_free_uri($errorUri); return $errorCount; } /** * Constructor * * @return object EasyRdf_Parser_Redland */ public function __construct() { if (extension_loaded('redland')) { $this->world = librdf_php_get_world(); if (!$this->world) { throw new EasyRdf_Exception( "Failed to initialise librdf world." ); } } else { throw new EasyRdf_Exception( "Redland PHP extension is not available." ); } } /** * Parse an RDF document into an EasyRdf_Graph * * @param object EasyRdf_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 * @return integer The number of triples added to the graph */ 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; } }