Location: PHPKode > scripts > VXML Parser > vxml-parser/vxml.parser.php
<?php

/*
*   +------------------------------------------------------------------------------+
*     VXML Parser : A first implementation of the VXML parser using PHP XML functions
*   +------------------------------------------------------------------------------+
*     © 2002 all rights reserved by Dynamix S.a.r.l.
*   +------------------------------------------------------------------------------+
*     OSI License : 
*       Free For Educational Use
*   +------------------------------------------------------------------------------+
*     Disclaimer Notice(s)
*  	 The use of any code included into this website is prohibed without a written
*  	 authorisation from the author of this project itself.
*  	 This copyright notice cannot be removed in any case, and should be included
*  	 in every Dynamix project or demo code.
*
*  	 The author isn't responsible of any damage could be caused by this software.
*  	 Use it at your own risk !
*
*   +------------------------------------------------------------------------------+
*     Updates
*   +------------------------------------------------------------------------------+
*
*   +------------------------------------------------------------------------------+
*     Author(s): Ben Yacoub Hatem	hide@address.com
*   +------------------------------------------------------------------------------+
*	Generated the 27-09-2002 12:20:45 with PHP Class Builder by Hatem Ben Yacoub.
*
*/
  class vxml_parser
  {
	var $_file;
	var $depth = array();
	var $err_code;
	var $err_line;
	var $err_col;
	var $err_msg;
	var $size;
	var $elements;
	var $attributes;
	var $texts;
	var $text_size;
	var $tags = array();
	var $attrs = array();
	
	// Validation tests var
	var $err_test1 = false;
	var $err_test2 = false;
	var $err_test3 = false;
	var $err_test4 = false;
	
	var $test3 = array( "assign.name" => false,
						"assign.expr" => false,
						"audio.src" => false,
						"assign.expr" => false,
						"catch.event" => false,
						"choice.next" => false,
						"clear.namelist" => false,
						"elseif.cond" => false,
						"exit.namelist" => false,
						"exit.expr" => false,
						"field.name" => false,
						"goto.next" => false,
						"goto.expr" => false,
						"goto.nextitem" => false,
						"goto.expritem" => false,
						"if.cond" => false,
						"initial.name" => false,
						"submit.next" => false,
						"submit.expr" => false,
						"throw.event" => false,
						"value.expr" => false,
						"var.name" => false,
						"var.expr" => false
						);
	
	var $validtags = array("",
						   "assign",
						   "audio",
						   "block",
						   "break",
						   "catch",
						   "choice",
						   "clear",
						   "disconnect",
						   "else",
						   "elseif",
						   "enumerate",
						   "error",
						   "exit",
						   "field",
						   "filled",
						   "form",
						   "goto",
						   "grammar",
						   "help",
						   "if",
						   "initial",
						   "link",
						   "log",
						   "menu",
						   "meta",
						   "noinput",
						   "nomatch",
						   "object",
						   "option",
						   "param",
						   "prompt",
						   "property",
						   "record",
						   "reprompt",
						   "return",
						   "script",
						   "subdialog",
						   "submit",
						   "throw",
						   "transfer",
						   "value",
						   "var",
						   "vxml");
	var $vxml_tags = array("",
						   "assign"=> array("","name","expr"),
						   "audio"=>array("","src","fetchtimeout","fetchhint","maxage","maxstale","expr"),
						   "block"=>array("","name","expr","cond","type","slot","modal"),
						   "break"=>array("","time","size"),
						   "catch"=>array("","event","count","cond"),
						   "choice"=>array("","dtmf","accept","next","event","expr","fetchaudio","fetchtimeout","fetchhint","maxage","maxstale"),
						   "clear"=>array("","namelist"),
						   "disconnect"=>array(""),
						   "else"=>array(""),
						   "elseif"=>array("","cond"),
						   "enumerate"=>array(""),
						   "error"=>array("","count","cond"),
						   "exit"=>array("","expr","namelist"),
						   "field"=>array("","name","expr","cond","type","slot","modal"),
						   "filled"=>array("","mode","namelist"),
						   "form"=>array("","id","scope"),
						   "goto"=>array("","next","expr","nextitem","expritem","fetchaudio","fetchtimeout","fetchhint","maxage","maxstale"),
						   "grammar"=>array("","lang","src","scope","type","mode","root","version","weight","fetchhint","fetchtimeout","maxage","maxstale"),
						   "help"=>array("","count","cond"),
						   "if"=>array("","cond"),
						   "initial"=>array("","name","expr","cond"),
						   "link"=>array("","next","expr","dtfm","fetchaudio","fetchhint","fetchtimeout","maxage","maxstale"),
						   "log"=>array("","label","expr"),
						   "menu"=>array("","id","scope","dtmf","accept"),
						   "meta"=>array("","author","copyright","description","keywords","maintainer","robots"),
						   "noinput"=>array("","count","cond"),
						   "nomatch"=>array("","count","cond"),
						   "object"=>array("","name","expr","cond","classid","codebase","codetype","data","type","archive","fetchhint","fetchtimeout","maxage","maxstale"),
						   "option"=>array("","value","dtmf"),
						   "param"=>array("","name","expr","value","valuetype","type"),
						   "prompt"=>array("","bargein","bargeintype","cond","count","timeout","lang"),
						   "property"=>array("","name","value"),
						   "record"=>array("","name","expr","cond","modal","beep","maxtime","finalsilence","dtmfterm","type","dest"),
						   "reprompt"=>array(""),
						   "return"=>array("","event","namelist"),
						   "script"=>array("","src","charset","fetchhint","fetchtimeout","maxage","maxstale"),
						   "subdialog"=>array("","name","expr","cond","namelist","src","method","enctype","fetchaudio","fetchhint","fetchtimeout","maxage","maxstale"),
						   "submit"=>array("","next","method","namelist","method","enctype","fetchaudio","fetchhint","fetchtimeout","maxage","maxstale"),
						   "throw"=>array("","event","eventexpr","message","messageexpr"),
						   "transfer"=>array("","name","expr","cond","dest","destexpr","bridge","connecttimeout","maxtime","transferaudio"),
						   "value"=>array("","expr"),
						   "var"=>array("","name","expr"),
						   "vxml"=>array("","version","xml:lang","application","base"));
		
	function _init()
	{
		$this->err_code = "";
		$this->err_line = "";
		$this->err_col = "";
		$this->err_msg = "";
		$this->size = 0;
		$this->elements = 0;
		$this->attributes = 0;
		$this->texts = 0;
		$this->text_size = 0; 
	}
	
	function _startElement($parser, $name, $attrs)
	{
	    for ($i = 0; $i < $this->depth[$parser]; $i++) {
    	    print "&nbsp;&nbsp;";
		}
	    print "$name <br>";
		$this->attrs[$name]=$attrs;
		$this->tags[] = $name;
    	$this->depth[$parser]++;
		$this->elements++;
		$this->attributes+=count($attrs);
		return true;
	}
	
	function _endElement($parser, $name)
	{
	    $this->depth[$parser]--;
		return true;
	}
	
	function parse()
	{
		$this->_init();
		$this->parser = xml_parser_create();
		xml_set_element_handler($this->parser, "_startElement", "_endElement");
		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
		xml_set_object($this->parser,&$this);
		xml_set_character_data_handler($this->parser,"_data");
		if (!($fp = fopen($this->_file, "r"))) {
		    die("could not open VXML input");
		}
		while ($data = fread($fp, 4096)) {
			$this->size+=strlen($data);
	    	if (!xml_parse($this->parser, $data, feof($fp))) {
		        $this->err_code = xml_get_error_code($this->parser);
        		$this->err_line = xml_get_current_line_number($this->parser);
		        $this->err_col = xml_get_current_column_number($this->parser);
        		$this->err_msg = xml_error_string($this->error_code);
				return false;
		    }
		}
		xml_parser_free($this->parser);
		return true;
	}

	function _data($parser,$data)
	{
		$this->texts++;
		$this->text_size+=strlen($data);
	}
	
	function test1()
	{
		// Test if tags are valid with VXML 2.0
		for ($i=0;$i<count($this->tags);$i++){
			if (!array_search($this->tags[$i], $this->validtags)) {
			    echo "<font color=red>VXML Error : Invalid VXML tag &lt;".$this->tags[$i]."&gt; in ".$this->_file."</font><br/>";
				$this->err_test1 = true;
			} 
		}
	}
  	
	function test2()
	{		// 
			if ($this->tags[0]!== "vxml") {
			    echo "<font color=red>VXML Error : first tag &lt;vxml&gt; doesn't exist in ".$this->_file."</font><br/>";
				$this->err_test2 = true;
			} 
			
			while (list ($key, $val) = each ($this->attrs["vxml"]))
			{
				if ($key == "version"){
					if ($val == "2.0"){
						echo "<font color=green><b>Test passed : actual &lt;vxml&gt; element is including a \"version\" attribute with the value \"2.0\".</b></font><br/>";
						return true;
					} else {
						echo "<font color=red>VXML version Error : The &lt;vxml&gt; element must include a \"version\" attribute with the value \"2.0\".</font><br/>";
					}
				}
			}
	}
	
	function test_assign_tag()
	{
	// Assign element should have at least two attributes.
			while (list ($key, $val) = each ($this->attrs["assign"]))
			{
				if ($key == "name"){
					$this->test3["assign.name"] = true;
				}
				if ($key == "expr"){
					$this->test3["assign.expr"] = true;
				}
			}
			
			if ($this->test3["assign.name"])
			{
				echo "<font color=green><b>Test passed : actual &lt;assign&gt; element is including a \"name\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;assign&gt; element require a \"name\" attribute value.</font><br/>";
			}
			if ($this->test3["assign.expr"])
			{
				echo "<font color=green><b>Test passed : actual &lt;assign&gt; element include a \"expr\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;assign&gt; element require a \"expr\" attribute value.</font><br/>";
			}
	}
	
	function test_audio_tag()
	{
	// Audio element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["audio"]))
			{
				if ($key == "src"){
					$this->test3["audio.src"] = true;
				}
			}
			
			if ($this->test3["audio.src"])
			{
				echo "<font color=green><b>Test passed : actual &lt;audio&gt; element include a \"src\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;audio&gt; element require a \"src\" attribute value.</font><br/>";
			}
	}
	
	function test_catch_tag()
	{
	// Catch element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["catch"]))
			{
				if ($key == "event"){
					$this->test3["catch.event"] = true;
				}
			}
			
			if ($this->test3["catch.event"])
			{
				echo "<font color=green><b>Test passed : actual &lt;catch&gt; element include a \"event\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;catch&gt; element require a \"event\" attribute value.</font><br/>";
			}
	}
	
	function test_choice_tag()
	{
	// choice element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["choice"]))
			{
				if ($key == "next"){
					$this->test3["choice.next"] = true;
				}
			}
			
			if ($this->test3["choice.next"])
			{
				echo "<font color=green><b>Test passed : actual &lt;choice&gt; element include a \"next\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;choice&gt; element require a \"next\" attribute value.</font><br/>";
			}
	}
	
	function test_clear_tag()
	{
	// clear element should have at least namelist attribute.
			while (list ($key, $val) = each ($this->attrs["clear"]))
			{
				if ($key == "namelist"){
					$this->test3["clear.namelist"] = true;
				}
			}
			
			if ($this->test3["clear.namelist"])
			{
				echo "<font color=green><b>Test passed : actual &lt;clear&gt; element include a \"namelist\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;clear&gt; element require a \"namelist\" attribute value.</font><br/>";
			}
	}
	
	function test_elseif_tag()
	{
	// elseif element should have at least cond attribute.
			while (list ($key, $val) = each ($this->attrs["elseif"]))
			{
				if ($key == "cond"){
					$this->test3["elseif.cond"] = true;
				}
			}
			
			if ($this->test3["elseif.cond"])
			{
				echo "<font color=green><b>Test passed : actual &lt;elseif&gt; element include a \"cond\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;elseif&gt; element require a \"cond\" attribute value.</font><br/>";
			}
	}

	function test_exit_tag()
	{
	// exit element should have at least namelist of expr attribute.
			while (list ($key, $val) = each ($this->attrs["exit"]))
			{
				if ($key == "namelist"){
					$this->test3["exit.namelist"] = true;
				}
				if ($key == "expr"){
					$this->test3["exit.expr"] = true;
				}
			}
			
			if ($this->test3["exit.namelist"] or $this->test3["exit.expr"])
			{
				echo "<font color=green><b>Test passed : actual &lt;exit&gt; element include a \"namelist\" or \"expr\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;exit&gt; element require a \"namelist\" or \"expr\" attribute value.</font><br/>";
			}
	}
	
	function test_field_tag()
	{
	// field element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["field"]))
			{
				if ($key == "name"){
					$this->test3["field.name"] = true;
				}
			}
			
			if ($this->test3["field.name"])
			{
				echo "<font color=green><b>Test passed : actual &lt;field&gt; element include a \"name\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;field&gt; element require a \"name\" attribute value.</font><br/>";
			}
	}
	
	function test_goto_tag()
	{
	// Goto element should have at least next, expr, expritem or nextitem attribute.
			while (list ($key, $val) = each ($this->attrs["goto"]))
			{
				if ($key == "next"){
					$this->test3["goto.next"] = true;
				}
				if ($key == "expr"){
					$this->test3["goto.expr"] = true;
				}
				if ($key == "expritem"){
					$this->test3["goto.expritem"] = true;
				}
				if ($key == "nextitem"){
					$this->test3["goto.nextitem"] = true;
				}
			}
			
			if ($this->test3["goto.next"] or $this->test3["goto.expr"] or $this->test3["goto.nextitem"] or $this->test3["goto.expritem"])
			{
				echo "<font color=green><b>Test passed : &lt;Goto&gt; element is including at least one of the \"next\",\"expr\",\"nextitem\",\"expritem\" attributes.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : &lt;Goto&gt; element require at least one of the \"next\",\"expr\",\"nextitem\",\"expritem\" attributes.</font><br/>";
			}
	}
	
	function test_if_tag()
	{
	// if element should have at least cond attribute.
			while (list ($key, $val) = each ($this->attrs["if"]))
			{
				if ($key == "cond"){
					$this->test3["if.cond"] = true;
				}
			}
			
			if ($this->test3["if.cond"])
			{
				echo "<font color=green><b>Test passed : actual &lt;if&gt; element include a \"cond\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;if&gt; element require a \"cond\" attribute value.</font><br/>";
			}
	}
	
	function test_initial_tag()
	{
	// initial element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["initial"]))
			{
				if ($key == "name"){
					$this->test3["initial.name"] = true;
				}
			}
			
			if ($this->test3["initial.name"])
			{
				echo "<font color=green><b>Test passed : actual &lt;initial&gt; element include a \"name\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;initial&gt; element require a \"name\" attribute value.</font><br/>";
			}
	}
	
	function test_throw_tag()
	{
	// throw element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["throw"]))
			{
				if ($key == "event"){
					$this->test3["throw.event"] = true;
				}
			}
			
			if ($this->test3["throw.event"])
			{
				echo "<font color=green><b>Test passed : actual &lt;throw&gt; element include a \"event\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;throw&gt; element require a \"event\" attribute value.</font><br/>";
			}
	}
	
	function test_value_tag()
	{
	// value element should have at least one attribute.
			while (list ($key, $val) = each ($this->attrs["value"]))
			{
				if ($key == "expr"){
					$this->test3["value.expr"] = true;
				}
			}
			
			if ($this->test3["value.expr"])
			{
				echo "<font color=green><b>Test passed : actual &lt;value&gt; element include a \"expr\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;value&gt; element require a \"expr\" attribute value.</font><br/>";
			}
	}

	function test_var_tag()
	{
	// Assign element should have at least two attributes.
			while (list ($key, $val) = each ($this->attrs["var"]))
			{
				if ($key == "name"){
					$this->test3["var.name"] = true;
				}
				if ($key == "expr"){
					$this->test3["var.expr"] = true;
				}
			}
			
			if ($this->test3["var.name"])
			{
				echo "<font color=green><b>Test passed : actual &lt;var&gt; element include a \"name\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;var&gt; element require a \"name\" attribute value.</font><br/>";
			}
			if ($this->test3["var.expr"])
			{
				echo "<font color=green><b>Test passed : actual &lt;var&gt; element include a \"expr\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;var&gt; element require a \"expr\" attribute value.</font><br/>";
			}
	}
	
	function test_submit_tag()
	{
	// Assign element should have at least two attributes.
			while (list ($key, $val) = each ($this->attrs["submit"]))
			{
				if ($key == "next"){
					$this->test3["submit.next"] = true;
				}
				if ($key == "expr"){
					$this->test3["submit.expr"] = true;
				}
			}
			
			if ($this->test3["submit.next"] or $this->test3["submit.expr"])
			{
				echo "<font color=green><b>Test passed : actual &lt;var&gt; element include a \"name\" attribute.</b></font><br/>";
			} else {
				echo "<font color=red>VXML Error : actual &lt;var&gt; element require a \"name\" attribute value.</font><br/>";
			}
	}
	
	function _test3()
	{
			for ($i=0;$i<count($this->tags);$i++){
				switch ($this->tags[$i])
				{
					case "assign" : $this->test_assign_tag();
					break;
					
					case "audio" :	$this->test_audio_tag();
					break;
					
					case "catch" :	$this->test_catch_tag();
					break;
					
					case "choice" :	$this->test_choice_tag();
					break;
					
					case "clear" :	$this->test_clear_tag();
					break;
					
					case "elseif" :	$this->test_elseif_tag();
					break;
					
					case "exit" :	$this->test_exit_tag();
					break;
					
					case "field" :	$this->test_field_tag();
					break;
					
					case "goto" :	$this->test_goto_tag();
					break;
					
					case "initial" :	$this->test_initial_tag();
					break;
					
					case "submit" :	$this->test_submit_tag();
					break;
					
					case "throw" :	$this->test_throw_tag();
					break;
					
					case "value" :	$this->test_value_tag();
					break;
					
					case "var" :	$this->test_var_tag();
					break;
				}
			}
			
		if (!array_search(true, $this->test3)) {
			return false;
		} else {
			return true;
		}
	}
	
	function test4()
	{
		for ($i=0;$i<count($this->tags);$i++){
			if (!array_search($this->tags[$i], $this->validtags)) {
//			    echo "<font color=red>VXML Error : Invalid VXML tag &lt;".$this->tags[$i]."&gt; in ".$this->_file."</font><br/>";
				$this->err_test1 = true;
			} else {
					while (list ($key, $val) = each ($this->attrs[$this->tags[$i]]))
					{
						if (!array_search($key,$this->vxml_tags[$this->tags[$i]])) {
							echo "<font color=red>VXML Error : Invalid attribute in VXML tag &lt;".$this->tags[$i]."$key=\"....\"&gt; in ".$this->_file."</font><br/>";
							$this->err_test4 = true;
						}
					}
			}
		}	
	}
}


?>
Return current item: VXML Parser