[Paulscriptsmods-checkin] ajax_shout/root/includes/cortex-xml/xml xml_element.php, NONE, 1.1.2.1 xm
Status: Beta
Brought to you by:
paulsohier
From: Paul S. <pau...@us...> - 2007-06-20 17:30:02
|
Update of /cvsroot/paulscriptsmods/ajax_shout/root/includes/cortex-xml/xml In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19022/root/includes/cortex-xml/xml Added Files: Tag: phpbb3_ver xml_element.php xml_reader.php xml_writer.php Log Message: EEEk, Added cortext XML class, written by coronis.nl (Thanks Vic!) for generating XML. --- NEW FILE: xml_element.php --- <?php /** * XML element * * @package Cortex * @subpackage cortex_xml * * @copyright 2006 Coronis - http://www.coronis.nl * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * See copyright.txt for more information * * @version $Id: xml_element.php,v 1.1.2.1 2007/06/20 17:29:56 paulsohier Exp $ */ /** * cortex_xml_element class * * @package Cortex * @subpackage cortex_xml */ class cortex_xml_element { /** * Name of the XML element * * @access public * @var string */ var $name; /** * Element attributes * * @access public * @var array */ var $attributes; /** * Element's contents * * @access public * @var string */ var $content; /** * Element's child elements * * @access public * @var array */ var $children; var $root = false; /** * cortex_xml_element constructor * * @access public * @param string Element name * @return void */ function cortex_xml_element($name = '') { $this->name = $name; } /** * Find a child element by its name * * @access public * @param string Child name * @return mixed Child object on success, otherwise FALSE */ function get_child($child_name, $get_contents = false) { for ( $i = 0, $_i = sizeof($this->children); $i < $_i; $i++ ) { if ( $this->children[$i]->name == $child_name ) { if ( $get_contents ) { return $this->children[$i]->content; } else { return $this->children[$i]; } } } return false; } function get_xml($include_doctype = true) { $xml_writer = cortex_xml::factory('writer'); return $xml_writer->format_data($this, $include_doctype); } function output_xml($no_cache = false, $exit = false) { $xml_writer = cortex_xml::factory('writer'); $xml_writer->format_data($this, true); $xml_writer->output_data($no_cache, $exit); } function add_attribute($attribute_name, $attribute_value) { $this->attributes[$attribute_name] = $attribute_value; } function set_attributes($attributes) { $this->attributes = $attributes; } function add_child(&$child) { @$this->children[] =& $child; } function set_parent($parent) { $parent->children[] = $this; } function get_child_by_attribute($attribute_name, $attribute_value, $get_contents = false) { for ( $i = 0, $_i = sizeof($this->children); $i < $_i; $i++ ) { if ( isset($this->children[$i]->attributes[$attribute_name]) && $this->children[$i]->attributes[$attribute_name] == $attribute_value ) { if ( $get_contents ) { return $this->children[$i]->content; } else { return $this->children[$i]; } } } return false; } } ?> --- NEW FILE: xml_reader.php --- <?php /** * XML reader * * @package Cortex * @subpackage cortex_xml * * @copyright 2006 Coronis - http://www.coronis.nl * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * See copyright.txt for more information * * @version $Id: xml_reader.php,v 1.1.2.1 2007/06/20 17:29:56 paulsohier Exp $ */ /** * cortex_xml_reader class * * @package Cortex * @subpackage cortex_xml */ class cortex_xml_reader extends cortex_xml { /** * Raw XML data * * @access public * @var string */ var $xml_raw; /** * Root XML element (object of cortex_xml_element) * * @access public * @var object */ var $root_element; /** * Parse an XML file * * @access public * @param string Filename * @return object Root XML element (object of cortex_xml_element) */ function parse_file($filename) { $this->xml_raw = @file_get_contents($filename); if ( $this->xml_raw === false ) { new cortex_exception('cortex_xml_reader: Could not parse XML', 'Input file "' . $filename . '" could not be loaded'); } return $this->parse_data(); } /** * Parse the XML data in $this->xml_raw * * @access public * @return object Root XML element (object of cortex_xml_element) */ function parse_data($xml_raw = '') { if ( !empty($xml_raw) ) { $this->xml_raw = $xml_raw; } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0); xml_parse_into_struct($parser, $this->xml_raw, $tags); xml_parser_free($parser); $elements = array(); $stack = array(); foreach ( $tags as $tag ) { $index = sizeof($elements); if ( $tag['type'] == 'complete' || $tag['type'] == 'open' ) { $elements[$index] = new cortex_xml_element(); $elements[$index]->name = $tag['tag']; if (array_key_exists('attributes', $tag)) $elements[$index]->attributes = $tag['attributes']; if (array_key_exists('value', $tag)) $elements[$index]->content = trim($tag['value']); if ( $tag['type'] == 'open' ) { $elements[$index]->children = array(); $stack[sizeof($stack)] = &$elements; $elements = &$elements[$index]->children; } } if ( $tag['type'] == 'close' ) { $elements = &$stack[sizeof($stack) - 1]; unset($stack[sizeof($stack) - 1]); } } $this->root_element = $elements[0]; return $this->root_element; } } ?> --- NEW FILE: xml_writer.php --- <?php /** * XML writer * * @package Cortex * @subpackage cortex_xml * * @copyright 2006 Coronis - http://www.coronis.nl * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * See copyright.txt for more information * * @version $Id: xml_writer.php,v 1.1.2.1 2007/06/20 17:29:56 paulsohier Exp $ */ /** * cortex_xml_writer class * * @package Cortex * @subpackage cortex_xml */ class cortex_xml_writer extends cortex_xml { /** * Raw XML data * * @access protected * @var string */ var $xml_data = ''; var $xml_indent = " "; var $xml_stylesheet = ''; var $xml_charset = 'utf-8'; /** * Convert a cortex_xml_element into raw XML * * @access public * @param object cortex_xml_element object * @param int Number of tabs we should indent * @return string XML */ function object_to_xml($object, $indent = 0) { $data = ''; if ($object->root === true) { $data .= '<?xml version="1.0" encoding="' . $this->xml_charset . '" standalone="yes"?'.'>'; $data .= "\n"; if ($this->xml_stylesheet != '') { $data .= '<?xml-stylesheet type="text/xsl" href="' . $this->xml_stylesheet . '"?'.'>'; } $data .= "\n" . str_pad('', $indent, $this->xml_indent, STR_PAD_LEFT) . '<' . $object->name; } else { $data .= "\n" . str_pad('', $indent, $this->xml_indent, STR_PAD_LEFT) . '<' . $object->name; } $indent += strlen($this->xml_indent); // Attributes if ( is_array($object->attributes) ) { foreach ( $object->attributes as $attribute => $attribute_value ) { $data .= ' ' . $attribute . '="' . $attribute_value . '"'; } } if (is_array($object->children) || is_array($object->content) || !($object->content === null)) { $data .= ">"; // Children if ( is_array($object->children) ) { foreach ( $object->children as $child_element ) { $data .= $this->object_to_xml($child_element, $indent); } } // Content (array)... this is a quick 'n dirty way of having multiple // elements elseif ( is_array($object->content) ) { foreach ( $object->content as $element => $element_value ) { $data .= str_pad('', $indent, $this->xml_indent, STR_PAD_LEFT) . '<' . $element . ">" . $this->cdata($element_value) . '</' . $element . ">\n"; } } // Content (string) else { $data .= $this->cdata($object->content); } // Closing tag $indent -= strlen($this->xml_indent); if ( is_array($object->children) ) { $data .= "\n" . str_pad('', $indent, $this->xml_indent, STR_PAD_LEFT) . '</' . $object->name . '>'; } else { $data .= '</' . $object->name . '>'; } } else { $data .= " />"; } return $data; } /** * Return <![CDATA[$contents]]> * * @access protected * @param string Contents to escape * @return string Escaped contents on success, otherwise original string */ function cdata($contents) { $contents = ( $this->xml_encoding == 'utf-8' ) ? utf8_encode(trim($contents)) : trim($contents); if ( preg_match('/\<(.*?)\>/xsi', $contents) ) { return '<![CDATA[' . $contents . ']]>'; } return $contents; } /** * Convert an object of cortex_xml_element and its children to a valid XML file * * @access public * @param object Object of cortex_xml_element * @return string XML data */ function format_data($object, $include_doctype) { $this->xml_data = ''; if ( $include_doctype ) { $this->xml_data = '<?xml version="1.0" encoding="' . $this->xml_encoding . '"? >' . "\n"; } $this->xml_data .= $this->object_to_xml($object); return $this->xml_data; } /** * Directly output the data to the client * * @access public * @param bool Set to TRUE to send the 'no-cache' header. Should be done in order * to prevent headaches and other mental disorders when using AJAX in IE * @param bool Set to TRUE to call exit() after having sent the data * @return null */ function output_data($no_cache = false, $exit = false) { ob_clean(); header('Content-type: text/xml'); if ( $no_cache ) { header('Pragma: no-cache'); header('Cache-Control: no-cache'); header('Expires: ' . date('r', 0)); } print $this->xml_data; if ( $exit ) { exit(); } } /** * Write $this->xml_data to a file * * @access public * @param string filename to write to * @return void */ function save_file($filename) { if ( !@file_put_contents($filename, $this->xml_data) ) { new cortex_exception('Could not store XML data', 'Could not write to "' . $filename . '"'); } } } ?> |