Author: dr
Date: Tue Aug 14 10:28:27 2007
New Revision: 5909
Log:
- Implemented a new visitor that displays a tree structure as a text
representation.
Added:
trunk/Tree/src/visitors/plain_text.php (with props)
Modified:
trunk/Tree/design/class_diagram.png
trunk/Tree/src/tree_autoload.php
trunk/Tree/tests/visitor.php
Modified: trunk/Tree/design/class_diagram.png
==============================================================================
Binary files - no diff available.
Modified: trunk/Tree/src/tree_autoload.php
==============================================================================
--- trunk/Tree/src/tree_autoload.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree_autoload.php [iso-8859-1] Tue Aug 14 10:28:27 2007
@@ -32,6 +32,7 @@
'ezcTreeNodeListIterator' =>
'Tree/tree_node_list_iterator.php',
'ezcTreeTransactionItem' =>
'Tree/structs/transaction_item.php',
'ezcTreeVisitorGraphViz' =>
'Tree/visitors/graphviz.php',
+ 'ezcTreeVisitorPlainText' =>
'Tree/visitors/plain_text.php',
'ezcTreeXml' => 'Tree/backends/xml.php',
'ezcTreeXmlInternalDataStore' =>
'Tree/stores/xml_internal.php',
);
Added: trunk/Tree/src/visitors/plain_text.php
==============================================================================
--- trunk/Tree/src/visitors/plain_text.php (added)
+++ trunk/Tree/src/visitors/plain_text.php [iso-8859-1] Tue Aug 14 10:28:27 2007
@@ -1,0 +1,198 @@
+<?php
+/**
+ * File containing the ezcTreeVisitorPlainText class.
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * An implementation of the ezcTreeVisitor interface that generates
+ * a plain text representatation of a tree structure.
+ *
+ * <code>
+ * <?php
+ * $visitor = new ezcTreeVisitorPlainText(
ezcTreeVisitorPlainText::SYMBOL_UTF8 );
+ * $tree->accept( $visitor );
+ * echo (string) $visitor; // print the plot
+ * ?>
+ * </code>
+ *
+ * Shows (something like):
+ * <code>
+ * Hylobatidae
+ * ââHylobates
+ * â ââLar Gibbon
+ * â ââAgile Gibbon
+ * â ââMüller's Bornean Gibbon
+ * â ââSilvery Gibbon
+ * â ââPileated Gibbon
+ * â ââKloss's Gibbon
+ * ââHoolock
+ * â ââWestern Hoolock Gibbon
+ * â ââEastern Hoolock Gibbon
+ * ââSymphalangus
+ * ââNomascus
+ * ââBlack Crested Gibbon
+ * ââEastern Black Crested Gibbon
+ * ââWhite-cheecked Crested Gibbon
+ * ââYellow-cheecked Gibbon
+ * </code>
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeVisitorPlainText implements ezcTreeVisitor
+{
+ /**
+ * Represents the ASCII symbol set
+ */
+ const SYMBOL_ASCII = 1;
+
+ /**
+ * Represents the UTF-8 symbol set
+ */
+ const SYMBOL_UTF8 = 2;
+
+ /**
+ * Holds all the edges of the graph.
+ *
+ * @var array(string=>array(string))
+ */
+ protected $edges = array();
+
+ /**
+ * Holds the root ID
+ *
+ * @var string
+ */
+ protected $root = null;
+
+ /**
+ * Constructs a new ezcTreeVisitorPlainText visualizer using
$symbolCharset as character set
+ *
+ * This class only supports 'ascii' and 'utf-8' as character sets.
+ * @see SYMBOL_UTF8
+ * @see SYMBOL_ASCII
+ *
+ * @param int $symbolCharset
+ */
+ public function __construct( $symbolCharset = self::SYMBOL_UTF8 )
+ {
+ switch( $symbolCharset )
+ {
+ case self::SYMBOL_ASCII:
+ $symbols = array( '|', '+', '-', '+' );
+ break;
+ case self::SYMBOL_UTF8:
+ default:
+ $symbols = array( 'â', 'â', 'â', 'â' );
+ }
+ $this->symbolPipe = $symbols[0];
+ $this->symbolTee = $symbols[1];
+ $this->symbolLine = $symbols[2];
+ $this->symbolCorner = $symbols[3];
+ }
+
+ /**
+ * Visits the node and sets the the member variables according to the node
+ * type and contents.
+ *
+ * @param ezcTreeVisitable $visitable
+ * @return boolean
+ */
+ public function visit( ezcTreeVisitable $visitable )
+ {
+ if ( $visitable instanceof ezcTree )
+ {
+ }
+
+ if ( $visitable instanceof ezcTreeNode )
+ {
+ if ( $this->root === null )
+ {
+ $this->root = $visitable->id;
+ }
+
+ $parent = $visitable->fetchParent();
+ if ( $parent )
+ {
+ $this->edges[$parent->id][] = $visitable->id;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Loops over the children of the node with ID $id.
+ *
+ * This methods loops over all the node's children and adds the correct
+ * layout for each node depending on the state that is collected in the
+ * $level and $levelLast variables.
+ *
+ * @param string $id
+ * @param int $level
+ * @param array(int=>bool) $levelLast
+ *
+ * @return string
+ */
+ private function doChildren( $id, $level = 0, $levelLast = array() )
+ {
+ $text = '';
+
+ if ( !isset( $this->edges[$id] ) )
+ {
+ return $text;
+ }
+ $children = $this->edges[$id];
+ $numChildren = count( $children );
+
+ $count = 0;
+ foreach ( $children as $child )
+ {
+ $count++;
+ for ( $i = 0; $i < $level; $i++ )
+ {
+ if ( isset( $levelLast[$i] ) && $levelLast[$i] )
+ {
+ $text .= ' ';
+ }
+ else
+ {
+ $text .= "{$this->symbolPipe} ";
+ }
+ }
+ if ( $count != $numChildren )
+ {
+ $text .= "{$this->symbolTee}{$this->symbolLine}";
+ $levelLast[$level] = false;
+ }
+ else
+ {
+ $text .= "{$this->symbolCorner}{$this->symbolLine}";
+ $levelLast[$level] = true;
+ }
+ $text .= $child . "\n";
+ $text .= $this->doChildren( $child, $level + 1, $levelLast );
+ }
+
+ return $text;
+ }
+
+ /**
+ * Returns a text representatation of a tree.
+ *
+ * @return string
+ * @ignore
+ */
+ public function __toString()
+ {
+ $tree = $this->root . "\n";
+ $tree .= $this->doChildren( $this->root );
+ return $tree;
+ }
+}
+?>
Propchange: trunk/Tree/src/visitors/plain_text.php
------------------------------------------------------------------------------
svn:eol-style = native
Modified: trunk/Tree/tests/visitor.php
==============================================================================
--- trunk/Tree/tests/visitor.php [iso-8859-1] (original)
+++ trunk/Tree/tests/visitor.php [iso-8859-1] Tue Aug 14 10:28:27 2007
@@ -109,6 +109,109 @@
self::assertSame( 'c422c6271ff3c9a213156e660a1ba8b2', md5( (string)
$visitor ) );
}
+ public function testVisitor2()
+ {
+ $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+ $this->addTestData( $tree );
+
+ $expected = <<<END
+Hominoidea
+ââHylobatidae
+â ââHylobates
+â â ââLar Gibbon
+â â ââAgile Gibbon
+â â ââMüller's Bornean Gibbon
+â â ââSilvery Gibbon
+â â ââPileated Gibbon
+â â ââKloss's Gibbon
+â ââHoolock
+â â ââWestern Hoolock Gibbon
+â â ââEastern Hoolock Gibbon
+â ââSymphalangus
+â ââNomascus
+â ââBlack Crested Gibbon
+â ââEastern Black Crested Gibbon
+â ââWhite-cheecked Crested Gibbon
+â ââYellow-cheecked Gibbon
+ââHominidae
+ ââPongo
+ â ââBornean Orangutan
+ â ââSumatran Orangutan
+ ââGorilla
+ â ââWestern Gorilla
+ â â ââWestern Lowland Gorilla
+ â â ââCross River Gorilla
+ â ââEastern Gorilla
+ â ââMountain Gorilla
+ â ââEastern Lowland Gorilla
+ ââHomo
+ â ââHomo Sapiens
+ â ââHomo Sapiens Sapiens
+ â ââHomo Superior
+ ââPan
+ ââCommon Chimpanzee
+ ââBonobo
+
+END;
+
+ $visitor = new ezcTreeVisitorPlainText;
+ $tree->accept( $visitor );
+ self::assertSame( $expected, (string) $visitor );
+
+ $visitor = new ezcTreeVisitorPlainText(
ezcTreeVisitorPlainText::SYMBOL_UTF8 );
+ $tree->accept( $visitor );
+ self::assertSame( $expected, (string) $visitor );
+ }
+
+ public function testVisitor3()
+ {
+ $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
+ $this->addTestData( $tree );
+
+ $visitor = new ezcTreeVisitorPlainText(
ezcTreeVisitorPlainText::SYMBOL_ASCII );
+ $tree->accept( $visitor );
+ $expected = <<<END
+Hominoidea
++-Hylobatidae
+| +-Hylobates
+| | +-Lar Gibbon
+| | +-Agile Gibbon
+| | +-Müller's Bornean Gibbon
+| | +-Silvery Gibbon
+| | +-Pileated Gibbon
+| | +-Kloss's Gibbon
+| +-Hoolock
+| | +-Western Hoolock Gibbon
+| | +-Eastern Hoolock Gibbon
+| +-Symphalangus
+| +-Nomascus
+| +-Black Crested Gibbon
+| +-Eastern Black Crested Gibbon
+| +-White-cheecked Crested Gibbon
+| +-Yellow-cheecked Gibbon
++-Hominidae
+ +-Pongo
+ | +-Bornean Orangutan
+ | +-Sumatran Orangutan
+ +-Gorilla
+ | +-Western Gorilla
+ | | +-Western Lowland Gorilla
+ | | +-Cross River Gorilla
+ | +-Eastern Gorilla
+ | +-Mountain Gorilla
+ | +-Eastern Lowland Gorilla
+ +-Homo
+ | +-Homo Sapiens
+ | +-Homo Sapiens Sapiens
+ | +-Homo Superior
+ +-Pan
+ +-Common Chimpanzee
+ +-Bonobo
+
+END;
+ self::assertSame( $expected, (string) $visitor );
+ }
+
public static function suite()
{
return new PHPUnit_Framework_TestSuite( "ezcTreeVisitorTest" );
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components