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:it.cnr.ilc.ilcioutils.IlcInputToString.java

/**
 * Convert an inputstream into a string//from   w  ww  . ja  v a  2 s . c  o m
 *
 * @param is the inputstream
 * @return the string from the input
 */
public static String convertInputStreamToString(InputStream is) {
    StringWriter writer = new StringWriter();
    String encoding = "UTF-8";
    String message = "";
    String theString = "";
    try {
        IOUtils.copy(is, writer, encoding);
        theString = writer.toString();
    } catch (Exception e) {
        message = "IOException in coverting the stream into a string " + e.getMessage();
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }

    //System.err.println("DDDD " + theString);
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(writer);
    return theString;
}

From source file:Main.java

public static StringWriter getStringWriter(Document doc) throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, streamResult);
    return writer;
}

From source file:Main.java

/**
 * Return the absolute path to the hub database.
 *
 * @return path The path to the db as a string
 *///w  ww .  ja  v a  2s. c  o  m
private static String getDbPath() {
    if (Debug)
        Log.i(TAG, "getDBPath() called.");

    String path = Environment.getExternalStorageDirectory().getPath() + "/" + BASE_DIR;

    File dbDir = new File(path);
    if (!dbDir.isDirectory()) {
        try {
            if (Debug)
                Log.i(TAG, "Trying to create " + path);
            dbDir.mkdirs();
        } catch (Exception e) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            Log.e(TAG, result.toString());
        }
    }
    return path;
}

From source file:Main.java

/**
 * method used to convert a xml document to a string
 * //from www . j a  v a 2 s  . c  om
 * @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;
}

From source file:Main.java

/**
 * Transform a org.w3c.dom.Node to a String
 * @param node - the Node//from  ww  w. ja v  a  2 s.  c o m
 * @param omitXmlDeclaration - omit XML declaration
 * @param prettyPrint - apply indentation
 * @return the String result
 * @throws TransformerException
 */
public static String elementToString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) {
    String result = null;
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        if (prettyPrint) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        result = buffer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static String marshal(Object object) throws JAXBException {
    StringWriter sw = new StringWriter();
    marshal(object, sw);//from w  w  w.  j av  a 2s.  c  o m
    return sw.toString();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);//from   www.j  a  v  a  2s  .c  om
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:Main.java

public static String documentToString(Document doc) throws TransformerException {
    // Configure the transformer
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    // Transform the document
    DOMSource source = new DOMSource(doc);
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    transformer.transform(source, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}