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:Main.java

public static String serialize(Document document, boolean prettyPrint) {
    DOMImplementationLS impl = getDOMImpl();
    LSSerializer serializer = impl.createLSSerializer();
    // document.normalizeDocument();
    DOMConfiguration config = serializer.getDomConfig();
    if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", true);
    }/*from  w  w w  . jav  a  2 s  .  c o m*/
    config.setParameter("xml-declaration", true);
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    Writer writer = new StringWriter();
    output.setCharacterStream(writer);
    serializer.write(document, output);
    return writer.toString();
}

From source file:Main.java

public static String getErrorInfo(Throwable arg1) {
    Writer writer = new StringWriter();
    PrintWriter pw = new PrintWriter(writer);
    arg1.printStackTrace(pw);//ww w  . j a  va2 s. c o  m
    pw.close();
    String error = writer.toString();
    return error;
}

From source file:Main.java

/**
 * Marshals a bean to XML./*from   ww  w  . j a  va2s. c om*/
 * 
 * @param bean
 * @param namespaceURI
 * @param localPart
 * @return XML {@link String}
 * @throws JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> String marshal(T bean, String namespaceURI, String localPart) throws JAXBException {
    QName qName = new QName(namespaceURI, localPart);
    Class<T> clazz = (Class<T>) bean.getClass();
    Marshaller marshaller = createMarshaller(clazz);
    Writer stw = new StringWriter();
    marshaller.marshal(new JAXBElement<T>(qName, clazz, bean), stw);

    return stw.toString();
}

From source file:ch.entwine.weblounge.common.impl.security.AccessControlParser.java

/**
 * Serializes an AccessControlList to its XML form.
 * //from w w  w .  j av  a  2 s.  com
 * @param acl
 *          the access control list
 * @return the xml as a string
 * @throws IOException
 *           if there is a problem marshaling the xml
 */
public static String toXml(AccessControlList acl) throws IOException {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        Writer writer = new StringWriter();
        marshaller.marshal(acl, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}

From source file:com.tera.common.util.ExceptionUtil.java

/**
 * Simplified version of getting stacktracer from {@link Throwable} For
 * advanced use - see {@link org.apache.commons.lang.exception.ExceptionUtils}
 * /*from   w w w  .  ja  va 2s . co m*/
 * @param throwable
 * @return
 */
public static String getStackTrace(Throwable throwable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    throwable.printStackTrace(printWriter);
    return result.toString();
}

From source file:com.netsteadfast.greenstep.util.DynamicHqlUtils.java

public static String process(String resource, String queryName, Map<String, Object> paramMap) throws Exception {
    DynamicHql dynamicHql = loadResource(resource);
    if (null == dynamicHql) {
        logger.error("no dynamic hql resource.");
        throw new Exception("no dynamic hql resource.");
    }//  ww  w  .java 2  s . co  m
    String hql = "";
    for (int i = 0; i < dynamicHql.getQuery().size() && hql.length() < 1; i++) {
        Query queryObj = dynamicHql.getQuery().get(i);
        if (!queryObj.getName().equals(queryName)) {
            continue;
        }
        StringTemplateLoader templateLoader = new StringTemplateLoader();
        templateLoader.putTemplate(TEMPLATE_ID, queryObj.getContent());
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
        cfg.setTemplateLoader(templateLoader);
        Template template = cfg.getTemplate(TEMPLATE_ID, Constants.BASE_ENCODING);
        Writer out = new StringWriter();
        template.process(paramMap, out);
        hql = out.toString();
    }
    if (StringUtils.isBlank(hql)) {
        logger.warn("hql is blank.");
    }
    return hql;
}

From source file:info.magnolia.cms.util.FreeMarkerUtil.java

/**
 * Process this template with the passed data
 * @param name/*  w  w  w .java2 s  .  c  o  m*/
 * @param data
 * @return the resuling string
 */
public static String process(String name, Map data) {
    Writer writer = new StringWriter();
    process(name, data, writer);
    return writer.toString();
}

From source file:com.flexive.shared.FxXMLUtils.java

/**
 * Convenience method to create a more compact XML representation
 * of an object than {@link com.thoughtworks.xstream.XStream#toXML(Object)} creates (no pretty-printing).
 *
 * @param xStream the xstream instance/* w  ww.  j a v a2s.  c o m*/
 * @param object  the object to be marshalled
 * @return the XML representation of the object
 */
public static String toXML(XStream xStream, Object object) {
    Writer writer = new StringWriter();
    xStream.marshal(object, new CompactWriter(writer));
    return writer.toString();
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {// www  . j av a 2  s  .  c  o m
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {/*w w w.j a v  a2 s .c  o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}