Example usage for javax.xml.transform TransformerFactory newTemplates

List of usage examples for javax.xml.transform TransformerFactory newTemplates

Introduction

In this page you can find the example usage for javax.xml.transform TransformerFactory newTemplates.

Prototype

public abstract Templates newTemplates(Source source) throws TransformerConfigurationException;

Source Link

Document

Process the Source into a Templates object, which is a a compiled representation of the source.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(new StreamSource(new FileInputStream("xsl.xlt")));
    Transformer xformer = template.newTransformer();
    Source source = new StreamSource(new FileInputStream("in.xml"));
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.newDocument();
    Result result = new DOMResult(doc);
    xformer.transform(source, result);//from   w w w.j  a  va  2 s .  c om
}

From source file:MainClass.java

public static void main(String argv[]) throws Exception {
    Properties props = System.getProperties();
    props.put("javax.xml.transform.TransformerFactory", "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
    System.setProperties(props);/*w  w  w  .j  a v a 2 s .  c o  m*/
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Templates translet = tFactory.newTemplates(new StreamSource("OrderProcessing.xslt"));

    Transformer transformer = translet.newTransformer();

    transformer.transform(new StreamSource("CustomerOrders.xml"),
            new StreamResult(new FileOutputStream("SortedOrders.html")));

    transformer.transform(new StreamSource("CustomerOrders1.xml"),
            new StreamResult(new FileOutputStream("SortedOrders1.html")));

}

From source file:JAXPTransletMultipleTransformations.java

public static void main(String argv[]) {
    // Set the TransformerFactory system property to generate and use translets.
    // Note: To make this sample more flexible, load properties from a properties file.
    // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
    String key = "javax.xml.transform.TransformerFactory";
    String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
    Properties props = System.getProperties();
    props.put(key, value);/* w w  w . j a v  a 2 s  . c  o  m*/

    System.setProperties(props);

    String xslInURI = "todo.xsl";

    try {
        // Instantiate the TransformerFactory, and use it along with a SteamSource
        // XSL stylesheet to create a translet as a Templates object.
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Templates translet = tFactory.newTemplates(new StreamSource(xslInURI));

        // Perform each transformation
        doTransform(translet, "todo.xml", "todo.html");
        System.out.println("Produced todo.html");

        doTransform(translet, "todotoo.xml", "todotoo.html");
        System.out.println("Produced todotoo.html");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {//  www. j  a  v  a 2s.  co  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
        Transformer xformer = template.newTransformer();
        Source source = new StreamSource(new FileInputStream(inFilename));
        Result result = new StreamResult(new FileOutputStream(outFilename));
        xformer.transform(source, result);
    } catch (FileNotFoundException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
        SourceLocator locator = e.getLocator();
        int col = locator.getColumnNumber();
        int line = locator.getLineNumber();
        String publicId = locator.getPublicId();
        String systemId = locator.getSystemId();
    }
}

From source file:Main.java

/**
 * Perform an xsl transformation//from w  w  w  . ja v  a  2  s.  co m
 */
public static void transform(Source xsl, Source xml, Result result) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(xsl);
    Transformer transformer = template.newTransformer();
    transformer.transform(xml, result);
}

From source file:Main.java

/**
 * @param xmlSource//from ww  w. j  a  v a  2 s .  co m
 * @param xslSource
 * @param resolver
 * @param output
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void formatXML(Source xmlSource, Source xslSource, URIResolver resolver, Result result)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates t = factory.newTemplates(xslSource);
    Transformer transformer = t.newTransformer();
    if (resolver != null) {
        transformer.setURIResolver(resolver);
    }
    transformer.transform(xmlSource, result);
}

From source file:Main.java

/**
 * @param xmlsource/*ww  w.j ava2s. c o  m*/
 * @param xsltfile
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, File xsltfile) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltfile);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param xmlsource//from   w w  w.  j  a va 2 s  .  co  m
 * @param xsltstream
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, InputStream xsltstream) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltstream);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

private static void formatXML(Source xmlSource, Source xslSource, Writer output) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates t = factory.newTemplates(xslSource);
    Transformer transformer = t.newTransformer();
    StreamResult result = new StreamResult(output);
    transformer.transform(xmlSource, result);
}

From source file:Main.java

public static Templates TrAXPath(String xpath) throws TransformerConfigurationException {
    StringBuffer sb = new StringBuffer();

    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    sb.append("<xsl:stylesheet version=\"1.0\" ");
    sb.append(" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">");
    sb.append("<xsl:output method=\"xml\" indent=\"yes\"/>");
    sb.append("<xsl:template match=\"" + xpath + "\">");
    sb.append("<xsl:copy-of select=\".\"/>");
    sb.append("</xsl:template>");
    sb.append("<xsl:template match=\"*|@*|text()\">");
    sb.append("<xsl:apply-templates />");
    sb.append("</xsl:template>");
    sb.append("</xsl:stylesheet>");

    TransformerFactory tf = TransformerFactory.newInstance();
    String stylesheet = sb.toString();
    Reader r = new StringReader(stylesheet);
    StreamSource ssrc = new StreamSource(r);

    return tf.newTemplates(ssrc);
}