Example usage for java.io StringWriter flush

List of usage examples for java.io StringWriter flush

Introduction

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

Prototype

public void flush() 

Source Link

Document

Flush the stream.

Usage

From source file:Main.java

/**
 * Helper method to serialize an object to XML. Requires object to be Serializable
 * @param entity Object to serialize//from  w  ww .j  a  va  2 s. c o m
 * @param <T> class that implements Serializable
 * @return String XML representation of object
 * @throws IOException if errors during serialization
 * @throws JAXBException if errors during serialization
 */
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException {
    StringWriter sw = new StringWriter();

    if (null != entity) {
        JAXBContext ctx = JAXBContext.newInstance(entity.getClass());

        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(entity, sw);
    }
    sw.flush();
    sw.close();

    return sw.toString();
}

From source file:Main.java

public static String getStackTrace(Exception e) {
    if (e == null) {
        return "";
    }/*from   w ww . j av a  2  s.  c o m*/
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:Main.java

public static String getStackTrace(@Nullable Throwable e) {
    if (e == null) {
        return "";
    }//from  ww  w .  j  a va2s  .  com
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:Main.java

/**
 * Serialize XML Document to string using Transformer
 * //w ww.j av  a2 s .co m
 * @param doc XML Document
 * @return String representation of the the Document
 * @throws IOException
 */
public static String serializeToString(Node node, String encoding) throws IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IOException("Unable to serialize XML document");
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    DOMSource source = new DOMSource(node);
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Unable to serialize XML document");
    }
    writer.flush();

    return writer.toString();
}

From source file:org.apache.hise.utils.DOMUtils.java

/**
 * Convert a DOM node to a stringified XML representation.
 */// w w  w  .  jav  a  2  s .  co  m
static public String domToString(Node node) {
    if (node == null) {
        throw new IllegalArgumentException("Cannot stringify null Node!");
    }

    String value = null;
    short nodeType = node.getNodeType();
    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.DOCUMENT_NODE) {
        // serializer doesn't handle Node type well, only Element
        DOMSerializerImpl ser = new DOMSerializerImpl();
        ser.setParameter(Constants.DOM_NAMESPACES, Boolean.TRUE);
        ser.setParameter(Constants.DOM_WELLFORMED, Boolean.FALSE);
        ser.setParameter(Constants.DOM_VALIDATE, Boolean.FALSE);

        // create a proper XML encoding header based on the input document;
        // default to UTF-8 if the parent document's encoding is not accessible
        String usedEncoding = "UTF-8";
        Document parent = node.getOwnerDocument();
        if (parent != null) {
            String parentEncoding = parent.getXmlEncoding();
            if (parentEncoding != null) {
                usedEncoding = parentEncoding;
            }
        }

        // the receiver of the DOM
        DOMOutputImpl out = new DOMOutputImpl();
        out.setEncoding(usedEncoding);

        // we write into a String
        StringWriter writer = new StringWriter(4096);
        out.setCharacterStream(writer);

        // out, ye characters!
        ser.write(node, out);
        writer.flush();

        // finally get the String
        value = writer.toString();
    } else {
        value = node.getNodeValue();
    }
    return value;
}

From source file:com.netsteadfast.greenstep.util.Pivot4JUtils.java

public static String rendererHtml(String mondrianUrl, String mdx, boolean showDimensionTitle,
        boolean showParentMembers) throws Exception {
    if (StringUtils.isBlank(mondrianUrl) || StringUtils.isBlank(mdx)) {
        throw new java.lang.IllegalArgumentException("mondrian url and MDX cannot blank.");
    }// www  .  j a  v  a  2 s.c o  m
    String body = "";
    OlapConnection connection = OlapUtils.getConnection(mondrianUrl);
    OlapDataSource dataSource = new SingleConnectionOlapDataSource(connection);
    try {
        PivotModel model = getPivotModel(dataSource, mdx);
        TableRenderer renderer = getTableRenderer(showDimensionTitle, showParentMembers);
        StringWriter writer = new StringWriter();
        renderer.render(model, new HtmlRenderCallback(writer));

        //writer.write(body);
        body = writer.toString();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        throw e;
    } finally {
        OlapUtils.nullConnection(connection);
        connection = null;
        dataSource = null;
    }
    return body;
}

From source file:org.eclipse.gyrex.common.console.CommandUtil.java

static void printCommandHelp(final CommandInterpreter ci, final String name, final String command,
        final CmdLineParser parser) {
    final StringWriter stringWriter = new StringWriter();
    stringWriter.append(String.format("%s %s", name, command));
    parser.printSingleLineUsage(stringWriter, null);
    stringWriter.append(SystemUtils.LINE_SEPARATOR);
    parser.printUsage(stringWriter, null);
    stringWriter.flush();
    ci.println(stringWriter.toString());
}

From source file:Main.java

public static String toXml(Class className, Object object) {
    String strXml = "";
    StringWriter writer = null;
    try {/*from  www  .j  a v a 2 s  . c o  m*/
        writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(className);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, writer);
        strXml = writer.toString();
        writer.flush();

        strXml = strXml.replace("&lt;", "<");
        strXml = strXml.replace("&gt;", ">");
    } catch (Exception e) {
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (Exception e) {
            }
        }
    }
    return strXml;
}

From source file:Main.java

public static String getStringFromElement(Element doc) throws TransformerException {

    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    // transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, result);
    writer.flush();
    return writer.toString();
}

From source file:Main.java

/**
 * Get DOM as a string/*from w  ww  .jav a  2 s  .co m*/
 * @param doc
 * @return
 */
public static String getStringFromDoc(org.w3c.dom.Document doc) {
    if (doc == null) {
        System.out.println("XML document is null");
    }
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://commons.omniupdate.com/dtd/standard.dtd");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        System.out.println("Transformer Exception");
        // ex.printStackTrace();
        return "Error in transformation";
    }
}