home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Sqlite / Tools / XMLParser.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  6.6 KB  |  246 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. // {{{ Header
  5.  
  6. /**
  7.  * DB_Sqlite_Tools_XMLParser, XML parser class.
  8.  *
  9.  * PHP version 5
  10.  *
  11.  * LICENSE:
  12.  *
  13.  * BSD License
  14.  *
  15.  * Copyright (c) 2004-2006 David Costa
  16.  * All rights reserved.
  17.  *
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  *
  22.  * 1. Redistributions of source code must retain the above copyright
  23.  *    notice, this list of conditions and the following disclaimer.
  24.  * 2. Redistributions in binary form must reproduce the above
  25.  *    copyright notice, this list of conditions and the following
  26.  *    disclaimer in the documentation and/or other materials provided
  27.  *    with the distribution.
  28.  * 3. Neither the name of David Costa nor the names of
  29.  *    contributors may be used to endorse or promote products derived
  30.  *    from this software without specific prior written permission.
  31.  *
  32.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  37.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  38.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  42.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  43.  * POSSIBILITY OF SUCH DAMAGE.
  44.  *
  45.  * @category XML
  46.  * @package DB_Sqlite_Tools
  47.  * @author David Costa <gurugeek@php.net>
  48.  * @author Ashley Hewson <morbidness@gmail.com>
  49.  * @copyright Copyright (c) 2004-2006 David Costa
  50.  * @license http://www.opensource.org/licenses/bsd-license.php
  51.  *          BSD License
  52.  * @version CVS: $Id: XMLParser.php,v 1.2 2006/05/26 17:24:26 firman Exp $
  53.  */
  54.  
  55. // }}}
  56. // {{{ Class: DB_Sqlite_Tools_XMLParser
  57.  
  58. /**
  59.  * Alpha version of the XML parser to be possibly replaced by a proper parse or
  60.  * extension as suggested by Stephan Schimdt.
  61.  *
  62.  * @category XML
  63.  * @package DB_Sqlite_Tools
  64.  * @author David Costa <gurugeek@php.net>
  65.  * @author Ashley Hewson <morbidness@gmail.com>
  66.  * @copyright Copyright (c) 2004-2006 David Costa
  67.  * @license http://www.opensource.org/licenses/bsd-license.php
  68.  *          BSD License
  69.  * @version Release: 0.1.5
  70.  * @todo Add more comments
  71.  */
  72. class DB_Sqlite_Tools_XMLParser
  73. {
  74.     // {{{ Properties
  75.  
  76.     /**
  77.      * File resource handler.
  78.      *
  79.      * @var resource
  80.      */
  81.     public $fh;
  82.  
  83.     /**
  84.      * Current parse element.
  85.      *
  86.      * @var string
  87.      */
  88.     public $element;
  89.  
  90.     /**
  91.      * Enclosed element.
  92.      *
  93.      * @var string
  94.      */
  95.     public $enclosed;
  96.  
  97.     /**
  98.      * List of elements to ignore.
  99.      *
  100.      * @var array
  101.      */
  102.     public $ignoreList = array();
  103.  
  104.     private $str;
  105.  
  106.     // {{{ Constructor
  107.  
  108.     /**
  109.      * Construct.
  110.      *
  111.      * @param resource $fh File pointer resource handler.
  112.      * @param int $pos (optional) Byte as start position.
  113.      */
  114.     public function __construct($fh, $pos = 0)
  115.     {
  116.         $this->fh = $fh;
  117.         fseek($fh, $pos);
  118.         $this->strlen = strlen($this->str);
  119.     }
  120.  
  121.     // }}}
  122.     // {{{ getNextElement()
  123.  
  124.     public function getNextElement() 
  125.     {
  126.         // the loop is so that if we are told to ignore a certain
  127.         // element, then we can continue on to the next one and
  128.         // return that.
  129.         while (true) {
  130.             // obviously, if we're at EOF, then there are no more
  131.             // elements ;)
  132.             if (feof($this->fh)) {
  133.                 return false;
  134.             }
  135.  
  136.             // read up to the first open bracket, storing what's
  137.             // in between
  138.             $this->enclosed = '';
  139.             $c = fgetc($this->fh);
  140.             while (($c != '<') && !feof($this->fh)) {
  141.                 $this->enclosed .= $c;
  142.                 $c = fgetc($this->fh);
  143.             }
  144.  
  145.             // read up to the first close bracket that isn't within
  146.             // quote marks, storing what's in between.
  147.             $this->element = '';
  148.             $inQuote = false;
  149.             $c = fgetc($this->fh);
  150.             while (($c != '>') // end if $c == '>'
  151.                    || ($inQuote) // unless this is within a quote,
  152.                    || (feof($this->fh)) // or if we have reached EOF.
  153.                   )
  154.             {
  155.                 // toggle quote flag
  156.                 if ($c == '"') {
  157.                     $inQuote = !$inQuote;
  158.                 }
  159.  
  160.                 $this->element .= $c;
  161.                 $c = fgetc($this->fh);
  162.             }
  163.  
  164.             // default action is to accept this element, however we have
  165.             // to check it against the list of elements to ignore, like
  166.             // <!-- -->
  167.             $break = true;
  168.             foreach($this->ignoreList as $ignore) {
  169.                 if (preg_match("/$ignore/", $this->getElement())) $break = false;
  170.             }
  171.             // break the while loop if this is an acceptable element
  172.             if ($break) {
  173.                 break;
  174.             }
  175.         }
  176.  
  177.         return true;
  178.     }
  179.  
  180.     // }}}
  181.     // {{{ ignore()
  182.  
  183.     /**
  184.      * Add $str to ignore list.
  185.      *
  186.      * @param string $str The element to ignore.
  187.      */
  188.     public function ignore($str) 
  189.     {
  190.         $this->ignoreList[] = $str;
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ getElement()
  195.  
  196.     /**
  197.      * Get the element.
  198.      *
  199.      * @return string
  200.      */
  201.     public function getElement() 
  202.     {
  203.         return trim($this->element);
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ getEnclosed()
  208.  
  209.     public function getEnclosed() 
  210.     {
  211.         return $this->enclosed;
  212.     }
  213.  
  214.     // }}}
  215.     // {{{ getElementAttribute()
  216.  
  217.     public function getElementAttribute($name) 
  218.     {  
  219.         $el = $this->getElement();
  220.         preg_match('/[ ]+'.$name.'[ ]*=[ ]*"([^"]*)"/', $el, $result);
  221.         return $result[1];
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ getElementName()
  226.  
  227.     public function getElementName() 
  228.     {
  229.         preg_match("/^([^ ]*).*$/", $this->getElement(), $result);
  230.         return $result[1];
  231.     }
  232.  
  233.     // }}}
  234. }
  235.  
  236. // }}}
  237.  
  238. /*
  239.  * Local variables:
  240.  * mode: php
  241.  * tab-width: 4
  242.  * c-basic-offset: 4
  243.  * c-hanging-comment-ender-p: nil
  244.  * End:
  245.  */
  246. ?>