<?php
/**
* Project: CWF: Cricava Web Framework
* File: Dommer.class.php
*
* @link http://www.cricava.com/
* @copyright 2005-2009 Cricava Technologies, Inc.
* @author Mariano Iglesias <hide@address.com>
* @package com.cricava.cwf
* @version 1.0
*/
if (!defined('XML_PARSER_METHOD'))
{
define('XML_PARSER_METHOD', 'dom');
}
if (!defined('XML_PARSER_PEAR_XML_PARSER_PATH'))
{
define ('XML_PARSER_PEAR_XML_PARSER_PATH', dirname(__FILE__) . '/XML_Parser-1.2.6');
}
if (defined('XML_PARSER_METHOD') && strcasecmp(XML_PARSER_METHOD, 'dom') == 0 && (version_compare(phpversion(), "5.0", ">=") || function_exists('domxml_open_mem')))
{
/**
* Uses DOM xml to parse an XML file
*
* @author Mariano Iglesias
* @package com.cricava.cwf
* @subpackage xml
* @since 1.0
*/
class Dommer
{
/**
* The path to the XML file to parse
*
* @access private
* @since 1.0
* @var string
*/
var $xmlFile = "";
/**
* The DOM root element
*
* @access private
* @since 1.0
* @var object
*/
var $root = null;
/**
* Constructs a new instance of Dommer based on the path to the XML file to parse.
*
* @param string xmlFile The XML file to parse (full path)
*
* @access public
* @since 1.0
*/
function & Dommer($xmlFile = null)
{
$this->setXmlFile($xmlFile);
}
/**
* Set the path to the xml file to parse
*
* @param string The path to the XML file
*
* @access public
* @since 1.0
*/
function setXmlFile($xmlFile)
{
$this->xmlFile = $xmlFile;
}
/**
* Get the path to the xml file to parse
*
* @return string The path to the XML file
*
* @access public
* @since 1.0
*/
function getXmlFile()
{
return $this->xmlFile;
}
/**
* Parses the whole XML file to an internal tree, and closes the file handler.
*
* @return array The array containing all DOM elements parsed
*
* @access public
* @since 1.0
*/
function & dominate($xmlString = null)
{
if (isset($xmlString))
{
if (version_compare(phpversion(), "5.0", ">="))
{
$domObject = new DOMDocument();
$domObject->loadXML($xmlString);
if (isset($domObject))
{
$nodeList = $domObject->getElementsByTagName("*");
if (isset($nodeList))
{
$this->root = $nodeList->item(0);
}
}
return $this->root;
} else {
$domObject = domxml_open_mem($xmlString);
if (isset($domObject))
{
$this->root = $domObject->document_element();
}
return $this->root;
}
}
else if (isset($this->xmlFile) && is_readable($this->xmlFile))
{
if (version_compare(phpversion(), "5.0", ">="))
{
$domObject = new DOMDocument();
$domObject->loadXML(file_get_contents($this->xmlFile));
if (isset($domObject))
{
$nodeList = $domObject->getElementsByTagName("*");
if (isset($nodeList))
{
$this->root = $nodeList->item(0);
}
}
return $this->root;
} else {
$domObject = domxml_open_file($this->xmlFile);
if (isset($domObject))
{
$this->root = $domObject->document_element();
}
return $this->root;
}
}
return null;
}
/**
* Get the children of a node
*
* @param mixed The parent node
*
* @return mixed The children of a node
*
* @access public
* @since 1.0
*/
function & getChildren(&$domNode)
{
$returnValue = null;
if (version_compare(phpversion(), "5.0", ">="))
{
if (isset($domNode) && $domNode->hasChildNodes())
{
$i=0;
$domNodeList =& $domNode->childNodes;
if (isset($domNodeList))
{
while ($node = $domNodeList->item($i))
{
$nodeSet[] = $node;
$i++;
}
$returnValue =& $nodeSet;
}
}
}
else
{
if (isset($domNode) && $domNode->has_child_nodes())
{
$returnValue =& $domNode->child_nodes();
}
}
return $returnValue;
}
/**
* Get the type of a node
*
* @param mixed The parent node
*
* @return mixed The type of a node
*
* @access public
* @since 1.0
*/
function & getNodeType(&$domNode)
{
$returnValue = null;
if (isset($domNode))
{
if (version_compare(phpversion(), "5.0", ">="))
{
$returnValue = $domNode->nodeType;
}
else
{
$returnValue = $domNode->node_type();
}
}
return $returnValue;
}
/**
* Get the name of a node
*
* @param mixed The parent node
*
* @return mixed The name of a node
*
* @access public
* @since 1.0
*/
function & getNodeName(&$domNode)
{
$returnValue = null;
if (isset($domNode) && $this->getNodeType($domNode) == XML_ELEMENT_NODE)
{
if (version_compare(phpversion(), "5.0", ">="))
{
$returnValue = $domNode->nodeName;
}
else
{
$returnValue = $domNode->node_name();
}
}
return $returnValue;
}
/**
* Get the attribute of a node
*
* @param mixed The parent node
* @param string The attribute to get
*
* @return string The value for the attribute
*
* @access public
* @since 1.0
*/
function & getNodeAttribute(&$domNode, $attribute)
{
$returnValue = null;
if (isset($domNode))
{
if (version_compare(phpversion(), "5.0", ">="))
{
$returnValue = $domNode->getAttribute($attribute);
}
else
{
$returnValue = $domNode->get_attribute($attribute);
}
}
return $returnValue;
}
/**
* Get the value of a node
*
* @param mixed The parent node
*
* @return string The value
*
* @access public
* @since 1.0
*/
function & getNodeValue(&$domNode)
{
$returnValue = null;
if (isset($domNode))
{
if (version_compare(phpversion(), "5.0", ">="))
{
$returnValue = $domNode->nodeValue;
}
else
{
$returnValue = $domNode->node_value();
}
}
return $returnValue;
}
}
}
else
{
require_once(XML_PARSER_PEAR_XML_PARSER_PATH . '/Parser.php');
/**
* Uses XML_Parser to parse an XML file
*
* @author Mariano Iglesias
* @package com.cricava.cwf
* @subpackage xml
* @since 1.0
*/
class Dommer extends XML_Parser
{
/**
* The path to the XML file to parse
*
* @access private
* @since 1.0
* @var string
*/
var $xmlFile = "";
/**
* The parsed XML tree
*
* @access private
* @since 1.0
* @var array
*/
var $xmlTree;
var $processingNodes;
/**
* Constructs a new instance of Dommer based on the path to the XML file to parse.
*
* @param string xmlFile The XML file to parse (full path)
*
* @access public
* @since 1.0
*/
function & Dommer($xmlFile = null)
{
parent::XML_Parser();
$this->setXmlFile($xmlFile);
}
/**
* Set the path to the xml file to parse
*
* @param string The path to the XML file
*
* @access public
* @since 1.0
*/
function setXmlFile($xmlFile)
{
$this->xmlFile = $xmlFile;
}
/**
* Get the path to the xml file to parse
*
* @return string The path to the XML file
*
* @access public
* @since 1.0
*/
function getXmlFile()
{
return $this->xmlFile;
}
/**
* Parses the whole XML file to an internal tree, and closes the file handler.
*
* @return array The array containing all DOM elements parsed
*
* @access public
* @since 1.0
*/
function & dominate()
{
if (isset($this->xmlFile) && is_readable($this->xmlFile))
{
$this->setInputFile($this->xmlFile);
$this->parse();
return $this->xmlTree[0];
}
return null;
}
/**
* handle start element
*
* @param resource xml parser resource
* @param string name of the element
* @param array attributes
*
* @access private
* @since 1.0
*/
function startHandler($xp, $name, $attribs)
{
if (!isset($this->xmlTree))
{
$this->xmlTree = array();
}
$currentNode = array (
"id" => count($this->xmlTree),
"name" => strtolower($name),
"attributes" => $attribs,
"children" => array()
);
$this->xmlTree[] = $currentNode;
$currentNodeIndex = count($this->xmlTree) - 1;
if (!isset($this->processingNodes))
{
$this->processingNodes = array();
}
else
{
$parentIndex = $this->processingNodes[count($this->processingNodes) - 1];
$this->xmlTree[$parentIndex]['children'][] = $currentNodeIndex;
}
array_push($this->processingNodes, $currentNodeIndex);
}
/**
* handle end element
*
* @param resource xml parser resource
* @param string name of the element
*
* @access private
* @since 1.0
*/
function endHandler($xp, $name)
{
if (isset($this->processingNodes))
{
array_pop($this->processingNodes);
}
}
/**
* handle character data
*
* @param resource xml parser resource
* @param string character data
*
* @access private
* @since 1.0
*/
function cdataHandler($xp, $cdata)
{
if (trim($cdata) != '')
{
$childrenWithDataIndex = -1;
for ($i=0, $limiti = count($this->xmlTree[count($this->xmlTree) - 1]['children']); $childrenWithDataIndex == -1 && $i < $limiti; $i++)
{
if (isset($this->xmlTree[count($this->xmlTree) - 1]['children'][$i]['data']))
{
$childrenWithDataIndex = $i;
}
}
if ($childrenWithDataIndex == -1)
{
$this->xmlTree[count($this->xmlTree) - 1]['children'][] = array ( 'data' => '' );
$childrenWithDataIndex = count($this->xmlTree[count($this->xmlTree) - 1]['children']) - 1;
}
$newCData = "";
if (trim($this->xmlTree[count($this->xmlTree) - 1]['children'][$childrenWithDataIndex]['data']) != '')
{
$newCData .= $this->xmlTree[count($this->xmlTree) - 1]['children'][$childrenWithDataIndex]['data'];
$newCData .= "\n";
}
$newCData .= $cdata;
$this->xmlTree[count($this->xmlTree) - 1]['children'][$childrenWithDataIndex] = array (
'data' => $newCData
);
}
}
/**
* Get the children of a node
*
* @param mixed The parent node
*
* @return mixed The children of a node
*
* @access public
* @since 1.0
*/
function & getChildren(&$domNode)
{
$nodeId = (is_array($domNode) ? $domNode['id'] : $domNode);
if (isset($this->xmlTree[$nodeId]['children']) && is_array($this->xmlTree[$nodeId]['children']) && count($this->xmlTree[$nodeId]['children']) > 0)
{
return $this->xmlTree[$nodeId]['children'];
}
return null;
}
/**
* Get the type of a node
*
* @param mixed The parent node
*
* @return mixed The type of a node
*
* @access public
* @since 1.0
*/
function & getNodeType(&$domNode)
{
$nodeId = (is_array($domNode) ? $domNode['id'] : $domNode);
if (isset($this->xmlTree[$nodeId]['type']))
{
return $this->xmlTree[$nodeId]['type'];
}
return null;
}
/**
* Get the name of a node
*
* @param mixed The parent node
*
* @return mixed The name of a node
*
* @access public
* @since 1.0
*/
function & getNodeName(&$domNode)
{
$nodeId = (is_array($domNode) ? $domNode['id'] : $domNode);
if (isset($this->xmlTree[$nodeId]['name']))
{
return $this->xmlTree[$nodeId]['name'];
}
return null;
}
/**
* Get the attribute of a node
*
* @param mixed The parent node
* @param string The attribute to get
*
* @return string The value for the attribute
*
* @access public
* @since 1.0
*/
function & getNodeAttribute(&$domNode, $attribute)
{
$nodeId = (is_array($domNode) ? $domNode['id'] : $domNode);
if (isset($this->xmlTree[$nodeId]['attributes']) && is_array($this->xmlTree[$nodeId]['attributes']) && count($this->xmlTree[$nodeId]['attributes']) > 0)
{
return $this->xmlTree[$nodeId]['attributes'][$attribute];
}
return null;
}
/**
* Get the value of a node
*
* @param mixed The parent node
*
* @return string The value
*
* @access public
* @since 1.0
*/
function & getNodeValue(&$domNode)
{
if (is_array($domNode) && isset($domNode['data']))
{
return $domNode['data'];
}
else
{
$nodeId = (is_array($domNode) ? $domNode['id'] : $domNode);
if (isset($this->xmlTree[$nodeId]['data']))
{
return $this->xmlTree[$nodeId]['data'];
}
}
return null;
}
}
}
?>