Example usage for java.io StringWriter append

List of usage examples for java.io StringWriter append

Introduction

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

Prototype

public StringWriter append(char c) 

Source Link

Document

Appends the specified character to this writer.

Usage

From source file:com.redhat.refarch.microservices.trigger.service.TriggerService.java

private static URIBuilder getUriBuilder(Object... path) {

    URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http");
    uriBuilder.setHost("gateway-service");
    uriBuilder.setPort(9091);/*w  w w.  ja  v a2s.c  om*/

    StringWriter stringWriter = new StringWriter();
    for (Object part : path) {
        stringWriter.append('/').append(String.valueOf(part));
    }
    uriBuilder.setPath(stringWriter.toString());
    return uriBuilder;
}

From source file:Main.java

static String escape(String input) {
    StringWriter output = new StringWriter(input.length() * 2);
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '"') {
            output.append("&quot;");
        } else if (c == '\'') {
            output.append("&apos;");
        } else if (c == '<') {
            output.append("&lt;");
        } else if (c == '>') {
            output.append("&gt;");
        } else if (c == '&') {
            output.append("&amp;");
        } else {/*from   w  w  w .j a v a  2 s.co m*/
            output.append(c);
        }
    }
    return output.toString();
}

From source file:io.coala.config.YamlUtil.java

/**
 * @param tree the root {@link JsonNode} of the value tree
 * @return the YAML {@link String}/*w ww.  j ava 2  s . com*/
 * @throws IOException
 */
public static String toYAML(final String comment, final JsonNode tree) {
    final StringWriter writer = new StringWriter();
    if (comment != null && !comment.isEmpty())
        writer.append(toComment(comment));
    try {
        getYamlMapper().writer().writeValue(writer, tree);
    } catch (final IOException e) {
        Thrower.rethrowUnchecked(e);
    }
    return writer.toString();
}

From source file:Main.java

public static String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;/*  w ww  .j a  va  2s  .  c o  m*/
    try {
        transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        buffer.append("\n");
        return buffer.toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.mpg.escidoc.services.cone.util.RdfHelper.java

/**
 * Formats an List&lt;Pair&gt; into an RDF list.
 * //  w ww . j  av  a  2  s  .  c o  m
 * @param pairs A list of key-value pairs
 * 
 * @return The RDF
 */
public static String formatList(List<? extends Describable> pairs, Model model) {

    StringWriter result = new StringWriter();

    result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    result.append("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
            + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");

    if (model.getRdfAboutTag() != null && model.getRdfAboutTag().getNamespaceURI() != null
            && !model.getRdfAboutTag().getNamespaceURI().equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))

    {
        result.append(" xmlns:" + model.getRdfAboutTag().getPrefix() + "=\""
                + model.getRdfAboutTag().getNamespaceURI() + "\"");
    }

    result.append(">\n");
    if (pairs != null) {

        for (Describable pair : pairs) {
            if (pair instanceof Pair) {
                String key = ((Pair) pair).getKey();
                try {
                    result.append("\t<rdf:Description rdf:about=\""
                            + PropertyReader.getProperty("escidoc.cone.service.url") + key.replace("\"", "\\\"")
                            + "\">\n");
                    if (((Pair) pair).getValue() instanceof LocalizedString) {
                        if (((LocalizedString) ((Pair) pair).getValue()).getLanguage() != null) {
                            result.append("\t\t<dc:title xml:lang=\""
                                    + ((LocalizedString) ((Pair) pair).getValue()).getLanguage() + "\">"
                                    + StringEscapeUtils.escapeXml10(
                                            ((LocalizedString) ((Pair) pair).getValue()).getValue())
                                    + "</dc:title>\n");
                        } else {
                            result.append("\t\t<dc:title>"
                                    + StringEscapeUtils.escapeXml10(
                                            ((LocalizedString) ((Pair) pair).getValue()).getValue())
                                    + "</dc:title>\n");
                        }
                    } else {
                        result.append("\t\t<dc:title>"
                                + StringEscapeUtils.escapeXml10(((Pair) pair).getValue().toString())
                                + "</dc:title>\n");
                    }
                    result.append("\t</rdf:Description>\n");
                } catch (Exception exception) {
                    throw new RuntimeException(exception);
                }
            } else if (pair instanceof TreeFragment) {
                result.append(((TreeFragment) pair).toRdf(model));
            }
        }
    }

    result.append("</rdf:RDF>\n");

    return result.toString();
}

From source file:org.commonjava.aprox.bind.jaxrs.util.ResponseUtils.java

public static CharSequence formatEntity(final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    if (message != null) {
        sw.append(message);
        sw.append("\nError was:\n\n");
    }/* w ww.  ja  v  a2  s. c  o m*/

    sw.append(error.getMessage());

    final Throwable cause = error.getCause();
    if (cause != null) {
        sw.append("\n\n");
        cause.printStackTrace(new PrintWriter(sw));
    }

    sw.write('\n');

    return sw.toString();
}

From source file:net.sourceforge.openutils.mgnlcriteria.tests.CriteriaTestUtils.java

private static String arrayToString(Object[] array) {
    StringWriter writer = new StringWriter();
    for (Object string : array) {
        writer.append(string.toString());
        writer.append("\n");
    }/*from  w  ww .j av a 2  s. c o m*/
    return writer.toString();
}

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

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload// w w w  . j a va2  s.  c om
 * @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:org.commonjava.cartographer.rest.util.ResponseUtils.java

public static CharSequence formatEntity(final String id, final Throwable error, final String message) {
    final StringWriter sw = new StringWriter();
    sw.append("Id: ").append(id);
    if (message != null) {
        sw.append("\nMessage: ").append(message);
    }/*from  www.  j av  a 2s.c o  m*/

    sw.append(error.getMessage());

    final Throwable cause = error.getCause();
    if (cause != null) {
        sw.append("\nError:\n\n");
        cause.printStackTrace(new PrintWriter(sw));
    }

    sw.write('\n');

    return sw.toString();
}

From source file:Main.java

public static String toString(NodeList nodes) {
    try {//from   ww  w  . j  a va  2 s  . c  o  m
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node item = nodes.item(i);
            switch (item.getNodeType()) {
            case Node.TEXT_NODE:
                stw.append(item.getTextContent());
                break;
            default:
                serializer.transform(new DOMSource(item), new StreamResult(stw));
            }
        }
        return stw.toString();
    } catch (Exception e) {
        return e.toString();
    }
}