cvs: pear /XML_Util Util.php /XML_Util/examples example.php example2.php

From: Date: Sun, 14 Sep 2003 19:10:00 +0000
Subject: cvs: pear /XML_Util Util.php /XML_Util/examples example.php example2.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-12728@lists.php.net to get a copy of this message
schst Sun Sep 14 15:10:00 2003 EDT Modified files: /pear/XML_Util Util.php /pear/XML_Util/examples example.php example2.php Log: * added createCDataSection() * added support for CData Section in createTag* methods Index: pear/XML_Util/Util.php diff -u pear/XML_Util/Util.php:1.4 pear/XML_Util/Util.php:1.5 --- pear/XML_Util/Util.php:1.4 Fri Sep 12 06:23:23 2003 +++ pear/XML_Util/Util.php Sun Sep 14 15:09:59 2003 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php-tools.net> | // +----------------------------------------------------------------------+ // -// $Id: Util.php,v 1.4 2003/09/12 10:23:23 schst Exp $ +// $Id: Util.php,v 1.5 2003/09/14 19:09:59 schst Exp $ require_once 'PEAR.php'; @@ -35,6 +35,15 @@ */ define("XML_UTIL_ERROR_NON_SCALAR_CONTENT", 60); +/** + * replace XML entities + */ +define("XML_UTIL_REPLACE_ENTITIES", 1); + +/** + * embedd content in a CData Section + */ +define("XML_UTIL_CDATA_SECTION", 2); /** * utility class for working with XML documents @@ -214,12 +223,12 @@ * @param array $attributes array containg attributes * @param mixed $content * @param string $namespaceUri URI of the namespace - * @param boolean $replaceEntities whether to replace XML special chars in content or not + * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both * @return string $string XML tag * @see XML_Util::createTagFromArray() * @uses XML_Util::createTagFromArray() to create the tag */ - function createTag( $qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = true ) + function createTag( $qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES ) { $tag = array( "qname" => $qname, @@ -260,7 +269,7 @@ * "qname" => "foo:bar", * "namespaceUri" => "http://foo.com", * "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ), - * "content" => "I'm inside the tag" + * "content" => "I'm inside the tag", * ); * // creating a tag with qualified name and namespaceUri * $string = XML_Util::createTagFromArray($tag); @@ -269,13 +278,13 @@ * @access public * @static * @param array $tag tag definition - * @param boolean $replaceEntities whether to replace XML special chars in content or not + * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both * @return string $string XML tag * @see XML_Util::createTag() * @uses XML_Util::attributesToString() to serialize the attributes of the tag * @uses XML_Util::splitQualifiedName() to get local part and namespace of a qualified name */ - function createTagFromArray( $tag, $replaceEntities = true ) + function createTagFromArray( $tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES ) { // if no attributes hav been set, use empty attributes if (!isset($tag["attributes"]) || !is_array($tag["attributes"])) { @@ -314,9 +323,11 @@ if (!isset($tag["content"]) || (string)$tag["content"] == '') { $tag = sprintf("<%s%s/>", $tag["qname"], $attList); } elseif (is_scalar($tag["content"])) { - if ($replaceEntities) { + if ($replaceEntities == XML_UTIL_REPLACE_ENTITIES) { $tag["content"] = XML_Util::replaceEntities($tag["content"]); - } + } elseif ($replaceEntities == XML_UTIL_CDATA_SECTION) { + $tag["content"] = sprintf("<![CDATA[%s]]>", $tag["content"]); + } $tag = sprintf("<%s%s>%s</%s>", $tag["qname"], $attList, $tag["content"], $tag["qname"] ); } else { return PEAR::raiseError( "Supplied non-scalar value as tag content", XML_UTIL_ERROR_NON_SCALAR_CONTENT ); @@ -342,7 +353,7 @@ * @return string $string XML start element * @see XML_Util::createEndElement(), XML_Util::createTag() */ - function createStartElement( $qname, $attributes = array(), $namespaceUri = null ) + function createStartElement($qname, $attributes = array(), $namespaceUri = null) { // if no attributes hav been set, use empty attributes if (!isset($attributes) || !is_array($attributes)) { @@ -385,12 +396,32 @@ * @return string $string XML end element * @see XML_Util::createStartElement(), XML_Util::createTag() */ - function createEndElement( $qname ) + function createEndElement($qname) { $element = sprintf("</%s>", $qname); return $element; } + /** + * create a CData section + * + * <code> + * require_once 'XML/Util.php'; + * + * // create a CData section + * $tag = XML_Util::createCDataSection("I am content."); + * </code> + * + * @access public + * @static + * @param string $data data of the CData section + * @return string $string CData section with content + */ + function createCDataSection($data) + { + return sprintf("<![CDATA[%s]]>", $data); + } + /** * split qualified name and return namespace and local part * Index: pear/XML_Util/examples/example.php diff -u pear/XML_Util/examples/example.php:1.3 pear/XML_Util/examples/example.php:1.4 --- pear/XML_Util/examples/example.php:1.3 Fri Sep 12 06:23:24 2003 +++ pear/XML_Util/examples/example.php Sun Sep 14 15:10:00 2003 @@ -114,6 +114,19 @@ print "\n<br><br>\n"; /** + * creating an XML tag with a CData Section + */ + $tag = array( + "qname" => "foo", + "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ), + "content" => "I'm inside the tag" + ); + + print "creating a tag with CData section:<br>\n"; + print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_CDATA_SECTION)); + print "\n<br><br>\n"; + + /** * creating an XML tag with createTag */ print "creating a tag with createTag:<br>"; Index: pear/XML_Util/examples/example2.php diff -u pear/XML_Util/examples/example2.php:1.1 pear/XML_Util/examples/example2.php:1.2 --- pear/XML_Util/examples/example2.php:1.1 Thu Sep 11 11:50:26 2003 +++ pear/XML_Util/examples/example2.php Sun Sep 14 15:10:00 2003 @@ -1,6 +1,6 @@ <?PHP require_once 'XML/Util.php'; - + /** * creating a start element */ @@ -22,6 +22,13 @@ */ print "creating an end element:<br>"; print htmlentities(XML_Util::createEndElement("myNs:myTag")); + print "\n<br><br>\n"; + + /** + * creating a CData section + */ + print "creating a CData section:<br>"; + print htmlentities(XML_Util::createCDataSection("I am content.")); print "\n<br><br>\n"; ?>

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