Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

In this page you can find the example usage for java.io StringWriter write.

Prototype

public void write(String str) 

Source Link

Document

Write a string.

Usage

From source file:com.espertech.esper.schedule.ScheduleHandleCallbackProxy.java

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

    if (m.getName().equals(target.getName())) {
        if (AuditPath.isInfoEnabled()) {
            StringWriter message = new StringWriter();
            message.write("trigger handle ");
            JavaClassHelper.writeInstance(message, scheduleHandleCallback, true);
            AuditPath.auditLog(engineURI, statementName, AuditEnum.SCHEDULE, message.toString());
        }/*from  w  ww . j a v a 2  s.co m*/
    }

    return m.invoke(scheduleHandleCallback, args);
}

From source file:org.powertac.common.msg.CustomerBootstrapDataTests.java

@Test
public void xmlSerializationTest() {
    CustomerBootstrapData cbd = new CustomerBootstrapData(customer, PowerType.CONSUMPTION, data);
    XStream xstream = new XStream();
    xstream.processAnnotations(CustomerBootstrapData.class);
    StringWriter serialized = new StringWriter();
    serialized.write(xstream.toXML(cbd));
    //System.out.println(serialized.toString());
    CustomerBootstrapData xcbd = (CustomerBootstrapData) xstream.fromXML(serialized.toString());
    assertNotNull("deserialized something", xcbd);
    assertEquals("correct id", cbd.getId(), xcbd.getId());
    assertEquals("correct 5th element", 1.7, xcbd.getNetUsage()[4], 1e-6);
}

From source file:dk.miles.messenger.message.XMLIntrospector.java

public String mapToXML(Message message) {

    String out = "";

    try {/*w  ww.ja v a  2 s  . c o m*/
        StringWriter outputWriter = new StringWriter();

        outputWriter.write("<?xml version='1.0' ?>");

        BeanWriter beanWriter = new BeanWriter(outputWriter);
        beanWriter.getXMLIntrospector().setAttributesForPrimitives(false);
        beanWriter.setWriteIDs(false);
        beanWriter.enablePrettyPrint();
        beanWriter.write("message", message);

        out = outputWriter.toString();
    } catch (Exception e) {
        log.error("mapToXML(): " + e);
    }

    return out;
}

From source file:org.ebayopensource.turmeric.eclipse.utils.ui.UIUtil.java

/**
 * Show error dialog./*from w w  w.j a v a  2  s  . co m*/
 *
 * @param shell the shell
 * @param title the title
 * @param message the message
 */
public static void showErrorDialog(Shell shell, String title, String message) {
    StringWriter msg = new StringWriter();
    if (message != null) {
        msg.write(message);
        msg.write("\n\n"); //$NON-NLS-1$
    }
    MessageDialog.openError(shell, title, msg.toString());
}

From source file:org.powertac.common.BalancingTransactionTests.java

@Test
public void xmlSerializationTest() {
    BalancingTransaction bt = new BalancingTransaction(broker, 24, 42.1, 3.22);
    XStream xstream = new XStream();
    xstream.processAnnotations(BalancingTransaction.class);
    StringWriter serialized = new StringWriter();
    serialized.write(xstream.toXML(bt));
    //System.out.println(serialized.toString());
    BalancingTransaction xbt = (BalancingTransaction) xstream.fromXML(serialized.toString());
    assertNotNull("deserialized something", xbt);
    assertEquals("correct time", 24, xbt.getPostedTimeslotIndex());
    assertEquals("correct qty", 42.1, xbt.getKWh(), 1e-6);
    assertEquals("correct charge", 3.22, xbt.getCharge(), 1e-6);
}

From source file:org.ebayopensource.turmeric.eclipse.utils.ui.UIUtil.java

/**
 * Show error dialog./*from w w  w  .j  a v a 2 s. co  m*/
 *
 * @param shell the shell
 * @param exceptionMessage the exception message
 * @param title the title
 * @param message the message
 */
public static void showErrorDialog(Shell shell, String exceptionMessage, String title, String message) {
    StringWriter msg = new StringWriter();
    if (message != null) {
        msg.write(message);
        msg.write("\n\n"); //$NON-NLS-1$
    }
    if (exceptionMessage == null || exceptionMessage.length() == 0) {
        // bug http://quickbugstage.arch.ebay.com/show_bug.cgi?id=16775
        // in fact there is no more details 
        // msg.write("See error log for more details.");
    } else {
        msg.write(exceptionMessage);
    }
    MessageDialog.openError(shell, title, msg.toString());
}

From source file:org.powertac.common.msg.TimeslotUpdateTests.java

@Test
public void xmlSerializationTest() {
    List<Timeslot> enabled = timeslotRepo.enabledTimeslots();
    TimeslotUpdate tsu = new TimeslotUpdate(timeService.getCurrentTime(), enabled.get(0).getSerialNumber(),
            enabled.get(enabled.size() - 1).getSerialNumber());
    XStream xstream = new XStream();
    xstream.processAnnotations(TimeslotUpdate.class);
    StringWriter serialized = new StringWriter();
    serialized.write(xstream.toXML(tsu));
    //System.out.println(serialized.toString());
    TimeslotUpdate xtsu = (TimeslotUpdate) xstream.fromXML(serialized.toString());
    assertNotNull("deserialized something", xtsu);
    assertEquals("correct time", timeService.getCurrentTime(), xtsu.getPostedTime());
    assertEquals("correct first", enabled.get(0).getSerialNumber(), xtsu.getFirstEnabled());
    assertEquals("correct last", enabled.get(23).getSerialNumber(), xtsu.getLastEnabled());
    assertEquals("correct length", 24, xtsu.size());
}

From source file:org.biokoframework.system.services.cron.impl.CronFailureNotifier.java

@Override
public <C extends ICommand> void commandFailed(Class<C> command, Throwable cause) {
    try {/*from ww  w .  j  a v a 2 s  .  c  o m*/
        StringWriter writer = new StringWriter();
        writer.write("Command " + command.getName() + " failed");
        if (cause.getMessage() != null) {
            writer.write(" with message " + cause.getMessage() + "\n");
        } else {
            writer.write("\n");
        }

        writer.write("Time stamp: " + DateTime.now() + "\n");

        writer.write("Stacktrace: ");
        cause.printStackTrace(new PrintStream(new WriterOutputStream(writer)));

        fMailService.sendASAP(fDestinationEmailAddress, fSourceEmailAddress, writer.toString(),
                SCHEDULED_ERROR_SUBJECT);

    } catch (EmailException exception) {
        // The system failed at failing (looks like Windows) 

        LOGGER.error("Error while reporting error for cron", exception);
    }

}

From source file:org.sipfoundry.sipxconfig.phone.cisco.BinaryFilter.java

public void copy(InputStream in, OutputStream out) throws IOException {

    File txtFile = File.createTempFile(TEMP_FILE_PREFIX, ".txt");
    FileOutputStream tempOut = new FileOutputStream(txtFile);
    IOUtils.copy(in, tempOut);/*from www. j av  a  2 s .c  o m*/
    tempOut.close();

    File binFile = File.createTempFile(TEMP_FILE_PREFIX, ".bin");
    requireFile(getCfgfmtUtility());
    requireFile(getPtagDat());
    try {
        String[] cmd = { getCfgfmtUtility(), "-t" + getPtagDat(), txtFile.getAbsolutePath(),
                binFile.getAbsolutePath() };
        LOG.info(StringUtils.join(cmd, ' '));
        Process p = Runtime.getRuntime().exec(cmd);
        int errCode = p.waitFor();
        if (errCode != 0) {
            String msg = "Cisco profile conversion failed status code: " + errCode;
            StringWriter err = new StringWriter();
            err.write(msg.toCharArray());
            IOUtils.copy(p.getErrorStream(), err);
            throw new RuntimeException(err.toString());
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    FileInputStream binStream = new FileInputStream(binFile);
    IOUtils.copy(binStream, out);
}

From source file:ubc.pavlab.aspiredb.server.util.JSONUtil.java

/**
 * Converts a collection of objects to a json string
 * /* w  w  w . j av  a  2s  . c om*/
 * @param collection
 * @return
 * @throws JSONException
 */
public String collectionToJson(Collection<?> collection) throws JSONException {
    StringWriter jsonText = new StringWriter();
    jsonText.write("[");

    try {
        Iterator<?> it = collection.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            String delim = it.hasNext() ? "," : "";
            jsonText.append(new JSONObject(obj).toString() + delim);
        }
        jsonText.append("]");
        jsonText.flush();
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        JSONObject json = new JSONObject();
        json.put("success", false);
        json.put("message", e.getLocalizedMessage());
        jsonText.write(json.toString());
        log.info(jsonText);
    }

    try {
        jsonText.close();
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        JSONObject json = new JSONObject();
        json.put("success", false);
        json.put("message", e.getLocalizedMessage());
        jsonText.write(json.toString());
        log.info(jsonText);
    }

    return jsonText.toString();
}