Java Code Examples for org.apache.commons.io.output.StringBuilderWriter#toString()

The following examples show how to use org.apache.commons.io.output.StringBuilderWriter#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpinRendererTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static String toRDF(StatementCollector stmts) throws RDFHandlerException {
	WriterConfig config = new WriterConfig();
	config.set(BasicWriterSettings.PRETTY_PRINT, false);
	StringBuilderWriter writer = new StringBuilderWriter();
	final RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, writer);
	rdfWriter.setWriterConfig(config);

	rdfWriter.startRDF();
	for (Map.Entry<String, String> entry : stmts.getNamespaces().entrySet()) {
		rdfWriter.handleNamespace(entry.getKey(), entry.getValue());
	}
	for (final Statement st : stmts.getStatements()) {
		rdfWriter.handleStatement(st);
	}
	rdfWriter.endRDF();

	writer.close();
	return writer.toString();
}
 
Example 2
Source File: Driver.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * Performs rendering (apply a render list) on an http response body (as a String).
 * 
 * @param pageUrl
 *            The remove url from which the body was retrieved.
 * @param originalRequest
 *            The request received by esigate.
 * @param response
 *            The Http Reponse.
 * @param body
 *            The body of the Http Response which will be rendered.
 * @param renderers
 *            list of renderers to apply.
 * @return The rendered response body.
 * @throws HttpErrorPage
 * @throws IOException
 */
private String performRendering(String pageUrl, DriverRequest originalRequest, CloseableHttpResponse response,
        String body, Renderer[] renderers) throws IOException, HttpErrorPage {
    // Start rendering
    RenderEvent renderEvent = new RenderEvent(pageUrl, originalRequest, response);
    // Create renderer list from parameters.
    renderEvent.getRenderers().addAll(Arrays.asList(renderers));

    String currentBody = body;

    this.eventManager.fire(EventManager.EVENT_RENDER_PRE, renderEvent);
    for (Renderer renderer : renderEvent.getRenderers()) {
        StringBuilderWriter stringWriter = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
        renderer.render(originalRequest, currentBody, stringWriter);
        stringWriter.close();
        currentBody = stringWriter.toString();
    }
    this.eventManager.fire(EventManager.EVENT_RENDER_POST, renderEvent);

    return currentBody;
}
 
Example 3
Source File: XsltRendererTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
private String renderSimpleStyleSheet(String src) throws IOException {
    String template = "<?xml version=\"1.0\"?>";
    template +=
            "<xsl:stylesheet version=\"1.0\" xmlns=\"http://www.w3.org/1999/xhtml\" "
                    + "xmlns:html=\"http://www.w3.org/1999/xhtml\" "
                    + "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
    template += "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\"/>";
    template += "<xsl:template match=\"//html:body\">";
    template += "<xsl:copy-of select=\".\"/>";
    template += "</xsl:template>";
    template += "<xsl:template match=\"text()\"/>";
    template += "</xsl:stylesheet>";
    StringBuilderWriter out = new StringBuilderWriter();
    XsltRenderer tested = new XsltRenderer(template);
    tested.render(null, src, out);
    return out.toString();
}
 
Example 4
Source File: XsltRendererTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
private String renderExtensionStyleSheet(String src) throws IOException {
    String template = "<?xml version=\"1.0\"?>";
    template +=
            "<xsl:stylesheet version=\"1.0\" xmlns=\"http://www.w3.org/1999/xhtml\" "
                    + "xmlns:html=\"http://www.w3.org/1999/xhtml\" "
                    + "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
    template += "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\"/>";
    template +=
            "<xsl:template match=\"/\"  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:rt=\"http://xml.apache.org/xalan/java/java.lang.Runtime\">";

    template += "<xsl:variable name=\"rtObj\" select=\"rt:getRuntime()\"/>";
    template += "<xsl:variable name=\"process\" select=\"rt:totalMemory($rtObj)\"/>";
    template += "Process: <xsl:value-of select=\"$process\"/>\n";

    template += "</xsl:template>";

    template += "</xsl:stylesheet>";
    StringBuilderWriter out = new StringBuilderWriter();
    XsltRenderer tested = new XsltRenderer(template);
    tested.render(null, src, out);
    return out.toString();
}
 
Example 5
Source File: WordprocessingMLTemplateWriter.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public String writeToString(File docFile) throws IOException, Docx4JException {
	WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(docFile);
	StringBuilderWriter output = new StringBuilderWriter();
	try {
		this.writeToWriter(wmlPackage, output);
	} finally {
		IOUtils.closeQuietly(output);
	}
	return output.toString();
}
 
Example 6
Source File: WordprocessingMLPackageExtractor.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public String extract(File inputfile) throws Exception {
	WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(inputfile);
	StringBuilderWriter output = new StringBuilderWriter();
	try {
		this.extract(wmlPackage, output);
	} finally {
		IOUtils.closeQuietly(output);
	}
	return output.toString();
}
 
Example 7
Source File: WordprocessingMLPackageExtractor.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public String extract(WordprocessingMLPackage wmlPackage) throws Exception {
	StringBuilderWriter output = new StringBuilderWriter(); 
	try {
		this.extract(wmlPackage, output);
	} finally {
		IOUtils.closeQuietly(output);
	}
	return output.toString();
}
 
Example 8
Source File: HttpErrorPage.java    From esigate with Apache License 2.0 5 votes vote down vote up
private static HttpEntity toMemoryEntity(Exception exception) {
    StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
    PrintWriter pw = new PrintWriter(out);
    exception.printStackTrace(pw);
    String content = out.toString();
    try {
        return toMemoryEntity(content);
    } finally {
        pw.close();
    }
}
 
Example 9
Source File: AbstractElementTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
protected String render(String page) throws HttpErrorPage, IOException {
    IncomingRequest incomingRequest = requestBuilder.build();
    DriverRequest request = new DriverRequest(incomingRequest, provider, page);
    StringBuilderWriter out = new StringBuilderWriter();
    tested.render(request, page, out);
    return out.toString();
}
 
Example 10
Source File: AbstractElementTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
protected String render(String page) throws HttpErrorPage, IOException {
    IncomingRequest incomingRequest = requestBuilder.build();
    DriverRequest request = new DriverRequest(incomingRequest, provider, page);
    StringBuilderWriter out = new StringBuilderWriter();
    tested.render(request, page, out);
    return out.toString();
}
 
Example 11
Source File: IOUtils.java    From aion-germany with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the contents of an <code>InputStream</code> as a String
 * using the specified character encoding.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * </p>
 * @param input  the <code>InputStream</code> to read from
 * @param encoding  the encoding to use, null means platform default
 * @return the requested String
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since 2.3
 */
public static String toString(InputStream input, Charset encoding) throws IOException {
    StringBuilderWriter sw = new StringBuilderWriter();
    copy(input, sw, encoding);
    return sw.toString();
}
 
Example 12
Source File: IOUtils.java    From aion-germany with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the contents of a <code>Reader</code> as a String.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * 
 * @param input  the <code>Reader</code> to read from
 * @return the requested String
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 */
public static String toString(Reader input) throws IOException {
    StringBuilderWriter sw = new StringBuilderWriter();
    copy(input, sw);
    return sw.toString();
}
 
Example 13
Source File: IOUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the contents of an <code>InputStream</code> as a String
 * using the specified character encoding.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * </p>
 *
 * @param input the <code>InputStream</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested String
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3
 */
public static String toString(final InputStream input, final Charset encoding) throws IOException {
    final StringBuilderWriter sw = new StringBuilderWriter();
    copy(input, sw, encoding);
    return sw.toString();
}
 
Example 14
Source File: IOUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the contents of a <code>Reader</code> as a String.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 *
 * @param input the <code>Reader</code> to read from
 * @return the requested String
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 */
public static String toString(final Reader input) throws IOException {
    final StringBuilderWriter sw = new StringBuilderWriter();
    copy(input, sw);
    return sw.toString();
}