PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » XML » XML_Tree » Bug #2968

patch: generalize options, use XML_Util

Details

Request #2968patch: generalize options, use XML_Util
Submitted2004-12-15 00:03 UTC
Fromaaron dot hawley at uvm dot edu
StatusSuspended
PackageXML_Tree
PHP Version4.3.4
OSGNU/Linux
Roadmaps(Not assigned)

Comments

[2004-12-15 00:03 UTC] aaron dot hawley at uvm dot edu

Description:
------------
The following patch generalizes options for this package, including version and 'use_cdata_sections', and now gives support for Doctypes (DTD). Doctype and XML Declarations are generated with methods available in the XML_UTil package. A proposal to deprecate the method useCdataSections() is also contained. The patch applies to a Unix version of the file. The file is currently in DOS format.

Reproduce code:
---------------
--- XML/Tree.php 2004-05-26 10:58:18-04 1.35
+++ XML/Tree.php 2004-12-14 17:03:07-05
@@ -19,9 +19,10 @@
// | Michele Manzato <michele.manzato@verona.miz.it> |
// +----------------------------------------------------------------------+
//
-// $Id: Tree.php,v 1.35 2004-05-26 10:58:18-04 davey Exp ashawley $
+// $Id: Tree.php,v 1.36 2004-12-14 11:17:40-05 ashawley Exp ashawley $
//

+require_once 'XML/Util.php';
require_once 'XML/Parser.php';
require_once 'XML/Tree/Node.php';

@@ -77,41 +78,101 @@
var $root = NULL;

/**
- * XML Version
+ * Options
*
- * @var string
+ * @var array $_options
+ * @access private
+ * @see XML_Util::getXMLDeclaration(), XML_Util::getDocTypeDeclaration()
*/
- var $version = '1.0';
+ var $_options = array('version' => '1.0', // XML Version, string or array
+ // for XML_Util::getXMLDeclaration()
+ 'doctype' => null); // Document type declaration
+ // string or array for
+ // XML_Util::getDocTypeDeclaration()
+

/**
- * Whether to encapsulate the all CDATA in a <![CDATA[]]> section
+ * XML Tree Defaults - Default options.
*
- * @var boolean
+ * @var array $_options
+ * @access private
+ * @see XML_Util::getXMLDeclaration(), XML_Util::getDocTypeDeclaration()
*/
-
- var $use_cdata_sections = false;
+ var $_XML_Tree_defaults
+ = array('version' => '1.0', // XML Version, string or array
+ // for XML_Util::getXMLDeclaration()
+ 'doctype' => null, // Document type declaration
+ // string or array for
+ // XML_Util::getDocTypeDeclaration()
+ 'use_cdata_sections' => false); // Whether to encapsulate
+ // all CDATA in a <![CDATA[]]>
+ // section.

/**
* Constructor
*
+ * @access public
* @param string filename Filename where to read the XML
* @param string version XML Version to apply
+ * @param array options Options
*/
- function XML_Tree($filename = '', $version = '1.0')
+ function XML_Tree($filename = '', $version = '1.0', $options)
{
$this->filename = $filename;
- $this->version = $version;
+ $this->setOptions($this->_XML_Tree_defaults);
+ $options['version'] = $version;
+ $this->setOptions($options);
}

/**
* Use <![CDATA[]]> for all CDATA sections
*
* @return void
+ * @deprecated deprecated after 2.0RC2
*/
-
function useCdataSections()
{
- $this->use_cdata_sections = true;
+ $this->setOption('use_cdata_sections', true);
+ }
+
+ /**
+ * Set Option
+ *
+ * @access public
+ * @param string $option
+ * @param mixed $val
+ * @return void
+ */
+ function setOption($option, $val)
+ {
+ $this->_options[$option] = $val;
+ }
+
+ /**
+ * Get Option
+ *
+ * @accesspp public
+ * @param string $option
+ * @return mixed
+ */
+ function getOption($option)
+ {
+ return $this->_options[$option];
+ }
+
+ /**
+ * Set Options
+ *
+ * @access public
+ * @param array $options
+ * @return void
+ */
+ function setOptions($options)
+ {
+ $this->_options = array_merge($this->_options, $options);
+ // Using array_merge() so as not to overwrite default options
+ // and if class inherited from ever uses same variable name
+ // for storing options.
}

/**
@@ -359,7 +420,7 @@
*/
function cloneTree()
{
- $clone = new XML_Tree($this->filename, $this->version);
+ $clone = new XML_Tree($this->filename, $options['version'], $this->_options);
if (!is_null($this->root)) {
$clone->root = $this->root->cloneTree();
}
@@ -388,7 +449,7 @@
if ($xmlHeader) {
header('Content-type: text/xml');
}
- echo $this->get($this->use_cdata_sections);
+ echo $this->get($this->getOption('use_cdata_sections'));
}

/**
@@ -399,13 +460,29 @@
*/
function &get()
{
- $out = '<?xml version="' . $this->version . "\"?>\n";
+ $version = $this->getOption('version');
+ if (!empty($version)) {
+ if (is_array($version)) {
+ $out = call_user_func_array(array('XML_Util', 'getXMLDeclaration'), $version);
+ } else {
+ $out = '<?xml version="' . $version . "\"?>\n";
+ }
+
+ }
+ $doctype = $this->getOption('doctype');
+ if (!empty($doctype)) {
+ if (is_array($doctype)) {
+ $out = call_user_func_array(array('XML_Util', 'getDoctypeDeclaration'), $doctype);
+ } else {
+ $out .= "<!DOCTYPE ${doctype}>\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($this->use_cdata_sections);
+ $out .= $this->root->get($this->getOption('use_cdata_sections'));
}
return $out;
}

http://www.uvm.edu/~ashawley/php/XML_Tree-Tree.php-suggest.diff-u

Expected result:
----------------
These features added in this patch should allow for full backwards compatability.

Willing to make necessary documentation change or write tests to verify backward compatibility. Let me know.

[2004-12-15 19:50 UTC] aaron dot hawley at uvm dot edu

The above patch is to version 2.0RC2 of the package. The patch uploaded at the link above has now changed, and fixes a few typos and also is against the CVS version of the package.

[2004-12-16 17:26 UTC] aaron dot hawley at uvm dot edu

Patch reported to the PEAR development mailing list.

http://news.php.net/php.pear.dev/35107