[ PHPXref.com ] [ Generated: Thu Jul 24 12:24:35 2008 ] [ Concerto 2.1.2 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/domit/ -> xml_domit_utilities.php (source)

   1  <?php
   2  /**
   3  * DOMIT Utilities are a set of utilities for the DOMIT! parser
   4  * @package domit-xmlparser
   5  * @copyright (C) 2004 John Heinstein. All rights reserved
   6  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   7  * @author John Heinstein <johnkarl@nbnet.nb.ca>
   8  * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
   9  * DOMIT! is Free Software
  10  **/
  11  
  12  /**
  13  *@global Array Translation table for predefined XML entities
  14  */
  15  $GLOBALS['DOMIT_PREDEFINED_ENTITIES'] = array('&' => '&amp;', '<' => '&lt;', '>' => '&gt;',
  16                                              '"' => '&quot;', "'" => '&apos;');
  17  /**
  18  * A set of utilities for the DOMIT! parser
  19  * 
  20  * These methods are intended to be called statically
  21  *
  22  * @package domit-xmlparser
  23  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  24  */
  25  class DOMIT_Utilities {
  26      /**
  27      * Raises an error if an attempt to instantiate the class is made
  28      */
  29  	function DOMIT_Utilities() {        
  30          die("DOMIT_Utilities Error: this is a static class that should never be instantiated.\n" . 
  31              "Please use the following syntax to access methods of this class:\n" .
  32              'DOMIT_Utilities::methodName(parameters)');
  33      } //DOMIT_Utilities    
  34  
  35      /**
  36      * Generates a normalized (formatted for readability) representation of the node and its children
  37      * @param Object The root node of the narmalization
  38      * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
  39      * @return string The formatted string representation 
  40      */
  41  	function toNormalizedString (&$node, $subEntities=false, $definedEntities) {
  42          $node_level = 0;
  43          $response = '';
  44          
  45          //$node is a DOMIT_Document
  46          if ($node->nodeType == DOMIT_DOCUMENT_NODE) { 
  47              $total = $node->childCount;
  48  
  49              for ($i = 0; $i < $total; $i++) {
  50                  $response .= DOMIT_Utilities::getNormalizedString($node->childNodes[$i], 
  51                                              $node_level, $subEntities, $definedEntities);
  52              }
  53              
  54              return $response;
  55          }
  56          else {
  57              return ($response . DOMIT_Utilities::getNormalizedString($node, 
  58                                  $node_level, $subEntities, $definedEntities));
  59          }
  60      } //toNormalizedString        
  61      
  62      /**
  63      * Converts illegal XML characters to their entity representations
  64      * @param string The text to be formatted
  65      * @param array User defined translation table for entities
  66      * @return string The formatted text 
  67      */
  68  	function convertEntities($text, $definedEntities) {
  69          global $DOMIT_PREDEFINED_ENTITIES;
  70          $result = strtr($text, $DOMIT_PREDEFINED_ENTITIES);
  71          $result = strtr($result, $definedEntities);
  72          return $result;
  73      } //convertEntities
  74      
  75      /**
  76      * Gets a normalized (formatted for readability) representation of the current node
  77      * @param Object The node to be normalized
  78      * @param int The level in the DOM hierarchy where the node is located
  79      * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
  80      * @param array User defined translation table for entities
  81      * @return string The normalized string representation 
  82      */
  83  	function getNormalizedString(&$node, $node_level, $subEntities=false, $definedEntities) {
  84          $response = '';
  85  
  86          switch ($node->nodeType)  {
  87              case DOMIT_ELEMENT_NODE: 
  88                  $response .= DOMIT_Utilities::getNormalizedElementString($node, $response, 
  89                                               $node_level, $subEntities, $definedEntities);                                
  90                  break;
  91              
  92              case DOMIT_TEXT_NODE: 
  93                  if ($node->nextSibling == null) {
  94                      $node_level--;
  95                  }
  96                      
  97                  $response .= ($subEntities ? 
  98                                  DOMIT_Utilities::convertEntities($node->nodeValue, $definedEntities) : 
  99                                  $node->nodeValue);
 100                  break;
 101              
 102              case DOMIT_CDATA_SECTION_NODE:
 103                  if ($node->nextSibling == null) {
 104                      $node_level--;
 105                  }
 106                  
 107                  $response .= '<![CDATA[' . $node->nodeValue . ']]>';
 108                  break;
 109                  
 110              case DOMIT_ATTRIBUTE_NODE:
 111                  $response .= $node->toString(false, $subEntities);
 112                  break;
 113                  
 114              case DOMIT_DOCUMENT_FRAGMENT_NODE: 
 115                  $total = $node->childCount;
 116                  
 117                  for ($i = 0; $i < $total; $i++) {
 118                      $response .= DOMIT_Utilities::getNormalizedString($node->childNodes[$i], $node_level,
 119                                                              $subEntities, $definedEntities);
 120                  }
 121                  
 122                  break;
 123                  
 124              case DOMIT_COMMENT_NODE: 
 125                  $response .= '<!--' . $node->nodeValue . '-->';
 126  
 127                  if ($node->nextSibling == null) {
 128                      $node_level--;
 129                  }
 130                                  
 131                  $response .= DOMIT_Utilities::getIndentation($node_level) ;
 132                  
 133                  break;
 134                  
 135              case DOMIT_PROCESSING_INSTRUCTION_NODE: 
 136                  $response .= '<' . '?' . $node->nodeName . ' ' . $node->nodeValue . '?' . '>';
 137  
 138                  if ($node->nextSibling == null) {
 139                      $node_level--;
 140                  }
 141                                  
 142                  $response .= DOMIT_Utilities::getIndentation($node_level) ;
 143                  
 144                  break;
 145                  
 146              case DOMIT_DOCUMENT_TYPE_NODE:
 147                  $response .= $node->toString() . "\n";
 148                  break;
 149          } 
 150  
 151          return $response;
 152      } //getNormalizedString
 153      
 154      /**
 155      * Gets a normalized (formatted for readability) representation of the current element
 156      * @param Object The node to be normalized
 157      * @param string The current normalized text
 158      * @param int The level in the DOM hierarchy where the node is located
 159      * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
 160      * @param array User defined translation table for entities
 161      * @return string The normalized string representation 
 162      */
 163  	function getNormalizedElementString(&$node, $response, $node_level,  
 164                                              $subEntities, $definedEntities) {
 165          $response .= '<' . $node->nodeName;
 166                  
 167          //get attributes text
 168          if (is_object($node->attributes)) { //DOMIT
 169              $response .= $node->attributes->toString(false, $subEntities);
 170              
 171              /*
 172              if ($node->ownerDocument->isNamespaceAware) {
 173                  foreach ($node->namespaceURIMap as $key => $value) {
 174                      $response .= ' xmlns:' . $value . '="' . $key . '"';            
 175                  }
 176              }
 177              */
 178          }
 179          else { //DOMIT_Lite
 180              foreach ($node->attributes as $key => $value) {
 181                  $response .= ' ' . $key . '="';
 182                  $response .= ($subEntities ? DOMIT_Utilities::convertEntities($value, 
 183                                                          $definedEntities) : $value); 
 184                  $response .= '"';
 185              }
 186          }
 187          
 188          $node_level++;
 189          
 190          //if node is childless
 191          if ($node->childCount == 0) {
 192              if ($node->ownerDocument->doExpandEmptyElementTags) { 
 193                  if (in_array($node->nodeName, $node->ownerDocument->expandEmptyElementExceptions)) {
 194                      $response .= ' />';
 195                  }
 196                  else {
 197                      $response .= '></' . $node->nodeName . '>';
 198                  }
 199              }
 200              else {
 201                  if (in_array($node->nodeName, $node->ownerDocument->expandEmptyElementExceptions)) {
 202                      $response .= '></' . $node->nodeName . '>';
 203                  }
 204                  else {
 205                      $response .= ' />';
 206                  }
 207              }
 208          }
 209          else {
 210              $response .= '>';                        
 211              
 212              //get children
 213              $myNodes =& $node->childNodes;
 214              $total = $node->childCount;
 215              
 216              //no indentation if first child is a text node 
 217              if (!DOMIT_Utilities::isTextNode($node->firstChild)) {
 218                  $response .= DOMIT_Utilities::getIndentation($node_level); 
 219               } 
 220  
 221              for ($i = 0; $i < $total; $i++) {
 222                  $child =& $myNodes[$i];
 223                  $response .= DOMIT_Utilities::getNormalizedString($child, $node_level,
 224                                                  $subEntities, $definedEntities);
 225              }
 226      
 227              $response .= '</' . $node->nodeName . '>';
 228          }
 229  
 230          $node_level--;
 231  
 232          if ($node->nextSibling == null) {
 233              $node_level--;
 234              $response .= DOMIT_Utilities::getIndentation($node_level);
 235          }
 236          else {                
 237              //no indentation if next sibling is a text node 
 238              if (!DOMIT_Utilities::isTextNode($node->nextSibling)) {
 239                  $response .= DOMIT_Utilities::getIndentation($node_level); 
 240              }
 241          } 
 242          
 243          return $response;
 244      } //getNormalizedElementString
 245      
 246      /**
 247      * Determines whether the specified node is a Text node
 248      * @param Object The node to be tested
 249      * @return boolean True if the node is a Text node 
 250      */
 251  	function isTextNode(&$node) {
 252          $type = $node->nodeType;
 253          return (($type == DOMIT_TEXT_NODE) || ($type == DOMIT_CDATA_SECTION_NODE));
 254      } //isTextNode
 255      
 256      /**
 257      * Returns the indentation required for the specified node level
 258      * @param int The current node level
 259      * @return string The indentation required for the specified node level
 260      */
 261  	function getIndentation($node_level) {
 262          $INDENT_LEN = '    ';
 263          $indentation = "\n";
 264  
 265          for ($i = 0; $i < $node_level; $i++) {
 266              $indentation .= $INDENT_LEN;
 267          }
 268          
 269          return $indentation;
 270      } //getIndentation
 271      
 272      /**
 273      * Removes the extension from the specified file name
 274      * @param string The file name
 275      * @return string The file name, stripped of its extension
 276      */
 277  	function removeExtension($fileName) {
 278          $total = strlen($fileName);
 279          $index = -1;
 280          
 281          for ($i = ($total - 1); $i >= 0; $i--) {
 282              if ($fileName{$i} == '.') {
 283                  $index = $i;
 284              }
 285          }
 286          
 287          if ($index == -1) {
 288              return $fileName;
 289          }
 290          
 291          return (substr($fileName, 0, $index));
 292      } //removeExtension    
 293      
 294      /**
 295      * Determines whether the XML string is valid (NOT FULLY IMPLEMENTED!)
 296      * @param string The XML text
 297      * @return boolean True if the XML text is valid
 298      */
 299  	function validateXML($xmlText) {
 300          //this does only rudimentary validation
 301          //at this point in time
 302          $isValid = true;
 303          
 304          if (is_string($xmlText)) {        
 305              $text = trim($xmlText);
 306              
 307              switch ($text) {
 308                  case '':
 309                      $isValid = false;
 310                      break;
 311              }
 312          }
 313          else {
 314              $isValid = false;
 315          }
 316          
 317          return $isValid;
 318      } //validateXML
 319      
 320      /**
 321      * Set the browser header to interpret data as UTF-8 formatted
 322      * @param string The content type of the data
 323      */
 324  	function printUTF8Header($contentType = 'text/html') {
 325          echo header('Content-type: ' . $contentType . '; charset=utf-8');
 326      } //printUTF8Header
 327      
 328      /**
 329      * Formats a string for presentation as HTML
 330      * @param string The string to be formatted
 331      * @param boolean True if the string is to be sent directly to output
 332      * @return string The HTML formatted string  
 333      */
 334  	function forHTML($text, $doPrint = false) {
 335          if ($doPrint) {
 336              print ('<pre>' . htmlspecialchars($text) . '</pre>');
 337          }
 338          else {
 339              return ('<pre>' . htmlspecialchars($text) . '</pre>');
 340          }        
 341      } //forHTML
 342      
 343      /**
 344      * Generates a node tree from an array and appends it to the specified document or node
 345      * @param object The document or node to which the child nodes should be appended
 346      * @param array An associative multidimensional array of elements and values
 347      */
 348  	function fromArray (&$node, &$myArray) {
 349          if ($node->nodeType == DOMIT_DOCUMENT_NODE) {
 350              $docNode =& $node;
 351          }
 352          else {
 353              $docNode =& $node->ownerDocument;
 354          }
 355  
 356          foreach ($myArray as $key => $value) {            
 357              if (is_array($value)) {
 358                  //check for numeric indices
 359                  $total = count($value);
 360                  
 361                  if (($total > 0)  && isset($value[0])){
 362                      for ($i = 0; $i < $total; $i++) {
 363                          $node->appendChild($docNode->createElement($key));
 364                          DOMIT_Utilities::fromArray($node->lastChild, $value[$i]);                    
 365                      }
 366                  }
 367                  else {
 368                      //generate child elements
 369                      $node->appendChild($docNode->createElement($key));
 370                      DOMIT_Utilities::fromArray($node->lastChild, $value);
 371                  }
 372              }
 373              else {
 374                  $node->appendChild($docNode->createElement($key));
 375                  $node->lastChild->appendChild($docNode->createTextNode($value));
 376              }            
 377          }        
 378      } //fromArray
 379      
 380  	function parseAttributes() {
 381          
 382      } //parseAttributes
 383  } //DOMIT_Utilities
 384  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]