cvs: pear /XML_Tree Tree.php /XML_Tree/Tree Node.php

From: Date: Fri, 21 May 2004 13:10:44 +0000
Subject: cvs: pear /XML_Tree Tree.php /XML_Tree/Tree Node.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-19734@lists.php.net to get a copy of this message
davey Fri May 21 09:10:44 2004 EDT Modified files: /pear/XML_Tree Tree.php /pear/XML_Tree/Tree Node.php Log: Added the ability to encase all the Tree's cdata in <![CDATA]]>. Also the ability to encase a single node's cdata in <![CDATA]]>. You can also excluse a node when the entire tree is set to use <![CDATA[]]> New method: XML_Tree::useCdataSections(); Modified: XML_Tree_Node::XML_Tree_Node() XML_Tree_Node::addChild() XML_Tree_Node::get() XML_Tree_Node::dump() Added $use_cdata_section param to all, defaults to null (off) http://cvs.php.net/diff.php/pear/XML_Tree/Tree.php?r1=1.32&r2=1.33&ty=u Index: pear/XML_Tree/Tree.php diff -u pear/XML_Tree/Tree.php:1.32 pear/XML_Tree/Tree.php:1.33 --- pear/XML_Tree/Tree.php:1.32 Thu May 20 07:32:17 2004 +++ pear/XML_Tree/Tree.php Fri May 21 09:10:43 2004 @@ -19,7 +19,7 @@ // | Michele Manzato <michele.manzato@verona.miz.it> | // +----------------------------------------------------------------------+ // -// $Id: Tree.php,v 1.32 2004/05/20 11:32:17 davey Exp $ +// $Id: Tree.php,v 1.33 2004/05/21 13:10:43 davey Exp $ // require_once 'XML/Parser.php'; @@ -82,6 +82,14 @@ * @var string */ var $version = '1.0'; + + /** + * Whether to encapsulate the all CDATA in a <![CDATA[]]> section + * + * @var boolean + */ + + var $use_cdata_sections = false; /** * Constructor @@ -94,6 +102,17 @@ $this->filename = $filename; $this->version = $version; } + + /** + * Use <![CDATA[]]> for all CDATA sections + * + * @return void + */ + + function useCdataSections() + { + $this->use_cdata_sections = true; + } /** * Gets the root node @@ -369,7 +388,7 @@ if ($xmlHeader) { header('Content-type: text/xml'); } - echo $this->get(); + echo $this->get($this->use_cdata_sections); } /** @@ -381,11 +400,12 @@ function &get() { $out = '<?xml version="' . $this->version . "\"?>\n"; + if (!is_null($this->root)) { if(!is_object($this->root) || (strtolower(get_class($this->root)) != 'xml_tree_node')) return $this->raiseError("Bad XML root node"); - $out .= $this->root->get(); + $out .= $this->root->get($this->use_cdata_sections); } return $out; } http://cvs.php.net/diff.php/pear/XML_Tree/Tree/Node.php?r1=1.19&r2=1.20&ty=u Index: pear/XML_Tree/Tree/Node.php diff -u pear/XML_Tree/Tree/Node.php:1.19 pear/XML_Tree/Tree/Node.php:1.20 --- pear/XML_Tree/Tree/Node.php:1.19 Thu May 20 07:32:18 2004 +++ pear/XML_Tree/Tree/Node.php Fri May 21 09:10:44 2004 @@ -18,7 +18,7 @@ // | Michele Manzato <michele.manzato@verona.miz.it> | // +----------------------------------------------------------------------+ // -// $Id: Node.php,v 1.19 2004/05/20 11:32:18 davey Exp $ +// $Id: Node.php,v 1.20 2004/05/21 13:10:44 davey Exp $ // /** @@ -78,6 +78,14 @@ */ var $error = null; + + /** + * Whether to encapsulate the CDATA in a <![CDATA[]]> section + * + * @var boolean + */ + + var $use_cdata_section = null; /** @@ -88,7 +96,7 @@ * @param array attributes Attribute-hash for the node */ - function XML_Tree_Node($name, $content = '', $attributes = array(), $lineno = null) + function XML_Tree_Node($name, $content = '', $attributes = array(), $lineno = null, $use_cdata_section = null) { $check_name = XML_Tree::isValidName($name, 'element'); if (PEAR::isError($check_name)) { @@ -103,7 +111,7 @@ } } $this->name = $name; - $this->setContent($content); + $this->setContent($content, $use_cdata_section); $this->attributes = $attributes; $this->children = array(); $this->lineno = $lineno; @@ -122,7 +130,7 @@ * @access public */ - function &addChild($child, $content = '', $attributes = array(), $lineno = null) + function &addChild($child, $content = '', $attributes = array(), $lineno = null, $use_cdata_section = null) { $index = sizeof($this->children); @@ -135,7 +143,7 @@ $this->children[$index] = $child->root->getElement(); } } else { - $node = new XML_Tree_Node($child, $content, $attributes, $lineno); + $node = new XML_Tree_Node($child, $content, $attributes, $lineno, $use_cdata_section); if (PEAR::isError($node->error)) { return $node->error; } @@ -279,7 +287,7 @@ * @access public */ - function &get() + function &get($use_cdata_section = false) { static $deep = -1; static $do_ident = true; @@ -310,13 +318,22 @@ $out .= ' />'; $empty = true; } else { - $out .= '>' . $this->content; + $out .= '>'; + if ($this->use_cdata_section == true || ($use_cdata_section == true && $this->use_cdata_section !== false)) { + if (trim($this->content) != '') { + $out .= '<![CDATA[' .$this->content. ']]>'; + } else { + $out .= $this->content; + } + } else { + $out .= $this->content; + } } if (sizeof($this->children) > 0) { $out .= "\n"; foreach ($this->children as $child) { - $out .= $child->get(); + $out .= $child->get($use_cdata_section); } } else { $ident = ''; @@ -390,9 +407,15 @@ * @access public */ - function setContent(&$content) + function setContent($content, $use_cdata_section = false) { - $this->content = $this->encodeXmlEntities($content); + $this->use_cdata_section = $use_cdata_section; + + if ($use_cdata_section == true) { + $this->content = $content; + } else { + $this->content = $this->encodeXmlEntities($content); + } } /**

« previous php.pear.cvs (#19734) next »