Compiler from XSLT to Java bytecode. Allows faster application of XSL transformations as compared to classic XSL.
import java.net.URL; import java.util.Properties; import java.io.FileInputStream; import java.io.FileOutputStream; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.xalan.xsltc.compiler.XSLTC; import org.xml.sax.SAXException; public class TransletUtil { // Return Fully Qualified Translet Classname as generated public static String compileXSL(String styleSheet, String transletClassName) { String compiledClass = null; if ((transletClassName == null || transletClassName.length() == 0) || (styleSheet == null || styleSheet.length() == 0)) { return compiledClass; } try { XSLTC xsltc = new XSLTC(); xsltc.init(); xsltc.setClassName(transletClassName); xsltc.setDestDirectory("C:\\Projects\\TransletsTest\\src"); // xsltc.setPackageName("xsl.translet"); boolean compileResult = xsltc.compile((new FileInputStream(styleSheet)), transletClassName); if(compileResult) { compiledClass = xsltc.getClassName(); System.out.println("COMPILE: Translet Class " + compiledClass + " generated. " + compileResult); } } catch(Exception e) { e.printStackTrace(); } return compiledClass; } public static void transformByTranslet(String xmlInput, String outputFile, String transletClassName) { init(); if ((transletClassName == null || transletClassName.length() == 0) || (xmlInput == null || xmlInput.length() == 0)) { return; } else { try { TransformerFactory tf = TransformerFactory.newInstance(); try { tf.setAttribute("use-classpath", Boolean.TRUE); tf.setAttribute("auto-translet", Boolean.TRUE); tf.setAttribute("debug", Boolean.TRUE); } catch (IllegalArgumentException iae) { System.err.println("TRANSFORM: Could not set XSLTC-specific TransformerFactory " + "attributes. Transformation failed."); } System.out.println("TRANSFORM: Translet Class: " + transletClassName); Transformer transformer = tf.newTransformer(new StreamSource(transletClassName)); // Do the actual transformation final long start = System.currentTimeMillis(); transformer.transform(new StreamSource(xmlInput), new StreamResult(new FileOutputStream(outputFile))); final long done = System.currentTimeMillis() - start; System.out.println("TRANSFORM: Transformed by XSLTC in " + done +" msecs."); } catch(Exception e) { e.printStackTrace(); } } } public static void transformByXSL (String xmlInput, String outputFile, String xslInput) { try { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(xslInput)); transformer.transform(new StreamSource(xmlInput), new StreamResult(new FileOutputStream(outputFile))); System.out.println("The result output file created is: " + outputFile); } catch(Exception e) { e.printStackTrace(); } } public static void init() { System.out.println("Setting TRAX property."); String key = "javax.xml.transform.TransformerFactory"; String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; Properties props = System.getProperties(); props.put(key, value); System.setProperties(props); } public static void main(String[] args) { if(args.length < 4) { System.out.println("Usage XmlUtil XmlInput XmlOutput XslInput TransletClassName"); } // Usage XmlUtil XmlInput XmlOutput XslInput TransletClassName String xmlInput = args[0]; String xmlOutput = args[1]; String xslInput = args[2]; String transletClass = args[3]; String compiledClass = compileXSL(xslInput, transletClass); if(compiledClass != null) { transformByXSL(xmlInput, xmlOutput + "1", xslInput); // transformByTranslet(xmlInput, xmlOutput, "xsl.translet." + transletClass); // Or transformByTranslet(xmlInput, xmlOutput, compiledClass); } } }