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

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