home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Plain.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  9.6 KB  |  258 lines

  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.net>                             |
  17. // +----------------------------------------------------------------------+
  18.  
  19. /**
  20.  * XML/Beautifier/Renderer/Plain.php
  21.  *
  22.  * @category XML
  23.  * @package  XML_Beautifier
  24.  * @author   Stephan Schmidt <schst@php.net>
  25.  */
  26.  
  27. /**
  28.  * XML_Util is needed to create the tags
  29.  */
  30. require_once 'XML/Util.php';
  31.  
  32. /**
  33.  * Renderer base class
  34.  */
  35. require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Renderer.php';
  36.  
  37. /**
  38.  * Basic XML Renderer for XML Beautifier
  39.  *
  40.  * @category XML
  41.  * @package  XML_Beautifier
  42.  * @author   Stephan Schmidt <schst@php.net>
  43.  * @todo     option to specify inline tags
  44.  * @todo     option to specify treatment of whitespac in data sections
  45.  * @todo     automatically create <![CDATA[ ]]> sections
  46.  */
  47. class XML_Beautifier_Renderer_Plain extends XML_Beautifier_Renderer {
  48.  
  49.    /**
  50.     * Serialize the XML tokens
  51.     *
  52.     * @access   public
  53.     * @param    array   XML tokens
  54.     * @return   string  XML document
  55.     */
  56.     function serialize($tokens)
  57.     {
  58.         $tokens = $this->normalize($tokens);
  59.         
  60.         $xml    = '';
  61.         $cnt    = count($tokens);
  62.         for($i = 0; $i < $cnt; $i++ )
  63.         {
  64.             $xml .= $this->_serializeToken($tokens[$i]);
  65.         }
  66.         return $xml;
  67.     }
  68.  
  69.     /**
  70.      * serialize a token
  71.      *
  72.      * This method does the actual beautifying.
  73.      *
  74.      * @access  private 
  75.      * @param   array   $token structure that should be serialized
  76.      * @todo    split this method into smaller methods
  77.      */
  78.     function _serializeToken($token)
  79.     {
  80.         switch ($token["type"]) {
  81.  
  82.             /*
  83.             * serialize XML Element
  84.             */
  85.             case    XML_BEAUTIFIER_ELEMENT:
  86.                 $indent = $this->_getIndentString($token["depth"]);
  87.  
  88.                 // adjust tag case
  89.                 if ($this->_options["caseFolding"] === true) {
  90.                     switch ($this->_options["caseFoldingTo"]) {
  91.                         case "uppercase":
  92.                             $token["tagname"] = strtoupper($token["tagname"]);
  93.                             $token["attribs"] = array_change_key_case($token["attribs"], CASE_UPPER);
  94.                             break;
  95.                         case "lowercase":
  96.                             $token["tagname"] = strtolower($token["tagname"]);
  97.                             $token["attribs"] = array_change_key_case($token["attribs"], CASE_LOWER);
  98.                             break;
  99.                     }
  100.                 }
  101.                 
  102.                 if ($this->_options["multilineTags"] == true) {
  103.                     $attIndent = $indent . str_repeat(" ", (2+strlen($token["tagname"])));
  104.                 } else {
  105.                     $attIndent = null;
  106.                 }
  107.                 // check for children
  108.                 switch ($token["contains"]) {
  109.                     
  110.                     // contains only CData or is empty
  111.                     case    XML_BEAUTIFIER_CDATA:
  112.                     case    XML_BEAUTIFIER_EMPTY:
  113.                         if (sizeof($token["children"]) >= 1) {
  114.                         $data = $token["children"][0]["data"];
  115.                         } else {
  116.                             $data = '';
  117.                         }
  118.  
  119.                         if( strstr( $data, "\n" ) )
  120.                         {
  121.                             $data   =   "\n" . $this->_indentTextBlock( $data, $token['depth']+1, true );
  122.                         } 
  123.                         
  124.                         $xml  = $indent . XML_Util::createTag($token["tagname"], $token["attribs"], $data, null, XML_UTIL_REPLACE_ENTITIES, $this->_options["multilineTags"], $attIndent)
  125.                               . $this->_options["linebreak"];
  126.                         break;
  127.                     // contains mixed content
  128.                     default:
  129.                         $xml = $indent . XML_Util::createStartElement($token["tagname"], $token["attribs"], null, $this->_options["multilineTags"], $attIndent)
  130.                              . $this->_options["linebreak"];
  131.                         
  132.                         $cnt = count($token["children"]);
  133.                         for ($i = 0; $i < $cnt; $i++) {
  134.                             $xml .= $this->_serializeToken($token["children"][$i]);
  135.                         }
  136.                         $xml .= $indent . XML_Util::createEndElement($token["tagname"])
  137.                              . $this->_options["linebreak"];
  138.                         break;
  139.                     break;
  140.                 }
  141.                 break;
  142.             
  143.             /*
  144.             * serialize CData
  145.             */
  146.             case    XML_BEAUTIFIER_CDATA:
  147.                 if ($token["depth"] > 0) {
  148.                     $xml = str_repeat($this->_options["indent"], $token["depth"]);
  149.                 } else {
  150.                     $xml = "";
  151.                 }
  152.                 
  153.                 $xml .= XML_Util::replaceEntities( $token["data"] ) . $this->_options["linebreak"];
  154.                 break;      
  155.  
  156.             /*
  157.             * serialize entity
  158.             */
  159.             case    XML_BEAUTIFIER_ENTITY:
  160.                 if ($token["depth"] > 0) {
  161.                     $xml = str_repeat($this->_options["indent"], $token["depth"]);
  162.                 } else {
  163.                     $xml = "";
  164.                 }
  165.                 $xml .= "&".$token["name"].";".$this->_options["linebreak"];
  166.                 break;      
  167.  
  168.  
  169.             /*
  170.             * serialize Processing instruction
  171.             */
  172.             case    XML_BEAUTIFIER_PI:
  173.                 $indent = $this->_getIndentString($token["depth"]);
  174.  
  175.                 $xml  = $indent."<?".$token["target"].$this->_options["linebreak"]
  176.                       . $this->_indentTextBlock(rtrim($token["data"]), $token["depth"])
  177.                       . $indent."?>".$this->_options["linebreak"];
  178.                 break;      
  179.  
  180.             /*
  181.             * comments
  182.             */
  183.             case    XML_BEAUTIFIER_COMMENT:
  184.                 $lines   = count(explode("\n",$token["data"]));
  185.                 
  186.                 /*
  187.                 * normalize comment, i.e. combine it to one
  188.                 * line and remove whitespace
  189.                 */
  190.                 if ($this->_options["normalizeComments"] && $lines > 1){
  191.                     $comment = preg_replace("/\s\s+/s", " ", str_replace( "\n" , " ", $token["data"]));
  192.                     $lines   = 1;
  193.                 } else {
  194.                     $comment = $token["data"];
  195.                 }
  196.     
  197.                 /*
  198.                 * check for the maximum length of one line
  199.                 */
  200.                 if ($this->_options["maxCommentLine"] > 0) {
  201.                     if ($lines > 1) {
  202.                         $commentLines = explode("\n", $comment);
  203.                     } else {
  204.                         $commentLines = array($comment);
  205.                     }
  206.     
  207.                     $comment = "";
  208.                     for ($i = 0; $i < $lines; $i++) {
  209.                         if (strlen($commentLines[$i]) <= $this->_options["maxCommentLine"]) {
  210.                             $comment .= $commentLines[$i];
  211.                             continue;
  212.                         }
  213.                         $comment .= wordwrap($commentLines[$i], $this->_options["maxCommentLine"] );
  214.                         if ($i != ($lines-1)) {
  215.                             $comment .= "\n";
  216.                         }
  217.                     }
  218.                     $lines   = count(explode("\n",$comment));
  219.                 }
  220.  
  221.                 $indent = $this->_getIndentString($token["depth"]);
  222.  
  223.                 if ($lines > 1) {
  224.                     $xml  = $indent . "<!--" . $this->_options["linebreak"]
  225.                           . $this->_indentTextBlock($comment, $token["depth"]+1, true)
  226.                           . $indent . "-->" . $this->_options["linebreak"];
  227.                 } else {
  228.                     $xml = $indent . sprintf( "<!-- %s -->", trim($comment) ) . $this->_options["linebreak"];
  229.                 }
  230.                 break;      
  231.  
  232.             /*
  233.             * xml declaration
  234.             */
  235.             case    XML_BEAUTIFIER_XML_DECLARATION:
  236.                 $indent = $this->_getIndentString($token["depth"]);
  237.                 $xml    = $indent . XML_Util::getXMLDeclaration($token["version"], $token["encoding"], $token["standalone"]);
  238.                 break;      
  239.  
  240.             /*
  241.             * xml declaration
  242.             */
  243.             case    XML_BEAUTIFIER_DT_DECLARATION:
  244.                 $xml    = $token["data"];
  245.                 break;      
  246.  
  247.             /*
  248.             * all other elements
  249.             */
  250.             case    XML_BEAUTIFIER_DEFAULT:
  251.             default:
  252.                 $xml    = XML_Util::replaceEntities( $token["data"] );
  253.                 break;      
  254.         }
  255.         return $xml;
  256.     }
  257. }
  258. ?>