View Javadoc

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