Example usage for java.beans XMLEncoder close

List of usage examples for java.beans XMLEncoder close

Introduction

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

Prototype

public void close() 

Source Link

Document

This method calls flush , writes the closing postamble and then closes the output stream associated with this stream.

Usage

From source file:Main.java

public static String parseObj2Xml(Object object) throws Exception {
    XMLEncoder encoder = null;
    String str = null;//from   w  w  w .j  ava2  s.c o m
    BufferedOutputStream bufOut = null;
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        bufOut = new BufferedOutputStream(out);
        encoder = new XMLEncoder(bufOut);
        encoder.writeObject(object);
        encoder.close();
        str = new String(out.toByteArray());
    } catch (Exception ex) {
        throw ex;
    } finally {
        if (out != null)
            out.close();
        if (bufOut != null)
            bufOut.close();
    }
    return str;
}

From source file:resources.ResourceManager.java

public static void saveLayoutTable(File file, LayoutTableModel tableModel) {
    try {//from   w ww . jav a2 s.  co  m
        final XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
        encoder.writeObject(tableModel.getLayoutTableItemList());
        encoder.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.clickframes.testframes.TestRunner.java

/**
 * Use this advanced interface to only run tests approved by this filter
 *
 * @param appspec/*w w w . j  av  a2  s .  co m*/
 * @param filenameFilter
 * @return
 * @throws Exception
 *
 * @author Vineet Manohar
 */
private static ProjectTestResults runAllTestSuits(TestProfile testProfile, Techspec techspec, Appspec appspec,
        String filterName, FileFilter fileFilter) throws Exception {
    // prepare tests
    TestPreparationUtil.prepareAllTestSuites(testProfile, techspec, appspec, filterName, fileFilter);

    File suiteTargetDirectory = new File(
            "target" + File.separator + "clickframes" + File.separator + "selenium" + File.separator
                    + ClickframeUtils.convertSlashToPathSeparator(filterName) + File.separator + "tests");

    Properties properties = new Properties();

    properties.put("browser", testProfile.getBrowser());

    properties.put("suite", suiteTargetDirectory.getAbsolutePath());

    if (testProfile.getBaseUrl() != null) {
        properties.put("startURL", testProfile.getBaseUrl());
    }

    File resultsDir = new File(
            "target" + File.separator + "clickframes" + File.separator + "selenium" + File.separator
                    + ClickframeUtils.convertSlashToPathSeparator(filterName) + File.separator + "results");

    FileUtils.deleteDirectory(resultsDir);
    resultsDir.mkdirs();
    properties.put("result", resultsDir.getAbsolutePath());

    // prepare user extensions
    File userExtensionsFile = new File(System.getProperty("java.io.tmpdir"), "user-extensions.js");
    if (userExtensionsFile.exists()) {
        userExtensionsFile.delete();
    }

    IOUtils.copy(TestRunner.class.getResourceAsStream("/user-extensions.js"),
            new FileOutputStream(userExtensionsFile));
    properties.put("userExtensions", userExtensionsFile.getAbsolutePath());

    MultiHTMLSuiteRunner runner = null;

    boolean hasFiles = true;

    if (hasFiles) {
        runner = MultiHTMLSuiteRunner.execute(properties);
    }

    ProjectTestResults projectTestResults = createFrom(runner);

    // serialize the projectTestResults using java serialization
    XMLEncoder e = new XMLEncoder(
            new BufferedOutputStream(new FileOutputStream(new File(resultsDir, "testResults.xml"))));
    e.writeObject(projectTestResults);
    e.close();

    // write test result summary
    writeTestResultSummaryFile(techspec, appspec, projectTestResults, filterName);

    // aggregrateTestResults(techspec, appspec, filterName);

    return projectTestResults;
}

From source file:org.dawnsci.common.richbeans.beans.BeanUI.java

/**
 * Used externally to the GDA.//  w  ww  .  ja  va  2s. c  om
 * 
 * @param bean
 * @return the string
 */
public static String getString(Object bean) throws Exception {

    final ByteArrayOutputStream stream = new ByteArrayOutputStream();

    final ClassLoader original = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(bean.getClass().getClassLoader());
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(stream));
        e.writeObject(bean);
        e.close();
        return stream.toString("UTF-8");
    } finally {
        Thread.currentThread().setContextClassLoader(original);
        stream.close();
    }
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * SerializableXML???/*from  w ww. j  ava2  s .  c om*/
 * @param obj Serializable
 * @return ??XML
 * @throws IOException
 */
public static String encodeXML(Serializable obj) throws IOException {
    ByteArrayOutputStream baos = null;
    XMLEncoder enc = null;
    String xml = null;

    try {
        baos = new ByteArrayOutputStream();
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        xml = baos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        log.warn(e);
    } finally {
        if (enc != null) {
            enc.close();
        }
        if (baos != null) {
            baos.close();
        }
    }

    return xml;
}

From source file:gov.llnl.lc.smt.command.config.SmtConfig.java

public static boolean writeConfigFile(String fileName, Map<String, String> config) {
    boolean success = false;
    logger.info("Writing config to: " + fileName);
    if ((fileName != null) && (config != null)) {
        FileOutputStream fos;// w w  w.j a v a  2 s .  c  o  m
        try {
            fos = new FileOutputStream(fileName);
            XMLEncoder xenc = new XMLEncoder(fos);

            // save this file name within the config
            config.put(SmtProperty.SMT_WRITE_CONFIG.getName(), fileName);

            // Write object.
            xenc.writeObject(config);
            xenc.close();
            success = true;
        } catch (FileNotFoundException e) {
            logger.severe("Unable to write to: " + fileName);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {

    }
    return success;
}

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();/* ww  w.  j a v a 2s .  co m*/
    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:de.willuhn.jameica.hbci.io.csv.ProfileUtil.java

/**
 * Speichert die Profile./*from w  w  w  .j  av a 2s. com*/
 * @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:org.jumpmind.vaadin.ui.sqlexplorer.DefaultSettingsProvider.java

@Override
public void save(Settings settings) {
    synchronized (getClass()) {
        File file = getSettingsFile();
        FileOutputStream os = null;
        try {//www  .j  a  v a  2  s  .c  om
            os = new FileOutputStream(file, false);
            XMLEncoder encoder = new XMLEncoder(os);
            encoder.writeObject(settings);
            encoder.close();
            this.settings = settings;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            IOUtils.closeQuietly(os);
        }
    }
}

From source file:org.seasar.cadhelin.ControllerMetadata.java

public void saveConverterSettings(OutputStream os) {
    XMLEncoder encoder = new XMLEncoder(os);
    encoder.writeObject(getConverters());
    encoder.close();
}