Location: PHPKode > scripts > DTD 2 PHP > dtd-2-php/dtd2php.class.php
<?php

/**
 * dtd2php class : PHP Class generator from DTD description
 * 
 * @package 
 * @author Ben Yacoub Hatem <hide@address.com>
 * @copyright Copyright (c) 2004
 * @version $Id$ - 20/04/2004 17:12:04 - dtd2php.class.php
 * @access public
 **/
class dtd2php{
	/**
     * Constructor
     * @access protected
     */
	function dtd2php(){
		
	}
	var $ATTLIST = array();
	var $ELEMENT = array();
	var $ENTITY = array();
	
	/**
	 *
	 * @access public
	 * @return void 
	 **/
	function version(){
		return "1.0.0";
	}
	
	
	/**
	 * dtd2php::open()	Open dtd file
	 * 
	 * @param $dtd_file
	 * @return 
	 **/
	function open($dtd_file){
		return implode('',file("$dtd_file"));
	}
	
	/**
	 * dtd2php::parse()	 Parse dtd file
	 * 
	 * @return 
	 **/
	function parse($cont){
		$cont = trim(str_replace(">","" ,$cont ));
		$quit = true;
		$els = array();	
		do
		{
			$cont = strstr($cont,"<" );
			$cont = ereg_replace("^<", "",$cont );
			$pos =  strpos($cont,"<" );
			if ($pos == 0){$quit = false;	}
			else {
				$val = substr($cont, 0, $pos);
				if (!in_array($val,$els))
				{
					$els[] = $val;
				}
			}
		} while ($quit);
  
		foreach($els as $v){
			if (ereg("^!ENTITY",trim($v))) {
				$this->set_value($v, "ENTITY");
			}
			if (ereg("!ELEMENT",trim($v))) {
				$this->set_value($v, "ELEMENT");
			}
			if (ereg("!ATTLIST",trim($v))) {
			    $this->set_value($v, "ATTLIST");
			}
		}
		
	}
	
	/**
	 *
	 * @access public
	 * @return void 
	 **/
	function set_value($v,$in){
		if (trim($v)=="") {
		    return;
		}
		switch($in){
			case "ATTLIST": 
				$d = explode("\n", $v);
				$i = 0;
				foreach($d as $k){
					if ($i==0) {
					    $name = str_replace("!ATTLIST ","" , $k);
						$i++;
					} elseif(trim($k)!="") {
						$this->ATTLIST[trim($name)][] = explode(" ", trim($k));
					}
				}
			break;
			
		    case "ELEMENT": 
				$this->ELEMENT[] = $v;
			break;
		
		    case "ENTITY": 
				$this->ENTITY[] = $v;
			break;
		} // switch
	}
	
	/**
	 * Generate PHP class 
	 *
	 * @access public
	 * @return void 
	 **/
	function gen($Package = "",$ModuleName = ""){

		$i = 0;
		foreach($this->ELEMENT as $v){
			$data = explode(" ",$v);
			$name = trim($data[1]);
			$function_name = str_replace("-","_" ,$name );
			
			
			if ($i==0) {
				$ClassName = $name;
			    $GonxClass = "<?php\n\n/**
 * gonx_$ModuleName class : 
 * 
 * @package GONX
 * @author Ben Yacoub Hatem <hide@address.com>
 * @copyright Copyright (c) ".date("Y")."
 * @version \$Id$ - ".date("Y-m-d H:i:s")." - $name.class.php
 * @generator dtd2php version ".$this->version()."
 * @access public
 **/
class gonx_$ModuleName{
	/**
     * Constructor
     * @access protected
     */
	function gonx_$ModuleName(){
		\$this->_document = \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";
	}

	var \$_document;
";
			$i++;
			}
			
			if (ereg("EMPTY",$v)) {
				if (!in_array($name, array_keys($this->ATTLIST) )) {
			    $GonxClass .= "
	/*
	* Create  $function_name tag.
	*/
	function start_$function_name()
	{
		\$this->_document .= \"<$name />\\n\";
	}
";   
				} else {
					foreach($this->ATTLIST[$name] as $v){
						if (!ereg("%",$v[0])) {
						$vname = str_replace(":", "_", $v[0]);
						$vname = str_replace("-","_" ,$vname );
						
							$var[] = $vname;
							$comments .= "	* @param	".$vname;
							$isset .= "
		if ((isset(\$".$vname.")) and (\$".$vname."!==\"\")){
			\$resu.=(\" ".$v[0]."=\".\$".$vname.".\"\");
		}";
							if (in_array("#REQUIRED",$v )) {
							    $isset .= " else {
			\$resu.=(\" <!-- Error occured in $name. Need to pass \\\"".$v[0]."\\\"=... -->\");
		}\n";
								$comments .= "	this variable is required\n";
							} else $comments .= "\n";
						}
					}
					
					if (is_array($var)) {
					    $vs = implode(" = \"\", $",$var );
			    $GonxClass .= "
	/*
	* Open  $function_name tag.
	*
$comments
	*/
	function start_$function_name($$vs = \"\")
	{
		\$resu = (\"<$name\");
$isset
		\$resu .= (\">\\n  \");
		\$this->_document = \$resu;
	}
	
	/*
	* Close  $function_name tag.
	*
	*/
	function end_$function_name()
	{
		\$this->_document .= \"</$name>\\n\";
	}
"; 
					}  else {
			    $GonxClass .= "
	/*
	* Open  $function_name tag.
	*/
	function start_$function_name()
	{
		\$this->_document .= \"<$name>\\n\";
	}
	
	/*
	* Close  $function_name tag.
	*
	*/
	function end_$function_name()
	{
		\$this->_document .= \"</$name>\\n\";
	}
"; 
					} 
				unset($comments,$vs,$var,$isset);
				}
			} else {

				if (in_array($name, array_keys($this->ATTLIST) )) {
					
//					$GonxClass .= "\n // TODO $name function \n";
					
					foreach($this->ATTLIST[$name] as $v){
						if (!ereg("%",$v[0])) {
						$vname = str_replace(":", "_", $v[0]);
						$vname = str_replace("-","_" ,$vname );
						if (ereg("\t",$vname)) {
						    $tox = explode("\t",$vname);
							$vname = $tox[0];
						}

						if (in_array("#FIXED",$v )) {
							$isset .= "
		\$resu.= \" ".$v[0]." = ".addslashes($v[sizeof($v)-1])." \";\n";
						} else {
							$var[] = $vname;
							$comments .= "	* @param	".$vname;
							$isset .= "
		if ((isset(\$".$vname.")) and (\$".$vname."!==\"\")){
			\$resu.=(\" ".$v[0]."=\".\$".$vname.".\"\");
		}";
						}

							if (in_array("#REQUIRED",$v )) {
							    $isset .= " else {
			\$resu.=(\" <!-- Error occured in $name. Need to pass \\\"".$v[0]."\\\"=... -->\");
		}\n";
								$comments .= "	this variable is required\n";
							} else $comments .= "\n";
						}
					}
					
					if (is_array($var)) {
					    $vs = implode(" = \"\", $",$var );
			    $GonxClass .= "
	/*
	* Open  $function_name tag.
	*
$comments
	*/
	function start_$function_name($$vs = \"\")
	{
		\$resu = (\"<$name\");
$isset
		\$resu .= (\">\\n  \");
		\$this->_document = \$resu;
	}
	
	/*
	* Close  $function_name tag.
	*
	*/
	function end_$function_name()
	{
		\$this->_document .= \"</$name>\\n\";
	}
"; 
					} 
					unset($comments,$vs,$var,$isset,$vname);
				} else {
			    $GonxClass .= "
	/*
	* Open  $function_name tag.
	*/
	function start_$function_name()
	{
		\$this->_document .= \"<$name>\\n\";
	}
	
	/*
	* Close  $function_name tag.
	*
	*/
	function end_$function_name()
	{
		\$this->_document .= \"</$name>\\n\";
	}
"; 
				}
			}
		}
		
		$GonxClass = "$GonxClass
	/*
	* 
	* Echo generated  $ClassName application to user.
	* 
	*/
	function generate()
	{
		echo \$this->_document;
	}
 \n}\n\n?>";
		
		/**
		* Write to file
		*/
		if (!is_dir("output/$Package")) {
		    mkdir("output/$Package");
		}
		$filename = "output/$Package/".$ModuleName.".class.php";
		$handle = fopen($filename, 'a');
		if (fwrite($handle,$GonxClass) === FALSE) {
			echo "Impossible d'écrire dans le fichier ($filename)";
			exit;
		}
		fclose($handle);
	}
}


?>
Return current item: DTD 2 PHP