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

   1  <?php
   2  
   3  /*
   4   *  $Id: StripLineComments.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/ChainableReader.php';
  25  
  26  /*
  27   * This filter strips line comments.
  28   *
  29   * Example:
  30   *
  31   * <pre><striplinecomments>
  32   *   <comment value="#"/>
  33   *   <comment value="--"/>
  34   *   <comment value="REM "/>
  35   *   <comment value="rem "/>
  36   *   <comment value="//"/>
  37   * </striplinecomments></pre>
  38   *
  39   * Or:
  40   *
  41   * <pre><filterreader classname="phing.filters.StripLineComments">
  42   *   <param type="comment" value="#"/>
  43   *   <param type="comment" value="--"/>
  44   *   <param type="comment" value="REM "/>
  45   *   <param type="comment" value="rem "/>
  46   *   <param type="comment" value="//"/>
  47   * </filterreader></pre>
  48   *
  49   * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  50   * @author    hans lellelid, hans@velum.net
  51   * @version   $Revision: 1.8 $ $Date: 2005/02/27 20:52:08 $
  52   * @access    public
  53   * @see       BaseParamFilterReader
  54   * @package   phing.filters
  55   */
  56  class StripLineComments extends BaseParamFilterReader implements ChainableReader {
  57      
  58      /** Parameter name for the comment prefix. */
  59      const COMMENTS_KEY = "comment";
  60      
  61      /** Array that holds the comment prefixes. */
  62      private $_comments = array();
  63      
  64      /**
  65       * Returns stream only including
  66       * lines from the original stream which don't start with any of the 
  67       * specified comment prefixes.
  68       * 
  69       * @return mixed the resulting stream, or -1
  70       *         if the end of the resulting stream has been reached.
  71       * 
  72       * @throws IOException if the underlying stream throws an IOException
  73       *            during reading     
  74       */
  75      function read($len = null) {
  76      
  77          if ( !$this->getInitialized() ) {
  78              $this->_initialize();
  79              $this->setInitialized(true);
  80          }
  81          
  82          $buffer = $this->in->read($len);
  83          
  84          if ($buffer === -1) {
  85              return -1;
  86          }
  87          
  88          $lines = explode("\n", $buffer);        
  89          $filtered = array();    
  90              
  91          $commentsSize = count($this->_comments);
  92          
  93          foreach($lines as $line) {            
  94              for($i = 0; $i < $commentsSize; $i++) {
  95                  $comment = $this->_comments[$i]->getValue();
  96                  if ( StringHelper::startsWith($comment, ltrim($line)) ) {
  97                      $line = null;
  98                      break;
  99                  }
 100              }
 101              if ($line !== null) {
 102                  $filtered[] = $line;
 103              }
 104          }
 105                  
 106          $filtered_buffer = implode("\n", $filtered);    
 107          return $filtered_buffer;
 108      }        
 109  
 110      /*
 111       * Adds a <code>comment</code> element to the list of prefixes.
 112       * 
 113       * @return comment The <code>comment</code> element added to the
 114       *                 list of comment prefixes to strip.
 115      */
 116      function createComment() {
 117          $num = array_push($this->_comments, new Comment());
 118          return $this->_comments[$num-1];
 119      }
 120  
 121      /*
 122       * Sets the list of comment prefixes to strip.
 123       * 
 124       * @param comments A list of strings, each of which is a prefix
 125       *                 for a comment line. Must not be <code>null</code>.
 126      */
 127      function setComments($lineBreaks) {
 128          if (!is_array($lineBreaks)) {
 129              throw new Exception("Excpected 'array', got something else");
 130          }
 131          $this->_comments = $lineBreaks;
 132      }
 133  
 134      /*
 135       * Returns the list of comment prefixes to strip.
 136       * 
 137       * @return array The list of comment prefixes to strip.
 138      */
 139      function getComments() {
 140          return $this->_comments;
 141      }
 142  
 143      /*
 144       * Creates a new StripLineComments using the passed in
 145       * Reader for instantiation.
 146       * 
 147       * @param reader A Reader object providing the underlying stream.
 148       *               Must not be <code>null</code>.
 149       * 
 150       * @return a new filter based on this configuration, but filtering
 151       *         the specified reader
 152       */
 153      function chain(Reader $reader) {
 154          $newFilter = new StripLineComments($reader);
 155          $newFilter->setComments($this->getComments());
 156          $newFilter->setInitialized(true);
 157          $newFilter->setProject($this->getProject());        
 158          return $newFilter;
 159      }
 160  
 161      /*
 162       * Parses the parameters to set the comment prefixes.
 163      */
 164      private function _initialize() {
 165          $params = $this->getParameters();
 166          if ( $params !== null ) {
 167              for($i = 0 ; $i<count($params) ; $i++) {
 168                  if ( self::COMMENTS_KEY === $params[$i]->getType() ) {
 169                      $comment = new Comment();
 170                      $comment->setValue($params[$i]->getValue());
 171                      array_push($this->_comments, $comment);
 172                  }
 173              }
 174          }
 175      }
 176  }
 177  
 178  /*
 179   * The class that holds a comment representation.
 180  */
 181  class Comment {
 182      
 183      /** The prefix for a line comment. */
 184      private    $_value;
 185  
 186      /*
 187       * Sets the prefix for this type of line comment.
 188       *
 189       * @param string $value The prefix for a line comment of this type.
 190       *                Must not be <code>null</code>.
 191       */
 192      function setValue($value) {
 193          $this->_value = (string) $value;
 194      }
 195  
 196      /*
 197       * Returns the prefix for this type of line comment.
 198       * 
 199       * @return string The prefix for this type of line comment.
 200      */
 201      function getValue() {
 202          return $this->_value;
 203      }
 204  }
 205  ?>


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