PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/filters/XsltFilter.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 341 lines | 147 code | 49 blank | 145 comment | 17 complexity | de186837b143a92bc3bb298de3d88406 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: XsltFilter.php 420 2008-10-26 19:21:39Z alexeyshockov $
  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. include_once 'phing/filters/BaseParamFilterReader.php';
  22. include_once 'phing/filters/ChainableReader.php';
  23. /**
  24. * Applies XSL stylesheet to incoming text.
  25. *
  26. * Uses PHP XSLT support (libxslt).
  27. *
  28. * @author Hans Lellelid <hans@velum.net>
  29. * @author Yannick Lecaillez <yl@seasonfive.com>
  30. * @author Andreas Aderhold <andi@binarycloud.com>
  31. * @version $Revision$
  32. * @see FilterReader
  33. * @package phing.filters
  34. */
  35. class XsltFilter extends BaseParamFilterReader implements ChainableReader {
  36. /**
  37. * Path to XSL stylesheet.
  38. * @var string
  39. */
  40. private $xslFile = null;
  41. /**
  42. * Whether XML file has been transformed.
  43. * @var boolean
  44. */
  45. private $processed = false;
  46. /**
  47. * XSLT Params.
  48. * @var array
  49. */
  50. private $xsltParams = array();
  51. /**
  52. * Whether to use loadHTML() to parse the input XML file.
  53. */
  54. private $html = false;
  55. /**
  56. * Create new XSLT Param object, to handle the <param/> nested element.
  57. * @return XSLTParam
  58. */
  59. function createParam() {
  60. $num = array_push($this->xsltParams, new XSLTParam());
  61. return $this->xsltParams[$num-1];
  62. }
  63. /**
  64. * Sets the XSLT params for this class.
  65. * This is used to "clone" this class, in the chain() method.
  66. * @param array $params
  67. */
  68. function setParams($params) {
  69. $this->xsltParams = $params;
  70. }
  71. /**
  72. * Returns the XSLT params set for this class.
  73. * This is used to "clone" this class, in the chain() method.
  74. * @return array
  75. */
  76. function getParams() {
  77. return $this->xsltParams;
  78. }
  79. /**
  80. * Set the XSLT stylesheet.
  81. * @param mixed $file PhingFile object or path.
  82. */
  83. function setStyle(PhingFile $file) {
  84. $this->xslFile = $file;
  85. }
  86. /**
  87. * Whether to use HTML parser for the XML.
  88. * This is supported in libxml2 -- Yay!
  89. * @return boolean
  90. */
  91. function getHtml() {
  92. return $this->html;
  93. }
  94. /**
  95. * Whether to use HTML parser for XML.
  96. * @param boolean $b
  97. */
  98. function setHtml($b) {
  99. $this->html = (boolean) $b;
  100. }
  101. /**
  102. * Get the path to XSLT stylesheet.
  103. * @return mixed XSLT stylesheet path.
  104. */
  105. function getStyle() {
  106. return $this->xslFile;
  107. }
  108. /**
  109. * Reads stream, applies XSLT and returns resulting stream.
  110. * @return string transformed buffer.
  111. * @throws BuildException - if XSLT support missing, if error in xslt processing
  112. */
  113. function read($len = null) {
  114. if (!class_exists('XSLTProcessor')) {
  115. throw new BuildException("Could not find the XSLTProcessor class. Make sure PHP has been compiled/configured to support XSLT.");
  116. }
  117. if ($this->processed === true) {
  118. return -1; // EOF
  119. }
  120. if ( !$this->getInitialized() ) {
  121. $this->_initialize();
  122. $this->setInitialized(true);
  123. }
  124. // Read XML
  125. $_xml = null;
  126. while ( ($data = $this->in->read($len)) !== -1 )
  127. $_xml .= $data;
  128. if ($_xml === null ) { // EOF?
  129. return -1;
  130. }
  131. if(empty($_xml)) {
  132. $this->log("XML file is empty!", Project::MSG_WARN);
  133. return ''; // return empty string, don't attempt to apply XSLT
  134. }
  135. // Read XSLT
  136. $_xsl = null;
  137. $xslFr = new FileReader($this->xslFile);
  138. $xslFr->readInto($_xsl);
  139. $this->log("Tranforming XML " . $this->in->getResource() . " using style " . $this->xslFile->getPath(), Project::MSG_VERBOSE);
  140. $out = '';
  141. try {
  142. $out = $this->process($_xml, $_xsl);
  143. $this->processed = true;
  144. } catch (IOException $e) {
  145. throw new BuildException($e);
  146. }
  147. return $out;
  148. }
  149. // {{{ method _ProcessXsltTransformation($xml, $xslt) throws BuildException
  150. /**
  151. * Try to process the XSLT transformation
  152. *
  153. * @param string XML to process.
  154. * @param string XSLT sheet to use for the processing.
  155. *
  156. * @throws BuildException On XSLT errors
  157. */
  158. protected function process($xml, $xsl) {
  159. $processor = new XSLTProcessor();
  160. $xmlDom = new DOMDocument();
  161. $xslDom = new DOMDocument();
  162. if ($this->html) {
  163. $xmlDom->loadHTML($xml);
  164. } else {
  165. $xmlDom->loadXML($xml);
  166. }
  167. $xslDom->loadxml($xsl);
  168. $processor->importStylesheet($xslDom);
  169. // ignoring param "type" attrib, because
  170. // we're only supporting direct XSL params right now
  171. foreach($this->xsltParams as $param) {
  172. $this->log("Setting XSLT param: " . $param->getName() . "=>" . $param->getExpression(), Project::MSG_DEBUG);
  173. $processor->setParameter(null, $param->getName(), $param->getExpression());
  174. }
  175. $errorlevel = error_reporting();
  176. error_reporting($errorlevel & ~E_WARNING);
  177. @$result = $processor->transformToXML($xmlDom);
  178. error_reporting($errorlevel);
  179. if (false === $result) {
  180. //$errno = xslt_errno($processor);
  181. //$err = xslt_error($processor);
  182. throw new BuildException("XSLT Error");
  183. } else {
  184. return $result;
  185. }
  186. }
  187. /**
  188. * Creates a new XsltFilter using the passed in
  189. * Reader for instantiation.
  190. *
  191. * @param Reader A Reader object providing the underlying stream.
  192. * Must not be <code>null</code>.
  193. *
  194. * @return Reader A new filter based on this configuration, but filtering
  195. * the specified reader
  196. */
  197. function chain(Reader $reader) {
  198. $newFilter = new XsltFilter($reader);
  199. $newFilter->setProject($this->getProject());
  200. $newFilter->setStyle($this->getStyle());
  201. $newFilter->setInitialized(true);
  202. $newFilter->setParams($this->getParams());
  203. $newFilter->setHtml($this->getHtml());
  204. return $newFilter;
  205. }
  206. /**
  207. * Parses the parameters to get stylesheet path.
  208. */
  209. private function _initialize() {
  210. $params = $this->getParameters();
  211. if ( $params !== null ) {
  212. for($i = 0, $_i=count($params) ; $i < $_i; $i++) {
  213. if ( $params[$i]->getType() === null ) {
  214. if ($params[$i]->getName() === "style") {
  215. $this->setStyle($params[$i]->getValue());
  216. }
  217. } elseif ($params[$i]->getType() == "param") {
  218. $xp = new XSLTParam();
  219. $xp->setName($params[$i]->getName());
  220. $xp->setExpression($params[$i]->getValue());
  221. $this->xsltParams[] = $xp;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * Class that holds an XSLT parameter.
  229. */
  230. class XSLTParam {
  231. private $name;
  232. private $expr;
  233. /**
  234. * Sets param name.
  235. * @param string $name
  236. */
  237. public function setName($name) {
  238. $this->name = $name;
  239. }
  240. /**
  241. * Get param name.
  242. * @return string
  243. */
  244. public function getName() {
  245. return $this->name;
  246. }
  247. /**
  248. * Sets expression value (alias to the setExpression()) method.
  249. *
  250. * @param string $v
  251. * @see setExpression()
  252. */
  253. public function setValue($v)
  254. {
  255. $this->setExpression($v);
  256. }
  257. /**
  258. * Gets expression value (alias to the getExpression()) method.
  259. *
  260. * @param string $v
  261. * @see getExpression()
  262. */
  263. public function getValue()
  264. {
  265. return $this->getExpression();
  266. }
  267. /**
  268. * Sets expression value.
  269. * @param string $expr
  270. */
  271. public function setExpression($expr) {
  272. $this->expr = $expr;
  273. }
  274. /**
  275. * Sets expression to dynamic register slot.
  276. * @param RegisterSlot $expr
  277. */
  278. public function setListeningExpression(RegisterSlot $expr) {
  279. $this->expr = $expr;
  280. }
  281. /**
  282. * Returns expression value -- performs lookup if expr is registerslot.
  283. * @return string
  284. */
  285. public function getExpression() {
  286. if ($this->expr instanceof RegisterSlot) {
  287. return $this->expr->getValue();
  288. } else {
  289. return $this->expr;
  290. }
  291. }
  292. }