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 readAsString(@NonNull InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
    copy(reader, writer);//from  w  w  w  .  java2  s.  c  om
    return writer.toString();
}

From source file:Main.java

public static String DocumentToStr(Document doc) throws TransformerException {

    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();

}

From source file:Main.java

public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);/*from ww  w.j  av a 2  s  .c o m*/
    String xmlString = sw.toString();

    return xmlString;
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*w  w w .  j  a v a 2s .  c  o m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

From source file:Main.java

public static String formatXmlAsStringNoWhitespace(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {//  www  .  j  a v a2 s  . c  om

        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as no-whitespace string", ex);
    }

    return writer.toString();
}

From source file:Main.java

public static String formatXmlAsString(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {//from w  ww . j  a  v  a  2s  . c o m

        factory.setAttribute("indent-number", new Integer(2));
    } catch (IllegalArgumentException ex) {

    }

    try {

        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as pretty-printed string", ex);
    }

    return writer.toString();
}

From source file:Main.java

public static String nodeToString(Node noh) throws TransformerException {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    StringWriter wr = new StringWriter();
    StreamResult result = new StreamResult(wr);

    t.transform(new DOMSource(noh), result);
    return wr.toString();
}

From source file:Main.java

protected static Result internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace,
        String[] params) {// w w w  .  ja v  a  2s.  c om
    StringWriter sw = new StringWriter();

    try {
        Transformer transformer = templates.newTransformer();
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++)
                transformer.setParameter("param_" + i, params[i]);
        }

        transformer.transform(new StreamSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }

}

From source file:de.uni.bremen.monty.moco.Main.java

public static void main(String[] args) throws IOException, InterruptedException {
    Namespace ns = parseArgs(args);

    if (ns == null) {
        return;//from  w  w  w  .j av a 2 s  .  c  o  m
    }

    String inputFileName = ns.get("file");
    String outputFileName = ns.get("outputFile");
    boolean emitAssembly = ns.get("emitAssembly");
    boolean compileOnly = ns.get("compileOnly");
    boolean stopOnFirstError = ns.get("stopOnFirstError");
    boolean printAST = ns.get("printAST");
    boolean debugParseTree = ns.get("debugParseTree");

    if (debugParseTree) {
        debugParseTree(inputFileName);
        return;
    }

    StringWriter writer = new StringWriter();
    IOUtils.copy(Main.class.getResourceAsStream("/std_llvm_include.ll"), writer);

    Package ast = buildPackage(inputFileName);

    if (!visitVisitors(ast, stopOnFirstError, writer.getBuffer())) {
        return;
    }

    if (printAST) {
        (new PrintVisitor()).visitDoubleDispatched(ast);
    }

    if (emitAssembly) {
        writeAssembly(outputFileName, inputFileName, writer.toString());
        return;
    }

    File executable = buildExecutable(outputFileName, inputFileName, compileOnly, writer.toString());
    if (!compileOnly) {
        runExecutable(executable);
    }
}

From source file:Main.java

public static <T> String asXml(T object) {
    try {/*from   w ww .  j a va  2  s .  c  o  m*/
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}