1 18 package org.apache.tools.ant.taskdefs.optional.junit; 19 20 import java.io.OutputStream ; 21 import javax.xml.transform.Result ; 22 import javax.xml.transform.Source ; 23 import javax.xml.transform.Transformer ; 24 import javax.xml.transform.TransformerFactory ; 25 import javax.xml.transform.dom.DOMSource ; 26 import javax.xml.transform.stream.StreamResult ; 27 import javax.xml.transform.stream.StreamSource ; 28 29 import org.apache.tools.ant.BuildException; 30 31 39 public class Xalan2Executor extends XalanExecutor { 40 41 private static final String APAC = "org.apache.xalan."; 42 private static final String SPAC = "com.sun.org.apache.xalan."; 43 44 private TransformerFactory tfactory = TransformerFactory.newInstance(); 45 46 47 protected String getImplementation() throws BuildException { 48 return tfactory.getClass().getName(); 49 } 50 51 52 protected String getProcVersion(String classNameImpl) 53 throws BuildException { 54 try { 55 if (classNameImpl.equals(APAC + "processor.TransformerFactoryImpl") 57 || 58 classNameImpl.equals(APAC + "xslt.XSLTProcessorFactory")) { 59 return getXalanVersion(APAC + "processor.XSLProcessorVersion"); 60 } 61 if (classNameImpl.equals(APAC 63 + "xsltc.trax.TransformerFactoryImpl")) { 64 return getXSLTCVersion(APAC + "xsltc.ProcessorVersion"); 65 } 66 if (classNameImpl 68 .equals(SPAC + "internal.xsltc.trax.TransformerFactoryImpl")) { 69 return getXSLTCVersion(SPAC 70 + "internal.xsltc.ProcessorVersion"); 71 } 72 throw new BuildException("Could not find a valid processor version" 73 + " implementation from " 74 + classNameImpl); 75 } catch (ClassNotFoundException e) { 76 throw new BuildException("Could not find processor version " 77 + "implementation", e); 78 } 79 } 80 81 82 void execute() throws Exception { 83 String systemId = caller.getStylesheetSystemId(); 84 Source xslSrc = new StreamSource (systemId); 85 Transformer tformer = tfactory.newTransformer(xslSrc); 86 Source xmlSrc = new DOMSource (caller.document); 87 OutputStream os = getOutputStream(); 88 try { 89 tformer.setParameter("output.dir", caller.toDir.getAbsolutePath()); 90 Result result = new StreamResult (os); 91 tformer.transform(xmlSrc, result); 92 } finally { 93 os.close(); 94 } 95 } 96 } 97 | Popular Tags |