Location: PHPKode > scripts > BowML > BowHTMLWriter.inc.php
<?php

/**
 * Class : BowHTMLWriter
 * Developed by Jonas Eriksson
 * @copyright 2003 BICOM KB
 **/
 
 class BowHTMLWriter
 {
	/*Instance variables*/
	var $stream;
	var $document;
	var $processedNodes;
	
	/*Setter and getter for $stream*/
	function &stream() { return $this->stream; }
	function streamSet( $aStream ) { $this->stream =& $aStream; }

	/*Setter and Getter for $document*/
	function document() { return $this->document; }
	function documentSet( $aDocument ) { return $this->document = $aDocument; }
	
	/*Setter and Getter for $document*/
	function &processedNodes() { return $this->processedNodes; }
	function processedNodesSet( $someProcessedNodes ) { return $this->processedNodes =& $someProcessedNodes; }
	
	/*Constructor*/
	function BowHTMLWriter($aDocument)
	{
		$this->documentSet($aDocument);
		$this->streamSet(new BowStream());
		$this->processedNodesSet(new BowList());
	}

	function readFromDocument()
	{
		$myDocument = $this->document;
		$myNodes = $myDocument->nodes();
		$myNodes->doWithEach('toHTML', $this);
		$thisStream = $this->stream();
		return ($thisStream->bow_flush());
	}
	
	function isProcessed($nodeId)
	{
		$myProcessedNodes =& $this->processedNodes();
		if(!$myProcessedNodes->includes($nodeId))
		{
			$myProcessedNodes->addElement($nodeId);
			return true;
		}
		else
			return false;
	}
	
	function toAttributes($someAttributes)
	{
		$attributesString = "";
		
		foreach($someAttributes->elements as $key => $value)
		{
			if($key == 0 && $value == "last")
				break;
			else
				$attributesString = "$attributesString $key=\"$value\"";			
		}
		return $attributesString;
	}
	
	function toHTML($node, $extra = null)
	{
		if($node != null)
		{
			$name = $node->name();
			$attributes = $node->bowNodeAttributes();
			$attributesString = "";
			if (!$attributes->isEmpty())
			{
				$attributesString = $this->toAttributes($attributes);
			}
	
			if($name == "")
				$this->stringElement($node);
			else
			{
				if((strtolower($name) == "img") || (strtolower($name) == "br") || (strtolower($name) == "meta") || (strtolower($name) == "link") || (strtolower($name) == "hr"))
					$this->singleTag($node, $name, $attributesString);
				else
					$this->multiTag($node, $name, $attributesString);
			}
		}
	}

	function multiTag($node, $name, $attributesString)
	{
		$myStream =& $this->stream();
		$idChildren = $node->children();
		$myDocument = $this->document();
		$children = $myDocument->idsToElements($idChildren);
		
		if($this->isProcessed($node->id)) //If this node has not been processed yet, do it now!
		{
			$tabs = $this->addTabs($node);
			$myStream->bow_print("$tabs<$name$attributesString>\n"); //Start tag (<HTML... >)			
			if(!$children->isEmpty())
			{
				$children->doWithEach('toHTML', $this);	//Perform $this->toHTML on each child
			}								
			$myStream->bow_print("$tabs</$name>\n"); //End tag (</HTML>)
		}
	}
	
	function singleTag($node, $name, $attributesString)
	{
		if($this->isProcessed($node->id)) //If this node has not been processed yet, do it now!
		{
			$tabs = $this->addTabs($node);
			$myStream =& $this->stream();
			$myStream->bow_print("$tabs<$name$attributesString>\n");
		}
	}

	function stringElement(&$node)
	{
		if($this->isProcessed($node->id)) //If this node has not been processed yet, do it now!
		{
			$tabs = $this->addTabs($node);
			$textContents = $node->textContents();
			$myStream =& $this->stream();
			$myStream->bow_print("$tabs$textContents\n");
		}
	}
	
	function addTabs($node)
	{
		$returnString = "";
		$tabs = $node->stepsToRoot();
		for($i=1;$i<=$tabs;++$i)
			$returnString = "$returnString\t";
			
		return $returnString;
	}
 }

?>
Return current item: BowML