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. |
From: <cw...@us...> - 2007-08-13 10:00:39
|
Revision: 507 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=507&view=rev Author: cweiske Date: 2007-08-13 03:00:37 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Remove debug statements 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 09:55:33 UTC (rev 506) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 10:00:37 UTC (rev 507) @@ -89,7 +89,6 @@ $this->model=$m; $this->res=""; -var_dump($this->prefixes); // copy default namespaces global $default_prefixes; foreach($default_prefixes as $prefix => $namespace) @@ -101,7 +100,7 @@ $this->addNSPrefix($uri,$prefix); } } -var_dump($this->prefixes); + $namespaces=array(); $count=array(); $resources=array(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cw...@us...> - 2007-08-13 11:13:18
|
Revision: 508 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=508&view=rev Author: cweiske Date: 2007-08-13 04:13:15 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Add N3Parser class option for output compression via [] 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 10:00:37 UTC (rev 507) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:13:15 UTC (rev 508) @@ -43,6 +43,8 @@ var $res; var $anon; + var $styleCompress = false; + /** * Constructor * @@ -200,6 +202,14 @@ }; } + /** + * Set to true, if the N3 serializer should try to compress the blank node + * syntax using [] whereever possible. + */ + function setCompress($compress) + { + $this->styleCompress = $compress; + } /* ==================== Private Methods from here ==================== */ @@ -274,6 +284,7 @@ } $this->done[$r->getURI()]=TRUE; + $compress = false; if ( is_a($r, 'Resource') ) { @@ -281,11 +292,11 @@ //test, if this blanknode is referenced somewhere $rbn=$this->model->find(null, null, $r); - - if (count($rbn->triples)>0 | !N3SER_BNODE_SHORT) { + $compress = count($rbn->triples) == 0 && (N3SER_BNODE_SHORT || $this->styleCompress); + if ($compress) { + $out.='[ '; + } else { $out.='_:'.$r->getLabel(); - } else { - $out.='[ '; }; } else { @@ -347,7 +358,9 @@ } } - if (isset($rbn) && !count($rbn->triples)>0 && N3SER_BNODE_SHORT) {$out.=' ] ';}; + if ($compress) { + $out .= ' ] '; + }; $this->resourcetext[$r->getURI()]=$out; return TRUE; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cw...@us...> - 2007-08-13 11:32:22
|
Revision: 509 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=509&view=rev Author: cweiske Date: 2007-08-13 04:32:20 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Prettify N3Serializer::doResource to enhance readability 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:13:15 UTC (rev 508) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:32:20 UTC (rev 509) @@ -44,6 +44,7 @@ var $anon; var $styleCompress = false; + var $stylePretty = false; /** * Constructor @@ -211,6 +212,16 @@ $this->styleCompress = $compress; } + + + /** + * Enables pretty printing in semicolon delimited sentences. + */ + function setPrettyPrint($prettyPrint) + { + $this->stylePretty = $prettyPrint; + } + /* ==================== Private Methods from here ==================== */ @@ -251,121 +262,115 @@ } } -/** - * 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 - **/ + /** + * 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; + } - $ts=$this->model->find($r, null, null); - if (count($ts->triples)==0) return; + $out = ''; - $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. + $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->anon++; + } + } + return false; + } - if ( isset($this->done[$r->getURI()]) && $this->done[$r->getURI()] ) { - if ( is_a($r, 'BlankNode')) { + $this->done[$r->getURI()] = true; + $compress = false; - 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->anon++; - } + if (is_a($r, 'Resource')) { + if (is_a($r, 'BlankNode')) { + //test, if this blanknode is referenced somewhere + $rbn = $this->model->find(null, null, $r); + $compress = count($rbn->triples) == 0 && (N3SER_BNODE_SHORT || $this->styleCompress); + if ($compress) { + $out.='[ '; + } else { + $out.='_:'.$r->getLabel(); + } + } else { + $this->doURI($r, $out); + } + } - } - return false; - } + usort($ts->triples, 'statementsorter'); + $lastp = ''; + $out .= ' '; - $this->done[$r->getURI()]=TRUE; - $compress = false; + foreach ($ts->triples as $t) { + $p=$t->getPredicate(); - if ( is_a($r, 'Resource') ) { + if ($p == $lastp) { + $out.=' , '; + } else { + if ($lastp!='') { + $out.=' ; '; + } + $this->doURI($p, $out); + $lastp=$p; + } - if ( is_a($r, 'BlankNode') ) { + $out.=' '; - //test, if this blanknode is referenced somewhere - $rbn=$this->model->find(null, null, $r); - $compress = count($rbn->triples) == 0 && (N3SER_BNODE_SHORT || $this->styleCompress); - if ($compress) { - $out.='[ '; - } else { - $out.='_:'.$r->getLabel(); - }; - } else { + $o = $t->getObject(); - $this->doURI($r, $out); + if (is_a($o, 'Literal')) { + $l = $o->getLabel(); + if ( strpos($l, LINEFEED) === FALSE ) { + $out.="\"$l\""; + } else { + $out.="\"\"\"$l\"\"\""; + } + if ( $o->getLanguage()!='' ) { + $out.='@'.$o->getLanguage(); + } + 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')) { + // $this->doResource($o); + // $out.=MAGIC_STRING.$o->getURI().MAGIC_STRING; #$this->resourcetext[$o->getURI()]; + // $this->resourcetext_taken[$o->getURI()]=1; + $out .= '_:'.$o->getLabel(); + } else { + $this->doURI($o, $out); + } + } + } - usort($ts->triples, 'statementsorter'); - $lastp=''; + if ($compress) { + $out .= ' ] '; + }; + $this->resourcetext[$r->getURI()]=$out; - $out.=' '; - - foreach ($ts->triples as $t) { - - $p=$t->getPredicate(); - - if ($p == $lastp) { - $out.=' , '; - } else { - if ($lastp!='') $out.=' ; '; - $this->doURI($p, $out); - $lastp=$p; - } - - $out.=' '; - - $o=$t->getObject(); - - if ( is_a($o, 'Literal')) { - $l=$o->getLabel(); - if ( strpos($l, LINEFEED) === FALSE ) { - $out.="\"$l\""; - } else { - $out.="\"\"\"$l\"\"\""; - } - if ( $o->getLanguage()!='' ) { - $out.='@'.$o->getLanguage(); - } - 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')) { - -// $this->doResource($o); -// $out.=MAGIC_STRING.$o->getURI().MAGIC_STRING; #$this->resourcetext[$o->getURI()]; -// $this->resourcetext_taken[$o->getURI()]=1; - - $out .='_:'.$o->getLabel(); - } else { - - $this->doURI($o, $out); - } - } + return true; } - if ($compress) { - $out .= ' ] '; - }; - $this->resourcetext[$r->getURI()]=$out; - - return TRUE; - } - /** * Format a single URI * @param string $s This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cw...@us...> - 2007-08-13 11:45:18
|
Revision: 510 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=510&view=rev Author: cweiske Date: 2007-08-13 04:45:13 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Add prettyprinting option and reformat serialize() code 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:32:20 UTC (rev 509) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:45:13 UTC (rev 510) @@ -76,108 +76,115 @@ $this->prefixes = array(); } - /** - * Serializes a model to N3 syntax. - * - * @param object Model $model - * @return string - * @access public - */ - function & serialize(&$m) { + /** + * Serializes a model to N3 syntax. + * + * @param object Model $model + * @return string + * @access public + */ + function & serialize(&$m) + { + if (is_a($m, 'DbModel')) { + $m=$m->getMemModel(); + } - if (is_a($m, 'DbModel')) - $m=$m->getMemModel(); + $this->reset(); + $this->model = $m; + $this->res = ""; - $this->reset(); - $this->model=$m; - $this->res=""; - - // copy default namespaces - global $default_prefixes; - foreach($default_prefixes as $prefix => $namespace) + // copy default namespaces + global $default_prefixes; + foreach($default_prefixes as $prefix => $namespace) { $this->addNSPrefix($namespace,$prefix); + } - $nps= $this->model->getParsedNamespaces(); - if($nps!=false){ - foreach($nps as $uri => $prefix){ - $this->addNSPrefix($uri,$prefix); - } - } + $nps= $this->model->getParsedNamespaces(); + if ($nps!=false) { + foreach ($nps as $uri => $prefix){ + $this->addNSPrefix($uri,$prefix); + } + } - $namespaces=array(); - $count=array(); - $resources=array(); - foreach ($this->model->triples as $t) { - $s=$t->getSubject(); - if ( is_a($s, 'Resource')) - $namespaces[$s->getNamespace()]=1; + $namespaces =array(); + $count =array(); + $resources =array(); + foreach ($this->model->triples as $t) { + $s = $t->getSubject(); + if (is_a($s, 'Resource')) { + $namespaces[$s->getNamespace()] = 1; + } + $p = $t->getPredicate(); + if (is_a($p, 'Resource')) { + $namespaces[$p->getNamespace()] = 1; + } + $o = $t->getObject(); + if (is_a($o, 'Resource')) { + $namespaces[$o->getNamespace()] = 1; + } + $uri=$s->getURI(); - $p=$t->getPredicate(); - if ( is_a($p, 'Resource')) - $namespaces[$p->getNamespace()]=1; + if (isset($count[$uri])) { + $count[$uri]++; + } else { + $count[$uri]=0; + $resources[$uri]=$s; + } + } - $o=$t->getObject(); - if ( is_a($o, 'Resource')) - $namespaces[$o->getNamespace()]=1; + 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; + } - $uri=$s->getURI(); + $this->doNamespaces($namespaces); - if (isset($count[$uri])) { - $count[$uri]++; - } else { - $count[$uri]=0; - $resources[$uri]=$s; - } - } + $this->res .= LINEFEED.LINEFEED; - 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; + arsort($count); - $this->doNamespaces($namespaces); + foreach ( $count as $k=>$v) { + $this->doResource($resources[$k]); + // $this->res.=" .\n"; + } - $this->res.=LINEFEED.LINEFEED; + $c = 0; + foreach ( $this->resourcetext as $r=>$t) { + if ( preg_match_all('/'.MAGIC_STRING.'([^ ]+)'.MAGIC_STRING.'/', $t, $ms, PREG_SET_ORDER)) { + foreach($ms as $mseach) { + $rp=$this->resourcetext[$mseach[1]]; + $t=preg_replace('/'.MAGIC_STRING.$mseach[1].MAGIC_STRING.'/', $rp, $t); + } + } - arsort($count); + if ($this->debug) { + $this->res .= $c . ': '; + } + if (!(isset($this->resourcetext_taken[$r]) && $this->resourcetext_taken[$r]>0)) { + $this->res .= $t . '.' . LINEFEED; + if ($this->stylePretty) { + $this->res .= LINEFEED; + } + } else if ($this->debug ) { + $this->res.=' Skipping : '.$t.LINEFEED; + } + $c++; + } - foreach ( $count as $k=>$v) { - $this->doResource($resources[$k]); - // $this->res.=" .\n"; - } + // $max=-1111; + // $maxkey=""; + // foreach ($count as $k=>$c) { + // if ( $c>$max) { $maxkey=$k; $max=$c; } + // } - $c=0; - foreach ( $this->resourcetext as $r=>$t) { + // if ($this->debug) { + // print "$maxkey is subject of most triples! ($max) \n"; + // } - if ( preg_match_all('/'.MAGIC_STRING.'([^ ]+)'.MAGIC_STRING.'/', $t, $ms, PREG_SET_ORDER)) { - - 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 ) - $this->res.=' Skipping : '.$t.LINEFEED; - $c++; + return $this->res; } -// $max=-1111; -// $maxkey=""; -// foreach ($count as $k=>$c) { -// if ( $c>$max) { $maxkey=$k; $max=$c; } -// } - -// if ($this->debug) { -// print "$maxkey is subject of most triples! ($max) \n"; -// } - - return $this->res; - } - /** * Serializes a model and saves it into a file. * Returns FALSE if the model couldn't be saved to the file. @@ -303,7 +310,7 @@ $rbn = $this->model->find(null, null, $r); $compress = count($rbn->triples) == 0 && (N3SER_BNODE_SHORT || $this->styleCompress); if ($compress) { - $out.='[ '; + $out.='['; } else { $out.='_:'.$r->getLabel(); } @@ -323,13 +330,17 @@ $out.=' , '; } else { if ($lastp!='') { - $out.=' ; '; + if ($this->stylePretty) { + $out .= ";\n "; + } else { + $out .= ' ; '; + } } $this->doURI($p, $out); - $lastp=$p; + $lastp = $p; } - $out.=' '; + $out .= ' '; $o = $t->getObject(); @@ -364,8 +375,9 @@ } if ($compress) { - $out .= ' ] '; + $out .= ' ]'; }; + $this->resourcetext[$r->getURI()]=$out; return true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <cw...@us...> - 2007-08-13 15:59:48
|
Revision: 516 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=516&view=rev Author: cweiske Date: 2007-08-13 08:59:46 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Make serializing a bit more intelligent when quoting 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 15:43:35 UTC (rev 515) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 15:59:46 UTC (rev 516) @@ -410,11 +410,26 @@ if (is_a($o, 'Literal')) { $l = $o->getLabel(); - if ( strpos($l, LINEFEED) === FALSE ) { - $out.="\"$l\""; + if (strpos($l, LINEFEED) === false) { + $long = false; } else { - $out.="\"\"\"$l\"\"\""; + $long = true; } + + //try to be intelligent + $quoteSingle = strpos($l, '\'') !== false; + $quoteDouble = strpos($l, '"') !== false; + if ($quoteSingle && !$quoteDouble) { + $quoteChar = $long ? '"""' : '"'; + } else if ($quoteDouble && !$quoteSingle) { + $quoteChar = $long ? '\'\'\'' : '\''; + } else { + //both quotation chars inside + $quoteChar = $long ? '"""' : '"'; + $l = addslashes($l); + } + $out .= $quoteChar . $l . $quoteChar; + if ( $o->getLanguage()!='' ) { $out.='@'.$o->getLanguage(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fu...@us...> - 2008-01-22 10:52:51
|
Revision: 556 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=556&view=rev Author: fusel2k Date: 2008-01-22 02:52:48 -0800 (Tue, 22 Jan 2008) Log Message: ----------- [fix] bug where message 'Nesting level too deep - recursive dependency?' occurs if trying to export MemModels 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-12-11 20:27:34 UTC (rev 555) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2008-01-22 10:52:48 UTC (rev 556) @@ -402,7 +402,7 @@ foreach ($ts->triples as $t) { $p = $t->getPredicate(); - if ($p == $lastp) { + if ($p === $lastp) { $out .= ' , '; } else { if ($lastp!='') { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |