Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

In this page you can find the example usage for java.io Writer toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:de.shadowhunt.subversion.internal.ResolveOperation.java

@Override
protected HttpUriRequest createRequest() {
    final Writer body = new StringBuilderWriter();
    try {/* www  . j a va2 s. c o m*/
        final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body);
        writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0);
        writer.writeStartElement("get-locations");
        writer.writeDefaultNamespace(XmlConstants.SVN_NAMESPACE);
        writer.writeEmptyElement("path");
        writer.writeStartElement("peg-revision");
        writer.writeCharacters(revision.toString());
        writer.writeEndElement(); // peg-revision
        writer.writeStartElement("location-revision");
        writer.writeCharacters(expected.toString());
        writer.writeEndElement(); // location-revision
        writer.writeEndElement(); //get-locations
        writer.writeEndDocument();
        writer.close();
    } catch (final XMLStreamException e) {
        throw new SubversionException("could not create request body", e);
    }

    final URI uri = URIUtils.createURI(repository, resource);
    final DavTemplateRequest request = new DavTemplateRequest("REPORT", uri);
    request.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_XML));
    return request;
}

From source file:org.jumpmind.metl.core.runtime.component.DelimitedFormatter.java

private String processInputRow(Message inputMessage, EntityData inputRow) {
    Writer writer = new StringWriter();
    CsvWriter csvWriter = getCsvWriter(writer);
    try {/* w  w w .j  av  a 2  s  . c o  m*/
        if (attributes.size() > 0) {
            for (AttributeFormat attribute : attributes) {
                Object object = inputRow.get(attribute.getAttributeId());
                if (isNotBlank(attribute.getFormatFunction())) {
                    object = ModelAttributeScriptHelper.eval(inputMessage, context, attribute.getAttribute(),
                            object, attribute.getEntity(), inputRow, attribute.getFormatFunction());
                }

                csvWriter.write(object != null ? object.toString() : null);
            }
        } else {
            Collection<Object> values = inputRow.values();
            for (Object object : values) {
                csvWriter.write(object != null ? object.toString() : null);
            }

        }
    } catch (IOException e) {
        throw new IoException("Error writing to stream for formatted output. " + e.getMessage());
    }
    return writer.toString();
}

From source file:org.openmrs.module.dhisconnector.api.impl.DHISConnectorServiceImpl.java

private String beautifyXML(String xml) {
    if (StringUtils.isNotBlank(xml)) {
        try {/*from  w  w  w.  j av  a2 s  .  c  o m*/
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
            Transformer tf = TransformerFactory.newInstance().newTransformer();

            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");

            Writer out = new StringWriter();

            tf.transform(new DOMSource(document), new StreamResult(out));

            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
    return xml;
}

From source file:org.codice.ddf.spatial.ogc.wfs.v110.catalog.source.WfsSourceTest.java

private String marshal(final GetFeatureType getFeatureType) throws JAXBException {
    Writer writer = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(getGetFeatureTypeJaxbElement(getFeatureType), writer);
    return writer.toString();
}

From source file:org.broadleafcommerce.core.content.service.ContentServiceImpl.java

public String renderedContentDetails(String styleSheetString, List<ContentDetails> contentDetails, int rowCount)
        throws Exception {
    Source xmlSource;/*from  w  w w . ja  v a  2  s.c  o m*/
    int maxCount = (rowCount > -1 && contentDetails.size() > 0) ? rowCount : contentDetails.size();

    Writer resultWriter = new StringWriter();
    StreamResult result = new StreamResult(resultWriter);
    Source styleSheetSource = getSource(styleSheetString);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer(styleSheetSource);

    for (int i = 0; i < maxCount; i++) {
        ContentDetails contentDetail = contentDetails.get(i);
        xmlSource = getSource(contentDetail.getXmlContent());
        try {
            transformer.transform(xmlSource, result);
        } catch (Exception e) {
            LOG.error("Error during transformation. ", e);
            throw e;
        }

    }
    return StringEscapeUtils.unescapeXml(resultWriter.toString());
}

From source file:ddf.catalog.transformer.xml.XmlResponseQueueTransformer.java

@Override
public BinaryContent transform(SourceResponse response, Map<String, Serializable> args)
        throws CatalogTransformerException {
    try {/*from  ww  w  .j  a v  a 2 s.  c om*/
        Writer stringWriter = new StringWriter(BUFFER_SIZE);
        stringWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");

        MetacardPrintWriter writer = new MetacardPrintWriter(stringWriter);

        writer.startNode("metacards");
        for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
            writer.addAttribute(nsRow.getKey(), nsRow.getValue());
        }

        if (response.getResults() != null && !response.getResults().isEmpty()) {
            StringWriter metacardContent = fjp.invoke(new MetacardForkTask(
                    ImmutableList.copyOf(response.getResults()), fjp, geometryTransformer, threshold));

            writer.setRawValue(metacardContent.getBuffer().toString());
        }

        writer.endNode(); // metacards

        ByteArrayInputStream bais = new ByteArrayInputStream(stringWriter.toString().getBytes());

        return new BinaryContentImpl(bais, MIME_TYPE);
    } catch (Exception e) {
        LOGGER.info("Failed Query response transformation", e);
        throw new CatalogTransformerException("Failed Query response transformation");
    }
}

From source file:org.atricore.idbus.kernel.main.mediation.camel.component.binding.AbstractMediationHttpBinding.java

protected Html createHtmlErrorPage(MediationMessage fault) {

    Exception error = fault.getFault();

    Html html = new Html();
    html.setLang("en");

    Head head = new Head();
    html.setHead(head);/*from   w ww. j a v  a 2s .c  o m*/
    {
        Title t = new Title();
        t.setContent("JOSSO 2 - Error"); // TODO : i18n
        head.getContent().add(t);

    }

    // Body
    Body body = new Body();

    {
        // Main error
        P paragraph = new P();
        H1 h1 = new H1();
        h1.getContent().add("Error while processing your request "
                + (error != null ? error.getMessage() : "UNKNOWN ERROR"));
        body.getPOrH1OrH2().add(h1);
    }

    {

        H3 h3 = new H3();
        h3.getContent().add(fault.getFaultDetails());
        body.getPOrH1OrH2().add(h3);

    }

    {
        // Stack Trace

        if (error != null && logger.isDebugEnabled()) {

            P paragraph = new P();
            paragraph.getContent().add("Error Debug Information:");
            paragraph.getContent().add(new Br());
            paragraph.getContent().add(new Br());

            // Dump errors
            Throwable t = error;

            while (t != null) {

                Writer rootWriter = new StringWriter();
                PrintWriter rootPrintWriter = new PrintWriter(rootWriter);

                paragraph.getContent().add(new Br());
                paragraph.getContent().add(new Br());
                paragraph.getContent().add("Caused By:");
                paragraph.getContent().add(new Br());
                paragraph.getContent().add(new Br());

                t.printStackTrace(rootPrintWriter);
                paragraph.getContent().add(rootWriter.toString());
                paragraph.getContent().add(new Br());

                t = t.getCause();
            }

            body.getPOrH1OrH2().add(paragraph);
        }

    }

    {
        // Footer
        P paragraph = new P();
        body.getPOrH1OrH2().add(paragraph);
    }

    html.setBody(body);

    return html;
}

From source file:de.interactive_instruments.ShapeChange.BasicTest.java

private String readFile(String fileName) throws Exception {
    InputStream stream = new FileInputStream(new File(fileName));

    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader = null;/*from  www  .  j  a v a 2 s. c o  m*/
    try {
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        if (reader != null)
            reader.close();
    }
    return writer.toString();
}

From source file:org.codice.ddf.security.handler.api.BSTAuthenticationToken.java

/**
 * Creates a binary security token based on the provided credential.
 *///ww w  .j  a  v a2  s .  c o  m
private synchronized String getBinarySecurityToken(String credential) {
    Writer writer = new StringWriter();

    Marshaller marshaller = null;

    BinarySecurityTokenType binarySecurityTokenType = createBinarySecurityTokenType(credential);
    JAXBElement<BinarySecurityTokenType> binarySecurityTokenElement = new JAXBElement<BinarySecurityTokenType>(
            new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "BinarySecurityToken"),
            BinarySecurityTokenType.class, binarySecurityTokenType);

    if (BINARY_TOKEN_CONTEXT != null) {
        try {
            marshaller = BINARY_TOKEN_CONTEXT.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        } catch (JAXBException e) {
            LOGGER.error("Exception while creating UsernameToken marshaller.", e);
        }

        if (marshaller != null) {
            try {
                marshaller.marshal(binarySecurityTokenElement, writer);
            } catch (JAXBException e) {
                LOGGER.error("Exception while writing username token.", e);
            }
        }
    }

    String binarySecurityToken = writer.toString();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Binary Security Token: " + binarySecurityToken);
    }

    return binarySecurityToken;
}

From source file:org.jmxtrans.embedded.output.CopperEggWriter.java

public String convertStreamToString(InputStream is) throws IOException {
    ////from   w ww . j av  a 2s .  co m
    // To convert the InputStream to String we use the
    // Reader.read(char[] buffer) method. We iterate until the
    // Reader return -1 which means there's no more data to
    // read. We use the StringWriter class to produce the string.
    //
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}