[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: TidyFilter.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/filters/ChainableReader.php'; 24 25 /** 26 * This filter uses the bundled-with-PHP Tidy extension to filter input. 27 * 28 * <p> 29 * Example:<br/> 30 * <pre> 31 * <tidyfilter encoding="utf8"> 32 * <config name="indent" value="true"/> 33 * <config name="output-xhtml" value="true"/> 34 * </tidyfilter> 35 * </pre> 36 * 37 * @author Hans Lellelid <hans@xmpl.org> 38 * @version $Revision: 1.2 $ $Date: 2005/12/08 19:15:20 $ 39 * @package phing.filters 40 */ 41 class TidyFilter extends BaseParamFilterReader implements ChainableReader { 42 43 /** @var string Encoding of resulting document. */ 44 private $encoding = 'utf8'; 45 46 /** @var array Parameter[] */ 47 private $configParameters = array(); 48 49 /** 50 * Set the encoding for resulting (X)HTML document. 51 * @param string $v 52 */ 53 public function setEncoding($v) { 54 $this->encoding = $v; 55 } 56 57 /** 58 * Sets the config params. 59 * @param array Parameter[] 60 * @see chain() 61 */ 62 public function setConfigParameters($params) 63 { 64 $this->configParameters = $params; 65 } 66 67 /** 68 * Adds a <config> element (which is a Parameter). 69 * @return Parameter 70 */ 71 public function createConfig() { 72 $num = array_push($this->configParameters, new Parameter()); 73 return $this->configParameters[$num-1]; 74 } 75 76 /** 77 * Converts the Parameter objects being used to store configuration into a simle assoc array. 78 * @return array 79 */ 80 private function getDistilledConfig() { 81 $config = array(); 82 foreach($this->configParameters as $p) { 83 $config[$p->getName()] = $p->getValue(); 84 } 85 return $config; 86 } 87 88 /** 89 * Reads input and returns Tidy-filtered output. 90 * 91 * @return the resulting stream, or -1 if the end of the resulting stream has been reached 92 * 93 * @throws IOException if the underlying stream throws an IOException 94 * during reading 95 */ 96 function read($len = null) { 97 98 if (!class_exists('Tidy')) { 99 throw new BuildException("You must enable the 'tidy' extension in your PHP configuration in order to use the Tidy filter."); 100 } 101 102 if ( !$this->getInitialized() ) { 103 $this->_initialize(); 104 $this->setInitialized(true); 105 } 106 107 $buffer = $this->in->read($len); 108 if($buffer === -1) { 109 return -1; 110 } 111 112 $config = $this->getDistilledConfig(); 113 114 $tidy = new Tidy(); 115 $tidy->parseString($buffer, $config, $this->encoding); 116 $tidy->cleanRepair(); 117 118 return tidy_get_output($tidy); 119 120 } 121 122 123 /** 124 * Creates a new TidyFilter using the passed in Reader for instantiation. 125 * 126 * @param reader A Reader object providing the underlying stream. 127 * Must not be <code>null</code>. 128 * 129 * @return a new filter based on this configuration, but filtering 130 * the specified reader 131 */ 132 public function chain(Reader $reader) { 133 $newFilter = new TidyFilter($reader); 134 $newFilter->setConfigParameters($this->configParameters); 135 $newFilter->setEncoding($this->encoding); 136 $newFilter->setProject($this->getProject()); 137 return $newFilter; 138 } 139 140 /** 141 * Initializes any parameters (e.g. config options). 142 * This method is only called when this filter is used through a <filterreader> tag in build file. 143 */ 144 private function _initialize() { 145 $params = $this->getParameters(); 146 if ($params) { 147 foreach($params as $param) { 148 if ($param->getType() == "config") { 149 $this->configParameters[] = $param; 150 } else { 151 152 if ($param->getName() == "encoding") { 153 $this->setEncoding($param->getValue()); 154 } 155 156 } 157 158 } 159 } 160 } 161 162 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |