From: <p_f...@us...> - 2007-11-14 09:45:43
|
Revision: 547 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=547&view=rev Author: p_frischmuth Date: 2007-11-14 01:45:39 -0800 (Wed, 14 Nov 2007) Log Message: ----------- [added] initial version of a json parser [added] loadFromString method in Model class, in order to easily load statements from strings (yet only json is supported) [added] test class for new json parser and initial test routine [modified] added constant PACKAGE_SYNTAX_JSON to constants.php [added] SyntaxJSON package in order to load json parser dynamically [propset] added svn:keywords for new file (Id) Modified Paths: -------------- trunk/rdfapi-php/api/constants.php trunk/rdfapi-php/api/model/Model.php Added Paths: ----------- trunk/rdfapi-php/api/syntax/JsonParser.php trunk/rdfapi-php/api/syntax/SyntaxJSON.php trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php Modified: trunk/rdfapi-php/api/constants.php =================================================================== --- trunk/rdfapi-php/api/constants.php 2007-11-06 14:26:06 UTC (rev 546) +++ trunk/rdfapi-php/api/constants.php 2007-11-14 09:45:39 UTC (rev 547) @@ -31,6 +31,7 @@ define('PACKAGE_DBASE','model/DBase.php'); define('PACKAGE_SYNTAX_RDF','syntax/SyntaxRDF.php'); define('PACKAGE_SYNTAX_N3','syntax/SyntaxN3.php'); +define('PACKAGE_SYNTAX_JSON','syntax/SyntaxJSON.php'); define('PACKAGE_SYNTAX_GRDDL','syntax/SyntaxGRDDL.php'); define('PACKAGE_VOCABULARY','vocabulary/Vocabulary.php'); define('PACKAGE_RDQL','rdql/RDQL.php'); Modified: trunk/rdfapi-php/api/model/Model.php =================================================================== --- trunk/rdfapi-php/api/model/Model.php 2007-11-06 14:26:06 UTC (rev 546) +++ trunk/rdfapi-php/api/model/Model.php 2007-11-14 09:45:39 UTC (rev 547) @@ -133,8 +133,27 @@ $this->setBaseURI($temp->getBaseURI()); } + /** + * This method takes a string conatining data and adds the parsed data to this model. + * + * @param string $str The string containing the data to be parsed and loaded. + * @param type $type The type of the string, currently only 'json' is supported. + */ + function loadFromString($str, $type) { + + switch ($type) { + case 'json': + include_once(RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_JSON); + $parser = new JsonParser(); + break; + default: + trigger_error('(class: Model; method: loadFromString): type ' . $type . 'is currently not supported', + E_USER_ERROR); + } + + $parser->generateModelFromString($str, $this); + } - /** * Adds a statement from another model to this model. * If the statement to be added contains a blankNode with an identifier Added: trunk/rdfapi-php/api/syntax/JsonParser.php =================================================================== --- trunk/rdfapi-php/api/syntax/JsonParser.php (rev 0) +++ trunk/rdfapi-php/api/syntax/JsonParser.php 2007-11-14 09:45:39 UTC (rev 547) @@ -0,0 +1,52 @@ +<?php +/** + * This class provides capabilities to parse json encoded rdf models. + * + * @package syntax + * @author Philipp Frischmuth <ph...@fr...> + * @version $Id$ + */ +class JsonParser extends Object { + + /** + * This method takes a json encoded rdf-model and a reference to aa (usually empty) MemModel, parses the json + * string and adds the statements to the given MemModel. + * + * @param string $jsonString The string that contains the rdf model, encoded as a json-string. + * @param MemModel $model A reference to the model, where to add the statements, usually an empty MemModel. + */ + public function generateModelFromString($jsonString, $model) { + + $jsonModel = array(); + $jsonModel = json_decode($jsonString, true); + + // throws an excpetion if json model was corrupt + if (!is_array($jsonModel)) { + throw new Exception('error in json string'); + } + + foreach ($jsonModel as $subject=>$remain) { + foreach ($remain as $predicate=>$object) { + $s = (strpos($subject, '_') === 0) ? new BlankNode(substr($subject, 2)) : new Resource($subject); + $p = new Resource($predicate); + + foreach ($object as $obj) { + if ($obj['type'] === 'uri') { + $o = new Resource($obj['value']); + } else if ($obj['type'] === 'bnode') { + $o = new BlankNode(substr($obj['value'], 2)); + } else { + $dtype = (isset($obj['datatype'])) ? $obj['datatype'] : ''; + $lang = (isset($obj['lang'])) ? $obj['lang'] : ''; + + $o = new Literal($obj['value'], $lang); + $o->setDatatype($dtype); + } + + $model->add(new Statement($s, $p, $o)); + } + } + } + } +} +?> Property changes on: trunk/rdfapi-php/api/syntax/JsonParser.php ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/rdfapi-php/api/syntax/SyntaxJSON.php =================================================================== --- trunk/rdfapi-php/api/syntax/SyntaxJSON.php (rev 0) +++ trunk/rdfapi-php/api/syntax/SyntaxJSON.php 2007-11-14 09:45:39 UTC (rev 547) @@ -0,0 +1,5 @@ +<?php +// Include Syntax classes +// $Id$ +require_once( RDFAPI_INCLUDE_DIR . 'syntax/JsonParser.php' ); +?> \ No newline at end of file Property changes on: trunk/rdfapi-php/api/syntax/SyntaxJSON.php ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php (rev 0) +++ trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php 2007-11-14 09:45:39 UTC (rev 547) @@ -0,0 +1,119 @@ +<?php +require_once RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_JSON; +/** + * Tests the JsonParser + * + * @version $Id $ + * @author Philipp Frischmuth <ph...@fr...> + * + * @package unittests + * @access public + */ + +class testJsonParser extends UnitTestCase +{ + var $modelString; + + function testJsonParser() { + $this->UnitTestCase(); + + GLOBAL $short_datatype; + + $this->modelString = '{ "http://example.org/about" : { + "http://purl.org/dc/elements/1.1/creator" : [ + { + "value" : "Anna Wilder", + "type" : "literal" + } + ], + "http://purl.org/dc/elements/1.1/title" : [ + { + "value" : "Annas Homepage", + "type" : "literal", + "lang" : "en" + } + ], + "http://xmlns.com/foaf/0.1/maker" : [ + { + "value" : "_:person", + "type" : "bnode" + } + ], + "http://purl.org/dc/elements/1.1/title2" : [ + { + "value" : "Anns HP", + "type" : "literal", + "lang" : "en", + "datatype" : "' . $short_datatype['STRING'] . '" + } + ] + }, + "_:person" : { + "http://xmlns.com/foaf/0.1/homepage" : [ + { + "value" : "http://example.org/about", + "type" : "uri" + } + ] + }}'; + } + + function testGenerateModelFromString() { + + $parser = new JsonParser(); + $model = new MemModel('http://example.com/'); + + try { + $parser->generateModelFromString($this->modelString, $model); + } catch (Exception $e) { + $this->fail($e->getMessage()); + } + + GLOBAL $short_datatype; + $model2 = new MemModel('http://example.com/'); + + // Ceate new statements and add them to the model + $statement1 = new Statement(new Resource('http://example.org/about'), + new Resource('http://purl.org/dc/elements/1.1/creator'), + new Literal('Anna Wilder')); + + $statement2 = new Statement(new Resource('http://example.org/about'), + new Resource("http://purl.org/dc/elements/1.1/title"), + new Literal('Annas Homepage', 'en')); + + $statement3 = new Statement(new Resource('http://example.org/about'), + new Resource('http://xmlns.com/foaf/0.1/maker'), + new BlankNode('person')); + + $statement4 = new Statement(new BlankNode('person'), + new Resource("http://xmlns.com/foaf/0.1/homepage"), + new Resource('http://example.org/about')); + + $statement5 = new Statement(new Resource('http://example.org/about'), + new Resource("http://purl.org/dc/elements/1.1/title2"), + new Literal('Anns HP', 'en', $short_datatype['STRING'])); + + $statement6 = new Statement(new Resource('http://example.org/about'), + new Resource("http://purl.org/dc/elements/1.1/title2"), + new Literal('Anns HP', 'en', $short_datatype['INTEGER'])); + + + $model2->add($statement1); + $model2->add($statement2); + $model2->add($statement3); + $model2->add($statement4); + $model2->add($statement5); + + $this->assertTrue($model->containsAll($model2)); + + $model2->remove($statement5); + $model2->add($statement6); + + $this->assertFalse($model->containsAll($model2)); + +#echo "<pre>"; +#print_r($model2); +#echo "</pre>"; + } +} +?> Property changes on: trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php ___________________________________________________________________ Name: svn:keywords + Id This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |