Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:Main.java

public static String getStringFromXML(Node node, String dtdFilename) throws TransformerException {
    StringWriter sw = new StringWriter();
    getStringFromXML(node, dtdFilename, sw);
    return sw.getBuffer().toString();
}

From source file:Main.java

/**
 * Generates a XML string from DOM./*from   ww  w . ja  v a2 s. co  m*/
 * 
 * @param xmlDocument the DOM
 * @return XML string
 */
public static String build(Document xmlDocument) throws TransformerException {
    if (transformer == null) {
        TransformerFactory xformFactory = TransformerFactory.newInstance();
        try {
            xformFactory.setAttribute("indent-number", new Integer(4));
        } catch (IllegalArgumentException e) {
            // ignore
        }
        transformer = xformFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }
    StringWriter out = new StringWriter();
    transformer.transform(new DOMSource(xmlDocument), new StreamResult(out));
    return out.getBuffer().toString();
}

From source file:Main.java

public static String XMLtoString(Document doc) throws Exception {
    try {//from  w  w  w . j  av a2  s .c  om
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {//ww  w. j  a  v  a  2 s  .co m
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:com.streamreduce.util.CAGenerator.java

public static byte[] getCertificateAsBytes(final X509Certificate cert) throws IOException {
    StringWriter writer = new StringWriter();
    PEMWriter pemW = new PEMWriter(writer);
    pemW.writeObject(cert);//from  w ww.  j  av a 2 s. co m
    pemW.close();
    return writer.getBuffer().toString().getBytes();
}

From source file:scala.c24.demo.java.C24DemoUtils.java

public static String getDocumentAsString(Resource resource) throws Exception {

    Document document = getDocument(resource);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

/**
 * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
 * @param doc//from  w  w  w .  j a v a  2s .  c o m
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Object to XML/*  www . j  a va  2s. co  m*/
 * 
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        System.out.println(mMap.containsKey(object.getClass()) + "---mmap-----");
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            // marshaller.setProperty(CharacterEscapeHandler.class.getName(),
            // new CharacterEscapeHandler() {
            // public void escape(char[] ac, int i, int j, boolean
            // flag,Writer writer) throws IOException {
            // writer.write( ac, i, j ); }
            // });
            mMap.put(object.getClass(), marshaller);
        }
        System.out.println("----mmap--" + mMap.toString());
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.doculibre.constellio.utils.NamedListUtilsTest.java

private static String readFileAsString(File f) throws java.io.IOException {
    StringWriter sw = new StringWriter();
    IOUtils.copy(new FileReader(f), sw);
    return sw.getBuffer().toString();
}

From source file:Main.java

/**
 * method used to convert a xml document to a string
 * // www. j  av a 2  s .c o m
 * @param doc
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;

    try {
        /**
         * transformation of document happens here
         */
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();

        /**
         * logging success
         */
        logger.info("xml conversion to string  successful Class:XMLParserUtility line# 98");

        return output;

    } catch (TransformerException e) {
        e.printStackTrace();
        logger.fatal("Could not transform document to XML", e);
    }
    return null;
}