Permalink
Browse files

bugfixes, ERP static functions

  • Loading branch information...
1 parent 86f539e commit 6500be6a509cd9ee7db5d1f4e58afe3d59a8339f @m0mo committed Aug 22, 2011
View
@@ -8,7 +8,7 @@
* @author Alexander Aigner <alex.aigner (at) gmail.com>
*
* @name Model.php
- * @version 2011-08-12
+ * @version 2011-08-22
* @package model
* @access public
*
@@ -145,6 +145,15 @@ public function getNamespaces() {
public function getStatements() {
return $this->statements;
}
+
+ /**
+ * Returns an array of all stored statements
+ *
+ * @return array
+ */
+ public function getTripples() {
+ return $this->getStatements();
+ }
/**
* Remove a namespace from the model
@@ -504,36 +513,31 @@ public function generateUniqueId() {
* @return bool
*/
public function save($filename, $type ='rdf') {
+
+ switch ($type) {
+ case "rdf":
+ $serializer = ERP::getRDFXMLSerializer();
+ break;
+ case "nt":
+ $serializer = ERP::getNTripleSerializer();
+ break;
- // get suffix and create a corresponding serializer
- if ($type == 'rdf') {
-
- include_once(INCLUDE_DIR."serializers/RDFXMLSerializer.php");
- $ser = new RDFXMLSerializer();
-
- } else if ($type == 'nt') {
-
- include_once(INCLUDE_DIR."serializers/NTripleSerializer.php");
- $ser = new NTripleSerializer();
-
- } else if ($type == 'turtle') {
-
- include_once(INCLUDE_DIR."serializers/TurtleSerializer.php");
- $ser = new TurtleSerializer();
-
- } else if ($type == 'json') {
-
- include_once(INCLUDE_DIR."serializers/JsonSerializer.php");
- $ser = new JsonSerializer();
-
- } else {
- throw new APIException(API_ERROR_FILETYPE);
- };
+ case "turtle":
+ $serializer = ERP::getTurtleSerializer();
+ break;
- return $ser->serialize($filename, $this);
+ case "json":
+ $serializer = ERP::getRDFJsonSerializer();
+ break;
+
+ default :
+ throw new APIException(API_ERROR_FILETYPE);
+ }
+
+ return $serializer->serialize($filename, $this);
}
-
+
/**
* Loads a model from a file and returns it.
*
@@ -542,26 +546,30 @@ public function save($filename, $type ='rdf') {
* @return bool
*/
public function load($filename, $type ='rdf') {
-
- // get suffix and create a corresponding serializer
- if ($type == 'rdf') {
- include_once(INCLUDE_DIR."parsers/RDFXMLParser.php");
- $ser = new RDFXMLParser();
- } else if ($type == 'nt') {
- include_once(INCLUDE_DIR."parsers/NTripleParser.php");
- $ser = new NTripleParser();
- } else if ($type == 'turtle') {
- include_once(INCLUDE_DIR."parsers/TurtleParser.php");
- $ser = new TurtleParser();
- } else if ($type == 'json') {
- include_once(INCLUDE_DIR."parsers/JsonParser.php");
- $ser = new JsonParser();
- } else {
- throw new APIException(API_ERROR_FILETYPE);
- };
-
- return $ser->parse($filename, $this);
-
+
+ switch ($type) {
+ case "rdf":
+ $parser = ERP::getRDFXMLParser();
+ break;
+
+ case "nt":
+ $parser = ERP::getNTripleParser();
+ break;
+
+ case "turtle":
+ $parser = ERP::getTurtleParser();
+ break;
+
+ case "json":
+ $parser = ERP::getRDFJsonParser();
+ break;
+
+ default :
+ throw new APIException(API_ERROR_FILETYPE);
+ }
+
+
+ return $parser->parse($filename, $this);
}
// ------------------------------------------------------------------------
@@ -637,35 +645,29 @@ public function newStatement($subject, $predicate, $object) {
* @return string
*/
public function toString($type = null) {
-
+
// get suffix and create a corresponding serializer
if ($type == 'rdf') {
-
- include_once(INCLUDE_DIR."serializers/RDFXMLSerializer.php");
+
+
$ser = new RDFXMLSerializer();
-
} else if ($type == 'nt') {
-
- include_once(INCLUDE_DIR."serializers/NTripleSerializer.php");
+
+
$ser = new NTripleSerializer();
-
} else if ($type == 'turtle') {
-
- include_once(INCLUDE_DIR."serializers/TurtleSerializer.php");
+
+ include_once(INCLUDE_DIR . "serializers/TurtleSerializer.php");
$ser = new TurtleSerializer();
-
- } else if ($type == 'json') {
-
- include_once(INCLUDE_DIR."serializers/JsonSerializer.php");
+ } else if ($type == 'json') {
+
+ include_once(INCLUDE_DIR . "serializers/JsonSerializer.php");
$ser = new JsonSerializer();
-
} else {
return $this->statemensToString($this->statements);
};
return $ser->serializeToString($this);
-
-
}
/**
View
@@ -0,0 +1,125 @@
+<?php
+
+/**
+ * -----------------------------------------------------------------------------
+ * ERP API
+ * -----------------------------------------------------------------------------
+ *
+ * @author Alexander Aigner <alex.aigner (at) gmail.com>
+ *
+ * @name JsonParser.php
+ * @version 2011-08-22
+ * @package parsers
+ * @access public
+ *
+ * Description Parser for Json.
+ *
+ * -----------------------------------------------------------------------------
+ */
+class JsonParser implements IParser {
+
+ /**
+ * Constructor
+ */
+ function __construct() {
+
+ }
+
+ /**
+ * Loads a file into the model
+ *
+ * @param string $file
+ * @param Model $model
+ * @return bool
+ * @throws APIException
+ */
+ public function parse($file, &$model) {
+
+ if (!Check::isString($file))
+ throw new APIException(API_ERROR . "The filename need to be a string!");
+
+ if (!file_exists($file))
+ throw new APIException(API_ERROR . "The file to parse does not exist!");
+
+
+ $this->transform($file, $model);
+
+ return true;
+ }
+
+ /**
+ * Transforms the file content into a model
+ *
+ * @param string $file
+ * @param Model $model
+ * @throws APIException
+ */
+ private function transform($file, &$model) {
+
+ if (!Check::isModel($model))
+ throw new APIException(API_ERROR_MODEL);
+
+ if (0 == filesize($file)) {
+ throw new APIException(API_ERROR . "File appears to be empty!");
+ }
+
+ $jsonString = "";
+
+ $handle = fopen($file, "r");
+
+ if ($handle) {
+
+ while (($line = fgets($handle, 4096)) !== false) {
+ $jsonString.=$line;
+ }
+
+// if (!feof($handle)) {
+// throw new APIException(API_ERROR . "Unexpected fgets() fail");
+// }
+
+ fclose($handle);
+ }
+
+ $jsonModel = json_decode($jsonString, true);
+
+ // throws an excpetion if json model was corrupt
+ if (!is_array($jsonModel)) {
+ throw new APIException('error in json string');
+ }
+
+ foreach ($jsonModel as $s => $props) {
+
+ $subject = (strpos($s, '_:') === 0) ? new BlankNode(substr($s, 2)) : new Resource($s);
+
+ foreach ($props as $p => $os) {
+
+ $predicate = new Resource($p);
+
+ foreach ($os as $o) {
+
+ switch ($o["type"]) {
+ case "uri":
+ $object = new Resource($o['value']);
+ break;
+
+ case "bnode":
+ $object = new BlankNode(substr($o['value'], 2));
+ break;
+
+ case "literal":
+ $datatype = (isset($o['datatype'])) ? $o['datatype'] : '';
+ $language = (isset($o['lang'])) ? $o['lang'] : '';
+
+ $object = new LiteralNode($o['value'], $datatype, $language);
+ break;
+ }
+
+ $model->add(new Statement($subject, $predicate, $object));
+ }
+ }
+ }
+ }
+
+}
+
+?>
View
@@ -8,7 +8,7 @@
* @author Alexander Aigner <alex.aigner (at) gmail.com>
*
* @name RdfXmlParser.php
- * @version 2011-08-12
+ * @version 2011-08-22
* @package parsers
* @access public
*
@@ -45,8 +45,15 @@ class RDFXMLParser implements IParser {
* @var string
*/
private $file;
-
+
/**
+ * Constructor
+ */
+ function __construct() {
+
+ }
+
+ /**
* Loads an RDFXML-file into the a model an returns it;
*
* @param string $file
Oops, something went wrong.

0 comments on commit 6500be6

Please sign in to comment.