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

   1  <?php
   2  
   3  /*
   4   *  $Id: LineContains.php 3076 2006-12-18 08:52:12Z fabien $
   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://phing.info>.
  21   */
  22  
  23  include_once 'phing/filters/BaseParamFilterReader.php';
  24  include_once 'phing/filters/BaseFilterReader.php';
  25  include_once 'phing/filters/ChainableReader.php';
  26  
  27  /**
  28   * Filter which includes only those lines that contain all the user-specified
  29   * strings.
  30   *
  31   * Example:
  32   *
  33   * <pre><linecontains>
  34   *   <contains value="foo">
  35   *   <contains value="bar">
  36   * </linecontains></pre>
  37   *
  38   * Or:
  39   *
  40   * <pre><filterreader classname="phing.filters.LineContains">
  41   *    <param type="contains" value="foo"/>
  42   *    <param type="contains" value="bar"/>
  43   * </filterreader></pre>
  44   *
  45   * This will include only those lines that contain <code>foo</code> and
  46   * <code>bar</code>.
  47   *
  48   * @author    Yannick Lecaillez <yl@seasonfive.com>
  49   * @author    Hans Lellelid <hans@velum.net>
  50   * @version   $Revision: 1.11 $
  51   * @see       PhingFilterReader
  52   * @package   phing.filters
  53  */
  54  class LineContains extends BaseParamFilterReader implements ChainableReader {
  55  
  56      /**
  57       * The parameter name for the string to match on.
  58       * @var string
  59       */ 
  60      const CONTAINS_KEY = "contains";
  61  
  62      /**
  63       * Array of Contains objects.
  64       * @var array
  65       */ 
  66      private $_contains = array();
  67  
  68      /**
  69       * [Deprecated] 
  70       * @var string
  71       */ 
  72      private $_line = null;
  73  
  74      /**
  75       * Returns all lines in a buffer that contain specified strings.
  76       * @return mixed buffer, -1 on EOF
  77       */
  78      function read($len = null) {
  79          if ( !$this->getInitialized() ) {
  80              $this->_initialize();
  81              $this->setInitialized(true);
  82          }
  83          
  84          $buffer = $this->in->read($len);
  85          
  86          if ($buffer === -1) {
  87              return -1;
  88          }
  89          
  90          $lines = explode("\n", $buffer);        
  91          $matched = array();        
  92          $containsSize = count($this->_contains);
  93          
  94          foreach($lines as $line) {                                
  95              for($i = 0 ; $i < $containsSize ; $i++) {
  96                  $containsStr = $this->_contains[$i]->getValue();
  97                  if ( strstr($line, $containsStr) === false ) {
  98                      $line = null;
  99                      break;
 100                  }
 101              }                
 102              if($line !== null) {
 103                  $matched[] = $line;
 104              }                
 105          }        
 106          $filtered_buffer = implode("\n", $matched);    
 107          return $filtered_buffer;
 108      }
 109      
 110      /**
 111       * [Deprecated. For reference only, used to be read() method.] 
 112       * Returns the next character in the filtered stream, only including
 113       * lines from the original stream which contain all of the specified words.
 114       *
 115       * @return the next character in the resulting stream, or -1
 116       * if the end of the resulting stream has been reached
 117       *
 118       * @exception IOException if the underlying stream throws an IOException
 119       * during reading
 120       */
 121      function readChar() {
 122          if ( !$this->getInitialized() ) {
 123              $this->_initialize();
 124              $this->setInitialized(true);
 125          }
 126  
 127          $ch = -1;
 128  
 129          if ( $this->_line !== null ) {
 130              $ch = substr($this->_line, 0, 1);
 131              if ( strlen($this->_line) === 1 )
 132                  $this->_line = null;
 133              else
 134                  $this->_line = substr($this->_line, 1);
 135          } else {
 136              $this->_line = $this->readLine();
 137              if ( $this->_line === null ) {
 138                  $ch = -1;
 139              } else {
 140                  $containsSize = count($this->_contains);
 141                  for($i = 0 ; $i < $containsSize ; $i++) {
 142                      $containsStr = $this->_contains[$i]->getValue();
 143                      if ( strstr($this->_line, $containsStr) === false ) {
 144                          $this->_line = null;
 145                          break;
 146                      }
 147                  }
 148                  return $this->readChar();
 149              }
 150          }
 151  
 152          return $ch;
 153      }
 154  
 155      /**
 156       * Adds a <code><contains></code> nested element.
 157       *
 158       * @return Contains The <code>contains</code> element added.
 159       *                  Must not be <code>null</code>.
 160       */
 161      function createContains() {
 162          $num = array_push($this->_contains, new Contains());
 163          return $this->_contains[$num-1];
 164      }
 165  
 166      /**
 167       * Sets the array of words which must be contained within a line read
 168       * from the original stream in order for it to match this filter.
 169       *
 170       * @param array $contains An array of words which must be contained
 171       *                 within a line in order for it to match in this filter.
 172       *                 Must not be <code>null<code>.
 173       */
 174      function setContains($contains) {
 175          // type check, error must never occur, bad code of it does
 176          if ( !is_array($contains) ) {
 177              throw new Exception("Excpected array got something else");
 178          }
 179  
 180          $this->_contains = $contains;
 181      }
 182  
 183      /**
 184       * Returns the vector of words which must be contained within a line read
 185       * from the original stream in order for it to match this filter.
 186       *
 187       * @return array The array of words which must be contained within a line read
 188       *         from the original stream in order for it to match this filter. The
 189       *         returned object is "live" - in other words, changes made to the
 190       *         returned object are mirrored in the filter.
 191       */
 192      function getContains() {
 193          return $this->_contains;
 194      }
 195  
 196      /**
 197       * Creates a new LineContains using the passed in
 198       * Reader for instantiation.
 199       *
 200       * @param object A Reader object providing the underlying stream.
 201       *               Must not be <code>null</code>.
 202       *
 203       * @return object A new filter based on this configuration, but filtering
 204       *         the specified reader
 205       */
 206      function chain(Reader $reader) {
 207          $newFilter = new LineContains($reader);
 208          $newFilter->setContains($this->getContains());
 209          $newFilter->setInitialized(true);
 210          $newFilter->setProject($this->getProject());        
 211          return $newFilter;
 212      }
 213  
 214      /**
 215       * Parses the parameters to add user-defined contains strings.
 216       */
 217      private function _initialize() {
 218          $params = $this->getParameters();
 219          if ( $params !== null ) {
 220              foreach($params as $param) {
 221                  if ( self::CONTAINS_KEY == $param->getType() ) {
 222                      $cont = new Contains();
 223                      $cont->setValue($param->getValue());
 224                      array_push($this->_contains, $cont);
 225                      break; // because we only support a single contains
 226                  }
 227              }
 228          }
 229      }
 230  }
 231  
 232  /**
 233   * Holds a contains element.
 234   */
 235  class Contains {
 236  
 237      /**
 238       * @var string
 239       */ 
 240      private $_value;
 241      
 242      /**
 243       * Set 'contains' value.
 244       * @param string $contains
 245       */ 
 246      function setValue($contains) {
 247          $this->_value = (string) $contains;
 248      }
 249      
 250      /**
 251       * Returns 'contains' value.
 252       * @return string
 253       */ 
 254      function getValue() {
 255          return $this->_value;
 256      }
 257  }
 258  ?>


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