|
From: <cw...@us...> - 2007-08-13 11:58:26
|
Revision: 511
http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=511&view=rev
Author: cweiske
Date: 2007-08-13 04:58:24 -0700 (Mon, 13 Aug 2007)
Log Message:
-----------
Prettify all N3Serializer
Modified Paths:
--------------
trunk/rdfapi-php/api/syntax/N3Serializer.php
Modified: trunk/rdfapi-php/api/syntax/N3Serializer.php
===================================================================
--- trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:45:13 UTC (rev 510)
+++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:58:24 UTC (rev 511)
@@ -23,6 +23,7 @@
*
* @author Gunnar AA. Grimnes <ggr...@cs...>
* @author Daniel Westphal <ma...@d-...>
+ * @author Christian Weiske <cw...@cw...>
* @version $Id$
* @package syntax
* @access public
@@ -30,53 +31,57 @@
define('MAGIC_STRING', '~~~');
-class N3Serializer extends Object {
+class N3Serializer extends Object
+{
- var $debug;
+ var $debug = false;
- var $prefixes;
+ var $prefixes;
- var $done; // keeps track of already serialized resources
- var $resourcetext;
- var $resourcetext_taken;
- var $model;
- var $res;
- var $anon;
+ var $done; // keeps track of already serialized resources
+ var $resourcetext;
+ var $resourcetext_taken;
+ var $model;
+ var $res;
+ var $anon;
- var $styleCompress = false;
- var $stylePretty = false;
+ var $styleCompress = false;
+ var $stylePretty = false;
- /**
+ /**
* Constructor
*
* @access public
*/
- function N3Serializer() {
- $this->debug=FALSE;
- }
+ function N3Serializer()
+ {
+ $this->debug = false;
+ }
- /**
- * Adds a new namespace prefix to use.
- * Unknown namespaces will become ns0, ns1 etc.
- * @access public
- * @param string $s
- * @returns void
- **/
+ /**
+ * Adds a new namespace prefix to use.
+ * Unknown namespaces will become ns0, ns1 etc.
+ * @access public
+ * @param string $s
+ * @returns void
+ **/
+ function addNSPrefix( $ns, $prefix)
+ {
+ $this->prefixes[$ns]=$prefix;
+ }
- function addNSPrefix( $ns, $prefix) {
- $this->prefixes[$ns]=$prefix;
- }
- /**
- * Clears all previously set namespace prefixes
- */
- function clearNSPrefixes()
- {
- $this->prefixes = array();
- }
/**
+ * Clears all previously set namespace prefixes
+ */
+ function clearNSPrefixes()
+ {
+ $this->prefixes = array();
+ }
+
+ /**
* Serializes a model to N3 syntax.
*
* @param object Model $model
@@ -185,31 +190,33 @@
return $this->res;
}
-/**
- * Serializes a model and saves it into a file.
- * Returns FALSE if the model couldn't be saved to the file.
- *
- * @param object MemModel $model
- * @param string $filename
- * @return boolean
- * @access public
- */
- function saveAs(&$model, $filename) {
- // serialize model
- $n3 = $this->serialize($model);
- // write serialized model to file
- $file_handle = @fopen($filename, 'w');
- if ($file_handle) {
- fwrite($file_handle, $n3);
- fclose($file_handle);
- return TRUE;
- }else{
- return FALSE;
- };
- }
+ /**
+ * Serializes a model and saves it into a file.
+ * Returns FALSE if the model couldn't be saved to the file.
+ *
+ * @param object MemModel $model
+ * @param string $filename
+ * @return boolean
+ * @access public
+ */
+ function saveAs(&$model, $filename)
+ {
+ // serialize model
+ $n3 = $this->serialize($model);
+ // write serialized model to file
+ $file_handle = @fopen($filename, 'w');
+ if ($file_handle) {
+ fwrite($file_handle, $n3);
+ fclose($file_handle);
+ return true;
+ } else {
+ return false;
+ };
+ }
+
/**
* Set to true, if the N3 serializer should try to compress the blank node
* syntax using [] whereever possible.
@@ -232,42 +239,46 @@
/* ==================== Private Methods from here ==================== */
-/**
- * Readies this object for serializing another model
- * @access private
- * @param void
- * @returns void
- **/
- function reset() {
- $this->anon=0;
- $this->done=array();
- $this->resourcetext_taken=array();
- $this->resourcetext=array();
- $this->res='';
- $this->model=NULL;
- }
+ /**
+ * Readies this object for serializing another model
+ * @access private
+ * @param void
+ * @returns void
+ **/
+ function reset()
+ {
+ $this->anon=0;
+ $this->done=array();
+ $this->resourcetext_taken=array();
+ $this->resourcetext=array();
+ $this->res='';
+ $this->model=NULL;
+ }
-/**
- * Makes ns0, ns1 etc. prefixes for unknown prefixes.
- * Outputs @prefix lines.
- * @access private
- * @param array $n
- * @returns void
- **/
- function doNamespaces(&$n) {
- $c=0;
- foreach ($n as $ns => $nonsense) {
- if ( !$ns ) continue;
- if ( isset($this->prefixes[$ns]) ) {
- $p=$this->prefixes[$ns];
- } else {
- $p='ns'.$c;
- $this->prefixes[$ns]=$p;
- $c++;
- }
- $this->res.="@prefix $p: <".$ns.'> .'.LINEFEED;
+ /**
+ * Makes ns0, ns1 etc. prefixes for unknown prefixes.
+ * Outputs @prefix lines.
+ * @access private
+ * @param array $n
+ * @returns void
+ **/
+ function doNamespaces(&$n)
+ {
+ $c = 0;
+ foreach ($n as $ns => $nonsense) {
+ if (!$ns) {
+ continue;
+ }
+ if (isset($this->prefixes[$ns])) {
+ $p=$this->prefixes[$ns];
+ } else {
+ $p = 'ns' . $c;
+ $this->prefixes[$ns] = $p;
+ $c++;
+ }
+ $this->res .= "@prefix $p: <".$ns.'> .'.LINEFEED;
+ }
}
- }
/**
* Fill in $resourcetext for a single resource.
@@ -381,41 +392,46 @@
$this->resourcetext[$r->getURI()]=$out;
return true;
- }
+ }//function doResource(&$r)
- /**
- * Format a single URI
- * @param string $s
- * @access private
- * @return void
- **/
- function doURI(&$r, &$out) {
- if ( $r->getURI()=='http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
- $out.='a';
- return;
+
+
+ /**
+ * Format a single URI
+ * @param string $s
+ * @access private
+ * @return void
+ **/
+ function doURI(&$r, &$out)
+ {
+ if ($r->getURI() == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
+ $out .= 'a';
+ return;
+ }
+ if ($r->getNamespace()!='') {
+ $out .= $this->prefixes[$r->getNamespace()].':'.$r->getLocalName();
+ } else {
+ //Will this ever happen?
+ $out .= $r->getURI();
+ }
}
- if ($r->getNamespace()!='') {
- $out.=$this->prefixes[$r->getNamespace()].':'.$r->getLocalName();
- } else {
- //Will this ever happen?
- $out.=$r->getURI();
- }
- }
- /**
- * Fix the resourcetext for a blanknode where the _: construct was used
- * @param string $s
- * @param string $a
- * @access private
- * @return void
- **/
- function fixAnon($t,$a) {
- $t=preg_replace("/( \] $|^\[ )/", '', $t);
- return $a.$t;
- }
+ /**
+ * Fix the resourcetext for a blanknode where the _: construct was used
+ * @param string $s
+ * @param string $a
+ * @access private
+ * @return void
+ **/
+ function fixAnon($t,$a)
+ {
+ $t=preg_replace("/( \] $|^\[ )/", '', $t);
+ return $a.$t;
+ }
+
}
?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|