XML_Util
[ class tree: XML_Util ] [ index: XML_Util ] [ all elements ]

Source for file Util.php

Documentation is available at Util.php

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stephan Schmidt <schst@php-tools.net>                       |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //    $Id: Util.php,v 1.23 2004/11/19 19:32:53 schst Exp $
  20.  
  21. /**
  22.  * error code for invalid chars in XML name
  23.  */
  24. define("XML_UTIL_ERROR_INVALID_CHARS"51);
  25.  
  26. /**
  27.  * error code for invalid chars in XML name
  28.  */
  29. define("XML_UTIL_ERROR_INVALID_START"52);
  30.  
  31. /**
  32.  * error code for non-scalar tag content
  33.  */
  34. define("XML_UTIL_ERROR_NON_SCALAR_CONTENT"60);
  35.     
  36. /**
  37.  * error code for missing tag name
  38.  */
  39. define("XML_UTIL_ERROR_NO_TAG_NAME"61);
  40.     
  41. /**
  42.  * replace XML entities
  43.  */
  44. define("XML_UTIL_REPLACE_ENTITIES"1);
  45.  
  46. /**
  47.  * embedd content in a CData Section
  48.  */
  49. define("XML_UTIL_CDATA_SECTION"2);
  50.  
  51. /**
  52.  * do not replace entitites
  53.  */
  54. define("XML_UTIL_ENTITIES_NONE"0);
  55.  
  56. /**
  57.  * replace all XML entitites
  58.  * This setting will replace <, >, ", ' and &
  59.  */
  60. define("XML_UTIL_ENTITIES_XML"1);
  61.  
  62. /**
  63.  * replace only required XML entitites
  64.  * This setting will replace <, " and &
  65.  */
  66. define("XML_UTIL_ENTITIES_XML_REQUIRED"2);
  67.  
  68. /**
  69.  * replace HTML entitites
  70.  * @link    http://www.php.net/htmlentities
  71.  */
  72. define("XML_UTIL_ENTITIES_HTML"3);
  73.  
  74. /**
  75.  * Collapse all empty tags.
  76.  */
  77. define("XML_UTIL_COLLAPSE_ALL"1);
  78.  
  79. /**
  80.  * Collapse only empty XHTML tags that have no end tag.
  81.  */
  82. define("XML_UTIL_COLLAPSE_XHTML_ONLY"2);
  83.  
  84. /**
  85.  * utility class for working with XML documents
  86.  *
  87.  * @category XML
  88.  * @package  XML_Util
  89.  * @version  1.1.0
  90.  * @author   Stephan Schmidt <schst@php.net>
  91.  */
  92. class XML_Util {
  93.  
  94.    /**
  95.     * return API version
  96.     *
  97.     * @access   public
  98.     * @static
  99.     * @return   string  $version API version
  100.     */
  101.     function apiVersion()
  102.     {
  103.         return '1.1';
  104.     }
  105.  
  106.    /**
  107.     * replace XML entities
  108.     *
  109.     * With the optional second parameter, you may select, which
  110.     * entities should be replaced.
  111.     *
  112.     * <code>
  113.     * require_once 'XML/Util.php';
  114.     * 
  115.     * // replace XML entites:
  116.     * $string = XML_Util::replaceEntities("This string contains < & >.");
  117.     * </code>
  118.     *
  119.     * @access   public
  120.     * @static
  121.     * @param    string  string where XML special chars should be replaced
  122.     * @param    integer setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
  123.     * @return   string  string with replaced chars
  124.     * @see      reverseEntities()
  125.     */
  126.     function replaceEntities($string$replaceEntities = XML_UTIL_ENTITIES_XML)
  127.     {
  128.         switch ($replaceEntities{
  129.             case XML_UTIL_ENTITIES_XML:
  130.                 return strtr($string,array(
  131.                                           '&'  => '&amp;',
  132.                                           '>'  => '&gt;',
  133.                                           '<'  => '&lt;',
  134.                                           '"'  => '&quot;',
  135.                                           '\'' => '&apos;' ));
  136.                 break;
  137.             case XML_UTIL_ENTITIES_XML_REQUIRED:
  138.                 return strtr($string,array(
  139.                                           '&'  => '&amp;',
  140.                                           '<'  => '&lt;',
  141.                                           '"'  => '&quot;' ));
  142.                 break;
  143.             case XML_UTIL_ENTITIES_HTML:
  144.                 return htmlspecialchars($string);
  145.                 break;
  146.         }
  147.         return $string;
  148.     }
  149.  
  150.    /**
  151.     * reverse XML entities
  152.     *
  153.     * With the optional second parameter, you may select, which
  154.     * entities should be reversed.
  155.     *
  156.     * <code>
  157.     * require_once 'XML/Util.php';
  158.     * 
  159.     * // reverse XML entites:
  160.     * $string = XML_Util::reverseEntities("This string contains &lt; &amp; &gt;.");
  161.     * </code>
  162.     *
  163.     * @access   public
  164.     * @static
  165.     * @param    string  string where XML special chars should be replaced
  166.     * @param    integer setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
  167.     * @return   string  string with replaced chars
  168.     * @see      replaceEntities()
  169.     */
  170.     function reverseEntities($string$replaceEntities = XML_UTIL_ENTITIES_XML)
  171.     {
  172.         switch ($replaceEntities{
  173.             case XML_UTIL_ENTITIES_XML:
  174.                 return strtr($string,array(
  175.                                           '&amp;'  => '&',
  176.                                           '&gt;'   => '>',
  177.                                           '&lt;'   => '<',
  178.                                           '&quot;' => '"',
  179.                                           '&apos;' => '\'' ));
  180.                 break;
  181.             case XML_UTIL_ENTITIES_XML_REQUIRED:
  182.                 return strtr($string,array(
  183.                                           '&amp;'  => '&',
  184.                                           '&lt;'   => '<',
  185.                                           '&quot;' => '"' ));
  186.                 break;
  187.             case XML_UTIL_ENTITIES_HTML:
  188.                 $arr array_flip(get_html_translation_table(HTML_SPECIALCHARS));
  189.                 return strtr($string$arr);
  190.                 break;
  191.         }
  192.         return $string;
  193.     }
  194.  
  195.    /**
  196.     * build an xml declaration
  197.     *
  198.     * <code>
  199.     * require_once 'XML/Util.php';
  200.     * 
  201.     * // get an XML declaration:
  202.     * $xmlDecl = XML_Util::getXMLDeclaration("1.0", "UTF-8", true);
  203.     * </code>
  204.     *
  205.     * @access   public
  206.     * @static
  207.     * @param    string  $version     xml version
  208.     * @param    string  $encoding    character encoding
  209.     * @param    boolean $standAlone  document is standalone (or not)
  210.     * @return   string  $decl xml declaration
  211.     * @uses     XML_Util::attributesToString() to serialize the attributes of the XML declaration
  212.     */
  213.     function getXMLDeclaration($version "1.0"$encoding = null$standalone = null)
  214.     {
  215.         $attributes = array(
  216.                             "version" => $version,
  217.                            );
  218.         // add encoding
  219.         if ($encoding !== null{
  220.             $attributes["encoding"$encoding;
  221.         }
  222.         // add standalone, if specified
  223.         if ($standalone !== null{
  224.             $attributes["standalone"$standalone "yes" "no";
  225.         }
  226.         
  227.         return sprintf("<?xml%s?>"XML_Util::attributesToString($attributesfalse));
  228.     }
  229.  
  230.    /**
  231.     * build a document type declaration
  232.     *
  233.     * <code>
  234.     * require_once 'XML/Util.php';
  235.     * 
  236.     * // get a doctype declaration:
  237.     * $xmlDecl = XML_Util::getDocTypeDeclaration("rootTag","myDocType.dtd");
  238.     * </code>
  239.     *
  240.     * @access   public
  241.     * @static
  242.     * @param    string  $root         name of the root tag
  243.     * @param    string  $uri          uri of the doctype definition (or array with uri and public id)
  244.     * @param    string  $internalDtd  internal dtd entries
  245.     * @return   string  $decl         doctype declaration
  246.     * @since    0.2
  247.     */
  248.     function getDocTypeDeclaration($root$uri = null$internalDtd = null)
  249.     {
  250.         if (is_array($uri)) {
  251.             $ref sprintf' PUBLIC "%s" "%s"'$uri["id"]$uri["uri");
  252.         elseif (!empty($uri)) {
  253.             $ref sprintf' SYSTEM "%s"'$uri );
  254.         else {
  255.             $ref "";
  256.         }
  257.  
  258.         if (empty($internalDtd)) {
  259.             return sprintf("<!DOCTYPE %s%s>"$root$ref);
  260.         else {
  261.             return sprintf("<!DOCTYPE %s%s [\n%s\n]>"$root$ref$internalDtd);
  262.         }
  263.     }
  264.  
  265.    /**
  266.     * create string representation of an attribute list
  267.     *
  268.     * <code>
  269.     * require_once 'XML/Util.php';
  270.     * 
  271.     * // build an attribute string
  272.     * $att = array(
  273.     *              "foo"   =>  "bar",
  274.     *              "argh"  =>  "tomato"
  275.     *            );
  276.     *
  277.     * $attList = XML_Util::attributesToString($att);
  278.     * </code>
  279.     *
  280.     * @access   public
  281.     * @static
  282.     * @param    array         $attributes        attribute array
  283.     * @param    boolean|array$sort              sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities'
  284.     * @param    boolean       $multiline         use linebreaks, if more than one attribute is given
  285.     * @param    string        $indent            string used for indentation of multiline attributes
  286.     * @param    string        $linebreak         string used for linebreaks of multiline attributes
  287.     * @param    integer       $entities          setting for entities in attribute values (one of XML_UTIL_ENTITIES_NONE, XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML)
  288.     * @return   string                           string representation of the attributes
  289.     * @uses     XML_Util::replaceEntities() to replace XML entities in attribute values
  290.     * @todo     allow sort also to be an options array
  291.     */
  292.     function attributesToString($attributes$sort = true$multiline = false$indent '    '$linebreak "\n"$entities = XML_UTIL_ENTITIES_XML)
  293.     {
  294.         /**
  295.          * second parameter may be an array
  296.          */
  297.         if (is_array($sort)) {
  298.             if (isset($sort['multiline'])) {
  299.                 $multiline $sort['multiline'];
  300.             }
  301.             if (isset($sort['indent'])) {
  302.                 $indent $sort['indent'];
  303.             }
  304.             if (isset($sort['linebreak'])) {
  305.                 $multiline $sort['linebreak'];
  306.             }
  307.             if (isset($sort['entities'])) {
  308.                 $entities $sort['entities'];
  309.             }
  310.             if (isset($sort['sort'])) {
  311.                 $sort $sort['sort'];
  312.             else {
  313.                 $sort = true;
  314.             }
  315.         }
  316.         $string '';
  317.         if (is_array($attributes&& !empty($attributes)) {
  318.             if ($sort{
  319.                 ksort($attributes);
  320.             }
  321.             if!$multiline || count($attributes== 1{
  322.                 foreach ($attributes as $key => $value{
  323.                     if ($entities != XML_UTIL_ENTITIES_NONE{
  324.                         $value XML_Util::replaceEntities($value$entities);
  325.                     }
  326.                     $string .= ' '.$key.'="'.$value.'"';
  327.                 }
  328.             else {
  329.                 $first = true;
  330.                 foreach ($attributes as $key => $value{
  331.                     if ($entities != XML_UTIL_ENTITIES_NONE{
  332.                         $value XML_Util::replaceEntities($value$entities);
  333.                     }
  334.                     if ($first{
  335.                         $string .= " ".$key.'="'.$value.'"';
  336.                         $first = false;
  337.                     else {
  338.                         $string .= $linebreak.$indent.$key.'="'.$value.'"';
  339.                     }
  340.                 }
  341.             }
  342.         }
  343.         return $string;
  344.     }
  345.  
  346.    /**
  347.     * Collapses empty tags.
  348.     *
  349.     * @access   public
  350.     * @static
  351.     * @param    string  $xml  XML
  352.     * @param    integer $mode Whether to collapse all empty tags (XML_UTIL_COLLAPSE_ALL) or only XHTML (XML_UTIL_COLLAPSE_XHTML_ONLY) ones.
  353.     * @return   string  $xml  XML
  354.     */
  355.     function collapseEmptyTags($xml$mode = XML_UTIL_COLLAPSE_ALL{
  356.         if ($mode == XML_UTIL_COLLAPSE_XHTML_ONLY{
  357.             return preg_replace(
  358.               '/<(area|base|br|col|hr|img|input|link|meta|param)([^>]*)><\/\\1>/s',
  359.               '<\\1\\2 />',
  360.               $xml
  361.             );
  362.         else {
  363.             return preg_replace(
  364.               '/<(\w+)([^>]*)><\/\\1>/s',
  365.               '<\\1\\2 />',
  366.               $xml
  367.             );
  368.         }
  369.     }
  370.  
  371.    /**
  372.     * create a tag
  373.     *
  374.     * This method will call XML_Util::createTagFromArray(), which
  375.     * is more flexible.
  376.     *
  377.     * <code>
  378.     * require_once 'XML/Util.php';
  379.     * 
  380.     * // create an XML tag:
  381.     * $tag = XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#");
  382.     * </code>
  383.     *
  384.     * @access   public
  385.     * @static
  386.     * @param    string  $qname             qualified tagname (including namespace)
  387.     * @param    array   $attributes        array containg attributes
  388.     * @param    mixed   $content 
  389.     * @param    string  $namespaceUri      URI of the namespace
  390.     * @param    integer $replaceEntities   whether to replace XML special chars in content, embedd it in a CData section or none of both
  391.     * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
  392.     * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
  393.     * @param    string  $linebreak         string used for linebreaks
  394.     * @return   string  $string            XML tag
  395.     * @see      XML_Util::createTagFromArray()
  396.     * @uses     XML_Util::createTagFromArray() to create the tag
  397.     */
  398.     function createTag($qname$attributes = array()$content = null$namespaceUri = null$replaceEntities = XML_UTIL_REPLACE_ENTITIES$multiline = false$indent "_auto"$linebreak "\n")
  399.     {
  400.         $tag = array(
  401.                      "qname"      => $qname,
  402.                      "attributes" => $attributes
  403.                     );
  404.  
  405.         // add tag content
  406.         if ($content !== null{
  407.             $tag["content"$content;
  408.         }
  409.         
  410.         // add namespace Uri
  411.         if ($namespaceUri !== null{
  412.             $tag["namespaceUri"$namespaceUri;
  413.         }
  414.  
  415.         return XML_Util::createTagFromArray($tag$replaceEntities$multiline$indent$linebreak);
  416.     }
  417.  
  418.    /**
  419.     * create a tag from an array
  420.     * this method awaits an array in the following format
  421.     * <pre>
  422.     * array(
  423.     *  "qname"        => $qname         // qualified name of the tag
  424.     *  "namespace"    => $namespace     // namespace prefix (optional, if qname is specified or no namespace)
  425.     *  "localpart"    => $localpart,    // local part of the tagname (optional, if qname is specified)
  426.     *  "attributes"   => array(),       // array containing all attributes (optional)
  427.     *  "content"      => $content,      // tag content (optional)
  428.     *  "namespaceUri" => $namespaceUri  // namespaceUri for the given namespace (optional)
  429.     *   )
  430.     * </pre>
  431.     *
  432.     * <code>
  433.     * require_once 'XML/Util.php';
  434.     * 
  435.     * $tag = array(
  436.     *           "qname"        => "foo:bar",
  437.     *           "namespaceUri" => "http://foo.com",
  438.     *           "attributes"   => array( "key" => "value", "argh" => "fruit&vegetable" ),
  439.     *           "content"      => "I'm inside the tag",
  440.     *            );
  441.     * // creating a tag with qualified name and namespaceUri
  442.     * $string = XML_Util::createTagFromArray($tag);
  443.     * </code>
  444.     *
  445.     * @access   public
  446.     * @static
  447.     * @param    array   $tag               tag definition
  448.     * @param    integer $replaceEntities   whether to replace XML special chars in content, embedd it in a CData section or none of both
  449.     * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
  450.     * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
  451.     * @param    string  $linebreak         string used for linebreaks
  452.     * @return   string  $string            XML tag
  453.     * @see      XML_Util::createTag()
  454.     * @uses     XML_Util::attributesToString() to serialize the attributes of the tag
  455.     * @uses     XML_Util::splitQualifiedName() to get local part and namespace of a qualified name
  456.     */
  457.     function createTagFromArray($tag$replaceEntities = XML_UTIL_REPLACE_ENTITIES$multiline = false$indent "_auto"$linebreak "\n" )
  458.     {
  459.         if (isset($tag["content"]&& !is_scalar($tag["content"])) {
  460.             return XML_Util::raiseError"Supplied non-scalar value as tag content"XML_UTIL_ERROR_NON_SCALAR_CONTENT );
  461.         }
  462.  
  463.         if (!isset($tag['qname']&& !isset($tag['localPart'])) {
  464.             return XML_Util::raiseError'You must either supply a qualified name (qname) or local tag name (localPart).'XML_UTIL_ERROR_NO_TAG_NAME );
  465.         }
  466.  
  467.         // if no attributes hav been set, use empty attributes
  468.         if (!isset($tag["attributes"]|| !is_array($tag["attributes"])) {
  469.             $tag["attributes"= array();
  470.         }
  471.         
  472.         // qualified name is not given
  473.         if (!isset($tag["qname"])) {
  474.             // check for namespace
  475.             if (isset($tag["namespace"]&& !empty($tag["namespace"])) {
  476.                 $tag["qname"$tag["namespace"].":".$tag["localPart"];
  477.             else {
  478.                 $tag["qname"$tag["localPart"];
  479.             }
  480.         // namespace URI is set, but no namespace
  481.         elseif (isset($tag["namespaceUri"]&& !isset($tag["namespace"])) {
  482.             $parts XML_Util::splitQualifiedName($tag["qname"]);
  483.             $tag["localPart"$parts["localPart"];
  484.             if (isset($parts["namespace"])) {
  485.                 $tag["namespace"$parts["namespace"];
  486.             }
  487.         }
  488.  
  489.         if (isset($tag["namespaceUri"]&& !empty($tag["namespaceUri"])) {
  490.             // is a namespace given
  491.             if (isset($tag["namespace"]&& !empty($tag["namespace"])) {
  492.                 $tag["attributes"]["xmlns:".$tag["namespace"]] $tag["namespaceUri"];
  493.             else {
  494.                 // define this Uri as the default namespace
  495.                 $tag["attributes"]["xmlns"$tag["namespaceUri"];
  496.             }
  497.         }
  498.  
  499.         // check for multiline attributes
  500.         if ($multiline === true{
  501.             if ($indent === "_auto"{
  502.                 $indent str_repeat(" "(strlen($tag["qname"])+2));
  503.             }
  504.         }
  505.         
  506.         // create attribute list
  507.         $attList    =   XML_Util::attributesToString($tag["attributes"]true$multiline$indent$linebreak );
  508.         if (!isset($tag["content"]|| (string)$tag["content"== ''{
  509.             $tag    =   sprintf("<%s%s />"$tag["qname"]$attList);
  510.         else {
  511.             if ($replaceEntities == XML_UTIL_REPLACE_ENTITIES{
  512.                 $tag["content"XML_Util::replaceEntities($tag["content"]);
  513.             elseif ($replaceEntities == XML_UTIL_CDATA_SECTION{
  514.                 $tag["content"XML_Util::createCDataSection($tag["content"]);
  515.             }
  516.             $tag    =   sprintf("<%s%s>%s</%s>"$tag["qname"]$attList$tag["content"]$tag["qname");
  517.         }        
  518.         return  $tag;
  519.     }
  520.  
  521.    /**
  522.     * create a start element
  523.     *
  524.     * <code>
  525.     * require_once 'XML/Util.php';
  526.     * 
  527.     * // create an XML start element:
  528.     * $tag = XML_Util::createStartElement("myNs:myTag", array("foo" => "bar") ,"http://www.w3c.org/myNs#");
  529.     * </code>
  530.     *
  531.     * @access   public
  532.     * @static
  533.     * @param    string  $qname             qualified tagname (including namespace)
  534.     * @param    array   $attributes        array containg attributes
  535.     * @param    string  $namespaceUri      URI of the namespace
  536.     * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
  537.     * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
  538.     * @param    string  $linebreak         string used for linebreaks
  539.     * @return   string  $string            XML start element
  540.     * @see      XML_Util::createEndElement(), XML_Util::createTag()
  541.     */
  542.     function createStartElement($qname$attributes = array()$namespaceUri = null$multiline = false$indent '_auto'$linebreak "\n")
  543.     {
  544.         // if no attributes hav been set, use empty attributes
  545.         if (!isset($attributes|| !is_array($attributes)) {
  546.             $attributes = array();
  547.         }
  548.         
  549.         if ($namespaceUri != null{
  550.             $parts XML_Util::splitQualifiedName($qname);
  551.         }
  552.  
  553.         // check for multiline attributes
  554.         if ($multiline === true{
  555.             if ($indent === "_auto"{
  556.                 $indent str_repeat(" "(strlen($qname)+2));
  557.             }
  558.         }
  559.  
  560.         if ($namespaceUri != null{
  561.             // is a namespace given
  562.             if (isset($parts["namespace"]&& !empty($parts["namespace"])) {
  563.                 $attributes["xmlns:".$parts["namespace"]] $namespaceUri;
  564.             else {
  565.                 // define this Uri as the default namespace
  566.                 $attributes["xmlns"$namespaceUri;
  567.             }
  568.         }
  569.  
  570.         // create attribute list
  571.         $attList    =   XML_Util::attributesToString($attributestrue$multiline$indent$linebreak);
  572.         $element    =   sprintf("<%s%s>"$qname$attList);
  573.         return  $element;
  574.     }
  575.  
  576.    /**
  577.     * create an end element
  578.     *
  579.     * <code>
  580.     * require_once 'XML/Util.php';
  581.     * 
  582.     * // create an XML start element:
  583.     * $tag = XML_Util::createEndElement("myNs:myTag");
  584.     * </code>
  585.     *
  586.     * @access   public
  587.     * @static
  588.     * @param    string  $qname             qualified tagname (including namespace)
  589.     * @return   string  $string            XML end element
  590.     * @see      XML_Util::createStartElement(), XML_Util::createTag()
  591.     */
  592.     function createEndElement($qname)
  593.     {
  594.         $element    =   sprintf("</%s>"$qname);
  595.         return  $element;
  596.     }
  597.     
  598.    /**
  599.     * create an XML comment
  600.     *
  601.     * <code>
  602.     * require_once 'XML/Util.php';
  603.     * 
  604.     * // create an XML start element:
  605.     * $tag = XML_Util::createComment("I am a comment");
  606.     * </code>
  607.     *
  608.     * @access   public
  609.     * @static
  610.     * @param    string  $content           content of the comment
  611.     * @return   string  $comment           XML comment
  612.     */
  613.     function createComment($content)
  614.     {
  615.         $comment    =   sprintf("<!-- %s -->"$content);
  616.         return  $comment;
  617.     }
  618.     
  619.    /**
  620.     * create a CData section
  621.     *
  622.     * <code>
  623.     * require_once 'XML/Util.php';
  624.     * 
  625.     * // create a CData section
  626.     * $tag = XML_Util::createCDataSection("I am content.");
  627.     * </code>
  628.     *
  629.     * @access   public
  630.     * @static
  631.     * @param    string  $data              data of the CData section
  632.     * @return   string  $string            CData section with content
  633.     */
  634.     function createCDataSection($data)
  635.     {
  636.         return  sprintf("<![CDATA[%s]]>"$data);
  637.     }
  638.  
  639.    /**
  640.     * split qualified name and return namespace and local part
  641.     *
  642.     * <code>
  643.     * require_once 'XML/Util.php';
  644.     * 
  645.     * // split qualified tag
  646.     * $parts = XML_Util::splitQualifiedName("xslt:stylesheet");
  647.     * </code>
  648.     * the returned array will contain two elements:
  649.     * <pre>
  650.     * array(
  651.     *       "namespace" => "xslt",
  652.     *       "localPart" => "stylesheet"
  653.     *      );
  654.     * </pre>
  655.     *
  656.     * @access public
  657.     * @static
  658.     * @param  string    $qname      qualified tag name
  659.     * @param  string    $defaultNs  default namespace (optional)
  660.     * @return array     $parts      array containing namespace and local part
  661.     */
  662.     function splitQualifiedName($qname$defaultNs = null)
  663.     {
  664.         if (strstr($qname':')) {
  665.             $tmp explode(":"$qname);
  666.             return array(
  667.                           "namespace" => $tmp[0],
  668.                           "localPart" => $tmp[1]
  669.                         );
  670.         }
  671.         return array(
  672.                       "namespace" => $defaultNs,
  673.                       "localPart" => $qname
  674.                     );
  675.     }
  676.  
  677.    /**
  678.     * check, whether string is valid XML name
  679.     *
  680.     * <p>XML names are used for tagname, attribute names and various
  681.     * other, lesser known entities.</p>
  682.     * <p>An XML name may only consist of alphanumeric characters,
  683.     * dashes, undescores and periods, and has to start with a letter
  684.     * or an underscore.
  685.     * </p>
  686.     *
  687.     * <code>
  688.     * require_once 'XML/Util.php';
  689.     * 
  690.     * // verify tag name
  691.     * $result = XML_Util::isValidName("invalidTag?");
  692.     * if (XML_Util::isError($result)) {
  693.     *    print "Invalid XML name: " . $result->getMessage();
  694.     * }
  695.     * </code>
  696.     *
  697.     * @access  public
  698.     * @static
  699.     * @param   string  $string string that should be checked
  700.     * @return  mixed   $valid  true, if string is a valid XML name, PEAR error otherwise
  701.     * @todo    support for other charsets
  702.     */
  703.     function isValidName($string)
  704.     {
  705.         // check for invalid chars
  706.         if (!preg_match("/^[[:alnum:]_\-.]$/"$string{0})) {
  707.             return XML_Util::raiseError"XML names may only start with letter or underscore"XML_UTIL_ERROR_INVALID_START );
  708.         }
  709.         
  710.         // check for invalid chars
  711.         if (!preg_match("/^([a-zA-Z_]([a-zA-Z0-9_\-\.]*)?:)?[a-zA-Z_]([a-zA-Z0-9_\-\.]+)?$/"$string)) {
  712.             return XML_Util::raiseError"XML names may only contain alphanumeric chars, period, hyphen, colon and underscores"XML_UTIL_ERROR_INVALID_CHARS );
  713.          }
  714.         // XML name is valid
  715.         return true;
  716.     }
  717.  
  718.    /**
  719.     * replacement for XML_Util::raiseError
  720.     *
  721.     * Avoids the necessity to always require
  722.     * PEAR.php
  723.     *
  724.     * @access   public
  725.     * @param    string      error message
  726.     * @param    integer     error code
  727.     * @return   object PEAR_Error 
  728.     */
  729.     function raiseError($msg$code)
  730.     {
  731.         require_once 'PEAR.php';
  732.         return PEAR::raiseError($msg$code);
  733.     }
  734. }
  735. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.