|
From: <cw...@us...> - 2007-08-13 09:55:41
|
Revision: 506
http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=506&view=rev
Author: cweiske
Date: 2007-08-13 02:55:33 -0700 (Mon, 13 Aug 2007)
Log Message:
-----------
Don't clear namespaces when serializing - this removes
the option to add own prefixes
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 08:47:21 UTC (rev 505)
+++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 09:55:33 UTC (rev 506)
@@ -5,25 +5,24 @@
// ----------------------------------------------------------------------------------
/**
- * PHP Notation3 Serializer
- *
+ * PHP Notation3 Serializer
+ *
* This class serialises models to N3 Syntax.
- *
- * Supported N3 features:
+ *
+ * Supported N3 features:
* <ul>
- * <li>Using [ ] for blank nodes, or _: if nescessary</li>
- * <li>Literal datytype- and xmlLanguageTag support</li>
+ * <li>Using [ ] for blank nodes, or _: if necessary</li>
+ * <li>Literal datatype- and xmlLanguageTag support</li>
* </ul>
- *
- * Un-supported N3 Features include:
+ *
+ * Un-supported N3 Features include:
* <ul>
* <li>Reification</li>
* </ul>
*
*
- * TODO: * added namespace prefixes are persisent...
- *
- * @author Gunnar AA. Grimnes <ggr...@cs...>, Daniel Westphal <ma...@d-...>
+ * @author Gunnar AA. Grimnes <ggr...@cs...>
+ * @author Daniel Westphal <ma...@d-...>
* @version $Id$
* @package syntax
* @access public
@@ -33,15 +32,15 @@
class N3Serializer extends Object {
- var $debug;
+ var $debug;
- var $prefixes;
+ var $prefixes;
var $done; // keeps track of already serialized resources
- var $resourcetext;
- var $resourcetext_taken;
+ var $resourcetext;
+ var $resourcetext_taken;
var $model;
- var $res;
+ var $res;
var $anon;
/**
@@ -49,120 +48,129 @@
*
* @access public
*/
- function N3Serializer() {
+ function N3Serializer() {
$this->debug=FALSE;
}
- /**
- * Adds a new namespace prefix to use.
- * Unknown namespaces will become ns0, ns1 etc.
+ /**
+ * Adds a new namespace prefix to use.
+ * Unknown namespaces will become ns0, ns1 etc.
* @access public
* @param string $s
- * @returns void
+ * @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();
+ }
+
+ /**
* Serializes a model to N3 syntax.
*
* @param object Model $model
* @return string
* @access public
*/
- function & serialize(&$m) {
+ function & serialize(&$m) {
- if (is_a($m, 'DbModel'))
+ if (is_a($m, 'DbModel'))
$m=$m->getMemModel();
-
+
$this->reset();
$this->model=$m;
- $this->res="";
-
+ $this->res="";
+
+var_dump($this->prefixes);
// copy default namespaces
global $default_prefixes;
foreach($default_prefixes as $prefix => $namespace)
$this->addNSPrefix($namespace,$prefix);
-
- $nps= $this->model->getParsedNamespaces();
+
+ $nps= $this->model->getParsedNamespaces();
if($nps!=false){
foreach($nps as $uri => $prefix){
$this->addNSPrefix($uri,$prefix);
}
}
-
+var_dump($this->prefixes);
$namespaces=array();
$count=array();
- $resources=array();
- foreach ($this->model->triples as $t) {
+ $resources=array();
+ foreach ($this->model->triples as $t) {
$s=$t->getSubject();
- if ( is_a($s, 'Resource'))
+ if ( is_a($s, 'Resource'))
$namespaces[$s->getNamespace()]=1;
$p=$t->getPredicate();
- if ( is_a($p, 'Resource'))
+ if ( is_a($p, 'Resource'))
$namespaces[$p->getNamespace()]=1;
$o=$t->getObject();
- if ( is_a($o, 'Resource'))
+ if ( is_a($o, 'Resource'))
$namespaces[$o->getNamespace()]=1;
-
+
$uri=$s->getURI();
-
- if (isset($count[$uri])) {
+
+ if (isset($count[$uri])) {
$count[$uri]++;
- } else {
+ } else {
$count[$uri]=0;
$resources[$uri]=$s;
}
}
- if (!HIDE_ADVERTISE)
+ if (!HIDE_ADVERTISE)
$this->res .= '# Generated by N3Serializer.php from RDF RAP.'.LINEFEED
.'# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html'
.LINEFEED.LINEFEED;
- $this->doNamespaces($namespaces);
+ $this->doNamespaces($namespaces);
$this->res.=LINEFEED.LINEFEED;
arsort($count);
-
- foreach ( $count as $k=>$v) {
- $this->doResource($resources[$k]);
+
+ foreach ( $count as $k=>$v) {
+ $this->doResource($resources[$k]);
// $this->res.=" .\n";
}
$c=0;
- foreach ( $this->resourcetext as $r=>$t) {
+ foreach ( $this->resourcetext as $r=>$t) {
if ( preg_match_all('/'.MAGIC_STRING.'([^ ]+)'.MAGIC_STRING.'/', $t, $ms, PREG_SET_ORDER)) {
- foreach($ms as $mseach) {
+ foreach($ms as $mseach) {
$rp=$this->resourcetext[$mseach[1]];
$t=preg_replace('/'.MAGIC_STRING.$mseach[1].MAGIC_STRING.'/', $rp, $t);
}
}
if ($this->debug) $this->res.=$c.': ';
- if ( !( isset($this->resourcetext_taken[$r]) && $this->resourcetext_taken[$r]>0) )
- $this->res.=$t.' .'.LINEFEED;
- else if ( $this->debug )
+ if ( !( isset($this->resourcetext_taken[$r]) && $this->resourcetext_taken[$r]>0) )
+ $this->res.=$t.' .'.LINEFEED;
+ else if ( $this->debug )
$this->res.=' Skipping : '.$t.LINEFEED;
$c++;
- }
+ }
// $max=-1111;
// $maxkey="";
-// foreach ($count as $k=>$c) {
+// foreach ($count as $k=>$c) {
// if ( $c>$max) { $maxkey=$k; $max=$c; }
// }
-
-// if ($this->debug) {
-// print "$maxkey is subject of most triples! ($max) \n";
+
+// if ($this->debug) {
+// print "$maxkey is subject of most triples! ($max) \n";
// }
return $this->res;
@@ -192,8 +200,8 @@
return FALSE;
};
}
-
+
/* ==================== Private Methods from here ==================== */
@@ -201,7 +209,7 @@
* Readies this object for serializing another model
* @access private
* @param void
- * @returns void
+ * @returns void
**/
function reset() {
$this->anon=0;
@@ -210,23 +218,22 @@
$this->resourcetext=array();
$this->res='';
$this->model=NULL;
- unset($this->prefixes);
}
/**
- * Makes ns0, ns1 etc. prefixes for unknown prefixes.
- * Outputs @prefix lines.
+ * Makes ns0, ns1 etc. prefixes for unknown prefixes.
+ * Outputs @prefix lines.
* @access private
* @param array $n
- * @returns void
+ * @returns void
**/
- function doNamespaces(&$n) {
+ function doNamespaces(&$n) {
$c=0;
- foreach ($n as $ns => $nonsense) {
- if ( !$ns ) continue;
+ foreach ($n as $ns => $nonsense) {
+ if ( !$ns ) continue;
if ( isset($this->prefixes[$ns]) ) {
$p=$this->prefixes[$ns];
- } else {
+ } else {
$p='ns'.$c;
$this->prefixes[$ns]=$p;
$c++;
@@ -236,42 +243,42 @@
}
/**
- * Fill in $resourcetext for a single resource.
- * Will recurse into Objects of triples, but should never look ? (really?)
+ * Fill in $resourcetext for a single resource.
+ * Will recurse into Objects of triples, but should never look ? (really?)
* @access private
* @param object Resource $r
* @returns boolean
**/
- function doResource(&$r) {
- // print $r->getURI();
+ function doResource(&$r) {
+ // print $r->getURI();
$ts=$this->model->find($r, null, null);
- if (count($ts->triples)==0) return;
-
+ if (count($ts->triples)==0) return;
+
$out="";
if ( isset($this->done[$r->getURI()]) && $this->done[$r->getURI()] ) {
- if ( is_a($r, 'BlankNode')) {
-
- if ( $this->resourcetext_taken[$r->getURI()] == 1) {
- //Oh bother, we must use the _:blah construct.
+ if ( is_a($r, 'BlankNode')) {
+
+ if ( $this->resourcetext_taken[$r->getURI()] == 1) {
+ //Oh bother, we must use the _:blah construct.
$a=$this->resourcetext[$r->getURI()];
$this->resourcetext[$r->getURI()]='_:anon'.$this->anon;
$this->resourcetext['_:anon'.$this->anon]=$this->fixAnon($a, '_:anon'.$this->anon);
- $this->resourcetext_taken[$r->getURI()]=2;
+ $this->resourcetext_taken[$r->getURI()]=2;
$this->anon++;
}
}
- return false;
+ return false;
}
$this->done[$r->getURI()]=TRUE;
if ( is_a($r, 'Resource') ) {
- if ( is_a($r, 'BlankNode') ) {
+ if ( is_a($r, 'BlankNode') ) {
//test, if this blanknode is referenced somewhere
$rbn=$this->model->find(null, null, $r);
@@ -280,51 +287,51 @@
$out.='_:'.$r->getLabel();
} else {
$out.='[ ';
- };
- } else {
+ };
+ } else {
$this->doURI($r, $out);
};
};
-
+
usort($ts->triples, 'statementsorter');
$lastp='';
$out.=' ';
- foreach ($ts->triples as $t) {
-
- $p=$t->getPredicate();
-
+ foreach ($ts->triples as $t) {
+
+ $p=$t->getPredicate();
+
if ($p == $lastp) {
- $out.=' , ';
- } else {
- if ($lastp!='') $out.=' ; ';
+ $out.=' , ';
+ } else {
+ if ($lastp!='') $out.=' ; ';
$this->doURI($p, $out);
$lastp=$p;
}
$out.=' ';
-
+
$o=$t->getObject();
-
- if ( is_a($o, 'Literal')) {
+
+ if ( is_a($o, 'Literal')) {
$l=$o->getLabel();
- if ( strpos($l, LINEFEED) === FALSE ) {
+ if ( strpos($l, LINEFEED) === FALSE ) {
$out.="\"$l\"";
- } else {
- $out.="\"\"\"$l\"\"\"";
+ } else {
+ $out.="\"\"\"$l\"\"\"";
}
- if ( $o->getLanguage()!='' ) {
+ if ( $o->getLanguage()!='' ) {
$out.='@'.$o->getLanguage();
}
- if ( $o->getDatatype()!='' ) {
+ if ( $o->getDatatype()!='' ) {
$out.='^^<'.$o->getDatatype().'>';
}
-
- }
-
+
+ }
+
if (is_a($o, 'Resource')) {
if ($this->debug) print 'Doing object: '.$o->getURI().LINEFEED;
if ( is_a($o,'BlankNode')) {
@@ -334,7 +341,7 @@
// $this->resourcetext_taken[$o->getURI()]=1;
$out .='_:'.$o->getLabel();
- } else {
+ } else {
$this->doURI($o, $out);
}
@@ -342,40 +349,40 @@
}
if (isset($rbn) && !count($rbn->triples)>0 && N3SER_BNODE_SHORT) {$out.=' ] ';};
- $this->resourcetext[$r->getURI()]=$out;
+ $this->resourcetext[$r->getURI()]=$out;
return TRUE;
}
- /**
+ /**
* Format a single URI
* @param string $s
* @access private
* @return void
**/
- function doURI(&$r, &$out) {
+ function doURI(&$r, &$out) {
if ( $r->getURI()=='http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
$out.='a';
- return;
+ return;
}
if ($r->getNamespace()!='') {
$out.=$this->prefixes[$r->getNamespace()].':'.$r->getLocalName();
- } else {
- //Will this ever happen?
+ } 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);
+ function fixAnon($t,$a) {
+ $t=preg_replace("/( \] $|^\[ )/", '', $t);
return $a.$t;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|