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:Main.java

/**
 * Process a w3c XML document into a Java String.
 * /*w  w  w .ja va 2s .c  om*/
 * @param outputDoc
 * @return
 */
public static String printToString(Document doc) {
    try {
        doc.normalizeDocument();
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception e) {
        // e.printStackTrace();
        return null;
    }
}

From source file:org.fcrepo.indexer.IndexerGroupIT.java

private static TextMessage getMessage(String operation, String pid) throws Exception {
    Abdera abdera = new Abdera();

    Entry entry = abdera.newEntry();/*from   www .  j ava 2s. c om*/
    entry.setTitle(operation, TEXT).setBaseUri(serverAddress);
    entry.addCategory("xsd:string", pid, "fedora-types:pid");
    entry.addCategory("xsd:string", "/" + pid, "path");
    entry.setContent("contentds");
    StringWriter writer = new StringWriter();
    entry.writeTo(writer);

    String atomMessage = writer.toString();

    TextMessage msg = mock(TextMessage.class);
    when(msg.getText()).thenReturn(atomMessage);
    return msg;
}

From source file:org.ojbc.util.helper.HttpUtils.java

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload//from w w  w .  ja va  2s. com
 * @param url
 * @return the http response
 * @throws Exception
 */
public static String post(String payload, String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(payload, Consts.UTF_8));
    HttpResponse response = client.execute(post);
    HttpEntity reply = response.getEntity();
    StringWriter sw = new StringWriter();
    BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent()));
    String line;
    while ((line = isr.readLine()) != null) {
        sw.append(line);
    }
    sw.close();
    return sw.toString();
}

From source file:Main.java

public static String transformXmlToString(Document importPackageDocument)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {//w  w  w  .j  a  va2s .c o m
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    javax.xml.transform.Result result = new StreamResult(writer);
    Source source = new DOMSource(importPackageDocument);
    transformer.transform(source, result);
    writer.close();
    String xml = writer.toString();
    return xml;
}

From source file:csiro.pidsvc.helper.Http.java

/**
 * Return specific HTTP response code and add a message to HTTP header.
 * //from  www. ja  v  a  2  s . co  m
 * @param response HttpServletResponse object.
 * @param httpResponseCode HTTP response code.
 * @param exception Exception object.
 */
public static void returnErrorCode(HttpServletResponse response, int httpResponseCode, Exception exception) {
    try {
        String message = (exception.getMessage() == null ? "" : exception.getMessage())
                + (exception.getCause() == null ? "" : "\n" + exception.getCause().getMessage());

        StringWriter sw = new StringWriter();
        exception.printStackTrace(new PrintWriter(sw));

        response.addHeader(PID_SERVICE_ERROR_HTTP_HEADER, message);
        response.addHeader(PID_SERVICE_MSG_HTTP_HEADER, sw.toString());
        response.sendError(httpResponseCode, exception.getMessage());
    } catch (IOException e) {
        _logger.debug(e);
    }
}

From source file:com.jaeksoft.searchlib.util.ExceptionUtils.java

public final static String getFullStackTrace(StackTraceElement[] stackTrace) {
    StringWriter sw = null;
    PrintWriter pw = null;//  www.j a  va 2 s.c om
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        for (StackTraceElement element : stackTrace)
            pw.println(element);
        return sw.toString();
    } finally {
        IOUtils.close(pw, sw);
    }
}

From source file:com.cloudbees.workflow.rest.external.FlowNodeLogExt.java

public static FlowNodeLogExt create(FlowNode node) {
    FlowNodeLogExt logExt = new FlowNodeLogExt();

    logExt.setNodeId(node.getId());/* w  ww .j av  a2 s  .c  om*/
    logExt.setNodeStatus(StatusExt.valueOf(node.getError()));

    LogAction logAction = node.getAction(LogAction.class);
    if (logAction != null) {
        logExt.setConsoleUrl(ModelUtil.getFullItemUrl(node) + logAction.getUrlName());

        AnnotatedLargeText<? extends FlowNode> logText = logAction.getLogText();
        if (logText != null) {
            long logLen = logText.length();

            logExt.setLength(Math.min(MAX_RETURN_CHARS, logLen));
            logExt.setHasMore((logLen > MAX_RETURN_CHARS));

            if (logLen > 0) {
                StringWriter writer = new StringWriter();
                try {
                    logText.writeHtmlTo(logLen - logExt.getLength(), writer);
                    logExt.setText(writer.toString());
                } catch (IOException e) {
                    LOGGER.log(Level.SEVERE, "Error serializing log for", e);
                }
            }
        }
    }

    return logExt;
}

From source file:com.openforevent.main.UserPosition.java

public static Map<String, Object> userPositionByIP(DispatchContext ctx, Map<String, ? extends Object> context)
        throws IOException {
    URL url = new URL("http://api.ipinfodb.com/v3/ip-city/?" + "key=" + ipinfodb_key + "&" + "&ip=" + default_ip
            + "&format=xml");
    //URL url = new URL("http://ipinfodb.com/ip_query.php");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    // Set properties of the connection
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoInput(true);//from  w  w  w .  j ava2 s .  c o  m
    urlConnection.setDoOutput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    // Retrieve the output
    int responseCode = urlConnection.getResponseCode();
    InputStream inputStream;
    if (responseCode == HttpURLConnection.HTTP_OK) {
        inputStream = urlConnection.getInputStream();
    } else {
        inputStream = urlConnection.getErrorStream();
    }

    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    String theString = writer.toString();

    Debug.logInfo("Get user position by IP stream is = ", theString);

    Map<String, Object> paramOut = FastMap.newInstance();
    paramOut.put("stream", theString);
    return paramOut;
}

From source file:Main.java

public static String getStringFromNode(Node node) throws Exception {

    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    String xml = writer.toString();

    return xml;//from  ww  w  .j  a va2 s.  c om
}

From source file:com.cprassoc.solr.auth.util.Utils.java

public static String streamToString(InputStream in) {
    String result = "";
    try {//from   w  ww.ja va 2 s . c om
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        result = writer.toString();
    } catch (Exception e) {
        //e.printStackTrace();
    }
    return result;
}