PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/xslt/XSLTransformationFilter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 149 lines | 54 code | 20 blank | 75 comment | 8 complexity | cb14fd5a36c4f4a84bd3bcde6ed00378 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/XSLTransformationFilter.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class XSLTransformationFilter
  9. * @ingroup xslt
  10. *
  11. * @brief Class that transforms XML via XSL.
  12. */
  13. // $Id$
  14. import('filter.Filter');
  15. import('xslt.XSLTransformer');
  16. class XSLTransformationFilter extends Filter {
  17. /** @var DOMDocument|string either an XSL string or an XSL DOMDocument */
  18. var $_xsl;
  19. /** @var integer */
  20. var $_xslType;
  21. /** @var integer */
  22. var $_resultType;
  23. //
  24. // Getters and Setters
  25. //
  26. /**
  27. * Get the XSL
  28. * @return DOMDocument|string a document, xsl string or file name
  29. */
  30. function &getXSL() {
  31. return $this->_xsl;
  32. }
  33. /**
  34. * Get the XSL Type
  35. * @return integer
  36. */
  37. function getXSLType() {
  38. return $this->_xslType;
  39. }
  40. /**
  41. * Set the XSL
  42. * @param $xsl DOMDocument|string
  43. */
  44. function setXSL(&$xsl) {
  45. // Determine the xsl type
  46. if (is_string($xsl)) {
  47. $this->_xslType = XSL_TRANSFORMER_DOCTYPE_STRING;
  48. } elseif (is_a($xsl, 'DOMDocument')) {
  49. $this->_xslType = XSL_TRANSFORMER_DOCTYPE_DOM;
  50. } else assert(false);
  51. $this->_xsl =& $xsl;
  52. }
  53. /**
  54. * Set the XSL as a file name
  55. * @param unknown_type $xslFile
  56. */
  57. function setXSLFilename($xslFile) {
  58. $this->_xslType = XSL_TRANSFORMER_DOCTYPE_FILE;
  59. $this->_xsl = $xslFile;
  60. }
  61. /**
  62. * Get the result type
  63. * @return integer
  64. */
  65. function getResultType() {
  66. return $this->_resultType;
  67. }
  68. /**
  69. * Set the result type
  70. * @param $resultType integer
  71. */
  72. function setResultType($resultType) {
  73. $this->_resultType = $resultType;
  74. }
  75. //
  76. // Implement template methods from Filter
  77. //
  78. /**
  79. * We support either an XML string or a DOMDocument
  80. * as input and / or output.
  81. * @see Filter::supports()
  82. * @param $input mixed
  83. * @param $output mixed
  84. */
  85. function supports(&$input, &$output) {
  86. // Check input type
  87. if (!$this->_isValidXML($input)) return false;
  88. // Check output type
  89. if (is_null($output)) return true;
  90. return $this->_isValidXML($output);
  91. }
  92. /**
  93. * Process the given XML with the configured XSL
  94. * @see Filter::process()
  95. * @param $xml DOMDocument|string
  96. * @return DOMDocument|string
  97. */
  98. function &process(&$xml) {
  99. // Determine the input type
  100. if (is_string($xml)) {
  101. $xmlType = XSL_TRANSFORMER_DOCTYPE_STRING;
  102. } elseif (is_a($xml, 'DOMDocument')) {
  103. $xmlType = XSL_TRANSFORMER_DOCTYPE_DOM;
  104. } else assert(false);
  105. // Determine the result type based on
  106. // the input type if it has not been
  107. // set explicitly.
  108. if (is_null($this->_resultType)) {
  109. $this->_resultType = $xmlType;
  110. }
  111. // Transform the input
  112. $xslTransformer = new XSLTransformer();
  113. $result =& $xslTransformer->transform($xml, $xmlType, $this->_xsl, $this->_xslType, $this->_resultType);
  114. return $result;
  115. }
  116. //
  117. // Private helper methods
  118. //
  119. /**
  120. * Checks whether this is either a DOMDocument or a
  121. * string and whether the input combines with the XSL.
  122. * @param $input mixed
  123. * @return boolean
  124. */
  125. function _isValidXML(&$xml) {
  126. return (is_a($xml, 'DOMDocument') || is_string($xml));
  127. }
  128. }
  129. ?>