<?php
/**
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
	
include_once('config.inc.php');
include_once('classes/utils/Iterator.class.php');
include_once('classes/core/parser/tags/Tag.class.php');
include_once('classes/core/parser/tags/MenuTag.class.php');
include_once('classes/core/parser/tags/ContainerTag.class.php');
include_once('classes/core/parser/tags/ContainerIdTag.class.php');
include_once('classes/core/parser/tags/ContainersListTag.class.php');
include_once('classes/core/parser/tags/ContainerControlsTag.class.php');
include_once('classes/core/parser/tags/ContainersListControlsTag.class.php');
include_once('classes/core/parser/tags/MenuControlsTag.class.php');
include_once('classes/core/parser/tags/PageControlsTag.class.php');
include_once('classes/core/parser/tags/UserControlsTag.class.php');
include_once('classes/core/parser/tags/IfModeTag.class.php');
include_once('classes/core/parser/tags/IfLangTag.class.php');
include_once('classes/core/parser/tags/IfUserLoggedInTag.class.php');
include_once('classes/core/parser/tags/IfUserMemberOfTag.class.php');
include_once('classes/core/parser/tags/ActionTag.class.php');
include_once('classes/core/parser/tags/ValueTag.class.php');
include_once('classes/core/parser/tags/EditTag.class.php');
include_once('classes/core/parser/tags/SiteMapTag.class.php');

/**
 * Parser for html contents.
 *
 * @access   public
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	HTMLParser.class, Sun Dec 19 10:46:03 CET 2004
 * @package	Parser
 */
class XMLIterator extends _Iterator {
	var $xml;
	var $pos;
	
	
	var $hashTable;
	var $tagsTable;

	function XMLIterator(&$xml){
		$this->xml = &$xml;
		$this->reset();
	}

	function &getXML(){
		return $this->xml;
	}
	
	function &getOffset(){
		if($this->pos > 0 && isset($this->tagsTable[$this->pos-1])){
			$tagObject = &$this->tagsTable[$this->pos-1];
			if(!is_object($tagObject)) {
				echo "non object !!!";
				return 0;
			}
			return $tagObject->getOffset()+$tagObject->getLength();
		}
		return 0;
	}
	
	// Sets internal pointer to the first element
	function reset(){
		$this->pos = 0;	
		$this->hashTable = &$this->getTags($this->xml, HTML_TAGS_PREFIX);
	}

	function hasNext(){
		if (($this->hashTable != null) && ($this->pos < count($this->hashTable))) {
			return true;
		}
		return false;
	}

	// Returns next element in the iteration
	function &next(){
		$tag = &$this->hashTable[$this->pos][0];
		$offset = $this->hashTable[$this->pos][1];
		$name   = '';
		$type   = '';
		$length = '';
		Tag::getTagInfos($tag, $name, $type, $length);
		switch($name) {
			case "easy-cms:ifUserLoggedIn" :
				$tagObject = &new IfUserLoggedInTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:ifUserMemberOf" :
				$tagObject = &new IfUserMemberOfTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:ifMode" :
				$tagObject = &new IfModeTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:ifLang" :
				$tagObject = &new IfLangTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:action" :
				$tagObject = &new ActionTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:value" :
				$tagObject = &new ValueTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:edit" :
				$tagObject = &new EditTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:container" :
				$tagObject = &new ContainerTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:containerId" :
				$tagObject = &new ContainerIdTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:containersList" :
				$tagObject = &new ContainersListTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:container_controls" :
				$tagObject = &new ContainerControlsTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:containersList_controls" :
				$tagObject = &new ContainersListControlsTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:menu" :
				$tagObject = &new MenuTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:menu_controls" :
				$tagObject = &new MenuControlsTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:page_controls" :
				$tagObject = &new PageControlsTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:user_controls" :
				$tagObject = &new UserControlsTag($tag, $name, $type, $offset, $length);
				break;
			case "easy-cms:siteMap" :
				$tagObject = &new SiteMapTag($tag, $name, $type, $offset, $length);
				break;
			default :
				$tagObject = null;
				break;
		}
		$this->tagsTable[$this->pos] = &$tagObject;
		++$this->pos;		
		return $tagObject;
	}
		
	function getERegTag($tagName, $prefix = ""){
		if($prefix != "") {
			$prefix .= ":";
		}
		$ereg = "(<\s*/?\s*".$prefix;
		for($i = 0; $i < strlen($tagName);++$i){
			$ereg .= "[".strtoupper($tagName[$i]).strtolower($tagName[$i])."]";
		}
		$ereg .= "[^<>]+\s*>)";		
		return $ereg;	
	}
	
	function &getTags($xml, $tag = "", $prefix = ""){
		if ( ($res = preg_match_all($this->getERegTag($tag, $prefix), $xml, $matches, PREG_OFFSET_CAPTURE)) ){
			return $matches[0];
		}
		return null;	
	}
}
?>