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

public static String objectToXml(Object object) {
    if (null == object) {
        return NO_RESULT;
    }//from ww w. ja  v  a 2s .  c  o m

    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent = null;
    Marshaller marshaller = null;

    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);

        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

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

public static String rendererHtml(String mondrianUrl, String mdx, boolean showDimensionTitle,
        boolean showParentMembers) throws Exception {
    if (StringUtils.isBlank(mondrianUrl) || StringUtils.isBlank(mdx)) {
        throw new java.lang.IllegalArgumentException("mondrian url and MDX cannot blank.");
    }//from w  w w  . jav  a  2  s. com
    String body = "";
    OlapConnection connection = OlapUtils.getConnection(mondrianUrl);
    OlapDataSource dataSource = new SingleConnectionOlapDataSource(connection);
    try {
        PivotModel model = getPivotModel(dataSource, mdx);
        TableRenderer renderer = getTableRenderer(showDimensionTitle, showParentMembers);
        StringWriter writer = new StringWriter();
        renderer.render(model, new HtmlRenderCallback(writer));

        //writer.write(body);
        body = writer.toString();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        throw e;
    } finally {
        OlapUtils.nullConnection(connection);
        connection = null;
        dataSource = null;
    }
    return body;
}

From source file:io.wcm.caravan.pipeline.impl.JacksonFunctions.java

/**
 * Serializes the given JSON node using the default factory
 * @param node/*  w  ww  . j a  va  2s.  co  m*/
 * @return JSON string
 * @throws JsonPipelineOutputException if the given node can not be serialized
 */
public static String nodeToString(JsonNode node) {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator generator = jsonFactory.createGenerator(writer);
        generator.writeObject(node);
        return writer.toString();
    } catch (IOException ex) {
        throw new JsonPipelineOutputException("Failed to serialize JsonNode. This was quite unexpected.", ex);
    }
}

From source file:Transform.java

/**
 * convert a Throwable into an array of Strings
 * @param throwable//w w  w.  ja va2 s  . c om
 * @return string representation of the throwable
 */
public static String[] getThrowableStrRep(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList<String> lines = new ArrayList<String>();
    try {
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
    } catch (IOException ex) {
        lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
}

From source file:alfio.util.EventUtil.java

public static Optional<byte[]> getIcalForEvent(Event event, TicketCategory ticketCategory, String description) {
    ICalendar ical = new ICalendar();
    VEvent vEvent = new VEvent();
    vEvent.setSummary(event.getDisplayName());
    vEvent.setDescription(description);/*from  w  ww . j av a2  s .  co  m*/
    vEvent.setLocation(StringUtils.replacePattern(event.getLocation(), "[\n\r\t]+", " "));
    ZonedDateTime begin = Optional.ofNullable(ticketCategory)
            .map(tc -> tc.getTicketValidityStart(event.getZoneId())).orElse(event.getBegin());
    ZonedDateTime end = Optional.ofNullable(ticketCategory)
            .map(tc -> tc.getTicketValidityEnd(event.getZoneId())).orElse(event.getEnd());
    vEvent.setDateStart(Date.from(begin.toInstant()));
    vEvent.setDateEnd(Date.from(end.toInstant()));
    vEvent.setUrl(event.getWebsiteUrl());
    ical.addEvent(vEvent);
    StringWriter strWriter = new StringWriter();
    try (ICalWriter writer = new ICalWriter(strWriter, ICalVersion.V2_0)) {
        writer.write(ical);
        return Optional.of(strWriter.toString().getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        log.warn("was not able to generate iCal for event " + event.getShortName(), e);
        return Optional.empty();
    }
}

From source file:Main.java

/**
 * @param str/*from  ww w.j  a  v a 2 s  .c  o m*/
 * @return a code snippet ready to copy and paste into java code.
 */
public static String escapedJavaStringConstant(String str) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length() * 2);
        writer.write("String s = \"");
        escapeJavaStringConstant(writer, str, true);
        writer.write("\";");
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        ioe.printStackTrace();
        return null;
    }
}

From source file:com.cloudhopper.sxmp.SxmpWriter.java

/**
static public byte[] createByteArray(Operation operation) throws SxmpErrorException, IOException {
// most requests will be ~1000 bytes/*from w w  w .  j a  va 2s . co m*/
//FastByteArrayOutputStream baos = new FastByteArrayOutputStream(1000);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
OutputStreamWriter out = new OutputStreamWriter(baos, "ISO-8859-1");
SxmpWriter.write(out, operation);
byte[] data = baos.toByteArray();
//out.close();
//baos.close();
return data;
}
 */
static public String createString(Operation operation) throws SxmpErrorException, IOException {
    StringWriter sw = new StringWriter(1000);
    SxmpWriter.write(sw, operation);
    return sw.toString();
}

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

/**
 * Java Object->Xml with encoding.// w w  w.  ja va2 s.com
 */
public static String toXml(Object root, Class clazz, String encoding) {
    StringWriter writer = new StringWriter();
    try {
        createMarshaller(clazz, encoding).marshal(root, writer);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:com.notifier.desktop.Main.java

private static void printHelp(Options options) {
    HelpFormatter formatter = new HelpFormatter();
    String cmdSyntax = "android-notifier-desktop";
    if (OperatingSystems.CURRENT_FAMILY == OperatingSystems.Family.WINDOWS) {
        StringWriter s = new StringWriter();
        formatter.printHelp(new PrintWriter(s), 150, cmdSyntax, null, options, formatter.getLeftPadding(),
                formatter.getDescPadding(), null, true);
        showMessage(s.toString());
    } else {/*from   w w w  .  ja v  a  2  s .  c o  m*/
        formatter.printHelp(cmdSyntax, options, true);
    }
}