[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/tests/lib/ -> xml_writer_class.php (source)

   1  <?php
   2  if(!defined("METAL_LIBRARY_XML_XML_WRITER_CLASS"))
   3  {
   4      define("METAL_LIBRARY_XML_XML_WRITER_CLASS",1);
   5  
   6  /*
   7   *
   8   * Copyright © (C) Manuel Lemos 2001-2002
   9   *
  10   * @(#) $Id: xml_writer_class.php,v 1.3 2004/06/12 10:44:49 harryf Exp $
  11   *
  12   */
  13  
  14  class xml_writer_class
  15  {
  16      /*
  17       * Protected variables
  18       *
  19       */
  20      var $structure=array();
  21      var $nodes=array();
  22      
  23      /*
  24       * Public variables
  25       *
  26       */
  27      var $stylesheet="";
  28      var $stylesheettype="text/xsl";
  29      var $dtdtype="";
  30      var $dtddefinition="";
  31      var $dtdurl="";
  32      var $outputencoding="utf-8";
  33      var $inputencoding="iso-8859-1";
  34      var $linebreak="\n";
  35      var $indenttext=" ";
  36      var $generatedcomment="Generated by: http://www.phpclasses.org/xmlwriter";
  37      var $error="";
  38      
  39      
  40      /*
  41       * Protected functions
  42       *
  43       */
  44      Function escapedata($data)
  45      {
  46          $position=0;
  47          $length=strlen($data);
  48          $escapeddata="";
  49          for(;$position<$length;)
  50          {
  51              $character=substr($data,$position,1);
  52              $code=Ord($character);
  53              switch($code)
  54              {
  55                  case 34:
  56                      $character="&quot;";
  57                      break;
  58                  case 38:
  59                      $character="&amp;";
  60                      break;
  61                  case 39:
  62                      $character="&apos;";
  63                      break;
  64                  case 60:
  65                      $character="&lt;";
  66                      break;
  67                  case 62:
  68                      $character="&gt;";
  69                      break;
  70                  default:
  71                      if($code<32)
  72                          $character=("&#".strval($code).";");
  73                      break;
  74              }
  75              $escapeddata.=$character;
  76              $position++;
  77          }
  78          return $escapeddata;
  79      }
  80      
  81      Function encodedata($data,&$encodeddata)
  82      {
  83          if(!strcmp($this->inputencoding,$this->outputencoding))
  84              $encodeddata=$this->escapedata($data);
  85          else
  86          {
  87              switch(strtolower($this->outputencoding))
  88              {
  89                  case "utf-8":
  90                      if(!strcmp(strtolower($this->inputencoding),"iso-8859-1"))
  91                      {
  92                          $encoded_data=utf8_encode($this->escapedata($data));
  93                          $encodeddata=$encoded_data;
  94                      }
  95                      else
  96                      {
  97                          $this->error=("can not encode iso-8859-1 data in ".$this->outputencoding);
  98                          return 0;
  99                      }
 100                      break;
 101                  case "iso-8859-1":
 102                      if(!strcmp(strtolower($this->inputencoding),"utf-8"))
 103                      {
 104                          $decoded=utf8_decode($data);
 105                          $encodeddata=$this->escapedata($decoded);
 106                      }
 107                      else
 108                      {
 109                          $this->error=("can not encode utf-8 data in ".$this->outputencoding);
 110                          return 0;
 111                      }
 112                      break;
 113                  default:
 114                      $this->error=("can not encode data in ".$this->inputencoding);
 115                      return 0;
 116              }
 117          }
 118          return 1;
 119      }
 120      
 121      Function writetag(&$output,$path,$indent)
 122      {
 123          $tag=$this->structure[$path]["Tag"];
 124          $output.=("<".$tag);
 125          $attributecount=count($this->structure[$path]["Attributes"]);
 126          if($attributecount>0)
 127          {
 128              $attributes=$this->structure[$path]["Attributes"];
 129              Reset($attributes);
 130              $end=(GetType($key=Key($attributes))!="string");
 131              for(;!$end;)
 132              {
 133                  $output.=(" ".$key."=\"".$attributes[$key]."\"");
 134                  Next($attributes);
 135                  $end=(GetType($key=Key($attributes))!="string");
 136              }
 137          }
 138          $elements=$this->structure[$path]["Elements"];
 139          if($elements>0)
 140          {
 141              $output.=">";
 142              $doindent=$this->structure[$path]["Indent"];
 143              $elementindent=(($doindent) ? $this->linebreak.$indent.$this->indenttext : "");
 144              $element=0;
 145              for(;$element<$elements;)
 146              {
 147                  $elementpath=($path.",".strval($element));
 148                  $output.=$elementindent;
 149                  if(IsSet($this->nodes[$elementpath]))
 150                  {
 151                      if(!($this->writetag($output,$elementpath,$indent.$this->indenttext)))
 152                          return 0;
 153                  }
 154                  else
 155                      $output.=$this->structure[$elementpath];
 156                  $element++;
 157              }
 158              $output.=((($doindent) ? $this->linebreak.$indent : "")."</".$tag.">");
 159          }
 160          else
 161              $output.="/>";
 162          return 1;
 163      }
 164      
 165      /*
 166       * Public functions
 167       *
 168       */
 169      Function write(&$output)
 170      {
 171          if(strcmp($this->error,""))
 172              return 0;
 173          if(!(IsSet($this->structure["0"])))
 174          {
 175              $this->error="XML document structure is empty";
 176              return 0;
 177          }
 178          $output=("<?xml version=\"1.0\" encoding=\"".$this->outputencoding."\"?>".$this->linebreak);
 179          if(strcmp($this->dtdtype,""))
 180          {
 181              $output.=("<!DOCTYPE ".$this->structure["0"]["Tag"]." ");
 182              switch($this->dtdtype)
 183              {
 184                  case "INTERNAL":
 185                      if(!strcmp($this->dtddefinition,""))
 186                      {
 187                          $this->error="it was not specified a valid internal DTD definition";
 188                          return 0;
 189                      }
 190                      $output.=("[".$this->linebreak.$this->dtddefinition.$this->linebreak."]");
 191                      break;
 192                  case "SYSTEM":
 193                      if(!strcmp($this->dtdurl,""))
 194                      {
 195                          $this->error="it was not specified a valid system DTD url";
 196                          return 0;
 197                      }
 198                      $output.="SYSTEM";
 199                      if(strcmp($this->dtddefinition,""))
 200                          $output.=(" \"".$this->dtddefinition."\"");
 201                      $output.=(" \"".$this->dtdurl."\"");
 202                      break;
 203                  case "PUBLIC":
 204                      if(!strcmp($this->dtddefinition,""))
 205                      {
 206                          $this->error="it was not specified a valid public DTD definition";
 207                          return 0;
 208                      }
 209                      $output.=("PUBLIC \"".$this->dtddefinition."\"");
 210                      if(strcmp($this->dtdurl,""))
 211                          $output.=(" \"".$this->dtdurl."\"");
 212                      break;
 213                  default:
 214                      $this->error="it was not specified a valid DTD type";
 215                      return 0;
 216              }
 217              $output.=(">".$this->linebreak);
 218          }
 219          if(strcmp($this->stylesheet,""))
 220          {
 221              if(!strcmp($this->stylesheettype,""))
 222              {
 223                  $this->error="it was not specified a valid stylesheet type";
 224                  return 0;
 225              }
 226              $output.=("<?xml-stylesheet type=\"".$this->stylesheettype."\" href=\"".$this->stylesheet."\"?>".$this->linebreak);
 227          }
 228          if(strcmp($this->generatedcomment,""))
 229              $output.=("<!-- ".$this->generatedcomment." -->".$this->linebreak);
 230          return $this->writetag($output,"0","");
 231      }
 232      
 233      Function addtag($tag,&$attributes,$parent,&$path,$indent)
 234      {
 235          if(strcmp($this->error,""))
 236              return 0;
 237          $path=((!strcmp($parent,"")) ? "0" : ($parent.",".strval($this->structure[$parent]["Elements"])));
 238          if(IsSet($this->structure[$path]))
 239          {
 240              $this->error=("tag with path ".$path." is already defined");
 241              return 0;
 242          }
 243          $encodedattributes=array();
 244          Reset($attributes);
 245          $end=(GetType($attribute_name=Key($attributes))!="string");
 246          for(;!$end;)
 247          {
 248              $encodedattributes[$attribute_name]="";
 249              if(!($this->encodedata($attributes[$attribute_name],$encoded_data)))
 250                  return 0;
 251              $encodedattributes[$attribute_name]=$encoded_data;
 252              Next($attributes);
 253              $end=(GetType($attribute_name=Key($attributes))!="string");
 254          }
 255          $this->structure[$path]=array(
 256              "Tag"=>$tag,
 257              "Attributes"=>$encodedattributes,
 258              "Elements"=>0,
 259              "Indent"=>$indent
 260          );
 261          $this->nodes[$path]=1;
 262          if(strcmp($parent,""))
 263              $this->structure[$parent]["Elements"]=($this->structure[$parent]["Elements"]+1);
 264          return 1;
 265      }
 266      
 267      Function adddata($data,$parent,&$path)
 268      {
 269          if(strcmp($this->error,""))
 270              return 0;
 271          if(!(IsSet($this->structure[$parent])))
 272          {
 273              $this->error=("the parent tag path".$path."is not defined");
 274              return 0;
 275          }
 276          if(!strcmp($data,""))
 277              return 1;
 278          $path=($parent.",".strval($this->structure[$parent]["Elements"]));
 279          if(!($this->encodedata($data,$encoded_data)))
 280              return 0;
 281          $this->structure[$path]=$encoded_data;
 282          $this->structure[$parent]["Elements"]=($this->structure[$parent]["Elements"]+1);
 283          return 1;
 284      }
 285      
 286      Function adddatatag($tag,&$attributes,$data,$parent,&$path)
 287      {
 288          return $this->addtag($tag,$attributes,$parent,$path,0) && $this->adddata($data,$path,$datapath);
 289      }
 290  };
 291  
 292  }
 293  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5