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