Example usage for java.beans XMLDecoder XMLDecoder

List of usage examples for java.beans XMLDecoder XMLDecoder

Introduction

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

Prototype

public XMLDecoder(InputSource is) 

Source Link

Document

Creates a new decoder to parse XML archives created by the XMLEncoder class.

Usage

From source file:org.jtrfp.trcl.gui.ConfigWindow.java

private boolean readSettingsFromFile(File f) {
    try {/*from  ww  w . j  a v  a  2 s  .  c  om*/
        FileInputStream is = new FileInputStream(f);
        XMLDecoder xmlDec = new XMLDecoder(is);
        xmlDec.setExceptionListener(new ExceptionListener() {
            @Override
            public void exceptionThrown(Exception e) {
                e.printStackTrace();
            }
        });
        TRConfiguration src = (TRConfiguration) xmlDec.readObject();
        xmlDec.close();
        if (config != null)
            BeanUtils.copyProperties(config, src);
        else
            config = src;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Failed to read the specified file:\n" + e.getLocalizedMessage(),
                "File read failure", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    return true;
}

From source file:org.scantegrity.scanner.Scanner.java

private static void checkForPreviousCounters() {
    ListIterator<String> it = c_outDirs.listIterator();
    while (it.hasNext()) {
        try {/*  w w w  . ja  v a  2s.  co  m*/
            String l_path = it.next() + File.separator + "count.xml";
            File l_file = new File(l_path);

            if (l_file.exists()) {
                c_log.log(Level.WARNING, "count.xml exists. Updating counters.");

                //copy the file
                try {
                    FileUtils.copyFile(l_file, new File(l_path + "_bak"), true);
                } catch (IOException e) {
                    c_log.log(Level.WARNING, "Could not backup previous counter file.");
                }

                XMLDecoder l_countFile = new XMLDecoder(new BufferedInputStream(new FileInputStream(l_path)));

                l_countFile.readObject();
                c_scanCount = (Integer) l_countFile.readObject();
                l_countFile.readObject();
                c_ballotCount = (Integer) l_countFile.readObject();
                l_countFile.readObject();
                c_errorCount = (Integer) l_countFile.readObject();
                l_countFile.close();

                c_log.log(Level.WARNING, "Previous counts: ScanCount=" + c_scanCount + " BallotCount="
                        + c_ballotCount + " ErrorCount=" + c_errorCount);

            }
        } catch (FileNotFoundException e) {
            c_log.log(Level.SEVERE, "Unable to open count.xml");
        }
    }
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private void initRecentFiles() {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, RECENT_FILES_XML_FILENAME);

    if (file.isFile() && this.recentFiles == null) {
        XMLDecoder d = null;//from w w  w .  ja  v  a 2  s  .c o  m
        try {
            d = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));

            this.recentFiles = transformToList(String.class, d.readObject());
        } catch (Throwable ex) {
            if (logger.isWarnEnabled())
                logger.warn("Exception while loading recentFiles from file '" + file.getAbsolutePath() + "'!",
                        ex);
            IOUtilities.interruptIfNecessary(ex);
        } finally {
            if (d != null) {
                d.close();
            }
        }
    }

    if (this.recentFiles == null) {
        this.recentFiles = new ArrayList<>();
    }
}

From source file:statechum.analysis.learning.Visualiser.java

/** Loads the layout of the specific graph in the list.
 *
 *  @param whether to ignore loading errors.
 *  @param graphNumber the number of the graph to load.
 *///from  www .j av a 2s .c o  m
protected void restoreLayout(boolean ignoreErrors, int graphNumber) {
    XMLDecoder decoder = null;
    try {
        String fileName = getLayoutFileName(graphs.get(graphNumber));
        if (propName >= 0 && (new File(fileName)).canRead()) {
            decoder = new XMLDecoder(new FileInputStream(fileName));
            //@SuppressWarnings("unchecked")
            Map<Integer, DoublePair> map = (Map<Integer, DoublePair>) decoder.readObject();

            // Most rotate/share/translate are stateless, so I only need to get the cumulative transform
            // for layout and view via getTransform() which should return AffineTransform
            // which I should be able to persist into XML.
            // Only ScalingGraphMousePlugin has a state
            // in the form of getIn()/setIn()/getOut()/setOut().
            viewer.getViewTransformer().setToIdentity();
            viewer.getViewTransformer()
                    .concatenate(((XMLAffineTransformSerialised) decoder.readObject()).getAffineTransform());

            viewer.getLayoutTransformer().setToIdentity();
            viewer.getLayoutTransformer()
                    .concatenate(((XMLAffineTransformSerialised) decoder.readObject()).getAffineTransform());
            ((XMLModalGraphMouse) viewer.getGraphMouse()).restore(decoder);
            layoutOptions.put(propName, (LayoutOptions) decoder.readObject());
            ((XMLPersistingLayout) viewer.getModel().getGraphLayout()).initialize(getSize());
            ((XMLPersistingLayout) viewer.getModel().getGraphLayout()).restore(map);
            viewer.invalidate();
        }
    } catch (Exception e1) {
        if (!ignoreErrors) {
            e1.printStackTrace();
        }
    } finally {
        if (decoder != null) {
            decoder.close();
            decoder = null;
        }
    }
}

From source file:com.alvermont.terraj.fracplanet.ui.AbstractTerrainViewerFrame.java

private void importParamsItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_importParamsItemActionPerformed
{//GEN-HEADEREND:event_importParamsItemActionPerformed

    final ImportXMLSettingsDialog dialog = new ImportXMLSettingsDialog(this, true);

    try {//from www.j  a  v  a2s .  c  o  m
        dialog.setVisible(true);

        if (dialog.isConfirmed()) {
            log.debug("Import confirmed");

            try {
                final File target = FileUtils.addExtension(dialog.getSelectedFile(), ".xml");

                final XMLDecoder decoder = new XMLDecoder(new FileInputStream(target));

                final AllFracplanetParameters newParams = (AllFracplanetParameters) decoder.readObject();

                final AllFracplanetParameters params = getParameters();

                // set the requested items in the parameters
                if (dialog.isImportTerrain()) {
                    params.setTerrainParameters(newParams.getTerrainParameters());
                }

                if (dialog.isImportRender()) {
                    params.setRenderParameters(newParams.getRenderParameters());
                }

                if (dialog.isImportExport()) {
                    params.setExportParameters(newParams.getExportParameters());
                }

                if (dialog.isImportCamera()) {
                    params.setCameraPositionParameters(newParams.getCameraPositionParameters());
                }

                // ok, update with any changes to the parameters
                setParameters(params);

                display.setRenderParameters(params.getRenderParameters());
                display.setCameraPosition(params.getRenderParameters().getCameraPosition());
                cameraPosDialog.setPositions(params.getCameraPositionParameters().getPositions());

                regenerate();
            } catch (IOException ioe) {
                log.error("Error reading file", ioe);

                JOptionPane.showMessageDialog(this,
                        "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Loading",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    } finally {
        dialog.setVisible(false);
        dialog.dispose();
    }
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private void initPreviousSearchStrings() {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, PREVIOUS_SEARCH_STRINGS_XML_FILENAME);

    if (file.isFile() && this.previousSearchStrings == null) {
        XMLDecoder d = null;//from   ww w . j  av  a2 s . c  o  m
        try {
            d = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));

            this.previousSearchStrings = transformToList(String.class, d.readObject());
        } catch (Throwable ex) {
            if (logger.isWarnEnabled())
                logger.warn("Exception while loading previous search strings from file '"
                        + file.getAbsolutePath() + "'!", ex);
            IOUtilities.interruptIfNecessary(ex);
        } finally {
            if (d != null) {
                d.close();
            }
        }
    }

    if (this.previousSearchStrings == null) {
        this.previousSearchStrings = new ArrayList<>();
    }
}

From source file:com.alvermont.terraj.fracplanet.ui.AbstractTerrainViewerFrame.java

private void loadParamsItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loadParamsItemActionPerformed
{//GEN-HEADEREND:event_loadParamsItemActionPerformed

    final int choice = this.xmlChooser.showOpenDialog(this);

    if (choice == JFileChooser.APPROVE_OPTION) {
        try {// w w w  .j a v a  2  s .c o m
            final File target = FileUtils.addExtension(this.xmlChooser.getSelectedFile(), ".xml");

            final XMLDecoder decoder = new XMLDecoder(new FileInputStream(target));

            final AllFracplanetParameters params = (AllFracplanetParameters) decoder.readObject();

            setParameters(params);

            display.setRenderParameters(params.getRenderParameters());
            display.setCameraPosition(params.getRenderParameters().getCameraPosition());
            this.cameraPosDialog.setPositions(params.getCameraPositionParameters().getPositions());

            regenerate();
        } catch (IOException ioe) {
            log.error("Error reading file", ioe);

            JOptionPane.showMessageDialog(this,
                    "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Loading",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:com.alvermont.terraj.stargen.ui.StargenFrame.java

private void loadMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loadMenuItemActionPerformed
{//GEN-HEADEREND:event_loadMenuItemActionPerformed
    final int choice = this.xmlChooser.showOpenDialog(this);

    if (choice == JFileChooser.APPROVE_OPTION) {
        try {//w  w  w . jav  a 2 s.co m
            final File target = FileUtils.addExtension(this.xmlChooser.getSelectedFile(), ".xml");

            final XMLDecoder decoder = new XMLDecoder(new FileInputStream(target));

            final StargenParameters params = (StargenParameters) decoder.readObject();

            this.parameters = params;
            updateFromParameters(params);
        } catch (IOException ioe) {
            log.error("Error reading file", ioe);

            JOptionPane.showMessageDialog(this,
                    "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Loading",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private void initLevelColors() {
    if (levelColors == null) {
        File appPath = getStartupApplicationPath();
        File levelColorsFile = new File(appPath, LEVEL_COLORS_XML_FILENAME);

        if (levelColorsFile.isFile()) {
            XMLDecoder d = null;//from www.java2 s .  c  o  m
            try {
                d = new XMLDecoder(new BufferedInputStream(new FileInputStream(levelColorsFile)));

                levelColors = transformToMap(LoggingEvent.Level.class, ColorScheme.class, d.readObject());
            } catch (Throwable ex) {
                if (logger.isWarnEnabled())
                    logger.warn("Exception while loading Level-ColorSchemes from file '"
                            + levelColorsFile.getAbsolutePath() + "'!", ex);
                levelColors = null;
                IOUtilities.interruptIfNecessary(ex);
            } finally {
                if (d != null) {
                    d.close();
                }
            }
        }
    }

    if (levelColors != null && levelColors.size() != DEFAULT_LEVEL_COLOR_SCHEMES.size()) {
        if (logger.isWarnEnabled())
            logger.warn("Reverting Level-ColorSchemes to defaults.");
        levelColors = null;
    }

    if (levelColors == null) {
        levelColors = cloneLevelColors(DEFAULT_LEVEL_COLOR_SCHEMES);
    }
}

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

/**
 * To object./* w w w .  jav  a  2  s  .  c o  m*/
 *
 * @param xml
 *            the xml
 * @return the object
 */
// ////////////////////////////////////////////////////////////////////
public static Object toObject(final String xml) {
    // XStream x = createXStream();
    // return x.fromXML(xml);
    // try {
    final ByteArrayInputStream out = new ByteArrayInputStream(xml.getBytes());
    final XMLDecoder encoder = new XMLDecoder(out);
    final Object object = encoder.readObject();
    //
    encoder.close();
    return object;
    // } catch (Exception e) {
    // System.err.println("Failed to decode object : \n" + xml);
    // return null;
    // }
    // return null;
}