Example usage for java.beans XMLEncoder setExceptionListener

List of usage examples for java.beans XMLEncoder setExceptionListener

Introduction

In this page you can find the example usage for java.beans XMLEncoder setExceptionListener.

Prototype

public void setExceptionListener(ExceptionListener exceptionListener) 

Source Link

Document

Sets the exception handler for this stream to exceptionListener .

Usage

From source file:Main.java

public static byte[] encodeObject(Object object, final boolean noisy) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(outputStream);
    encoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception ex) {
            if (noisy)
                ex.printStackTrace();//w  w w .  j av  a 2  s  .co m
        }
    });

    encoder.writeObject(object);
    encoder.close();

    if (noisy)
        System.out.println(outputStream.toString());

    return outputStream.toByteArray();
}

From source file:de.willuhn.jameica.hbci.io.csv.ProfileUtil.java

/**
 * Speichert die Profile./*from www.j  a  v  a  2s .c  o  m*/
 * @param format das Format.
 * @param profiles die zu speichernden Profile.
 */
public static void store(Format format, List<Profile> profiles) {
    if (format == null) {
        Application.getMessagingFactory().sendMessage(
                new StatusBarMessage(i18n.tr("Kein Format ausgewhlt"), StatusBarMessage.TYPE_ERROR));
        return;
    }

    if (profiles == null) {
        Application.getMessagingFactory().sendMessage(
                new StatusBarMessage(i18n.tr("Keine Profile angegeben"), StatusBarMessage.TYPE_ERROR));
        return;
    }

    // 2. Mal schauen, ob wir ein gespeichertes Profil fuer das Format haben
    File dir = new File(Application.getPluginLoader().getPlugin(HBCI.class).getResources().getWorkPath(),
            "csv");
    if (!dir.exists()) {
        Logger.info("creating dir: " + dir);
        if (!dir.mkdirs()) {
            Application.getMessagingFactory()
                    .sendMessage(new StatusBarMessage(
                            i18n.tr("Ordner {0} kann nicht erstellt werden", dir.getAbsolutePath()),
                            StatusBarMessage.TYPE_ERROR));
            return;
        }
    }

    File file = new File(dir, format.getClass().getName() + ".xml");

    Logger.info("writing csv profile " + file);
    XMLEncoder encoder = null;
    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
        encoder.setExceptionListener(new ExceptionListener() {
            public void exceptionThrown(Exception e) {
                throw new RuntimeException(e);
            }
        });

        for (Profile p : profiles) {
            // Das System-Profil wird nicht mit gespeichert
            if (p.isSystem())
                continue;

            // Ebenso Profile ohne Namen.
            if (StringUtils.trimToNull(p.getName()) == null)
                continue;

            encoder.writeObject(p);
        }
    } catch (Exception e) {
        Logger.error("unable to store profile " + file, e);
    } finally {
        if (encoder != null) {
            try {
                encoder.close();
            } catch (Exception e) {
                /* useless */}
        }
    }
}

From source file:com.db4o.sync4o.ui.Db4oSyncSourceConfigPanel.java

private static void showTestUI() {

    // Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    // Create and set up the window.
    JFrame frame = new JFrame("Db4oSyncSourceConfigPanel Test Harness");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    final Db4oSyncSource source = new Db4oSyncSource();
    Db4oSyncSourceConfigPanel p = new Db4oSyncSourceConfigPanel();
    p.setManagementObject(new SyncSourceManagementObject(source, null, null, null, null));
    p.updateForm();//from  w w w.  j a va  2  s. c om
    p.setOpaque(true); // content panes must be opaque
    frame.setContentPane(p);

    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent ev) {

            XMLEncoder encoder = null;
            try {
                FileOutputStream s = new FileOutputStream("test.xml");
                encoder = new XMLEncoder(s);
                encoder.setExceptionListener(new ExceptionListener() {
                    public void exceptionThrown(Exception exception) {
                        exception.printStackTrace();
                    }
                });
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            encoder.writeObject((Object) source);
            encoder.flush();
            encoder.close();

        }

    });

    // Display the window.
    frame.pack();
    frame.setVisible(true);

}

From source file:lu.lippmann.cdb.graph.GraphUtil.java

/**
 * /*  w  ww .ja v a2s .  co  m*/
 * @param g
 * @return
 */
public static String saveGraphWithOperation(final GraphWithOperations g) {
    final ArrayList<GraphOperation> operations = new ArrayList<GraphOperation>(g.getOperations());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final XMLEncoder xmlEncoder = new XMLEncoder(baos);
    xmlEncoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(final Exception e) {
            throw new IllegalStateException(e);
        }
    });

    xmlEncoder.writeObject(new GraphTO(g.getId(), operations, g.getUntitledVertexCount(),
            g.getUntitledEdgeCount(), g.getVariables()));
    xmlEncoder.close();
    return baos.toString();
}

From source file:org.settings4j.objectresolver.JavaXMLBeansObjectResolverTest.java

private byte[] objectToContent(final Object value) {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    final XMLEncoder encoder = new XMLEncoder(byteArrayOutputStream);
    encoder.setExceptionListener(new LogEncoderExceptionListener(value));

    LOG.debug("START Writing Object {} with XMLEncoder", value.getClass().getName());
    encoder.writeObject(value);//from   www .  ja  va  2  s . co m
    LOG.debug("FINISH Writing Object {} with XMLEncoder", value.getClass().getName());

    encoder.flush();
    encoder.close();
    return byteArrayOutputStream.toByteArray();
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * To xml.//  w w w.  j ava 2 s .  c om
 *
 * @param obj
 *            the obj
 * @return the string
 */
// ////////////////////////////////////////////////////////////////////
public static String toXml(final Object obj) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    // XStream x = createXStream();
    // String xml = x.toXML(obj);
    // return xml;
    final XMLEncoder e = new XMLEncoder(out);
    e.setExceptionListener(new XmlEncoderExceptionListener());
    // e.setPersistenceDelegate(Object.class, new MyPersistenceDelegate());
    e.writeObject(obj);
    e.close();
    return out.toString();
    // return null;
}

From source file:net.sf.taverna.t2.provenance.lineageservice.EventProcessor.java

/**
 * log raw event to file system//from w  w w  .  ja  v  a 2  s . c om
 * 
 * @param content
 * @param eventType
 * @throws IOException
 */
public void saveEvent(ProvenanceItem provenanceItem, SharedVocabulary eventType) throws IOException {

    // HACK -- XMLEncoder fails on IterationEvents and there is no way to catch the exception...
    // so avoid this case
    if (eventType.equals(SharedVocabulary.ITERATION_EVENT_TYPE)) {
        return;
    }

    //      System.out.println("saveEvent: start");

    File f1 = null;

    f1 = new File(TEST_EVENTS_FOLDER);
    FileUtils.forceMkdir(f1);

    String fname = "event_" + eventCnt++ + "_" + eventType + ".xml";
    File f = new File(f1, fname);

    //      FileWriter fw = new FileWriter(f);

    XMLEncoder en = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(f)));

    en.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception e) {
            logger.warn("XML encoding ERROR", e);
            return;
        }
    });

    logger.debug("saving to " + f); // save event for later inspection
    logger.debug(provenanceItem);

    en.writeObject(provenanceItem);

    logger.debug("writer ok");
    en.close();
    logger.debug("closed");

    //      fw.write(content);
    //      fw.flush();
    //      fw.close();

    //      FileWriter fw = new FileWriter(f);
    //      fw.write(content);
    //      fw.flush();
    //      fw.close();

    //      System.out.println("saved as file " + fname);

}