Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

In this page you can find the example usage for java.io StringWriter StringWriter.

Prototype

public StringWriter() 

Source Link

Document

Create a new string writer using the default initial string-buffer size.

Usage

From source file:Main.java

public static String prettifyXmlString(String uglyXml) {
    Transformer transformer = null;
    try {//w  w w .java2 s. c  o m
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = null;
    try {
        source = new DOMSource(string2Document(uglyXml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static StringBuffer transformToString(Source xmlSource, Source xslSource) {
    StringWriter writer = new StringWriter();
    Transformer transformer;// w w  w .  jav  a2 s.co  m
    try {
        if (xslSource == null) {
            transformer = TransformerFactory.newInstance().newTransformer();
        } else {
            transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlSource, new StreamResult(writer));
        return writer.getBuffer();
    } catch (Exception e) {
        e.printStackTrace();
        return writer.getBuffer();
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String toXMLString(Document doc) throws TransformerException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    // create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);/*ww  w  .j  av a2s.  co m*/
    String xmlString = sw.toString();
    return xmlString;
}

From source file:com.xyxy.platform.modules.extension.tools.FreeMarkers.java

/**
 * ?//from w w w  .  ja v  a2s .c  o  m
 */
public static String renderString(String templateString, Map<String, ?> model) {
    try {
        StringWriter result = new StringWriter();
        Template t = new Template("default", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

public static void saveXMLDocumentAsFile(Document doc, File file) {
    try {/*  www . jav  a 2s  .  c  o m*/
        // Indent & Doctype declaration
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        // Prepares to write
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);

        // Output to file
        result = new StreamResult(file);
        transformer.transform(source, result);
    } catch (Exception e) {
        System.out.println("Error on saving XML file.");
        e.printStackTrace();
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from  w ww. ja v  a  2 s .  c o  m*/
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String XMLToString(Document doc, Boolean singleLine) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/* w ww  .jav  a2 s .  c  o m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String string = writer.getBuffer().toString();
    if (!singleLine) {
        return string;
    }
    return string.replaceAll("\n|\r", "");

}

From source file:Main.java

public static String GetStringFromDoc(Document d) {
    /*/*from   ww  w.j  a va  2 s  . com*/
     DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation();
     LSSerializer lsSerializer = domImplementation.createLSSerializer();
            
     return lsSerializer.writeToString(d);
     */
    StringWriter output = new StringWriter();

    try {
        //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output));
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toString();
}

From source file:Main.java

public static String XMLDocumentToString(Document _doc) {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans;//from w  ww.ja v  a2 s.  com
    String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    try {
        trans = transfac.newTransformer();

        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(_doc);
        trans.transform(source, result);
        xmlString += sw.toString();
    } catch (TransformerConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    } catch (TransformerException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return xmlString;
}

From source file:Main.java

public static String pretty(String xml) {
    try {//from  w w w.jav  a  2  s .c om
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new StreamSource(new StringReader(xml));
        transformer.transform(source, result);
        return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n");
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}