Permalink
Browse files

added support for CData sections (bug #1009)

git-svn-id: http://svn.php.net/repository/pear/packages/XML_Beautifier/trunk@156385 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 8dfa79e commit 1f730b3f7687319ade6a6652ef9a9f5101ed72fc Stephan Schmidt committed Apr 17, 2004
Showing with 69 additions and 25 deletions.
  1. +15 −3 Beautifier/Renderer/Plain.php
  2. +31 −3 Beautifier/Tokenizer.php
  3. +1 −1 examples/example2.php
  4. +1 −0 examples/test.xml
  5. +21 −18 examples/test2.xml
@@ -41,8 +41,7 @@
* @package XML_Beautifier
* @author Stephan Schmidt <schst@php.net>
* @todo option to specify inline tags
- * @todo option to specify treatment of whitespac in data sections
- * @todo automatically create <![CDATA[ ]]> sections
+ * @todo option to specify treatment of whitespace in data sections
*/
class XML_Beautifier_Renderer_Plain extends XML_Beautifier_Renderer {
@@ -120,7 +119,7 @@ function _serializeToken($token)
{
$data = "\n" . $this->_indentTextBlock( $data, $token['depth']+1, true );
}
-
+
$xml = $indent . XML_Util::createTag($token["tagname"], $token["attribs"], $data, null, XML_UTIL_REPLACE_ENTITIES, $this->_options["multilineTags"], $attIndent)
. $this->_options["linebreak"];
break;
@@ -153,6 +152,19 @@ function _serializeToken($token)
$xml .= XML_Util::replaceEntities( $token["data"] ) . $this->_options["linebreak"];
break;
+ /*
+ * serialize CData section
+ */
+ case XML_BEAUTIFIER_CDATA_SECTION:
+ if ($token["depth"] > 0) {
+ $xml = str_repeat($this->_options["indent"], $token["depth"]);
+ } else {
+ $xml = "";
+ }
+
+ $xml .= '<![CDATA['.$token["data"].']]>' . $this->_options["linebreak"];
+ break;
+
/*
* serialize entity
*/
View
@@ -64,6 +64,13 @@ class XML_Beautifier_Tokenizer extends XML_Parser {
*/
var $_mode = "xml";
+ /**
+ * indicates, whether parser is in cdata section
+ * @var boolean
+ * @access private
+ */
+ var $_inCDataSection = false;
+
/**
* Tokenize a document
*
@@ -150,8 +157,14 @@ function cdataHandler($parser, $cdata)
return true;
}
+ if ($this->_inCDataSection === true) {
+ $type = XML_BEAUTIFIER_CDATA_SECTION;
+ } else {
+ $type = XML_BEAUTIFIER_CDATA;
+ }
+
$struct = array(
- "type" => XML_BEAUTIFIER_CDATA,
+ "type" => $type,
"data" => $cdata,
"depth" => $this->_depth
);
@@ -168,7 +181,7 @@ function cdataHandler($parser, $cdata)
* @param string data
* @return void
*/
- function piHandler($parser, $target, $data)
+ function piHandler($parser, $target, $data)
{
$struct = array(
"type" => XML_BEAUTIFIER_PI,
@@ -268,6 +281,18 @@ function _handleXMLDefault($data)
"depth" => $this->_depth
);
/*
+ * handle start of cdata section
+ */
+ } elseif ($data == "<![CDATA[") {
+ $this->_inCDataSection = true;
+ $struct = null;
+ /*
+ * handle end of cdata section
+ */
+ } elseif ($data == "]]>") {
+ $this->_inCDataSection = false;
+ $struct = null;
+ /*
* handle XML declaration
*/
} elseif (strncmp("<?", $data, 2) == 0) {
@@ -313,7 +338,9 @@ function _handleXMLDefault($data)
);
}
- $this->_appendToParent($struct);
+ if (!is_null($struct)) {
+ $this->_appendToParent($struct);
+ }
return true;
}
@@ -367,6 +394,7 @@ function _resetVars()
$this->_depth = 0;
$this->_struct = array();
$this->_mode = "xml";
+ $this->_inCDataSection = false;
}
}
?>
View
@@ -10,7 +10,7 @@
*/
error_reporting( E_ALL );
- $xmlString = '<xml><foo bar="tomato &amp; Cheese"/><argh>foobar</argh></xml>';
+ $xmlString = '<xml><foo bar="tomato &amp; Cheese"/><argh>foobar<![CDATA[ Test ]]></argh></xml>';
require_once 'XML/Beautifier.php';
$fmt = new XML_Beautifier();
View
@@ -17,6 +17,7 @@
?>
&foo;&bar;
<intro>
+<test><![CDATA[This is a CData section with an &. ]]></test>
<!-- This Comment
has more
than one line.
View
@@ -3,38 +3,41 @@
<!ENTITY foo SYSTEM "foo.xml">
<!ENTITY bar SYSTEM "bar.xml">
]>
-<DOCUMENT TITLE="Current News">
- <META FOO="bar" PROJECT="none">
- <KEYWORDS />
- <DESCRIPTION />
- <AUTHOR>Stephan Schmidt</AUTHOR>
- <GETMETANAV />
- </META>
- <PAGE LABEL="PHP Application Tools" SUBLABEL="Current News">
+<document title="Current News">
+ <meta foo="bar" project="none">
+ <keywords />
+ <description />
+ <author>Stephan Schmidt</author>
+ <getMetaNav />
+ </meta>
+ <page label="PHP Application Tools" sublabel="Current News">
<?PHP
for($i = 0; $i < count($_GET); $i++) {
echo $_GET[$i]."<br>";
}
?>
&foo;
&bar;
- <INTRO>
+ <intro>
+ <test>
+ <![CDATA[ This is a CData section with an &. ]]>
+ </test>
<!--
This Comment
has more
than one line.
-->
- <INTROTITLE>Welcome to PHP Application Tools &amp; PEAR!</INTROTITLE>
- <PARA>
+ <introtitle>Welcome to PHP Application Tools &amp; PEAR!</introtitle>
+ <para>
If you&apos;re new to pat, and would like
<!-- This is a comment in a single line that contains an &amp; -->
to know what we do here, take a look at
- <LINK URL="/about/project.xml">&quot;About Pat&quot;</LINK>
+ <link url="/about/project.xml">&quot;About Pat&quot;</link>
or check out the
- <LINK URL="/about/projectsOverview.xml">&quot;projects overview&quot;</LINK>
+ <link url="/about/projectsOverview.xml">&quot;projects overview&quot;</link>
. Otherwise, you probably know your way around the site already
- <SMILEY TYPE="smile" />
- </PARA>
- </INTRO>
- </PAGE>
-</DOCUMENT>
+ <smiley type="smile" />
+ </para>
+ </intro>
+ </page>
+</document>

0 comments on commit 1f730b3

Please sign in to comment.