Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed bug #629

moved cdata treatment to renderer
added path constant (for testing)


git-svn-id: http://svn.php.net/repository/pear/packages/XML_Beautifier/trunk@150268 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit 8dfa79eb7a46954049d3256273506ebe2652ace4 1 parent 0a6d5fa
Stephan Schmidt authored
View
12 Beautifier.php
@@ -16,6 +16,10 @@
// | Authors: Stephan Schmidt <schst@php.net> |
// +----------------------------------------------------------------------+
+if( !defined( 'XML_BEAUTIFIER_INCLUDE_PATH' ) ) {
+ define( 'XML_BEAUTIFIER_INCLUDE_PATH', 'XML/Beautifier' );
+}
+
/**
* XML/Beautifier.php
*
@@ -133,6 +137,8 @@ class XML_Beautifier {
* @access private
*/
var $_defaultOptions = array(
+ "removeLineBreaks" => true,
+ "removeLeadingSpace"=> true, // not implemented, yet
"indent" => " ",
"linebreak" => "\n",
"caseFolding" => false,
@@ -223,7 +229,7 @@ function formatFile($file, $newFile = null, $renderer = "Plain")
* Split the document into tokens
* using the XML_Tokenizer
*/
- require_once "XML/Beautifier/Tokenizer.php";
+ require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
$tokenizer = new XML_Beautifier_Tokenizer();
$tokens = $tokenizer->tokenize( $file, true );
@@ -270,7 +276,7 @@ function formatString($string, $renderer = "Plain")
* Split the document into tokens
* using the XML_Tokenizer
*/
- require_once "XML/Beautifier/Tokenizer.php";
+ require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
$tokenizer = new XML_Beautifier_Tokenizer();
$tokens = $tokenizer->tokenize( $string, false );
@@ -306,7 +312,7 @@ function formatString($string, $renderer = "Plain")
*/
function &_loadRenderer($name, $options = array())
{
- $file = "XML/Beautifier/Renderer/$name.php";
+ $file = XML_BEAUTIFIER_INCLUDE_PATH . "/Renderer/$name.php";
$class = "XML_Beautifier_Renderer_$name";
@include_once $file;
View
21 Beautifier/Renderer.php
@@ -112,9 +112,26 @@ function _normalizeToken($token)
continue;
}
+ /**
+ * remove whitespace
+ */
+ if( $this->_options['removeLineBreaks'] == true )
+ {
+ $children[$i]['data'] = trim($children[$i]['data']);
+ if( $children[$i]['data'] == '' ) {
+ continue;
+ }
+ }
+
if ($inCData) {
$tmp = array_pop($token["children"]);
- $tmp["data"] .= " " . $children[$i]["data"];
+
+ if( $children[$i]['data'] != '' ) {
+ if( $tmp['data'] != '' && $this->_options['removeLineBreaks'] == true ) {
+ $tmp['data'] .= ' ';
+ }
+ $tmp["data"] .= $children[$i]["data"];
+ }
array_push($token["children"], $tmp);
} else {
array_push($token["children"], $children[$i]);
@@ -144,6 +161,8 @@ function _indentTextBlock($text, $depth, $trim = false)
if ($trim) {
$tmp[$i] = trim($tmp[$i]);
}
+ if( $tmp[$i] == '' )
+ continue;
$xml .= $indent.$tmp[$i].$this->_options["linebreak"];
}
return $xml;
View
11 Beautifier/Renderer/Plain.php
@@ -32,7 +32,7 @@
/**
* Renderer base class
*/
-require_once 'XML/Beautifier/Renderer.php';
+require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Renderer.php';
/**
* Basic XML Renderer for XML Beautifier
@@ -69,7 +69,7 @@ function serialize($tokens)
/**
* serialize a token
*
- * This method doeas the actual beautifying.
+ * This method does the actual beautifying.
*
* @access private
* @param array $token structure that should be serialized
@@ -115,6 +115,12 @@ function _serializeToken($token)
} else {
$data = '';
}
+
+ if( strstr( $data, "\n" ) )
+ {
+ $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;
@@ -143,6 +149,7 @@ function _serializeToken($token)
} else {
$xml = "";
}
+
$xml .= XML_Util::replaceEntities( $token["data"] ) . $this->_options["linebreak"];
break;
View
5 Beautifier/Tokenizer.php
@@ -146,11 +146,6 @@ function endHandler($parser, $element)
*/
function cdataHandler($parser, $cdata)
{
- /**
- * remove whitespace
- */
- $cdata = trim($cdata);
-
if ((string)$cdata === '') {
return true;
}
View
28 examples/example7.php
@@ -0,0 +1,28 @@
+<?PHP
+/**
+ * XML_Beautifier example 7
+ *
+ * This example shows to to change the treatment
+ * of data section
+ *
+ * @author Stephan Schmidt <schst@php.net>
+ */
+ error_reporting( E_ALL );
+
+ require_once 'XML/Beautifier.php';
+
+ $fmt = new XML_Beautifier( array( 'removeLineBreaks' => false ) );
+ $result = $fmt->formatFile('test3.xml');
+
+ echo "<h3>Original file</h3>";
+ echo "<pre>";
+ echo htmlspecialchars(implode("",file('test3.xml')));
+ echo "</pre>";
+
+ echo "<br><br>";
+
+ echo "<h3>Beautified output</h3>";
+ echo "<pre>";
+ echo htmlspecialchars($result);
+ echo "</pre>";
+?>
View
42 examples/test2.xml
@@ -3,14 +3,14 @@
<!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>";
@@ -18,19 +18,23 @@
?>
&foo;
&bar;
- <intro>
- <!-- This Comment has more than one line. -->
- <introtitle>Welcome to PHP Application Tools &amp; PEAR!</introtitle>
- <para>
+ <INTRO>
+ <!--
+ This Comment
+ has more
+ than one line.
+ -->
+ <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>
View
9 examples/test3.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<foo>
+1
+ 2
+ 3
+4
+5
+7
+</foo>
View
33 package.xml
@@ -13,30 +13,35 @@
</maintainer>
</maintainers>
<release>
- <version>1.0.1</version>
- <date>2004-01-12</date>
+ <version>1.1</version>
+ <date>2004-02-01</date>
<license>PHP License</license>
<state>stable</state>
- <notes>fixed bug #556 (notices)</notes>
+ <notes>fixed bug #629 (treatment of data sections),
+added XML_BEAUTIFER_INCLUDE_PATH constant for testing environments,
+moved data trim() to renderer</notes>
<deps>
- <dep type="php" rel="ge" version="4.2.0" />
+ <dep type="php" rel="ge" version="4.2.0"/>
<dep type="pkg" rel="ge" version="0.5">XML_Util</dep>
<dep type="pkg" rel="has">XML_Parser</dep>
</deps>
<provides type="class" name="XML_Beautifier_Renderer" />
+ <provides type="function" name="XML_Beautifier_Renderer::serialize" />
+ <provides type="function" name="XML_Beautifier_Renderer::normalize" />
<provides type="class" name="XML_Beautifier_Tokenizer" extends="XML_Parser" />
+ <provides type="function" name="XML_Beautifier_Tokenizer::tokenize" />
<provides type="class" name="XML_Beautifier_Renderer_Plain" extends="XML_Beautifier_Renderer" />
+ <provides type="function" name="XML_Beautifier_Renderer_Plain::serialize" />
<provides type="class" name="XML_Beautifier" />
<provides type="function" name="XML_Beautifier::resetOptions" />
<provides type="function" name="XML_Beautifier::setOption" />
<provides type="function" name="XML_Beautifier::setOptions" />
<provides type="function" name="XML_Beautifier::formatFile" />
<provides type="function" name="XML_Beautifier::formatString" />
- <provides type="function" name="XML_Beautifier::apiVersion" />
<filelist>
- <file role="php" baseinstalldir="XML" md5sum="ffaf6da7121fce7162e0a85083fefd09" name="Beautifier/Renderer.php"/>
- <file role="php" baseinstalldir="XML" md5sum="bff95c69fe70420c4c5312e3e0509bae" name="Beautifier/Tokenizer.php"/>
- <file role="php" baseinstalldir="XML" md5sum="6896aec67cda7b5c8aec64621ec114df" name="Beautifier/Renderer/Plain.php"/>
+ <file role="php" baseinstalldir="XML" md5sum="0fe8fdb3c92356385355ba71286628b7" name="Beautifier/Renderer.php"/>
+ <file role="php" baseinstalldir="XML" md5sum="ce7e636f4bacc6763238f4cefc9cf219" name="Beautifier/Tokenizer.php"/>
+ <file role="php" baseinstalldir="XML" md5sum="b527eb7f57511f27a39cd4e6e3affb90" name="Beautifier/Renderer/Plain.php"/>
<file role="doc" baseinstalldir="XML" md5sum="e77c0e6684083bf0ef4e47dba8fdd4da" name="examples/example1.php"/>
<file role="doc" baseinstalldir="XML" md5sum="083dae7bc56551fc681ca897e0d13b3a" name="examples/example2.php"/>
<file role="doc" baseinstalldir="XML" md5sum="c59676e7726c81466397ad2b1ef71857" name="examples/example3.php"/>
@@ -44,8 +49,7 @@
<file role="doc" baseinstalldir="XML" md5sum="c2ed8597eb9636a59a61332ea4aaa337" name="examples/example5.php"/>
<file role="doc" baseinstalldir="XML" md5sum="5c156a8686a0c1ed9d049791bf0673b8" name="examples/example6.php"/>
<file role="doc" baseinstalldir="XML" md5sum="553ed0097ee4d61490ea0ff4d186dbd6" name="examples/test.xml"/>
- <file role="doc" baseinstalldir="XML" md5sum="149ebc9269aa2c97ecbe3285a3c2250d" name="examples/test2.xml"/>
- <file role="php" baseinstalldir="XML" md5sum="cda1126f6e4e4504d15943d3a3d768c5" name="Beautifier.php"/>
+ <file role="php" baseinstalldir="XML" md5sum="4305cbc9b63747bf9717fe5efcb0c2a0" name="Beautifier.php"/>
</filelist>
</release>
<changelog>
@@ -97,5 +101,14 @@ did some small cleanup</notes>
<state>stable</state>
<notes>fixed bug #556 (notices)</notes>
</release>
+ <release>
+ <version>1.1</version>
+ <date>2004-02-01</date>
+ <license>PHP License</license>
+ <state>stable</state>
+ <notes>fixed bug #629 (treatment of data sections),
+added XML_BEAUTIFER_INCLUDE_PATH constant for testing environments,
+moved data trim() to renderer</notes>
+ </release>
</changelog>
</package>
Please sign in to comment.
Something went wrong with that request. Please try again.