PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/xml/XmlParser.php

http://zoop.googlecode.com/
PHP | 63 lines | 49 code | 12 blank | 2 comment | 1 complexity | d238dc9c6976129aeed447ea8bbca0ce MD5 | raw file
 1<?php
 2abstract class XmlParser
 3{
 4	private $dom;
 5	protected $container;
 6	
 7	function __construct()
 8	{
 9		$this->dom = new XmlDom();
10	}
11	
12	function parseText($xml)
13	{
14		$xml = "<xml>$xml</xml>";
15		$root = $this->dom->parseText($xml);
16		return $this->handleRoot($root);
17	}
18	
19	function parseFile($filename)
20	{
21		$root = $this->dom->parseFile($filename);
22		return $this->handleRoot($root);
23	}
24	
25	protected abstract function initExtra();
26	protected abstract function handleTextNode($child, $container, $extra);
27	
28	private function handleRoot($root)
29	{
30		// echo 'handleRoot 1<br>';
31		$this->container = $this->initRootContainer();
32		// echo 'handleRoot 2<br>';
33		
34		$extra = $this->initExtra();
35		$this->handleNode($root, $this->container, $extra);
36		
37		return $this->container;
38	}
39	
40	protected function handleXml($node, $container, $extra)
41	{
42		return $container;
43	}
44	
45	private function handleNode($node, $container, $extra)
46	{
47		$this->preNodeHandler($node, $container, $extra);
48		
49		$tagName = $node->getName();
50		$handler = "handle$tagName";
51		$container = $this->$handler($node, $container, $extra);
52		
53		foreach($node->children as $child)
54		{
55			if($child->isText())
56				$this->handleTextNode($child, $container, $extra);
57			else
58				$this->handleNode($child, $container, $extra);
59		}
60		
61		$this->postNodeHandler($node, $container, $extra);
62	}
63}