Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:com.aerospike.examples.BatchUpdateTTL.java

/**
 * Write usage to console.//from  w w w.java  2 s .  c  o m
 */
private static void logUsage(Options options) {
    HelpFormatter formatter = new HelpFormatter();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    String syntax = BatchUpdateTTL.class.getName() + " [<options>]";
    formatter.printHelp(pw, 100, syntax, "options:", options, 0, 2, null);
    log.info(sw.toString());
}

From source file:Main.java

public static String getXMLString(Node 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.transform(domSource, result);
    return writer.toString();
}

From source file:com.progressiveaccess.cmlspeech.base.FileHandler.java

/**
 * Builds the CML XOM element./*from   w ww . ja v  a 2  s.com*/
 *
 * @param molecule
 *          The molecule to rewritten.
 *
 * @return The CML document.
 *
 * @throws IOException
 *           Problems with StringWriter
 * @throws CDKException
 *           Problems with CMLWriter
 * @throws ParsingException
 *           Problems with building CML XOM.
 */
public static Document buildXom(final IAtomContainer molecule)
        throws IOException, CDKException, ParsingException {
    final StringWriter outStr = new StringWriter();
    final CMLWriter cmlwriter = new CMLWriter(outStr);
    cmlwriter.write(molecule);
    cmlwriter.close();
    final String cmlcode = outStr.toString();

    final Builder builder = new CMLBuilder();
    // this.doc.getRootElement().addNamespaceDeclaration
    // ("cml", "http://www.xml-cml.org/schema");
    final Document doc = builder.build(cmlcode, "");
    Logger.logging(doc.toXML());
    return doc;
}

From source file:org.jenkinsci.plugins.uithemes.util.JSONReadWrite.java

public static <T> T fromRequest(StaplerRequest req, Class<T> to) throws IOException {
    String contentEncoding = req.getCharacterEncoding();
    StringWriter writer = new StringWriter();

    if (contentEncoding == null) {
        contentEncoding = "UTF-8";
    }/*  w  w  w . j a  va 2s .co m*/

    Util.copyStreamAndClose(new InputStreamReader(req.getInputStream(), contentEncoding), writer);

    return fromString(writer.toString(), to);
}

From source file:com.iisigroup.cap.utils.CapXmlUtil.java

/**
 * xml document ? string//www .j  av  a  2 s . c  o m
 * 
 * @param doc
 *            document
 * @param format
 *            format
 * @return String
 */
public static String convertDocumentToString(Document doc, boolean format) {
    StringWriter out = new StringWriter();
    try {
        new XMLWriter(out, new OutputFormat(Constants.EMPTY_STRING, format, CharEncoding.UTF_8)).write(doc);
        return out.toString();
    } catch (IOException e) {
        throw new CapMessageException(e, CapXmlUtil.class);
    }
}

From source file:de.unikiel.inf.comsys.neo4j.inference.SPARQLInferenceTest.java

private static String getResourceAsString(String name) throws IOException {
    InputStream in = getResource(name);
    if (in == null) {
        return null;
    }//w w w. jav a2  s .  c  om
    StringWriter writer = new StringWriter();
    IOUtils.copy(in, writer, "UTF-8");
    return writer.toString();
}

From source file:Main.java

/**
 * <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
 *
 * @param str                String to escape values in, may be null
 * @param escapeSingleQuotes escapes single quotes if <code>true</code>
 * @param escapeForwardSlash escapes forward slashes if <code>true</code>
 * @return the escaped string/*from  w  w  w  .jav a  2 s.c o  m*/
 */
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes,
        boolean escapeForwardSlash) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length() * 2);
        escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        throw new RuntimeException(ioe);
    }
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {//from   w  ww.j a  v a2 s  .  c om
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:be.fedict.eid.dss.sp.servlet.PkiServlet.java

private static String toPem(Object object) {

    StringWriter buffer = new StringWriter();
    try {//  w w w  . j  a  va 2  s. com
        PEMWriter writer = new PEMWriter(buffer);
        LOG.debug("toPem: " + object.getClass().getName());
        writer.writeObject(object);
        writer.close();
        return buffer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Cannot convert object to " + "PEM format: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(buffer);
    }
}

From source file:org.apache.hadoop.hbase.rest.TestSchemaResource.java

private static byte[] toXML(TableSchemaModel model) throws JAXBException {
    StringWriter writer = new StringWriter();
    context.createMarshaller().marshal(model, writer);
    return Bytes.toBytes(writer.toString());
}