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 javax.xml.transform.Result;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerFactory;
  23. import javax.xml.transform.dom.DOMSource;
  24. import javax.xml.transform.stream.StreamResult;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.tools.ant.BuildException;
  27. /**
  28. * Xalan executor via JAXP. Nothing special must exists in the classpath
  29. * besides of course, a parser, jaxp and xalan.
  30. *
  31. * @ant.task ignore="true"
  32. */
  33. public class Xalan2Executor extends XalanExecutor {
  34. private static final String aPack = "org.apache.xalan.";
  35. private static final String sPack = "com.sun.org.apache.xalan.";
  36. private TransformerFactory tfactory = TransformerFactory.newInstance();
  37. protected String getImplementation() throws BuildException {
  38. return tfactory.getClass().getName();
  39. }
  40. protected String getProcVersion(String classNameImpl)
  41. throws BuildException {
  42. try {
  43. // xalan 2
  44. if (classNameImpl.equals(aPack + "processor.TransformerFactoryImpl")
  45. ||
  46. classNameImpl.equals(aPack + "xslt.XSLTProcessorFactory")) {
  47. return getXalanVersion(aPack + "processor.XSLProcessorVersion");
  48. }
  49. // xalan xsltc
  50. if (classNameImpl.equals(aPack
  51. + "xsltc.trax.TransformerFactoryImpl")){
  52. return getXSLTCVersion(aPack +"xsltc.ProcessorVersion");
  53. }
  54. // jdk 1.5 xsltc
  55. if (classNameImpl
  56. .equals(sPack + "internal.xsltc.trax.TransformerFactoryImpl")){
  57. return getXSLTCVersion(sPack
  58. + "internal.xsltc.ProcessorVersion");
  59. }
  60. throw new BuildException("Could not find a valid processor version"
  61. + " implementation from "
  62. + classNameImpl);
  63. } catch (ClassNotFoundException e){
  64. throw new BuildException("Could not find processor version "
  65. + "implementation", e);
  66. }
  67. }
  68. void execute() throws Exception {
  69. String system_id = caller.getStylesheetSystemId();
  70. Source xsl_src = new StreamSource(system_id);
  71. Transformer tformer = tfactory.newTransformer(xsl_src);
  72. Source xml_src = new DOMSource(caller.document);
  73. OutputStream os = getOutputStream();
  74. try {
  75. tformer.setParameter("output.dir", caller.toDir.getAbsolutePath());
  76. Result result = new StreamResult(os);
  77. tformer.transform(xml_src, result);
  78. } finally {
  79. os.close();
  80. }
  81. }
  82. }