File: 1.03.0a/server/base/node.xml.inc.php (View as HTML)

  1: <?php // node.xml.inc.php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: class TNodeXML
 24: {
 25: var $Catalogue=array();
 26: var $Init=false;
 27: var $Elements=0;
 28: var $Tests=0;
 29: var $Header=array();
 30: var $LastError="";
 31: 
 32: var $in_freenats=false;
 33: var $in_header=false;
 34: var $in_test=false;
 35: var $depth=0;
 36: var $last_element="";
 37: var $cur_testname="";
 38: 
 39: function startElement($parser, $name, $attrs)
 40: {
 41: if ($name=="FREENATS-DATA") $this->in_freenats=true;
 42: if (!$this->in_freenats) return 0;
 43: 
 44: if ($name=="TEST")
 45: 	{
 46: 	$this->in_test=true;
 47: 	$this->Tests++;
 48: 	$this->cur_testname=$attrs['NAME'];
 49: 	}
 50: else if ($name=="HEADER") $in_header=true;
 51: $this->last_element=$name;
 52: $this->depth++;
 53: $this->Elements++;
 54: }
 55: 
 56: function endElement($parser, $name)
 57: {
 58: $this->depth--;
 59: if ($name=="TEST") $in_test=false;
 60: else if ($name=="HEADER") $in_header=false;
 61: else if ($name=="FREENATS-DATA") $in_freenats=false;
 62: }
 63: 
 64: function charData($parser,$data)
 65: {
 66: if (!$this->in_freenats) return 0;
 67: $data=trim($data);
 68: if ($data!="")
 69: 	{
 70: 	if ($this->in_test)
 71: 		{
 72: 		$this->Catalogue[$this->cur_testname][$this->last_element]=$data;
 73: 		}
 74: 	else if ($this->in_header)
 75: 		$this->Header[$this->last_element]=$data;
 76: 	}
 77: }
 78: 
 79: function Error()
 80: {
 81: return $this->LastError;
 82: }
 83: 
 84: function Parse($xml)
 85: {
 86: if (get_magic_quotes_gpc()) $xml=stripslashes($xml);
 87: $this->Init=true;
 88: $parser=xml_parser_create();
 89: xml_set_element_handler($parser,Array( $this, "startElement" ),
 90: 	Array( $this, "endElement" ) );
 91: xml_set_character_data_handler($parser,Array( $this, "charData" ));
 92: 
 93: $return=true;
 94: 
 95: if (!xml_parse($parser,$xml,true))
 96: 	{
 97: 	$this->LastError="XML Error ".xml_error_string(xml_get_error_code($parser));
 98: 	$this->LastError.=" at line ".xml_get_current_line_number($parser);
 99: 	$return=false;
100: 	}
101: 
102: xml_parser_free($parser);
103: 
104: if ($return) return $this->Catalogue;
105: else return 0;
106: }
107: 
108: function ParseFile($xmlfile)
109: {
110: $fp=fopen($xmlfile,"r")
111:  or die("Could not open XML file ".$xmlfile);
112: $data="";
113: while (!feof($fp))
114: 	$data.=fgets($fp,4096);
115: fclose($fp);
116: return $this->Parse($data);
117: }
118: 
119: 
120: }
121: ?>