[ 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/ -> LineContainsRegexp.php (source)

   1  <?php
   2  /*
   3   *  $Id: LineContainsRegexp.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/filters/BaseParamFilterReader.php';
  23  include_once 'phing/types/RegularExpression.php';
  24  include_once 'phing/filters/ChainableReader.php';
  25  
  26  /**
  27   * Filter which includes only those lines that contain the user-specified
  28   * regular expression matching strings.
  29   *
  30   * Example:
  31   * <pre><linecontainsregexp>
  32   *   <regexp pattern="foo*">
  33   * </linecontainsregexp></pre>
  34   *
  35   * Or:
  36   *
  37   * <pre><filterreader classname="phing.filters.LineContainsRegExp">
  38   *    <param type="regexp" value="foo*"/>
  39   * </filterreader></pre>
  40   *
  41   * This will fetch all those lines that contain the pattern <code>foo</code>
  42   *
  43   * @author    Yannick Lecaillez <yl@seasonfive.com>
  44   * @author    Hans Lellelid <hans@xmpl.org>
  45   * @version   $Revision: 1.8 $
  46   * @see       FilterReader
  47   * @package   phing.filters
  48   */
  49  class LineContainsRegexp extends BaseParamFilterReader implements ChainableReader {
  50  
  51      /**
  52       * Parameter name for regular expression.
  53       * @var string
  54       */ 
  55      const REGEXP_KEY = "regexp";
  56      
  57      /**
  58       * Regular expressions that are applied against lines.
  59       * @var array
  60       */ 
  61      private    $_regexps = array();
  62          
  63      /**
  64       * Returns all lines in a buffer that contain specified strings.
  65       * @return mixed buffer, -1 on EOF
  66       */
  67      function read($len = null) {
  68      
  69          if ( !$this->getInitialized() ) {
  70              $this->_initialize();
  71              $this->setInitialized(true);
  72          }
  73          
  74          $buffer = $this->in->read($len);
  75          
  76          if ($buffer === -1) {
  77              return -1;
  78          }
  79          
  80          $lines = explode("\n", $buffer);        
  81          $matched = array();        
  82          
  83          $regexpsSize = count($this->_regexps);
  84          foreach($lines as $line) {    
  85               for($i = 0 ; $i<$regexpsSize ; $i++) {
  86                      $regexp = $this->_regexps[$i];
  87                      $re = $regexp->getRegexp($this->getProject());
  88                      $matches = $re->matches($line);
  89                      if ( !$matches ) {
  90                          $line = null;
  91                          break;
  92                      }
  93              }            
  94              if($line !== null) {
  95                  $matched[] = $line;
  96              }                
  97          }        
  98          $filtered_buffer = implode("\n", $matched);    
  99          return $filtered_buffer;
 100      }
 101      
 102      /**
 103       * Adds a <code>regexp</code> element.
 104       * 
 105       * @return object regExp The <code>regexp</code> element added. 
 106       */
 107      function createRegexp() {
 108          $num = array_push($this->_regexps, new RegularExpression());
 109          return $this->_regexps[$num-1];
 110      }
 111  
 112      /**
 113       * Sets the vector of regular expressions which must be contained within 
 114       * a line read from the original stream in order for it to match this 
 115       * filter.
 116       * 
 117       * @param regexps An array of regular expressions which must be contained 
 118       *                within a line in order for it to match in this filter. Must not be 
 119       *                <code>null</code>.
 120       */
 121      function setRegexps($regexps) {
 122          // type check, error must never occur, bad code of it does
 123          if ( !is_array($regexps) ) {
 124              throw new Exception("Excpected an 'array', got something else");
 125          }
 126          $this->_regexps = $regexps;
 127      }
 128  
 129      /**
 130       * Returns the array of regular expressions which must be contained within 
 131       * a line read from the original stream in order for it to match this 
 132       * filter.
 133       * 
 134       * @return array The array of regular expressions which must be contained within 
 135       *         a line read from the original stream in order for it to match this 
 136       *         filter. The returned object is "live" - in other words, changes made to 
 137       *         the returned object are mirrored in the filter.
 138       */
 139      function getRegexps() {
 140          return $this->_regexps;
 141      }
 142  
 143      /**
 144       * Creates a new LineContainsRegExp using the passed in
 145       * Reader for instantiation.
 146       * 
 147       * @param object A Reader object providing the underlying stream.
 148       *               Must not be <code>null</code>.
 149       * 
 150       * @return object A new filter based on this configuration, but filtering
 151       *         the specified reader
 152       */
 153      function chain(Reader $reader) {
 154          $newFilter = new LineContainsRegExp($reader);
 155          $newFilter->setRegexps($this->getRegexps());
 156          $newFilter->setInitialized(true);
 157          $newFilter->setProject($this->getProject());        
 158          return $newFilter;
 159      }
 160  
 161      /**
 162       * Parses parameters to add user defined regular expressions.
 163       */
 164      private function _initialize() {
 165          $params = $this->getParameters();
 166          if ( $params !== null ) {
 167              for($i = 0 ; $i<count($params) ; $i++) {
 168                  if ( self::REGEXP_KEY === $params[$i]->getType() ) {
 169                      $pattern = $params[$i]->getValue();
 170                      $regexp = new RegularExpression();
 171                      $regexp->setPattern($pattern);
 172                      array_push($this->_regexps, $regexp);
 173                  }
 174              }
 175          }
 176      }
 177  }
 178  
 179  ?>


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