Example usage for javax.xml.transform TransformerFactory newTransformer

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

Introduction

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

Prototype

public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException;

Source Link

Document

Process the Source into a Transformer Object .

Usage

From source file:UseStylesheetPI.java

public static void main(String[] args) throws TransformerException, TransformerConfigurationException {
    String media = null, title = null, charset = null;
    try {//  www  .ja  va  2 s.c  om
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Source stylesheet = tFactory.getAssociatedStylesheet(new StreamSource("fooX.xml"), media, title,
                charset);

        Transformer transformer = tFactory.newTransformer(stylesheet);

        transformer.transform(new StreamSource("fooX.xml"),
                new StreamResult(new java.io.FileOutputStream("foo.out")));

        System.out.println("************* The result is in foo.out *************");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:JAXPTransform.java

public static void main(String args[])
        throws TransformerConfigurationException, TransformerException, FileNotFoundException {

    TransformerFactory factory = TransformerFactory.newInstance();

    StreamSource stylesheet = new StreamSource(args[1]);
    StreamSource xmlDoc = new StreamSource(args[0]);
    StreamResult result = new StreamResult(new FileOutputStream(args[2]));

    Transformer transFormer = factory.newTransformer(stylesheet);

    transFormer.transform(xmlDoc, result);

}

From source file:JAXPTransformNode.java

public static void main(String args[]) throws Exception, TransformerException, FileNotFoundException {

    TransformerFactory factory = TransformerFactory.newInstance();

    DOMSource stylesheet = new DOMSource(buildDoc(args[1]));
    StreamSource xmlDoc = new StreamSource(args[0]);
    StreamResult result = new StreamResult(new FileOutputStream(args[2]));

    Transformer transFormer = factory.newTransformer(stylesheet);

    transFormer.transform(xmlDoc, result);

}

From source file:org.jasig.portlet.maps.tools.MapDataTransformer.java

/**
 * @param args//from w  ww  .j  ava2  s .  c o  m
 */
public static void main(String[] args) {

    // translate the KML file to the map portlet's native data format
    File kml = new File("map-data.xml");
    File xslt = new File("google-earth.xsl");

    try {
        TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(new StreamSource(xslt));
        trans.transform(new StreamSource(kml), new StreamResult(System.out));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    // deserialize the map data from XML
    MapData data = null;
    try {
        JAXBContext jc = JAXBContext.newInstance(MapData.class);
        Unmarshaller u = jc.createUnmarshaller();
        data = (MapData) u.unmarshal(new FileInputStream(new File("map-data-transformed.xml")));
    } catch (JAXBException e1) {
        e1.printStackTrace();
        return;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }

    // ensure each location has a unique, non-null abbreviation
    setAbbreviations(data);

    // update each location with an address
    //        setAddresses(data);

    // sort locations by name
    Collections.sort(data.getLocations(), new ByNameLocationComparator());

    // serialize new map data out to a file into JSON format
    try {
        mapper.defaultPrettyPrintingWriter().writeValue(new File("map.json"), data);
    } catch (JsonGenerationException e) {
        System.out.println("Error generating JSON data for map");
        e.printStackTrace();
    } catch (JsonMappingException e) {
        System.out.println("Error generating JSON data for map");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Error writing JSON data to map file");
        e.printStackTrace();
    }

}

From source file:SimpleTransform.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
    // Use the static TransformerFactory.newInstance() method to instantiate 
    // a TransformerFactory. The javax.xml.transform.TransformerFactory 
    // system property setting determines the actual class to instantiate --
    // org.apache.xalan.transformer.TransformerImpl.
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to instantiate a Transformer that will work with  
    // the stylesheet you specify. This method call also processes the stylesheet
    // into a compiled Templates object.
    Transformer transformer = tFactory.newTransformer(new StreamSource("birds.xsl"));

    // Use the Transformer to apply the associated Templates object to an XML document
    // (foo.xml) and write the output to a file (foo.out).
    transformer.transform(new StreamSource("birds.xml"), new StreamResult(new FileOutputStream("birds.out")));

    System.out.println("************* The result is in birds.out *************");
}

From source file:ExternalConnection.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {

    // Create a connection to the database server
    // Up the connection pool count for testing
    DefaultConnectionPool cp = new DefaultConnectionPool();
    cp.setDriver("org.apache.derby.jdbc.EmbeddedDriver");
    cp.setURL("jdbc:derby:sampleDB");
    //cp.setUser("sa");
    //cp.setPassword("");
    cp.setMinConnections(10);//  w  w w.  jav a 2 s .c  o m
    cp.setPoolEnabled(true);

    // Now let's register our connection pool so we can use
    // in a stylesheet
    ConnectionPoolManager pm = new ConnectionPoolManager();
    pm.registerPool("extpool", cp);

    // Use the static TransformerFactory.newInstance() method to instantiate
    // a TransformerFactory. The javax.xml.transform.TransformerFactory
    // system property setting determines the actual class to instantiate --
    // org.apache.xalan.transformer.TransformerImpl.
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Grab the Name of the Stylesheet from the commad line
    if (args.length == 0) {
        System.out.println("You must provide the path and name to a stylesheet to process");
        System.exit(0);
    }

    String stylesheet = args[0];
    System.out.println("Transforming Stylesheet " + stylesheet);

    // Use the TransformerFactory to instantiate a Transformer that will work with
    // the stylesheet you specify. This method call also processes the stylesheet
    // into a compiled Templates object.
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));

    // For this transformation, all the required information is in the stylesheet, so generate 
    // a minimal XML source document for the input.
    // Note: the command-line processor (org.apache.xalan.xslt.Process) uses this strategy when 
    // the user does not provide an -IN parameter.
    StringReader reader = new StringReader("<?xml version=\"1.0\"?> <doc/>");

    // Use the Transformer to apply the associated Templates object to an XML document
    // and write the output to a file.
    transformer.transform(new StreamSource(reader), new StreamResult(new FileOutputStream("dbtest-out.html")));

    System.out.println("************* The result is in dbtest-out.html *************");

    cp.setPoolEnabled(false);
}

From source file:DOM2DOM.java

public static void main(String[] args) throws TransformerException, TransformerConfigurationException,
        FileNotFoundException, ParserConfigurationException, SAXException, IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();

    if (tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)) {
        //Instantiate a DocumentBuilderFactory.
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

        // And setNamespaceAware, which is required when parsing xsl files
        dFactory.setNamespaceAware(true);

        //Use the DocumentBuilderFactory to create a DocumentBuilder.
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

        //Use the DocumentBuilder to parse the XSL stylesheet.
        Document xslDoc = dBuilder.parse("birds.xsl");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xslDomSource = new DOMSource(xslDoc);

        // Set the systemId: note this is actually a URL, not a local filename
        xslDomSource.setSystemId("birds.xsl");

        // Process the stylesheet DOMSource and generate a Transformer.
        Transformer transformer = tFactory.newTransformer(xslDomSource);

        //Use the DocumentBuilder to parse the XML input.
        Document xmlDoc = dBuilder.parse("birds.xml");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xmlDomSource = new DOMSource(xmlDoc);

        // Set the base URI for the DOMSource so any relative URIs it contains can
        // be resolved.
        xmlDomSource.setSystemId("birds.xml");

        // Create an empty DOMResult for the Result.
        DOMResult domResult = new DOMResult();

        // Perform the transformation, placing the output in the DOMResult.
        transformer.transform(xmlDomSource, domResult);

        //Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out
        // using the default output format, except for indent="yes"
        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(System.out);
        serializer.asDOMSerializer().serialize(domResult.getNode());
    } else {/*w ww .  ja  v  a  2  s.c om*/
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

From source file:Main.java

public static void transform(Document src, Writer dst, String xsl) throws TransformerException {

    DOMSource dsrc = new DOMSource(src);
    StreamResult res = new StreamResult(dst);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(new StreamSource(xsl));
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(dsrc, res);//  w w w . ja v  a  2s.  c  o m

}

From source file:Main.java

public static String transform(String xml, File stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String transform(String xml, String stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}