001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.junit;
019:
020: import java.io.OutputStream;
021: import javax.xml.transform.Result;
022: import javax.xml.transform.Source;
023: import javax.xml.transform.Transformer;
024: import javax.xml.transform.TransformerFactory;
025: import javax.xml.transform.dom.DOMSource;
026: import javax.xml.transform.stream.StreamResult;
027: import javax.xml.transform.stream.StreamSource;
028:
029: import org.apache.tools.ant.BuildException;
030:
031: /**
032: * This class is not used by the framework any more.
033: * We plan to remove it in Ant 1.8
034: * @deprecated since Ant 1.7
035: *
036: *
037: * @ant.task ignore="true"
038: */
039: public class Xalan2Executor extends XalanExecutor {
040:
041: private static final String APAC = "org.apache.xalan.";
042: private static final String SPAC = "com.sun.org.apache.xalan.";
043:
044: private TransformerFactory tfactory = TransformerFactory
045: .newInstance();
046:
047: /** {@inheritDoc}. */
048: protected String getImplementation() throws BuildException {
049: return tfactory.getClass().getName();
050: }
051:
052: /** {@inheritDoc}. */
053: protected String getProcVersion(String classNameImpl)
054: throws BuildException {
055: try {
056: // xalan 2
057: if (classNameImpl.equals(APAC
058: + "processor.TransformerFactoryImpl")
059: || classNameImpl.equals(APAC
060: + "xslt.XSLTProcessorFactory")) {
061: return getXalanVersion(APAC
062: + "processor.XSLProcessorVersion");
063: }
064: // xalan xsltc
065: if (classNameImpl.equals(APAC
066: + "xsltc.trax.TransformerFactoryImpl")) {
067: return getXSLTCVersion(APAC + "xsltc.ProcessorVersion");
068: }
069: // jdk 1.5 xsltc
070: if (classNameImpl.equals(SPAC
071: + "internal.xsltc.trax.TransformerFactoryImpl")) {
072: return getXSLTCVersion(SPAC
073: + "internal.xsltc.ProcessorVersion");
074: }
075: throw new BuildException(
076: "Could not find a valid processor version"
077: + " implementation from " + classNameImpl);
078: } catch (ClassNotFoundException e) {
079: throw new BuildException(
080: "Could not find processor version "
081: + "implementation", e);
082: }
083: }
084:
085: /** {@inheritDoc}. */
086: void execute() throws Exception {
087: String systemId = caller.getStylesheetSystemId();
088: Source xslSrc = new StreamSource(systemId);
089: Transformer tformer = tfactory.newTransformer(xslSrc);
090: Source xmlSrc = new DOMSource(caller.document);
091: OutputStream os = getOutputStream();
092: try {
093: tformer.setParameter("output.dir", caller.toDir
094: .getAbsolutePath());
095: Result result = new StreamResult(os);
096: tformer.transform(xmlSrc, result);
097: } finally {
098: os.close();
099: }
100: }
101: }
|