[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/etemplate/inc/ -> class.xmltool.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - eTemplates - xml-tools - helper class to create xml-files   *
   4      * http://www.eGroupWare.org                                                *
   5      * Originaly from the phpGW XSLT API                                        *
   6      * Adapted by Ralf Becker <RalfBecker@outdoor-training.de>                  *
   7      * --------------------------------------------                             *
   8      *  This program is free software; you can redistribute it and/or modify it *
   9      *  under the terms of the GNU General Public License as published by the   *
  10      *  Free Software Foundation; either version 2 of the License, or (at your  *
  11      *  option) any later version.                                              *
  12      \**************************************************************************/
  13  
  14      /* $Id: class.xmltool.inc.php 19711 2005-11-09 20:50:45Z ralfbecker $ */
  15  
  16  	function var2xml($name, $data)
  17      {
  18          $doc =& new xmltool('root','','');
  19          return $doc->import_var($name,$data,True,True);
  20      }
  21  
  22      class xmltool
  23      {
  24          /* for root nodes */
  25          var $xmlversion = '1.0';
  26          var $doctype = Array();
  27          /* shared */
  28          var $node_type = '';
  29          var $name = '';
  30          var $data_type;
  31          var $data;
  32          /* for nodes */
  33          var $attributes = Array();
  34          var $comments = Array();
  35          var $indentstring = "\t";
  36          
  37          /* start the class as either a root or a node */
  38  		function xmltool ($node_type = 'root', $name='',$indentstring="\t")
  39          {
  40              $this->node_type = $node_type;
  41              $this->indentstring = $indentstring;
  42              if ($this->node_type == 'node')
  43              {
  44                  if($name != '')
  45                  {
  46                      $this->name = $name;
  47                  }
  48                  else
  49                  {
  50                      echo 'You must name node type objects<br>';
  51                      exit;
  52                  }
  53              }
  54          }
  55          
  56  		function set_version ($version = '1.0')
  57          {
  58              $this->xmlversion = $version;
  59              return True;
  60          }
  61  
  62  		function set_doctype ($name, $uri = '')
  63          {
  64              if ($this->node_type == 'root')
  65              {
  66                  $this->doctype[$name] = $uri;
  67                  return True;
  68              }
  69              else
  70              {
  71                  return False;
  72              }
  73          }
  74  
  75  		function add_node ($node_object, $name = '')
  76          {
  77              switch ($this->node_type)
  78              {
  79                  case 'root':
  80                      if (is_object($node_object))
  81                      {
  82                          $this->data = $node_object;
  83                      }
  84                      else
  85                      {
  86                          $this->data = $this->import_var($name, $node_object);
  87                      }
  88                      break;
  89                  case 'node':
  90                      if(!is_array($this->data))
  91                      {
  92                          $this->data = Array();
  93                          $this->data_type = 'node';
  94                      }
  95                      if (is_object($node_object))
  96                      {
  97                          if ($name != '')
  98                          {
  99                              $this->data[$name] = $node_object;
 100                          }
 101                          else
 102                          {
 103                              $this->data[] = $node_object;
 104                          }
 105                      }
 106                      else
 107                      {
 108                          $this->data[$name] = $this->import_var($name, $node_object);
 109                      }
 110                      return True;
 111                      break;
 112              }
 113          }
 114  
 115  		function get_node ($name = '')    // what is that function doing: NOTHING !!!
 116          {
 117              switch    ($this->data_type)
 118              {
 119                  case 'root':
 120                      break;
 121                  case 'node':
 122                      break;
 123                  case 'object':
 124                      break;
 125              }
 126          
 127          }
 128  
 129  		function set_value ($string)
 130          {
 131              $this->data = $string;
 132              $this->data_type = 'value';
 133              return True;
 134          }
 135          
 136  		function get_value ()
 137          {
 138              if($this->data_type == 'value')
 139              {
 140                  return $this->data;
 141              }
 142              else
 143              {
 144                  return False;
 145              }
 146          }
 147  
 148  		function set_attribute ($name, $value = '')
 149          {
 150              $this->attributes[$name] = $value;
 151              return True;
 152          }
 153  
 154  		function get_attribute ($name)
 155          {
 156              return $this->attributes[$name];
 157          }
 158  
 159  		function get_attributes ()
 160          {
 161              return $this->attributes;
 162          }
 163          
 164  		function add_comment ($comment)
 165          {
 166              $this->comments[] = $comment;
 167              return True;
 168          }
 169  
 170  		function import_var($name, $value,$is_root=False,$export_xml=False)
 171          {
 172              echo "<p>import_var: this->indentstring='$this->indentstring'</p>\n";
 173              $node =& new xmltool('node',$name,$this->indentstring);
 174              switch (gettype($value))
 175              {
 176                  case 'string':
 177                  case 'integer':
 178                  case 'double':
 179                  case 'NULL':
 180                      $node->set_value($value);
 181                      break;
 182                  case 'boolean':
 183                      if($value == True)
 184                      {
 185                          $node->set_value('1');
 186                      }
 187                      else
 188                      {
 189                          $node->set_value('0');
 190                      }
 191                      break;
 192                  case 'array':
 193                      $new_index = False;
 194                      while (list ($idxkey, $idxval) = each ($value))
 195                      {
 196                          if(is_array($idxval))
 197                          {
 198                              while (list ($k, $i) = each ($idxval))
 199                              {
 200                                  if (is_int($k))
 201                                  {
 202                                      $new_index = True;
 203                                  }
 204                              }
 205                          }
 206                      }
 207                      reset($value);    
 208                      while (list ($key, $val) = each ($value))
 209                      {
 210                          if($new_index)
 211                          {
 212                              $keyname = $name;
 213                              $nextkey = $key;
 214                          }
 215                          else
 216                          {
 217                              $keyname = $key;
 218                              $nextkey = $key;
 219                          }
 220                          switch (gettype($val))
 221                          {
 222                              case 'string':
 223                              case 'integer':
 224                              case 'double':
 225                              case 'NULL':
 226                                  $subnode =& new xmltool('node', $nextkey,$this->indentstring);
 227                                  $subnode->set_value($val);
 228                                  $node->add_node($subnode);                            
 229                                  break;
 230                              case 'boolean':
 231                                  $subnode =& new xmltool('node', $nextkey,$this->indentstring);
 232                                  if($val == True)
 233                                  {
 234                                      $subnode->set_value('1');
 235                                  }
 236                                  else
 237                                  {
 238                                      $subnode->set_value('0');
 239                                  }
 240                                  $node->add_node($subnode);                            
 241                                  break;
 242                              case 'array':
 243                                  if($new_index)
 244                                  {
 245                                      while (list ($subkey, $subval) = each ($val))
 246                                      {
 247                                          $node->add_node($this->import_var($nextkey, $subval));
 248                                      }
 249                                  }
 250                                  else
 251                                  {
 252                                      $subnode = $this->import_var($nextkey, $val);
 253                                      $node->add_node($subnode);                            
 254                                  }
 255                                  break;
 256                              case 'object':
 257                                  $subnode =& new xmltool('node', $nextkey,$this->indentstring);
 258                                  $subnode->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($val));
 259                                  $node->add_node($subnode);                            
 260                                  break;
 261                              case 'resource':
 262                                  echo 'Halt: Cannot package PHP resource pointers into XML<br>';
 263                                  exit;
 264                              default:
 265                                  echo 'Halt: Invalid or unknown data type<br>';
 266                                  exit;
 267                          }
 268                      }
 269                      break;
 270                  case 'object':
 271                      $node->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($value));
 272                      break;
 273                  case 'resource':
 274                      echo 'Halt: Cannot package PHP resource pointers into XML<br>';
 275                      exit;
 276                  default:
 277                      echo 'Halt: Invalid or unknown data type<br>';
 278                      exit;
 279              }
 280      
 281              if($is_root)
 282              {
 283                  $this->add_node($node);
 284                  if($export_xml)
 285                  {
 286                      $xml = $this->export_xml();
 287                      return $xml;
 288                  }
 289                  else
 290                  {
 291                      return True;
 292                  }
 293              }
 294              else
 295              {
 296                  $this->add_node($node);
 297                  return $node;
 298              }
 299          }
 300  
 301  		function export_var()
 302          {
 303              if($this->node_type == 'root')
 304              {
 305                  return $this->data->export_var();
 306              }
 307  
 308              if($this->data_type != 'node')
 309              {    
 310                  $found_at = strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
 311                  if($found_at != False)
 312                  {
 313                      return unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data));
 314                  }
 315                  return $this->data;
 316              }
 317              else
 318              {
 319                  $new_index = False;
 320                  reset($this->data);
 321                  while(list($key,$val) = each($this->data))
 322                  {
 323                      if(!isset($found_keys[$val->name]))
 324                      {
 325                          $found_keys[$val->name] = True;
 326                      }
 327                      else
 328                      {
 329                          $new_index = True;
 330                      }
 331                  }
 332  
 333                  if($new_index)
 334                  {
 335                      reset($this->data);
 336                      while(list($key,$val) = each($this->data))
 337                      {
 338  
 339                          $return_array[$val->name][] = $val->export_var();
 340                      }                
 341                  }
 342                  else
 343                  {
 344                      reset($this->data);
 345                      while(list($key,$val) = each($this->data))
 346                      {
 347                          $return_array[$val->name] = $val->export_var();
 348                      }
 349                  }
 350                  return $return_array;
 351              }
 352          }
 353  
 354  		function export_struct()
 355          {
 356              if($this->node_type == 'root')
 357              {
 358                  return $this->data->export_struct();
 359              }
 360  
 361              $retval['tag'] = $this->name;
 362              $retval['attributes'] = $this->attributes;
 363              if($this->data_type != 'node')
 364              {    
 365                  $found_at = strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
 366                  if($found_at != False)
 367                  {
 368                      $retval['value'] = unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data));
 369                  }
 370                  else
 371                  {
 372                      $retval['value'] = $this->data;
 373                  }
 374                  return $retval;
 375              }
 376              else
 377              {
 378                  reset($this->data);
 379                  while(list($key,$val) = each($this->data))
 380                  {
 381                      $retval['children'][] = $val->export_struct();
 382                  }                
 383                  return $retval;
 384              }
 385          }
 386  
 387          
 388  		function import_xml_children($data, &$i, $parent_node)
 389          {
 390              while (++$i < count($data))
 391              {
 392                  switch ($data[$i]['type'])
 393                  {
 394                      case 'cdata':
 395                      case 'complete':
 396                          $node =& new xmltool('node',$data[$i]['tag'],$this->indentstring);
 397                          if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0)
 398                          {
 399                              while(list($k,$v)=each($data[$i]['attributes']))
 400                              {
 401                                  $node->set_attribute($k,$v);
 402                              }
 403                          }
 404                          $node->set_value($data[$i]['value']);
 405                          $parent_node->add_node($node);
 406                          break;
 407                      case 'open':
 408                          $node =& new xmltool('node',$data[$i]['tag'],$this->indentstring);
 409                          if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0)
 410                          {
 411                              while(list($k,$v)=each($data[$i]['attributes']))
 412                              {
 413                                  $node->set_attribute($k,$v);
 414                              }
 415                          }
 416                          
 417                          $node = $this->import_xml_children($data, $i, $node);
 418                          $parent_node->add_node($node);
 419                          break;
 420                      case 'close':
 421                          return $parent_node;
 422                  }
 423              }
 424          }
 425          
 426  		function import_xml($xmldata) 
 427          {
 428              $parser = xml_parser_create();
 429              xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
 430              xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,   1);
 431              xml_parse_into_struct($parser, $xmldata, $vals, $index);
 432              xml_parser_free($parser);
 433              unset($index);    
 434              $node =& new xmltool('node',$vals[0]['tag'],$this->indentstring);
 435              if(isset($vals[0]['attributes']))
 436              {
 437                  while(list($key,$value) = each($vals[0]['attributes']))
 438                  {
 439                      $node->set_attribute($key, $value);
 440                  }
 441              }
 442              switch ($vals[0]['type'])
 443              {
 444                  case 'complete':
 445                      $node->set_value($vals[0]['value']);
 446                      break;
 447                  case 'cdata':
 448                      $node->set_value($vals[0]['value']);
 449                      break;
 450                  case 'open':
 451                      $node = $this->import_xml_children($vals, $i = 0, $node);
 452                      break;
 453                  case 'closed':
 454                      exit;
 455              }
 456              $this->add_node($node);
 457          }
 458  
 459  		function export_xml($indent = 1)
 460          {
 461              if ($this->node_type == 'root')
 462              {
 463                  $result = '<?xml version="'.$this->xmlversion.'"?>'."\n";
 464                  if(count($this->doctype) == 1)
 465                  {
 466                      list($doctype_name,$doctype_uri) = each($this->doctype);
 467                      $result .= '<!DOCTYPE '.$doctype_name.' SYSTEM "'.$doctype_uri.'">'."\n";
 468                  }
 469                  if(count($this->comments) > 0 )
 470                  {
 471                      //reset($this->comments);
 472                      while(list($key,$val) = each ($this->comments))
 473                      {
 474                          $result .= "<!-- $val -->\n";
 475                      }
 476                  }
 477                  if(is_object($this->data))
 478                  {
 479                      $indent = 0;
 480                      $result .= $this->data->export_xml($indent);
 481                  }
 482                  return $result;
 483              }
 484              else /* For node objects */
 485              {
 486                  for ($i = 0; $i < $indent; $i++)
 487                  {
 488                      $indentstring .= $this->indentstring;
 489                  }
 490  
 491                  $result = $indentstring.'<'.$this->name;
 492                  if(count($this->attributes) > 0 )
 493                  {
 494                      reset($this->attributes);
 495                      while(list($key,$val) = each ($this->attributes))
 496                      {
 497                          $result .= ' '.$key.'="'.htmlspecialchars($val,ENT_COMPAT,'utf-8').'"';
 498                      }
 499                  }
 500  
 501                  $endtag_indent = $indentstring;
 502                  if (empty($this->data_type))
 503                  {
 504                      $result .= '/>'."\n";
 505                  }
 506                  else
 507                  {
 508                      $result .= '>';
 509  
 510                      switch ($this->data_type)
 511                      {
 512                          case 'value':
 513                              if(is_array($this->data))
 514                              {
 515                                  $type_error = True;
 516                                  break;
 517                              }
 518                              
 519                              /*if(preg_match("(&|<)", $this->data))    // this is unnecessary with htmlspecialchars($this->data)
 520                              {
 521                                  $result .= '<![CDATA['.$this->data.']]>';
 522                                  $endtag_indent = '';        
 523                              }
 524                              else*/if(strlen($this->data) > 30 && !empty($this->indentstring))
 525                              {
 526                                  $result .= "\n".$indentstring.$this->indentstring.htmlspecialchars($this->data,ENT_COMPAT,'utf-8')."\n";
 527                                  $endtag_indent = $indentstring;
 528                              }
 529                              else
 530                              {
 531                                  $result .= htmlspecialchars($this->data,ENT_COMPAT,'utf-8');
 532                                  $endtag_indent = '';
 533                              }
 534                              break;
 535                          case 'node':
 536                              $result .= "\n";
 537                              if(!is_array($this->data))
 538                              {
 539                                  $type_error = True;
 540                                  break;
 541                              }
 542                  
 543                              $subindent = $indent+1;
 544                              reset($this->data);
 545                              while(list($key,$val) = each ($this->data))
 546                              {
 547                                  if(is_object($val))
 548                                  {
 549                                      $result .= $val->export_xml($subindent);
 550                                  }
 551                              }
 552                              break;
 553                          default:
 554                          if($this->data != '')
 555                          {
 556                              echo 'Invalid or unset data type ('.$this->data_type.'). This should not be possible if using the class as intended<br>';
 557                          }
 558                      }
 559  
 560                      if ($type_error)
 561                      {
 562                          echo 'Invalid data type. Tagged as '.$this->data_type.' but data is '.gettype($this->data).'<br>';
 563                      }
 564  
 565                      $result .= $endtag_indent.'</'.$this->name.'>';
 566                      if($indent != 0)
 567                      {
 568                          $result .= "\n";
 569                      }
 570                  }
 571                  if(count($this->comments) > 0 )
 572                  {
 573                      reset($this->comments);
 574                      while(list($key,$val) = each ($this->comments))
 575                      {
 576                          $result .= $endtag_indent."<!-- $val -->\n";
 577                      }
 578                  }
 579                  return $result;
 580              }
 581          }
 582      }
 583  
 584      class xmlnode extends xmltool
 585      {
 586  		function xmlnode($name)
 587          {
 588              $this->xmltool('node',$name);
 589          }
 590      }
 591  
 592      class xmldoc extends xmltool
 593      {
 594  		function xmldoc($version = '1.0')
 595          {
 596              $this->xmltool('root');
 597              $this->set_version($version);
 598          }
 599  
 600  		function add_root($root_node)
 601          {
 602              return $this->add_node($root_node);
 603          }
 604      }
 605  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7