<?php
	include_once 'XML/Parser.php';

	class Price extends XML_Parser
	{
		var $aProducts = array();
		
		function Price()
		{
			$this->XML_Parser();
		}

		function AddProduct($aProduct)
		{
			$this->aProducts[] = $aProduct;

			print_r($this->aProducts);
		}
	}

	class Dustin extends Price
	{
		var $sCurrentNode;
		var $aCurrentProduct;
		
		var $handler = array(
			"character_data_handler"		=> "cDataHandler"
		);

		
		function Dustin()
		{
			$this->Price();
		}
	
		function OpenFile()
		{
			$fp = $this->setInputFile("test.xml");
			if (PEAR::isError($fp))
			{
				return $this->raiseError("could not open inputfile");
			}

			$error = $this->setInput($fp);
			if (PEAR::isError($error))
			{
				return $this->raiseError($error);
			}
		}

		function ParseFile()
		{
			$r = $this->OpenFile();
			if (PEAR::isError($r))
			{
				return $this->raiseError($r);
			}
			
			$this->Parse();
			
			echo "Successfully parsed ".count($this->aProducts)." products...\n";
		}
		
		function cDataHandler($xp, $data)
		{
			switch($this->sCurrentNode)
			{
				case "PARTNUMBER":			$use = "anr";							break;
				case "DESCRIPTION":			$use = "name";							break;
				case "PRICEWITHTAX":		$use = "price";							break;
				case "MANUFACTURER":		$use = "manufacturer";					break;
				case "MANUF_PARTNUMBER":	$use = "manufacturer.anr";				break;
				case "PRODUCTURL":			$use = "product.url";					break;
			}
			
			if ($use != "")
			{
				$this->aCurrentProduct[$use] = $data;
			}
			
			if (stristr($this->sCurrentNode, "PRODUCTGROUP_") && $data != "")
			{
				$this->aCurrentProduct["category"] .= $data."/";
			}
		}

		function StartHandler($xp, $elem, &$attribs)
		{
			$this->sCurrentNode = $elem;
		}

		function EndHandler($xp, $elem)
		{
			if ($elem == "ITEMADD")
			{
				//$this->aCurrentProduct["category"] = $this->translateCategory($this->aCurrentProduct["category"];
				$this->AddProduct($this->aCurrentProduct);
				$this->aCurrentProduct = array();
			}
			
			$this->sCurrentNode = "";
		}
	}

	$dust = new Dustin();
	print_r($dust->parsefile());
?>
