[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/xml/ -> pathparser.class.php (source)

   1  <?
   2  
   3      /**
   4       * \defgroup XML
   5       *
   6       * This module includes several external libraries and modules that deal with XML content
   7       * including parsing, RSS and so on. 
   8       */
   9  
  10  // ##################################################################################
  11  // Title                     : Class Path_parser
  12  // Version                   : 1.0
  13  // Author                    : Luis Argerich (lrargerich@yahoo.com)
  14  // Last modification date    : 06-24-2002
  15  // Description               : 
  16  // ##################################################################################
  17  // History: 
  18  // 06-24-2002                : First version of this class.
  19  // ##################################################################################
  20  // To-Dos:
  21  //
  22  // ##################################################################################
  23  // How to use it:
  24  // Read the documentation in path_parser.html
  25  // ##################################################################################
  26  
  27  
  28  /*
  29   * Adapted by me to extend the Object class so that it fits in my
  30   * class hierarchy
  31   */
  32  if(defined("_class_path_parser_is_included")) {
  33    // do nothing since the class is already included  
  34  } else {
  35    define("_class_path_parser_is_included",1);
  36  
  37  // This is a class to parse XML files and call user functions
  38  // when specific elements (paths) are observed by the parser.
  39  
  40  
  41  
  42  
  43  
  44  /**
  45   * \ingroup XML
  46   *
  47   * Allows to create XPath queries for XML documents, if needed.
  48   */
  49  class Path_parser  {
  50    var $paths;
  51    var $context=Array();
  52    var $parser;
  53    var $error;
  54    var $path;
  55  
  56  
  57    function get_error() {
  58      return $this->error;
  59    }
  60  
  61    function Path_parser() {
  62      
  63  
  64      $this->init();
  65    }
  66    
  67    function init() {
  68      $this->paths=Array(); 
  69      $this->parser = xml_parser_create_ns("",'^');
  70      xml_set_object($this->parser,&$this);
  71      xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  72      xml_set_element_handler($this->parser, "_startElement", "_endElement");
  73      xml_set_character_data_handler($this->parser,"_data");
  74    }
  75  
  76    function parse_file($xml) {
  77      if (!($fp = fopen($xml, "r"))) {
  78        $this->error="Cannot open $rddl";
  79        return false;
  80      }
  81      while ($data = fread($fp, 4096)) {
  82        if (!xml_parse($this->parser, $data, feof($fp))) {
  83          $this->error="XML error: ".xml_error_string(xml_get_error_code($this->parser))." at line ".xml_get_current_line_number($this->parser);
  84          return false;
  85        }
  86      }
  87      xml_parser_free($this->parser);
  88      return true;
  89    }
  90  
  91    function parse($data,$is_final) {
  92      $ret = xml_parse($this->parser,$data,$is_final);
  93      if ($is_final) {
  94        xml_parser_free($this->parser);
  95      }
  96      if(!$ret) {
  97           $this->error="XML error: ".xml_error_string(xml_get_error_code($this->parser))." at line ".xml_get_current_line_number($this->parser);
  98      }
  99      return $ret;
 100   }
 101    
 102  
 103  
 104    function set_handler($path,$handler_name) {
 105      $this->paths[$path]["handler"]=$handler_name;
 106      $this->paths[$path]["depth"]=-1;
 107    }
 108     
 109    function _startElement($parser,$name,$attribs) {
 110      // Add the element to the context
 111      $names=explode('^',$name);
 112      if(count($names)>1) {
 113        $name=$names[1];
 114        $name_namespace_uri=$names[0]; 
 115      } else {
 116        $name=$names[0]; 
 117      }
 118      
 119      array_push($this->context, $name);
 120      
 121      $path='/'.implode("/",$this->context);
 122      $this->path=$path;
 123      //print("Actual path: $path <br/>\n");
 124      // Check all opened paths and update them
 125      foreach(array_keys($this->paths) as $pathk) {
 126        if($this->paths[$pathk]["depth"]>0  ) {
 127          $this->paths[$pathk]["depth"]++; 
 128          $this->paths[$pathk]["content"].='<'.$name;
 129          foreach($attribs as $atk => $atv) {
 130            $this->paths[$pathk]["content"].=' '.$atk.'="'.$atv.'"'; 
 131          }
 132          $this->paths[$pathk]["content"].='>';
 133        }
 134      }
 135      
 136      // If the context path matches some UNMATCHED path then init element data
 137      if(in_array($path,array_keys($this->paths))) {
 138         //print("Match!<br/>\n"); 
 139         if($this->paths[$path]["depth"]==-1) {
 140           $this->paths[$path]["depth"]=1;
 141           $this->paths[$path]["content"]='';
 142           $this->paths[$path]["content"]='<'.$name;
 143           $this->paths[$path]["name"]=$name;
 144           $this->paths[$path]["attribs"]=$attribs;
 145           foreach($attribs as $atk => $atv) {
 146             $this->paths[$path]["content"].=' '.$atk.'="'.$atv.'"'; 
 147           }
 148           $this->paths[$path]["content"].='>';
 149         }
 150      }  
 151    }
 152    
 153    function _endElement($parser,$name) {
 154      // Decrement element depth
 155      array_pop($this->context);
 156      $path='/'.implode("/",$this->context);
 157      $this->path=$path;
 158      //print("Actual path: $path <br/>\n");
 159      foreach(array_keys($this->paths) as $pathk) {
 160        if($this->paths[$pathk]["depth"]>0  ) {
 161          $this->paths[$pathk]["depth"]--; 
 162          $this->paths[$pathk]["content"].='</'.$name.'>';
 163        }
 164        if($this->paths[$pathk]["depth"]==0) {
 165          //print("Reportar: $pathk <br/>\n");
 166          //print_r($this->paths[$pathk]);
 167          $this->paths[$pathk]["depth"]=-1;
 168          $this->paths[$pathk]["handler"]($this->paths[$pathk]["name"],$this->paths[$pathk]["attribs"],$this->paths[$pathk]["content"]);
 169        }
 170      }
 171  
 172    }
 173  
 174    function _data($parser,$data) {
 175      foreach(array_keys($this->paths) as $pathk) {
 176        if($this->paths[$pathk]["depth"]>0  ) {
 177          $this->paths[$pathk]["content"].=$data;
 178        }
 179  
 180      }
 181    }
 182  
 183  
 184  }
 185  
 186  }
 187  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics