/** * Create a new URI object from a string. * * @param string $uri_string The string to use for the URI * @return void * @throws LibRDF_Error If unable to create a new URI * @access public */ public function __construct($uri_string) { $this->uri = librdf_new_uri(librdf_php_get_world(), $uri_string); if (!$this->uri) { throw new LibRDF_Error("Unable to create new URI from string"); } }
/** * @param $inputStream * @param $baseUri * @param $serialization * @return StatementIterator * @throws Exception */ public function parseStreamToIterator($inputStream, $baseUri = null, $serialization = null) { $rdfUri = librdf_new_uri($this->world, $baseUri); if (false === $rdfUri) { throw new \Exception('Failed to create librdf_uri from: ' . $baseUri); } $data = file_get_contents($inputStream); return $this->parseStringToIterator($data, $baseUri, $serialization); }
/** * @param string $uri URI of the new named node. * @return NamedNode */ public function createNamedNode($uri) { if ($uri === null) { throw new \Exception('Can\'t initialize node with null.'); } if (!NodeUtils::simpleCheckURI($uri)) { throw new \Exception('Invalid URI was given for RDF NamedNode creation.'); } // TODO catch invalid URIs $world = librdf_php_get_world(); $uri = librdf_new_uri($world, $uri); $redlandNode = librdf_new_node_from_uri($world, $uri); if ($redlandNode === null) { throw new \Exception('Initialization of redland node failed.'); } return new NamedNode($redlandNode); }
$nval = '(unbound)'; } print " " . librdf_query_results_get_binding_name($results, $i) . "=" . $nval . "\n"; } print "}\n"; librdf_query_results_next($results); $count++; } if ($results) { print "Returned {$count} results\n"; } $results = null; print "\nExecuting query again\n"; $results = librdf_model_query_execute($model, $query); if ($results) { $str = librdf_query_results_to_string($results, null, null); print "Query results serialized to an XML string size " . strlen($str) . " bytes\n"; } else { print "Query results couldn't be serialized to an XML string\n"; } $serializer = librdf_new_serializer($world, 'rdfxml', null, null); print "Redland serializer created\n"; $base = librdf_new_uri($world, 'http://example.org/base.rdf'); print "Serializing...\n"; librdf_serializer_serialize_model_to_file($serializer, './test-out.rdf', $base, $model); print "Done...\n"; librdf_free_serializer($serializer); librdf_free_uri($base); librdf_free_model($model); librdf_free_storage($storage); print "Done\n";
/** * 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 boolean true if parsing was successful */ 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_Exception("Failed to parse RDF stream for: {$rdfUri}"); } 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::nodeToArray(librdf_statement_get_object($statement)); $graph->add($subject, $predicate, $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); // Success return true; }
/** * Parse an RDF document * * @param string $uri the base URI of the data * @param string $data the document data * @param string $format the format of the input data * @return array the parsed data */ public function parse($uri, $data, $format) { if (!is_string($uri) or $uri == null or $uri == '') { throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty"); } if (!is_string($data) or $data == null or $data == '') { throw new InvalidArgumentException("\$data should be a string and cannot be null or empty"); } if (!is_string($format) or $format == null or $format == '') { throw new InvalidArgumentException("\$format should be a string and cannot be null or empty"); } $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, $uri); if (!$rdfUri) { throw new EasyRdf_Exception("Failed to create librdf_uri from: {$uri}"); } $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri); if (!$stream) { throw new EasyRdf_Exception("Failed to parse RDF stream for: {$rdfUri}"); } $rdfphp = array(); 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::rdfPhpObject(librdf_statement_get_object($statement)); if (!isset($rdfphp[$subject])) { $rdfphp[$subject] = array(); } if (!isset($rdfphp[$subject][$predicate])) { $rdfphp[$subject][$predicate] = array(); } array_push($rdfphp[$subject][$predicate], $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); return $rdfphp; }
/** * Parse an RDF document into an EasyRdf_Graph * * @param string $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 boolean true if parsing was successful */ 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_Exception("Failed to parse RDF stream for: {$rdfUri}"); } $rdfphp = array(); 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::rdfPhpObject(librdf_statement_get_object($statement)); if (!isset($rdfphp[$subject])) { $rdfphp[$subject] = array(); } if (!isset($rdfphp[$subject][$predicate])) { $rdfphp[$subject][$predicate] = array(); } array_push($rdfphp[$subject][$predicate], $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); // FIXME: remove this second parse step return parent::parse($graph, $rdfphp, 'php', $baseUri); }
function append_objects($s0, $p0, $lst) { if (!is_null($s0)) { $s = strlen($s0) > 1 && $s0[0] == '_' && $s0[1] == ':' ? $this->_blankNode($s0) : $this->_uriNode(absolutize($this->_base, $s0)); } if (!is_null($p0)) { $p = $this->_uriNode(absolutize($this->_base, $p0)); } $r = 0; foreach ($lst as $elt) { if (isset($elt['type']) && isset($elt['value'])) { $type = $elt['type']; $value = $elt['value']; $datatype = isset($elt['datatype']) ? $elt['datatype'] : ''; if ($type == 'literal' && $datatype) { //$json = json_encode(array($s0=>array($p0=>array(array('type'=>'literal','value'=>$value))))); $datatype_uri = librdf_new_uri($this->_world, $datatype); $o = librdf_new_node_from_typed_literal($this->_world, $value, NULL, $datatype_uri); //librdf_free_uri($dt); } elseif ($type == 'literal') { $o = $this->_literalNode($value, null); } elseif ($elt['type'] == 'uri') { $o = $this->_uriNode(absolutize($this->_base, $value)); } elseif ($elt['type'] == 'bnode') { $o = $this->_blankNode($value); } $r += librdf_model_add($this->_model, $s, $p, $o) ? 0 : 1; //$o && librdf_free_node($o); } } //$p && librdf_free_node($p); //$s && librdf_free_node($s); return $r; }
/** * Parse an RDF document into an EasyRdf\Graph * * @param 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 * * @throws Exception * @throws \EasyRdf\Exception * @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 Exception("Failed to parse RDF stream"); } do { $statement = librdf_stream_get_object($stream); if ($statement) { $subject = self::nodeUriString(librdf_statement_get_subject($statement)); $predicate = self::nodeUriString(librdf_statement_get_predicate($statement)); $object = self::nodeToRdfPhp(librdf_statement_get_object($statement)); $this->addTriple($subject, $predicate, $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); return $this->tripleCount; }