Example usage for javax.xml.transform.stream StreamResult StreamResult

List of usage examples for javax.xml.transform.stream StreamResult StreamResult

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult StreamResult.

Prototype

public StreamResult(File f) 

Source Link

Document

Construct a StreamResult from a File.

Usage

From source file:Trace.java

public static void main(String[] args) throws java.io.IOException, TransformerException,
        TransformerConfigurationException, java.util.TooManyListenersException, org.xml.sax.SAXException {
    String fileName = "foo";
    if (args.length > 0)
        fileName = args[0];//from   w  w w . java 2s  .  c o  m

    // Set up a PrintTraceListener object to print to a file.
    java.io.FileWriter fw = new java.io.FileWriter("events.log");
    java.io.PrintWriter pw = new java.io.PrintWriter(fw, true);
    PrintTraceListener ptl = new PrintTraceListener(pw);

    // Print information as each node is 'executed' in the stylesheet.
    ptl.m_traceElements = true;
    // Print information after each result-tree generation event.
    ptl.m_traceGeneration = true;
    // Print information after each selection event.
    ptl.m_traceSelection = true;
    // Print information whenever a template is invoked.
    ptl.m_traceTemplates = true;
    // Print information whenever an extension call is made.
    ptl.m_traceExtension = true;

    // Set up the transformation    
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(fileName + ".xsl"));

    // Cast the Transformer object to TransformerImpl.
    if (transformer instanceof TransformerImpl) {
        TransformerImpl transformerImpl = (TransformerImpl) transformer;
        // Register the TraceListener with a TraceManager associated 
        // with the TransformerImpl.
        TraceManager trMgr = transformerImpl.getTraceManager();
        trMgr.addTraceListener(ptl);

        // Perform the transformation --printing information to
        // the events log during the process.
        transformer.transform(new StreamSource(fileName + ".xml"),
                new StreamResult(new java.io.FileWriter(fileName + ".out")));
    }
    // Close the PrintWriter and FileWriter.
    pw.close();
    fw.close();
    System.out.println("**The output is in " + fileName + ".out; the log is in events.log ****");

}

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. j a v a 2s.com
    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:com.arsdigita.util.parameter.ParameterPrinter.java

public static final void main(final String[] args) throws IOException {

    CommandLine line = null;//from w w w  .  j  a v a2  s .  com
    try {
        line = new PosixParser().parse(OPTIONS, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }

    String[] outFile = line.getArgs();
    if (outFile.length != 1) {
        System.out.println("Usage: ParameterPrinter [--html] [--file config-list-file] output-file");
        System.exit(1);
    }
    if (line.hasOption("usage")) {
        System.out.println("Usage: ParameterPrinter [--html] [--file config-list-file] output-file");
        System.exit(0);
    }

    if (line.hasOption("file")) {
        String file = line.getOptionValue("file");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String configClass;
            while ((configClass = reader.readLine()) != null) {
                register(configClass);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    } else {
        register("com.arsdigita.runtime.RuntimeConfig");
        register("com.arsdigita.web.WebConfig");
        register("com.arsdigita.templating.TemplatingConfig");
        register("com.arsdigita.kernel.KernelConfig");
        register("com.arsdigita.kernel.security.SecurityConfig");
        register("com.arsdigita.mail.MailConfig");
        register("com.arsdigita.versioning.VersioningConfig");
        register("com.arsdigita.search.SearchConfig");
        register("com.arsdigita.search.lucene.LuceneConfig");
        register("com.arsdigita.kernel.security.SecurityConfig");
        register("com.arsdigita.bebop.BebopConfig");
        register("com.arsdigita.dispatcher.DispatcherConfig");
        register("com.arsdigita.workflow.simple.WorkflowConfig");
        register("com.arsdigita.cms.ContentSectionConfig");
    }

    if (line.hasOption("html")) {
        final StringWriter sout = new StringWriter();
        final PrintWriter out = new PrintWriter(sout);

        writeXML(out);

        final XSLTemplate template = new XSLTemplate(
                ParameterPrinter.class.getResource("ParameterPrinter_html.xsl"));

        final Source source = new StreamSource(new StringReader(sout.toString()));
        final Result result = new StreamResult(new File(outFile[0]));

        template.transform(source, result);
    } else {
        final PrintWriter out = new PrintWriter(new FileWriter(outFile[0]));

        writeXML(out);
    }
}

From source file:dk.defxws.fedoragsearch.server.GTransformer.java

public static void main(String[] args) {
    int argCount = 2;
    try {//w  w w  . j  a  v a 2  s  .c  o m
        if (args.length == argCount) {
            File f = new File(args[1]);
            StreamSource ss = new StreamSource(new File(args[1]));
            GTransformer gt = new GTransformer();
            StreamResult destStream = new StreamResult(new StringWriter());
            gt.transform(args[0], ss, destStream);
            StringWriter sw = (StringWriter) destStream.getWriter();
            System.out.print(sw.getBuffer().toString());
        } else {
            throw new IOException("Must supply " + argCount + " arguments.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Usage: GTransformer xsltName xmlFileName");
    }
}

From source file:Main.java

static void transforming(DOMSource source, File file) throws Exception {
    StreamResult console = new StreamResult(System.out);
    StreamResult fileResult = new StreamResult(file);
    transformer.transform(source, console);
    transformer.transform(source, fileResult);
}

From source file:Main.java

/**
 * Creates a Result from an OutputStream.
 * @param os the outputStream//from   w  ww .  java 2s  .c  o m
 * @return a Result
 */
public static Result createResult(OutputStream os) {
    return new StreamResult(os);
}

From source file:Main.java

public static void DocumentToStream(final Document doc, OutputStream stream) {
    Result l_s = new StreamResult(stream);

    doc.normalize();/*w w  w  .  ja va 2 s.c o m*/

    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);
        stream.close();
    } catch (Exception e) {
        System.err.println(e);

        return;
    }
}

From source file:Main.java

public static String getXml(Document doc) {
    DOMSource doms = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    String xml = null;// w  w  w. j  av  a  2s  .c  o  m
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "GB2312");
        properties.setProperty(OutputKeys.METHOD, "xml");//!
        properties.setProperty(OutputKeys.VERSION, "1.0");
        properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        String dtd = doc.getDoctype().getInternalSubset();
        if ((null != dtd) && (dtd.length() > 0)) {
            dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n";
        }
        ;
        xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd;
        xml += sw.toString();
    } catch (TransformerConfigurationException tce) {
        //"Transformer Configuration Exception\n-----"
    } catch (TransformerException te) {
        //"Transformer Exception\n---------"
    }
    return xml;
}

From source file:Main.java

public static String transform(String xmlFilePath, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new File(xmlFilePath));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//from   w  w  w. j a  v a2s  . c om

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static void createSVGFile(File file, Document document) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Result svgOutput = new StreamResult(file);
    transformer.transform(new DOMSource(document), svgOutput);
}