public function setup() { // Reset to built-in parsers EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Ntriples'); EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_RdfXml'); EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Turtle'); }
} $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; } } ## FIXME: do this automatically EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_Redland'); EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Redland'); EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Redland'); EasyRdf_Format::registerParser('rdfa', 'EasyRdf_Parser_Redland');
* * @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."); } } } ## FIXME: do this automatically EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_Arc'); EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Arc'); EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Arc'); EasyRdf_Format::registerParser('rdfa', 'EasyRdf_Parser_Arc');
} /** * Parse an RDF/XML 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 ($format != 'rdfxml') { throw new EasyRdf_Exception("EasyRdf_Parser_RdfXml does not support: {$format}"); } $this->init($graph, $baseUri); $this->resetBnodeMap(); /* xml parser */ $this->initXMLParser(); /* parse */ if (!xml_parse($this->_xmlParser, $data, false)) { throw new EasyRdf_Exception('XML error: "' . xml_error_string(xml_get_error_code($this->_xmlParser)) . '" at line ' . xml_get_current_line_number($this->_xmlParser)); } xml_parser_free($this->_xmlParser); // Success return true; } } EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_RdfXml');
public function parse($graph, $data, $format, $baseUri) { parent::checkParseParams($graph, $data, $format, $baseUri); if ($format != 'php') { throw new EasyRdf_Exception("EasyRdf_Parser_RdfPhp does not support: {$format}"); } // Reset the bnode mapping $this->resetBnodeMap(); foreach ($data as $subject => $properties) { if (substr($subject, 0, 2) === '_:') { $subject = $this->remapBnode($graph, $subject); } elseif (preg_match('/^\\w+$/', $subject)) { # Cope with invalid RDF/JSON serialisations that # put the node name in, without the _: prefix # (such as net.fortytwo.sesametools.rdfjson) $subject = $this->remapBnode($graph, $subject); } foreach ($properties as $property => $objects) { foreach ($objects as $object) { if ($object['type'] === 'bnode') { $object['value'] = $this->remapBnode($graph, $object['value']); } $graph->add($subject, $property, $object); } } } return true; } } EasyRdf_Format::registerParser('php', 'EasyRdf_Parser_RdfPhp');
} /** * 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); } } } EasyRdf_Format::registerParser('json', 'EasyRdf_Parser_Json');
EasyRdf_Format::register('json', 'RDF/JSON Resource-Centric', 'http://n2.talis.com/wiki/RDF_JSON_Specification', array('application/json' => 1.0, 'text/json' => 0.9, 'application/rdf+json' => 0.9), array('json')); EasyRdf_Format::register('ntriples', 'N-Triples', 'http://www.w3.org/TR/rdf-testcases/#ntriples', array('text/plain' => 1.0, 'text/ntriples' => 0.9, 'application/ntriples' => 0.9, 'application/x-ntriples' => 0.9), array('nt')); EasyRdf_Format::register('turtle', 'Turtle Terse RDF Triple Language', 'http://www.dajobe.org/2004/01/turtle', array('text/turtle' => 0.8, 'application/turtle' => 0.7, 'application/x-turtle' => 0.7), array('ttl')); EasyRdf_Format::register('rdfxml', 'RDF/XML', 'http://www.w3.org/TR/rdf-syntax-grammar', array('application/rdf+xml' => 0.8), array('rdf', 'xrdf')); EasyRdf_Format::register('dot', 'Graphviz', 'http://www.graphviz.org/doc/info/lang.html', array('text/vnd.graphviz' => 0.8), array('gv', 'dot')); EasyRdf_Format::register('json-triples', 'RDF/JSON Triples'); EasyRdf_Format::register('n3', 'Notation3', 'http://www.w3.org/2000/10/swap/grammar/n3#', array('text/n3' => 0.5, 'text/rdf+n3' => 0.5), array('n3')); EasyRdf_Format::register('rdfa', 'RDF/A', 'http://www.w3.org/TR/rdfa/', array('text/html' => 0.4, 'application/xhtml+xml' => 0.4), array('html')); EasyRdf_Format::register('sparql-xml', 'SPARQL XML Query Results', 'http://www.w3.org/TR/rdf-sparql-XMLres/', array('application/sparql-results+xml' => 1.0)); EasyRdf_Format::register('sparql-json', 'SPARQL JSON Query Results', 'http://www.w3.org/TR/rdf-sparql-json-res/', array('application/sparql-results+json' => 1.0)); EasyRdf_Format::register('png', 'Portable Network Graphics (PNG)', 'http://www.w3.org/TR/PNG/', array('image/png' => 0.3), array('png')); EasyRdf_Format::register('gif', 'Graphics Interchange Format (GIF)', 'http://www.w3.org/Graphics/GIF/spec-gif89a.txt', array('image/gif' => 0.2), array('gif')); EasyRdf_Format::register('svg', 'Scalable Vector Graphics (SVG)', 'http://www.w3.org/TR/SVG/', array('image/svg+xml' => 0.3), array('svg')); /* Register default set of parsers and serialisers */ EasyRdf_Format::registerParser('json', 'EasyRdf_Parser_Json'); EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Ntriples'); EasyRdf_Format::registerParser('php', 'EasyRdf_Parser_RdfPhp'); EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_RdfXml'); EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Turtle'); EasyRdf_Format::registerSerialiser('json', 'EasyRdf_Serialiser_Json'); EasyRdf_Format::registerSerialiser('n3', 'EasyRdf_Serialiser_Turtle'); EasyRdf_Format::registerSerialiser('ntriples', 'EasyRdf_Serialiser_Ntriples'); EasyRdf_Format::registerSerialiser('php', 'EasyRdf_Serialiser_RdfPhp'); EasyRdf_Format::registerSerialiser('rdfxml', 'EasyRdf_Serialiser_RdfXml'); EasyRdf_Format::registerSerialiser('turtle', 'EasyRdf_Serialiser_Turtle'); EasyRdf_Format::registerSerialiser('dot', 'EasyRdf_Serialiser_GraphViz'); EasyRdf_Format::registerSerialiser('gif', 'EasyRdf_Serialiser_GraphViz'); EasyRdf_Format::registerSerialiser('png', 'EasyRdf_Serialiser_GraphViz'); EasyRdf_Format::registerSerialiser('svg', 'EasyRdf_Serialiser_GraphViz');
/** * Parse an N-Triples 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 ($format != 'ntriples') { throw new EasyRdf_Exception("EasyRdf_Parser_Ntriples does not support: {$format}"); } $lines = preg_split("/[\r\n]+/", strval($data)); foreach ($lines as $line) { if (preg_match("/^\\s*#/", $line)) { continue; } else { if (preg_match("/(.+)\\s+<([^<>]+)>\\s+(.+)\\s*\\./", $line, $matches)) { $graph->add($this->parseNtriplesSubject($matches[1]), $this->unescape($matches[2]), $this->parseNtriplesObject($matches[3])); } } } // Success return true; } } EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Ntriples');
public function testLoadMockParser() { EasyRdf_Format::registerParser('mock', 'Mock_RdfParser'); $graph = new EasyRdf_Graph(); $graph->load('http://www.example.com/foaf.mock', 'data', 'mock'); $this->assertStringEquals('Joseph Bloggs', $graph->get('http://www.example.com/joe#me', 'foaf:name')); }
$process = proc_open(escapeshellcmd($this->_rapperCmd) . " --quiet " . " --input " . escapeshellarg($format) . " --output json " . " --ignore-errors " . " --input-uri " . escapeshellarg($baseUri) . " --output-uri -" . " - ", $descriptorspec, $pipes, '/tmp', null); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // 2 => readable handle connected to child stderr fwrite($pipes[0], $data); fclose($pipes[0]); $data = stream_get_contents($pipes[1]); fclose($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $returnValue = proc_close($process); if ($returnValue) { throw new EasyRdf_Exception("Failed to parse RDF ({$returnValue}): " . $error); } } else { throw new EasyRdf_Exception("Failed to execute rapper command."); } // Parse in the JSON return parent::parse($graph, $data, 'json', $baseUri); } } ## FIXME: do this automatically EasyRdf_Format::registerParser('rdfxml', 'EasyRdf_Parser_Rapper'); EasyRdf_Format::registerParser('turtle', 'EasyRdf_Parser_Rapper'); EasyRdf_Format::registerParser('ntriples', 'EasyRdf_Parser_Rapper'); EasyRdf_Format::registerParser('rdfa', 'EasyRdf_Parser_Rapper');
public function testRegisterParserForUnknownFormat() { EasyRdf_Format::registerParser('testRegisterParser', 'MockParserClass'); $format = EasyRdf_Format::getFormat('testRegisterParser'); $this->assertNotNull($format); $this->assertEquals('MockParserClass', $format->getParserClass()); }