<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * EK_TplTransformer Class
 *
 * This class transforms Screen Designer Templates to HTML
 * It's the default Transformer for the EasyGUI Framework.
 * 
 * If you want to write your own Transformer refer to EasyGUI XP Developper Guide
 * for more information on how to write custom Transformers
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version	$Id: class.ek_tpltransformer.php 1 2006-01-31 14:55:23Z glauff $
 * @package template.transformer
 */
class EK_TplTransformer extends EK_Transformer
{
	/**
	 * @var object
	 */
	var $ekObject;
	
	/**
	 * @var object
	 */
	var $tpl;
	
	/**
	 * @var	string
	 */
	var $dialog;
	
	/**
	 * @var array
	 */
	var $components;
	
	/**
	 * @var string
	 */
	var $mapNS = 'ek';
	
	/**
	 * EK_TplTransformer Constructor
	 *
	 * This initializes the Transformer.
	 * If you want to write to own transformer make sure that your Transformer Class
	 * is in your library path and to implement the transformer interface. (implement a transform() Method) 
	 * 
	 * @param 	object	$ekObject	EK Object.
	 * @param 	object	$tpl		Template Object
	 * @return 	void
	 *
	 * @access 	public
	 */
	function EK_TplTransformer( &$ekObject, &$tpl )
	{
		$this->tpl 			=& $tpl;
		$this->dialog		= '';
		$this->ekObject		=& $ekObject;
		$this->components 	= array();
		$this->map 			=& new EK_Map();
		$this->mapNS		= $this->tpl->getMap();
		
		EK_Common::getRequiredClasses( EK_ROOT . '/lib/template/transformer/components/' );
		
		if ( defined( 'EK_TRANSFORMER_COMPLIB' ) ) {
			EK_Common::getRequiredClasses( EK_TRANSFORMER_COMPLIB );
		}
	}
	
	/**
	 * Parses Template
	 *
	 * This Method has to be implemented by every Transformer.
	 *
	 * @param 	string 	$xml
	 * @return 	string	The transformed Template
	 * @access 	public
	 */
	function transform( $xml )
	{
		$xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="'.EK_ENCODING.'"?>', $xml);
		
		$xmlParser = xml_parser_create( EK_ENCODING );
				
		xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, EK_ENCODING );
		xml_set_object($xmlParser, $this);
		xml_set_element_handler($xmlParser, 'startProcessing', 'endProcessing');
		xml_set_character_data_handler ( $xmlParser, 'cDataProcessing');
				
		if (!xml_parse($xmlParser, $xml)) {
			$errorxml = EK_ROOT . '/logs/' . $this->tpl->getTplFile(). '_error.xml';
			
			$fp = fopen( $errorxml, 'w');
			fputs( $fp, $xml );
			fclose($fp);
			
			trigger_error("XML Parser: Parse Error in Template: ". $this->tpl->getTplFile(). "</br>\r\n" . xml_error_string(xml_get_error_code($xmlParser))." at line ".xml_get_current_line_number($xmlParser) ."</br></br>\r\n\r\nErrorneous XML written to:</br>\r\n" .$errorxml, E_USER_ERROR);
		}
						
		xml_parser_free($xmlParser);
		return $this->dialog;
	}
	
	/**
	* Handles all opening XML Tags
	*
	* This function is called by the XMLParser.
	* Checks whether the required component exists in the Map,
	* or an appropriate class for Component Rendering exists, and then
	* creates an instance and pushes component on the componentStack.
	*
	* @param 	resource	$parser		The XML Parser
	* @param 	string		$name		The current tag
	* @param 	array		$attribs	The attribues of the tag
	* @see 	EK_Map
	* @see 	EK_XMLParser
	*
	* @access 	private
	*/
	function startProcessing(&$parser, &$name, &$attribs)
	{
		$class = explode(':', $name );
			
		if ( sizeof( $class ) > 1 ) {
			$namespace = strtolower( $class[0] );
			$name = $class[1];
			$class = $class[0] . "_" . $class[1];
		} else {
			$namespace = $this->mapNS;
			$class = $name;
			
			if ( !empty( $this->mapNS ) ) {
				$class = $this->mapNS . "_" . $name;
			}
		}
				
		if ( class_exists( $class )) {
			$component =& new $class( $attribs, $name, $namespace, $this->map, count( $this->components ), $this->ekObject );
		} elseif ($name == 'CCOMPONENT') {
			if (!class_exists($attribs['CCLASS'])) {
				require_once('class.'.$attribs['CCLASS'].'.php');
			}
			
			$name = $attribs['CCLASS'];
			$component =& new $name($attribs, $name, $namespace, $this->map, count( $this->components ), $this->ekObject );
		} elseif ( class_exists ( $namespace . "_TBaseComponent" ) ) {
			$class = $namespace . "_TBaseComponent";
			$component =& new $class( $attribs, $name, $namespace, $this->map, count( $this->components ), $this->ekObject );
		} else {
			$component =& new EK_TComponent( $attribs, $name, $namespace, $this->map, count( $this->components ), $this->ekObject );
		}
		
		array_push($this->components, $component);
	}
	
	/**
	* Handles all closing XML Tags
	*
	* This Function is called by the XMLParser.
	* Pops off last component of stack to register current component in previous component
	*
	* @param 	resource	$parser		The XML Parser	
	* @param 	string		$name		The current closing Tag name
	* @see 	EK_XMLParser
	* @access private
	*/
	function endProcessing(&$parser, &$name)
	{
		$recentComponent = array_pop($this->components);

		if ($currentComponent = array_pop($this->components)) {
			$currentComponent->register( $recentComponent );
			array_push( $this->components, $currentComponent );
		} else {
			$this->dialog = $recentComponent->buildComponents();
		}
	}
	
	/**
	 * Handles all Character Data
	 *
	 * This method is called by the XML Parser
	 * It registers character data on the stack
	 *
	 * @param 	resource	$parser	The XML Parser
	 * @param 	string		$data	Character Data
	 * @see 	EK_XMLParser
	 * @access 	public
	 */
	function cDataProcessing( &$parser, &$data )
	{
		$trim = trim ($data);
		
		if (! empty( $trim ) ) {
			$attribs = array( 'data' => $data );
			
			$component =& new EK_TComponent( $attribs, 'cdata', $this->defaultNS, $this->map, count($this->components), $this->ekObject->settings);	
			
			if ($currentComponent = array_pop($this->components)) {
				$currentComponent->register( $component );
				array_push( $this->components, $currentComponent);
			}
		}
	}
}
?>