1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.junit;
  18. import java.io.OutputStream;
  19. import org.apache.xalan.xslt.XSLTInputSource;
  20. import org.apache.xalan.xslt.XSLTProcessor;
  21. import org.apache.xalan.xslt.XSLTProcessorFactory;
  22. import org.apache.xalan.xslt.XSLTResultTarget;
  23. import org.apache.tools.ant.BuildException;
  24. import org.xml.sax.SAXException;
  25. /**
  26. * Xalan 1 executor. It will need a lot of things in the classpath:
  27. * xerces for the serialization, xalan and bsf for the extension.
  28. * @todo do everything via reflection to avoid compile problems ?
  29. *
  30. * @ant.task ignore="true"
  31. */
  32. public class Xalan1Executor extends XalanExecutor {
  33. private static final String xsltP = "org.apache.xalan.xslt.XSLTProcessor";
  34. XSLTProcessor processor = null;
  35. public Xalan1Executor() {
  36. try {
  37. processor = XSLTProcessorFactory.getProcessor();
  38. } catch (SAXException e) {
  39. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  40. }
  41. }
  42. protected String getImplementation() {
  43. return processor.getClass().getName();
  44. }
  45. protected String getProcVersion(String classNameImpl)
  46. throws BuildException {
  47. try {
  48. // xalan 1
  49. if (classNameImpl.equals(xsltP)){
  50. return getXalanVersion(xsltP + "Version");
  51. }
  52. throw new BuildException("Could not find a valid processor version"
  53. + " implementation from "
  54. + classNameImpl);
  55. } catch (ClassNotFoundException e){
  56. throw new BuildException("Could not find processor version "
  57. + "implementation", e);
  58. }
  59. }
  60. void execute() throws Exception {
  61. // need to quote otherwise it breaks because of "extra illegal tokens"
  62. processor.setStylesheetParam("output.dir", "'"
  63. + caller.toDir.getAbsolutePath() + "'");
  64. XSLTInputSource xml_src = new XSLTInputSource(caller.document);
  65. String system_id = caller.getStylesheetSystemId();
  66. XSLTInputSource xsl_src = new XSLTInputSource(system_id);
  67. OutputStream os = getOutputStream();
  68. try {
  69. XSLTResultTarget target = new XSLTResultTarget(os);
  70. processor.process(xml_src, xsl_src, target);
  71. } finally {
  72. os.close();
  73. }
  74. }
  75. }