Permalink
Browse files

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)


git-svn-id: http://svn.php.net/repository/pear/packages/XML_Tree/trunk@159263 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent a5cd12e commit ebafb94ae9c5235634773f4e16f4dd4ebdfec9f2 @dshafik dshafik committed May 21, 2004
Showing with 54 additions and 11 deletions.
  1. +22 −2 Tree.php
  2. +32 −9 Tree/Node.php
View
@@ -82,6 +82,14 @@ class XML_Tree extends XML_Parser
* @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 @@ function XML_Tree($filename = '', $version = '1.0')
$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 @@ function dump($xmlHeader = false)
if ($xmlHeader) {
header('Content-type: text/xml');
}
- echo $this->get();
+ echo $this->get($this->use_cdata_sections);
}
/**
@@ -381,11 +400,12 @@ function dump($xmlHeader = false)
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;
}
View
@@ -78,6 +78,14 @@ class XML_Tree_Node {
*/
var $error = null;
+
+ /**
+ * Whether to encapsulate the CDATA in a <![CDATA[]]> section
+ *
+ * @var boolean
+ */
+
+ var $use_cdata_section = null;
/**
@@ -88,7 +96,7 @@ class XML_Tree_Node {
* @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 @@ function XML_Tree_Node($name, $content = '', $attributes = array(), $lineno = nu
}
}
$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 @@ function XML_Tree_Node($name, $content = '', $attributes = array(), $lineno = nu
* @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 @@ function &addChild($child, $content = '', $attributes = array(), $lineno = null)
$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 @@ function registerName($name, $path) {
* @access public
*/
- function &get()
+ function &get($use_cdata_section = false)
{
static $deep = -1;
static $do_ident = true;
@@ -310,13 +318,22 @@ function &get()
$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 @@ function unsetAttribute($name)
* @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);
+ }
}
/**

0 comments on commit ebafb94

Please sign in to comment.