/**
  * 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;
 }
示例#2
0
    }
}
$world = librdf_php_get_world();
print "Redland world opened\n";
$storage = librdf_new_storage($world, 'hashes', 'dummy', "new=yes,hash-type='memory'");
print "Redland storage created\n";
$model = librdf_new_model($world, $storage, '');
print "Redland model created\n";
$parser = librdf_new_parser($world, 'rdfxml', 'application/rdf+xml', null);
print "Redland parser created\n";
$uri = librdf_new_uri($world, 'file:../data/dc.rdf');
print "Parsing...\n";
librdf_parser_parse_into_model($parser, $uri, $uri, $model);
print "Done...\n";
librdf_free_uri($uri);
librdf_free_parser($parser);
$query = librdf_new_query($world, 'sparql', null, "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c ?d WHERE { ?a dc:title ?c . OPTIONAL { ?a dc:related ?d } }", null);
print "Querying for dc:titles:\n";
$results = librdf_model_query_execute($model, $query);
$count = 1;
while ($results && !librdf_query_results_finished($results)) {
    print "result {$count}: {\n";
    for ($i = 0; $i < librdf_query_results_get_bindings_count($results); $i++) {
        $val = librdf_query_results_get_binding_value($results, $i);
        if ($val) {
            $nval = librdf_node_to_string($val);
        } else {
            $nval = '(unbound)';
        }
        print "  " . librdf_query_results_get_binding_name($results, $i) . "=" . $nval . "\n";
    }
示例#3
0
 /**
  * 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;
 }
示例#4
0
 /**
  * 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);
 }
示例#5
0
文件: Graph.php 项目: sgml/rww.io
 function append_file($content_type, $file, $base = null)
 {
     $p = librdf_new_parser($this->_world, $content_type, null, null);
     $file_uri = librdf_new_uri($this->_world, $file);
     $base_uri = librdf_new_uri($this->_world, is_null($base) ? $this->_base : $base);
     $r = librdf_parser_parse_into_model($p, $file_uri, $base_uri, $this->_model);
     librdf_free_parser($p);
     librdf_free_uri($base_uri);
     librdf_free_uri($file_uri);
     return $r == 0;
 }
示例#6
0
 /**
  * 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;
 }
示例#7
0
文件: Parser.php 项目: bakulf/raop
 /**
  * Free the parser's resources.
  *
  * @return  void
  * @access  public
  */
 public function __destruct()
 {
     if ($this->parser) {
         librdf_free_parser($this->parser);
     }
 }