/** * Parse a RDF/JSON 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); if ($format != 'json') { throw new EasyRdf_Exception("EasyRdf_Parser_Json does not support: {$format}"); } $rdfphp = json_decode(strval($data), true); return parent::parse($graph, $rdfphp, 'php', $baseUri); }
/** * 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); if (array_key_exists($format, self::$_supportedTypes)) { $className = self::$_supportedTypes[$format]; } else { throw new EasyRdf_Exception("EasyRdf_Parser_Arc does not support: {$format}"); } $parser = ARC2::getParser($className); if ($parser) { $parser->parse($baseUri, $data); $rdfphp = $parser->getSimpleIndex(false); return parent::parse($graph, $rdfphp, 'php', $baseUri); } else { throw new EasyRdf_Exception("ARC2 failed to get a {$className} parser."); } }
/** * Parse RDF/JSON 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); if ($format != 'json') { throw new EasyRdf_Exception("EasyRdf_Parser_Json does not support: {$format}"); } // Reset the bnode mapping $this->resetBnodeMap(); $decoded = @json_decode(strval($data), true); if ($decoded === null) { throw new EasyRdf_Exception($this->_jsonLastErrorString()); } if (array_key_exists('triples', $decoded)) { return $this->_parseJsonTriples($graph, $decoded, $baseUri); } else { return parent::parse($graph, $decoded, 'php', $baseUri); } }
/** * Parse RDF/JSON 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); if ($format != 'json') { throw new EasyRdf_Exception("EasyRdf_Parser_Json does not support: {$format}"); } // Reset the bnode mapping $this->resetBnodeMap(); $decoded = @json_decode(strval($data), true); if (!$decoded) { # FIXME: can we get an error message for json_decode? throw new EasyRdf_Exception("Failed to parse RDF/JSON"); } if (array_key_exists('triples', $decoded)) { return $this->_parseJsonTriples($graph, $decoded, $baseUri); } else { return parent::parse($graph, $decoded, 'php', $baseUri); } }
/** * 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); }
/** * Parse an N-Triples 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); if ($format != 'ntriples') { throw new EasyRdf_Exception("EasyRdf_Parser_Ntriples does not support: {$format}"); } $rdfphp = array(); $lines = preg_split("/[\r\n]+/", strval($data)); foreach ($lines as $line) { if (preg_match("/(.+)\\s+<([^<>]+)>\\s+(.+)\\s*\\./", $line, $matches)) { $subject = $this->parseNtriplesSubject($matches[1]); $predicate = $matches[2]; $object = $this->parseNtriplesObject($matches[3]); if (!isset($rdfphp[$subject])) { $rdfphp[$subject] = array(); } if (!isset($rdfphp[$subject][$predicate])) { $rdfphp[$subject][$predicate] = array(); } array_push($rdfphp[$subject][$predicate], $object); } } # FIXME: generate objects directly, instead of this second stage return parent::parse($graph, $rdfphp, 'php', $baseUri); }