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. |