class EasyRdf_Parser_Redland in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Redland.php \EasyRdf_Parser_Redland
Class to parse RDF using Redland (librdf) C library.
@package EasyRdf @copyright Copyright (c) 2009-2013 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php
Hierarchy
- class \EasyRdf_Parser
- class \EasyRdf_Parser_Redland
Expanded class hierarchy of EasyRdf_Parser_Redland
File
- vendor/
easyrdf/ easyrdf/ lib/ EasyRdf/ Parser/ Redland.php, line 45
View source
class EasyRdf_Parser_Redland extends EasyRdf_Parser {
/** Variable set to the librdf world */
private $world = null;
/** Parser feature URI string for getting the error count of last parse. */
const LIBRDF_PARSER_FEATURE_ERROR_COUNT = 'http://feature.librdf.org/parser-error-count';
/*
* Types supported by Redland:
*
* ntriples: N-Triples
* turtle: Turtle Terse RDF Triple Language
* trig: TriG - Turtle with Named Graphs
* rss-tag-soup: RSS Tag Soup
* grddl: Gleaning Resource Descriptions from Dialects of Languages
* guess: Pick the parser to use using content type and URI
* rdfa: RDF/A via librdfa
* raptor: (null)
* rdfxml: RDF/XML
*/
/**
* Convert a librdf node type into a string
* @ignore
*/
protected function nodeTypeString($node) {
switch (librdf_node_get_type($node)) {
case 1:
return 'uri';
break;
case 2:
return 'literal';
break;
case 4:
return 'bnode';
break;
default:
return 'unknown';
break;
}
}
/**
* Convert the URI for a node into a string
* @ignore
*/
protected function nodeUriString($node) {
$type = EasyRdf_Parser_Redland::nodeTypeString($node);
if ($type == 'uri') {
$uri = librdf_node_get_uri($node);
if (!$uri) {
throw new EasyRdf_Exception("Failed to get URI of node");
}
$str = librdf_uri_to_string($uri);
if (!$str) {
throw new EasyRdf_Exception("Failed to convert librdf_uri to string");
}
return $str;
}
elseif ($type == 'bnode') {
return $this
->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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EasyRdf_Parser:: |
protected | property | The base URI for the document currently being parsed | |
EasyRdf_Parser:: |
private | property | Mapping from source to graph bnode identifiers | |
EasyRdf_Parser:: |
protected | property | The format of the document currently being parsed | |
EasyRdf_Parser:: |
protected | property | The current graph to insert triples into | |
EasyRdf_Parser:: |
protected | property | ||
EasyRdf_Parser:: |
protected | function | Add a triple to the current graph, and keep count of the number of triples @ignore | 1 |
EasyRdf_Parser:: |
protected | function | Check, cleanup parameters and prepare for parsing @ignore | |
EasyRdf_Parser:: |
protected | function | Create a new, unique bnode identifier from a source identifier. If the source identifier has previously been seen, the same new bnode identifier is returned. @ignore | |
EasyRdf_Parser:: |
protected | function | Delete the bnode mapping - to be called at the start of a new parse @ignore | |
EasyRdf_Parser_Redland:: |
private | property | Variable set to the librdf world | |
EasyRdf_Parser_Redland:: |
constant | Parser feature URI string for getting the error count of last parse. | ||
EasyRdf_Parser_Redland:: |
protected | function | Convert a node into an associate array @ignore | |
EasyRdf_Parser_Redland:: |
protected | function | Convert a librdf node type into a string @ignore | |
EasyRdf_Parser_Redland:: |
protected | function | Convert the URI for a node into a string @ignore | |
EasyRdf_Parser_Redland:: |
public | function |
Parse an RDF document into an EasyRdf_Graph Overrides EasyRdf_Parser:: |
|
EasyRdf_Parser_Redland:: |
protected | function | Return the number of errors during parsing @ignore | |
EasyRdf_Parser_Redland:: |
public | function | Constructor |