[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/vendor/propel-generator/classes/propel/engine/database/transform/ -> XmlToData.php (source)

   1  <?php
   2  
   3  /*
   4   *  $Id: XmlToData.php 262 2005-11-07 20:19:14Z hans $
   5   *
   6   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   7   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   8   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   9   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17   *
  18   * This software consists of voluntary contributions made by many individuals
  19   * and is licensed under the LGPL. For more information please see
  20   * <http://propel.phpdb.org>.
  21   */
  22  
  23  require_once 'phing/parser/AbstractHandler.php';
  24  
  25  /**
  26   * A Class that is used to parse an input xml schema file and creates an
  27   * AppData object.
  28   *
  29   * @author Hans Lellelid <hans@xmpl.org> (Propel)
  30   * @author Leon Messerschmidt <leon@opticode.co.za> (Torque)
  31   * @author Jason van Zyl <jvanzyl@apache.org> (Torque)
  32   * @author Martin Poeschl <mpoeschl@marmot.at> (Torque)
  33   * @author Fedor Karpelevitch <fedor.karpelevitch@home.com> (Torque)
  34   * @version $Revision: 262 $
  35   * @package propel.engine.database.transform
  36   */
  37  class XmlToData extends AbstractHandler {
  38  
  39      private $database;
  40      private $data;
  41  
  42      private $encoding;
  43  
  44      public $parser;
  45  
  46      const DEBUG = false;
  47  
  48      /**
  49       * Construct new XmlToData class.
  50       *
  51       * This class is passed the Database object so that it knows what to expect from
  52       * the XML file.
  53       *
  54       * @param Database $database
  55       */
  56      public function __construct(Database $database, $encoding = 'iso-8859-1')
  57      {
  58          $this->database = $database;
  59          $this->encoding = $encoding;
  60      }
  61  
  62      /**
  63       *
  64       */
  65      public function parseFile($xmlFile)
  66      {
  67          try {
  68  
  69              $this->data = array();
  70              
  71              try {
  72                  $fr = new FileReader($xmlFile);
  73              } catch (Exception $e) {
  74                  $f = new PhingFile($xmlFile);
  75                  throw new BuildException("XML File not found: " . $f->getAbsolutePath());
  76              }
  77  
  78              $br = new BufferedReader($fr);
  79  
  80              $this->parser = new ExpatParser($br);
  81              $this->parser->parserSetOption(XML_OPTION_CASE_FOLDING, 0);
  82              $this->parser->setHandler($this);
  83  
  84              try {
  85                  $this->parser->parse();
  86              } catch (Exception $e) {
  87                  print $e->getMessage() . "\n";
  88                  $br->close();
  89              }
  90              $br->close();
  91          } catch (Exception $e) {
  92              print $e->getMessage() . "\n";
  93              print $e->getTraceAsString();
  94          }
  95  
  96          return $this->data;
  97      }
  98  
  99      /**
 100       * Handles opening elements of the xml file.
 101       */
 102      public function startElement($name, $attributes)
 103      {
 104          try {
 105              if ($name == "dataset") {
 106                  // we don't do anything w/ <dataset> tag right now.
 107              } else {
 108                  $table = $this->database->getTableByPhpName($name);
 109  
 110                  $this->columnValues = array();
 111                  foreach($attributes as $name => $value) {
 112                      $col = $table->getColumnByPhpName($name);
 113                      $this->columnValues[] = new ColumnValue($col, iconv('utf-8',$this->encoding, $value));
 114                  }
 115                  $this->data[] = new DataRow($table, $this->columnValues);
 116              }
 117          } catch (Exception $e) {
 118              print $e;
 119              throw $e;
 120          }
 121      }
 122  
 123  
 124      /**
 125       * Handles closing elements of the xml file.
 126       *
 127       * @param $name The local name (without prefix), or the empty string if
 128       *         Namespace processing is not being performed.
 129       */
 130      public function endElement($name)
 131      {
 132          if (self::DEBUG) {
 133              print("endElement(" . $name . ") called\n");
 134          }
 135      }
 136  
 137  } // XmlToData
 138  
 139      /**
 140       * "inner class"
 141       * @package propel.engine.database.transform
 142       */
 143      class DataRow
 144      {
 145          private $table;
 146          private $columnValues;
 147  
 148          public function __construct(Table $table, $columnValues)
 149          {
 150              $this->table = $table;
 151              $this->columnValues = $columnValues;
 152          }
 153  
 154          public function getTable()
 155          {
 156              return $this->table;
 157          }
 158  
 159          public function getColumnValues()
 160          {
 161              return $this->columnValues;
 162          }
 163      }
 164  
 165      /**
 166       * "inner" class
 167       * @package propel.engine.database.transform
 168       */
 169      class ColumnValue {
 170  
 171          private $col;
 172          private $val;
 173  
 174          public function __construct(Column $col, $val)
 175          {
 176              $this->col = $col;
 177              $this->val = $val;
 178          }
 179  
 180          public function getColumn()
 181          {
 182              return $this->col;
 183          }
 184  
 185          public function getValue()
 186          {
 187              return $this->val;
 188          }
 189      }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7