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

/**
 * Class : BowHTMLDocument
 * Developed by Jonas Eriksson
 * @copyright 2003 BICOM KB
 **/

 require_once('BowHTMLCreation.inc.php');
 
 class BowHTMLDocument
 {
	/*Instance variables*/
	var $nodes;
	var $nodeId=1;

	/*Constructor*/
	function BowHTMLDocument() 
	{ 
		$list =& new BowList();
		$this->nodesSet($list);
	}

	/*Setter and getter for $nodes*/
	function &nodes() { return $this->nodes; }
	function nodesSet( $someNodes ) { $this->nodes =& $someNodes; }

	/*Setter and Getter for $nodeId*/
	function nodeId() {return $this->nodeId;}
	function nodeIdSet( $anId ) { $this->nodeId =& $anId; }
	
	/*Return the root from the list of nodes*/
	function &root() 
	{ 
		$list =& $this->nodes();
		return $list->value("Root"); 
	}
	
	/*PRIVATE: Validate the procedure of adding a new node to the list of nodes*/
	function validateAddNewNode($pName)
	{
		$theRoot = $this->root();
		if ($theRoot == null)
		{
			trigger_error("Root does not exist. Root must be set before appending new nodes. This node: $pName", E_USER_NOTICE);
			print_r($this);
			die();
		}
		else
		{
			return true;
		}
	}
	
	
	function setStepsToRoot(&$newNode, $parentNode)
	{
		if($parentNode->id == 'Root')
		{
			$newNode->stepsToRootSet($newNode->stepsToRoot +1);
		}
		else
		{
			$parentStep = $parentNode->stepsToRoot;
			$newNode->stepsToRootSet($parentNode->stepsToRoot +1);
		}
	}

	/*PRIVATE: Creates a new unique integer which will be used 
	* as a node identifyer in the list of nodes.*/
	function &newNodeId()
	{
		$this->nodeIdSet($this->nodeId + 1);
		return $this->nodeId();
	}
	
	/*Add a new root to the document*/	
	function &addRoot($newName)
	{
		$newTag = new BowHTMLNode();
		$newTag->idSet("Root");
		$newTag->nameSet($newName);
		$newTag->parentNodeSet('None');
		$newTag->stepsToRootSet(0);
		$nodes =& $this->nodes();
		$nodes->addElement($newTag, "Root");
		return "Root";
	}	

	function allNodes()
	{
		return $this->nodes->elements();
	}
	
	function allStringNodes()
	{
		$tStringNodes = array();
		foreach($this->allNodes() as $iNode)
		{
			if(is_a($iNode, 'BowStringNode'))
				$tStringNodes[] = $iNode;
		}
		return $tStringNodes;
	}
	
	function tagInNodeNamed($pCurrentNodeId, $pNodeName)
	{
		$tCurrentNode = $this->idToElement($pCurrentNodeId);
		if($tCurrentNode->name() == $pNodeName)
			return true;
		elseif($tCurrentNode->parentNode() != 'Root')
			return $this->tagInNodeNamed($tCurrentNode->parentNode(), $pNodeName);
		else 
			return false;
	}
	
	function getParentNamed($pCurrentNodeId, $pNodeName)
	{
		$tCurrentNode = $this->idToElement($pCurrentNodeId);
		if($tCurrentNode->name() == $pNodeName)
			return $tCurrentNode;
		else
			return $this->getParentNamed($tCurrentNode->parentNode(), $pNodeName);
	}
	
	/*Add a new node to the root. Dies if root does not exists*/
	function &addTagToRoot($newName)
	{
		if ($this->validateAddNewNode($newName))
		{
			return $this->addTagTo($newName, 'Root');
		}
	}
	
	function getFirstNodeNamed($pName)
	{
		foreach($this->allNodes() as $iNode)
		{
			if(strtolower($pName) == strtolower($iNode->name()))
			{
				return $iNode;
			}
		}
		return null;
	}	

	function changeTitleTo($pString)
	{
		$tTitleNode =& $this->getFirstNodeNamed('title');
		$tTitleStringNode =& $this->idToElement($tTitleNode->children->value(0));
		$tTitleStringNode->textContentsSet("Lets change the title of Google with BowML!");
		$this->nodes->addElement($tTitleStringNode, $tTitleStringNode->id());
	}
	
	function getLastNodeNamed($pName)
	{
		foreach(array_revere($this->allNodes()) as $iNode)
		{
			if(strtolower($pName) == strtolower($iNode->name()))
			{
				return $iNode;
			}
		}
		return null;
	}

	/*Add a new node to an existing tag. Dies if root does not exists*/
	function &addTagTo($newName, $existingTagId)
	{
		if ($this->validateAddNewNode($newName))
		{
			$newTag =& new BowHTMLNode();
			$newTag->nameSet($newName);
			$nodes =& $this->nodes();
			$newTagId =& $this->newNodeId();
			$newTag->idSet($newTagId);
			$existingTag =& $this->idToElement($existingTagId);
			if($existingTag == null)
				die("Error while creating node named: '$newName': The parent node does not exist. Existing tag id: $existingTagId");
			$existingTag->appendTagTo($newTag, $existingTagId);
			$this->setStepsToRoot($newTag, $existingTag);
			$nodes->addElement($newTag, $newTagId);
			$this->nodesSet($nodes);
			return $newTagId;
		}
	}
	
	/*Add a new node to the last added tag. Dies if root does not exists*/
	function &addTagToLastTag($newName)
	{
		if ($this->validateAddNewNode($newName))
		{
			$existingTagId =& $this->nodeId();
			$existingTag =& $this->idToElement($existingTagId);
			$newTag =& new BowHTMLNode();
			$newTag->nameSet($newName);
			$nodes =& $this->nodes();
			$newTagId =& $this->newNodeId();
			$newTag->idSet($newTagId);
			$existingTag->appendTagTo($newTag, $existingTagId);
			$this->setStepsToRoot($newTag, $existingTag);
			$nodes->addElement($newTag, $newTagId);
			return $newTagId;
		}		
	}
	
	/*Add a new string node to an existing tag. Dies if root does not exists*/
	function &addTextTo($text, $existingTagId)
	{
		if ($this->validateAddNewNode($text))
		{
			$textTag =& new BowStringNode();
			$textTag->textContentsSet($text);
			$nodes =& $this->nodes();
			$textTagId =& $this->newNodeId();
			$textTag->idSet($textTagId);
			$existingTag =& $this->idToElement($existingTagId);
			$existingTag->appendTagTo($textTag, $existingTagId);
			$this->setStepsToRoot($textTag, $existingTag);
			$nodes->addElement($textTag, $textTagId);
			return $textTagId;
		}
	}
	
	function &addTextToLastTag($pText)
	{
		if ($this->validateAddNewNode($pText))
		{
			$textTag =& new BowStringNode();
			$textTag->textContentsSet($pText);
			$nodes =& $this->nodes();
			$textTagId =& $this->newNodeId();
			$textTag->idSet($textTagId);
			$lastElement =& $nodes->lastElement();
			$existingTagId =& $lastElement[0];
			$existingTag =& $lastElement[1];
			$existingTag->appendTagTo($textTag, $existingTagId);
			$this->setStepsToRoot($textTag, $existingTag);
			$nodes->addElement($textTag, $textTagId);
			return $textTagId;
		}
	}
	
	/**
	* @return void
	* @param String $name
	* @param String $value
	* @param Integer $existingTagId
	* @desc Add an attribute to an existing tag
	*/
	function addAttributeTo($name, $value, $existingTagId)
	{
		$existingTag = null;
		if ($this->validateAddNewNode($name))
		{
			$nodes =& $this->nodes();
			$existingTag =& $this->idToElement($existingTagId);
			$existingTag->addAttribute($name, $value);
		}
	}
	
	/**
	* @return void
	* @param Array $pAttributeArray
	* @param Integer $pExistingTag
	* @desc Add some attributes to an existing tag, each attribute name/attribute value pair
	* is an array element.
	*/
	function addAttributesTo($pAttributeArray, $pExistingTag)
	{
		foreach ($pAttributeArray as $iName => $iValue)
		{
			$this->addAttributeTo($iName, $iValue, $pExistingTag);
		}
	}

	function javaScriptToTag($pScript, $pParentTag)
	{
		$tSCRIPT = $this->addTagTo("SCRIPT", $pParentTag);
		$this->addAttributesTo(array("language"=>"JavaScript", "type"=>"text/javascript"), $tSCRIPT);
		$this->addTextTo($pScript, $tSCRIPT);
		return $tSCRIPT;
	}
	
	function javaScriptEventForToTag($pScript, $pEventName, $pFor, $pParentTag)
	{
		$tScript = $this->javaScriptToTag($pScript, $pParentTag);
		$this->addAttributesTo(array("event"=>$pEventName, "for"=>$pFor), $tScript);
		return $tScript;
	}
	
	function addImageTo($pImageSource, $pParentTag)
	{
		$tImg =& $this->addTagTo("img", $pParentTag);
		$this->addAttributesTo(array("src"=>$pImageSource, "border"=>"0"), $tImg);
	}
	
	function addImageHeightWidthTo($pImageSource, $pHeight, $pWidth, $pParentTag)
	{
		$tImg =& $this->addTagTo("img", $pParentTag);
		$this->addAttributesTo(array("src"=>$pImageSource, "height"=>$pHeight, "width"=>$pWidth, "border"=>"0"), $tImg);
	}
	
	function idToElement($pId)
	{
		if(!is_numeric($pId) && (!is_string($pId)))
		{
			bowWarning("$pId <-- Borde vara ett id nummer, men istället är det ett: ", $pId);
		}
		$tNodes = $this->nodes();
		foreach($tNodes->elements() as $iElement)
		{
			if(!is_object($iElement))
			{
				programmingError("Inte en nod:", $iElement, $this);
			}
			if($iElement->id() == $pId)
				return $iElement;
		}
		return null;
	}
	
	function idsToElements($idList)
	{
		$realElements = new BowList();
		foreach($idList->elements() as $i)
			$realElements->addElement($this->idToElement($i));
		return $realElements;
	}
	
	function nodeIdForName($pName)
	{
		$myNodes = $this->nodes();
		$nodeElements = $myNodes->elements;
		foreach($nodeElements as $iElement)
		{
			if($iElement->name() == $pName);
				return $iElement->id();
		}
		return null;
	}	
	
	function toHTML()
	{
		$writer = new BowHTMLWriter($this);
		print($writer->readFromDocument());
	}
	
	function fromStream()
	{
		$writer = new BowHTMLWriter($this);
		return $writer->readFromDocument();
	}
 }
?>
Return current item: BowML