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:com.accumulobook.designs.rdf.FreebaseExample.java

public static List<Pair<String, String>> getProperties(final String subject) throws Exception {

    ArrayList<Pair<String, String>> properties = new ArrayList<>();

    URL url = new URL("https://www.googleapis.com/freebase/v1/topic/en/" + subject);

    StringWriter writer = new StringWriter();
    IOUtils.copy(url.openStream(), writer);

    String string = writer.toString();

    JSONObject obj = new JSONObject(string);

    JSONObject propArray = obj.getJSONObject("property");

    Iterator iter = propArray.keys();
    while (iter.hasNext()) {
        String propertyName = (String) iter.next();
        JSONObject property = propArray.getJSONObject(propertyName);

        JSONArray values = property.getJSONArray("values");
        for (int i = 0; i < values.length(); i++) {
            properties.add(new Pair(propertyName, values.getJSONObject(i).getString("text")));
        }/*  w w  w  . ja v a2  s . c  o m*/
    }

    return properties;
}

From source file:com.topsem.common.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *//*  w  ww .  ja v a2  s.  co m*/
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.wsun.seap.common.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *///from w w w  .  j  a  va  2 s  . c  om
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}

From source file:Main.java

/**
 * Converts a DOM Tree to a String./*from   w w w  .j  av a 2  s  .c o  m*/
 *
 * @param xml The DOM document to convert.
 *
 * @return A string containing the XML if successful, null otherwise.
 */
public static String xmlToString(Document xml) {
    try {
        TransformerFactory f = TransformerFactory.newInstance();
        StringWriter writer = new StringWriter();
        Transformer t = f.newTransformer();
        DOMSource src = new DOMSource(xml);
        StreamResult result = new StreamResult(writer);
        t.transform(src, result);
        return writer.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:Main.java

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));

    return sw.toString();
}

From source file:com.androidwhy.modules.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *///from w w  w.  j av a 2s .  co  m
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil.java

/**
 * JsCheckOptionsxml????./*from w  w  w  . ja  va  2 s .  co m*/
 * 
 * 
 * @param checkOptions .
 * @param output .
 */
public static void saveJsCheckOption(JsCheckOption checkOptions, IFile output) {
    try {
        Marshaller marshall = jc.createMarshaller();
        marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
        StringWriter writer = new StringWriter();
        marshall.marshal(checkOptions, writer);
        if (output.exists()) {
            output.setContents(IOUtils.toInputStream(writer.toString(), "UTF-8"), IResource.FORCE, null);
        } else {
            output.create(IOUtils.toInputStream(writer.toString(), "UTF-8"), true, null);
        }
        output.refreshLocal(IResource.DEPTH_ONE, null);
    } catch (JAXBException e) {
        logger.put(Messages.EM0006, e, output.getName());
    } catch (CoreException e) {
        logger.put(Messages.EM0100, e);
    } catch (IOException e) {
        logger.put(Messages.EM0006, e, output.getName());
    }
}

From source file:net.dv8tion.jda.core.utils.JDALogger.java

/**
 * Utility function to enable logging of complex statements more efficiently (lazy).
 *
 * @param  lazyLambda/*w w w.  ja v  a2s . c  om*/
 *         The Supplier used when evaluating the expression
 *
 * @return An Object that can be passed to SLF4J's logging methods as lazy parameter
 */
public static Object getLazyString(LazyEvaluation lazyLambda) {
    return new Object() {
        @Override
        public String toString() {
            try {
                return lazyLambda.getString();
            } catch (Exception ex) {
                StringWriter sw = new StringWriter();
                ex.printStackTrace(new PrintWriter(sw));
                return "Error while evaluating lazy String... " + sw.toString();
            }
        }
    };
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return (writer.toString());
}

From source file:groovyx.net.http.optional.Jackson.java

/**
 * Used to encode the request content using the Jackson JSON encoder.
 *
 * @param config the configuration//from  w  ww  .  j  a v a 2  s  .  c  o m
 * @param ts the server request content accessor
 */
@SuppressWarnings("WeakerAccess")
public static void encode(final ChainedHttpConfig config, final ToServer ts) {
    try {
        if (handleRawUpload(config, ts)) {
            return;
        }

        final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
        final ObjectMapper mapper = (ObjectMapper) config.actualContext(request.actualContentType(),
                OBJECT_MAPPER_ID);
        final StringWriter writer = new StringWriter();
        mapper.writeValue(writer, request.actualBody());
        ts.toServer(new CharSequenceInputStream(writer.toString(), request.actualCharset()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}