Example usage for org.apache.commons.io.output StringBuilderWriter StringBuilderWriter

List of usage examples for org.apache.commons.io.output StringBuilderWriter StringBuilderWriter

Introduction

In this page you can find the example usage for org.apache.commons.io.output StringBuilderWriter StringBuilderWriter.

Prototype

public StringBuilderWriter(StringBuilder builder) 

Source Link

Document

Construct a new instance with the specified StringBuilder .

Usage

From source file:com.phoenixnap.oss.ramlapisync.generation.CodeModelHelper.java

/**
 * Returns the string equivalent of the code model element
 * @param type The element to stringify//from   ww w .  ja  v  a2  s  .  com
 * @return the element as a string (generated code)
 */
public static String getElementAsString(JDeclaration type) {
    StringBuilder builder = new StringBuilder();
    JFormatter jFormatter = new JFormatter(new StringBuilderWriter(builder));
    type.declare(jFormatter);
    return builder.toString();
}

From source file:com.addthis.hydra.query.web.LegacyHandler.java

public static Query handleQuery(Query query, KVPairs kv, HttpRequest request, ChannelHandlerContext ctx)
        throws IOException, QueryException {

    String async = kv.getValue("async");
    if (async == null) {
        return query;
    } else if (async.equals("new")) {
        StringBuilderWriter writer = new StringBuilderWriter(50);
        HttpResponse response = HttpUtils.startResponse(writer);
        String asyncUuid = genAsyncUuid();
        asyncCache.put(asyncUuid, query);
        Query.traceLog.info("async create {} from {}", asyncUuid, query);
        writer.write("{\"id\":\"" + asyncUuid + "\"}");
        ByteBuf textResponse = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(writer.getBuilder()),
                CharsetUtil.UTF_8);//  ww  w.j a v  a2s. co m
        HttpContent content = new DefaultHttpContent(textResponse);
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, textResponse.readableBytes());
        if (HttpHeaders.isKeepAlive(request)) {
            response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        ctx.write(response);
        ctx.write(content);
        ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        if (!HttpHeaders.isKeepAlive(request)) {
            lastContentFuture.addListener(ChannelFutureListener.CLOSE);
        }
        return null;
    } else {
        Query asyncQuery = asyncCache.getIfPresent(async);
        asyncCache.invalidate(async);
        Query.traceLog.info("async restore {} as {}", async, asyncQuery);
        if (asyncQuery != null) {
            return asyncQuery;
        } else {
            throw new QueryException("Missing Async Id");
        }
    }
}

From source file:com.spectralogic.ds3client.exceptions.AggregateException.java

public String toString() {
    final StringBuilder builder = new StringBuilder();
    builder.append("One or more exceptions were aggregated:");

    try (final StringBuilderWriter writer = new StringBuilderWriter(builder);
            final PrintWriter printWriter = new PrintWriter(writer)) {

        for (final Throwable t : exceptions) {
            printWriter.append("Exception: ").append(t.getMessage());
            t.printStackTrace(printWriter);
        }/*from  ww  w.  j  a v  a 2s . c om*/
    }

    return builder.toString();
}

From source file:br.msf.maven.compressor.processor.CssCompressor.java

@Override
protected CharSequence proccessMinify(final CharSequence originalContent, final CompressorSettings settings)
        throws Exception {
    Reader reader = null;/*from   w  ww. j a  v  a 2 s . c om*/
    Writer writer = null;

    try {
        reader = new CharSequenceReader(originalContent);
        com.yahoo.platform.yui.compressor.CssCompressor compressor = new com.yahoo.platform.yui.compressor.CssCompressor(
                reader);

        final StringBuilder out = new StringBuilder(originalContent.length());
        writer = new StringBuilderWriter(out);
        compressor.compress(writer, LINE_BREAK_POS);
        writer.flush();
        return out;
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }
}

From source file:com.px100systems.util.XmlParser.java

/**
 * Serialize the bean to XML/*from ww w  .j  a v a2  s  .c  o  m*/
 * @param bean teh bean to serialize
 * @return XML
 */
public String write(T bean) {
    StringBuilder result = new StringBuilder();
    Writer writer = new StringBuilderWriter(result);
    try {
        Marshaller m = jaxb.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(bean, writer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    return result.toString();
}

From source file:br.msf.maven.compressor.processor.JavaScriptCompressor.java

@Override
protected CharSequence proccessMinify(final CharSequence originalContent, final CompressorSettings settings)
        throws Exception {
    Reader reader = null;/*  ww  w. j av a 2  s.c om*/
    Writer writer = null;

    try {
        reader = new CharSequenceReader(originalContent);
        com.yahoo.platform.yui.compressor.JavaScriptCompressor compressor = new com.yahoo.platform.yui.compressor.JavaScriptCompressor(
                reader, new MavenErrorReporter(settings.getLog(), settings.isShowJsWarnings()));

        final StringBuilder out = new StringBuilder(originalContent.length());
        writer = new StringBuilderWriter(out);
        try {
            compressor.compress(writer, LINE_BREAK_POS, false, settings.isVerbose(), true, false);
        } catch (Exception e) {
            throw new YUICompressorException(
                    "An Error has occurred while compressing javascript. Probably there is a bad practice on the source...",
                    e);
        }
        writer.flush();
        return out;
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }
}

From source file:com.adaptris.core.common.MetadataStreamOutputParameter.java

@Override
public void insert(InputStreamWithEncoding data, InterlokMessage message) throws InterlokException {
    try {//from  w w w  .  j  a v a2  s.  c o  m
        StringBuilder builder = new StringBuilder();
        try (Reader in = getReader(data.inputStream, defaultIfEmpty(getContentEncoding(), data.encoding));
                StringBuilderWriter out = new StringBuilderWriter(builder)) {
            IOUtils.copy(in, out);
        }
        message.addMessageHeader(getMetadataKey(), builder.toString());
    } catch (IOException e) {
        throw ExceptionHelper.wrapCoreException(e);
    }
}

From source file:com.addthis.hydra.query.web.GoogleDriveAuthentication.java

/**
 * Send an HTML formatted error message.
 *///from  w  ww .jav a  2 s.co  m
private static void sendErrorMessage(ChannelHandlerContext ctx, String message) throws IOException {
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(CONTENT_TYPE, "text/html; charset=utf-8");
    StringBuilderWriter writer = new StringBuilderWriter(50);
    writer.append("<html><head><title>Hydra Query Master</title></head><body>");
    writer.append("<h3>");
    writer.append(message);
    writer.append("</h3></body></html>");
    ByteBuf textResponse = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(writer.getBuilder()),
            CharsetUtil.UTF_8);
    HttpContent content = new DefaultHttpContent(textResponse);
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, textResponse.readableBytes());
    ctx.write(response);
    ctx.write(content);
    ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}

From source file:name.martingeisse.trading_game.platform.util.logging.MyLayout.java

private void printException(StringBuilder builder, Throwable e) {
    if (e == null) {
        return;/*w w  w  .ja v a2s.  c o  m*/
    }
    builder.append('\n');
    e.printStackTrace(new PrintWriter(new StringBuilderWriter(builder)));
}

From source file:com.px100systems.util.JsonParser.java

/**
 * Write a bean (anything: standalone beans, collections, etc.)
 * @param bean bean to serialize/*w  ww. ja v a  2 s.co m*/
 * @return JSON
 */
public String write(Object bean) {
    StringBuilder result = new StringBuilder();
    Writer writer = new StringBuilderWriter(result);
    try {
        Gson gson = new GsonBuilder().setDateFormat(JSON_DATE_FORMAT).setPrettyPrinting().create();
        writer.write(gson.toJson(bean));
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    return result.toString();
}