[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/vendor/phing/filters/util/ -> ChainReaderHelper.php (source)

   1  <?php
   2  /*
   3   *  $Id: ChainReaderHelper.php 3076 2006-12-18 08:52:12Z fabien $
   4   *
   5   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   6   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   7   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   8   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   9   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16   *
  17   * This software consists of voluntary contributions made by many individuals
  18   * and is licensed under the LGPL. For more information please see
  19   * <http://phing.info>.
  20  */
  21  
  22  include_once 'phing/Project.php';
  23  include_once 'phing/filters/BaseFilterReader.php';
  24  include_once 'phing/types/PhingFilterReader.php';
  25  include_once 'phing/types/FilterChain.php';
  26  include_once 'phing/types/Parameter.php';
  27  include_once 'phing/util/FileUtils.php';
  28  include_once 'phing/util/StringHelper.php';
  29  include_once 'phing/filters/ChainableReader.php';
  30  
  31  /**
  32   * Process a FilterReader chain.
  33   *
  34   * Here, the interesting method is 'getAssembledReader'.
  35   * The purpose of this one is to create a simple Reader object which
  36   * apply all filters on another primary Reader object. 
  37   *
  38   * For example : In copyFile (phing.util.FileUtils) the primary Reader
  39   * is a FileReader object (more accuratly, a BufferedReader) previously 
  40   * setted for the source file to copy. So, consider this filterchain :
  41   *        
  42   *     <filterchain>
  43   *        <stripphpcomments />
  44   *        <linecontains>
  45   *            <contains value="foo">
  46   *        </linecontains>
  47   *      <tabtospaces tablength="8" />
  48   *    </filterchain>
  49   *
  50   *    getAssembledReader will return a Reader object wich read on each
  51   *    of these filters. Something like this : ('->' = 'which read data from') :
  52   *
  53   *  [TABTOSPACES] -> [LINECONTAINS] -> [STRIPPHPCOMMENTS] -> [FILEREADER]
  54   *                                                         (primary reader)
  55   *
  56   *  So, getAssembledReader will return the TABTOSPACES Reader object. Then
  57   *  each read done with this Reader object will follow this path.
  58   *
  59   *    Hope this explanation is clear :)
  60   *
  61   * TODO: Implement the classPath feature.
  62   *
  63   * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  64   * @version   $Revision: 1.8 $ $Date: 2005/02/27 20:52:09 $
  65   * @access    public
  66   * @package   phing.filters.util
  67  */
  68  class ChainReaderHelper {
  69      
  70      /** Primary reader to wich the reader chain is to be attached */
  71      private $primaryReader = null;
  72      
  73      /** The site of the buffer to be used. */
  74      private $bufferSize = 8192;
  75      
  76      /** Chain of filters */
  77      private $filterChains = array();
  78      
  79      /** The Phing project */
  80      private $project;
  81  
  82      /*
  83       * Sets the primary reader
  84      */
  85      function setPrimaryReader(Reader $reader) {
  86          $this->primaryReader = $reader;
  87      }
  88  
  89      /*
  90       * Set the project to work with
  91      */
  92      function setProject(Project $project) {
  93          $this->project = $project;
  94      }
  95  
  96      /*
  97       * Get the project
  98      */
  99      function getProject() {
 100          return $this->project;
 101      }
 102  
 103      /*
 104       * Sets the buffer size to be used.  Defaults to 8192,
 105       * if this method is not invoked.
 106      */
 107      function setBufferSize($size) {
 108          $this->bufferSize = $size;
 109      }
 110  
 111      /*
 112       * Sets the collection of filter reader sets
 113      */
 114      function setFilterChains(&$fchain) {
 115          $this->filterChains = &$fchain;
 116      }
 117  
 118      /*
 119       * Assemble the reader
 120      */
 121      function getAssembledReader() {
 122      
 123          $instream = $this->primaryReader;
 124          $filterReadersCount = count($this->filterChains);
 125          $finalFilters = array();
 126  
 127          // Collect all filter readers of all filter chains used ...
 128          for($i = 0 ; $i<$filterReadersCount ; $i++) {
 129              $filterchain = &$this->filterChains[$i];
 130              $filterReaders = $filterchain->getFilterReaders();
 131              $readerCount = count($filterReaders);
 132              for($j = 0 ; $j<$readerCount ; $j++) {
 133                  $finalFilters[] = $filterReaders[$j];
 134              }
 135          }
 136  
 137          // ... then chain the filter readers.
 138          $filtersCount = count($finalFilters);
 139          if ( $filtersCount > 0 ) {
 140              for($i = 0 ; $i<$filtersCount ; $i++) {
 141                  $filter = $finalFilters[$i];
 142                  
 143                  if ( $filter instanceof PhingFilterReader ) {
 144                  
 145                      // This filter reader is an external class.
 146                      $className = $filter->getClassName();
 147                      $classpath = $filter->getClasspath();
 148                      $project   = $filter->getProject();
 149                      
 150                      if ( $className !== null ) {
 151                          $cls = Phing::import($className, $classpath);
 152                          $impl = new $cls();                        
 153                      }
 154  
 155                      if ( !($impl instanceof FilterReader) ) {
 156                          throw new Exception($className." does not extend phing.system.io.FilterReader");
 157                      }
 158                      
 159                      $impl->setReader($instream); // chain
 160                      $impl->setProject($this->getProject()); // what about $project above ?
 161  
 162                      if ( $impl instanceof Parameterizable ) {
 163                          $impl->setParameters($filter->getParams());
 164                      }
 165                      
 166                      $instream = $impl; // now that it's been chained
 167                                                              
 168                  } elseif (($filter instanceof ChainableReader) && ($filter instanceof Reader)) {                   
 169                      if ( $this->getProject() !== null && ($filter instanceof BaseFilterReader) ) {
 170                          $filter->setProject($this->getProject());
 171                      }                    
 172                      $instream = $filter->chain($instream);
 173                  } else {
 174                      throw new Exception("Cannot chain invalid filter: " . get_class($filter));
 175                  }
 176              }
 177          }
 178  
 179          return $instream;
 180      }    
 181  
 182  }
 183  
 184  ?>


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