@Test
  public void testSaveLoad() throws FileNotFoundException, IOException {
    System.out.println("---  testSaveLoad...");
    XMLEncoder xmlEncoder = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;

    File f = new File("testProjects/TEMP_PROJECTS/Test.ser");

    System.out.println("Saving to: " + f.getCanonicalPath());

    fos = new FileOutputStream(f);
    bos = new BufferedOutputStream(fos);
    xmlEncoder = new XMLEncoder(bos);

    // Metric m = Metric.PATH_LENGTH_FROM_ROOT;
    Object o1 = pg1;

    System.out.println("Pre: " + o1);

    // ProximalPref p = ProximalPref.MOST_PROX_AT_0;

    xmlEncoder.writeObject(o1);
    xmlEncoder.close();

    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    XMLDecoder xd = new XMLDecoder(bis);

    Object o2 = xd.readObject();

    System.out.println("Post: " + o2);

    assertEquals(o2, o1);
  }
Esempio n. 2
0
 /**
  * Create a new <code>PanelModel</code> from an object serialized using <code>XMLEncoder</code>.
  * For example,
  *
  * <pre>
  *   PanelModel pModel;
  *   Page page = new Page();
  *       try {
  *    pModel = PanelModel.loadFromXML(new BufferedInputStream(
  *                       new FileInputStream(outpath)));
  *    page.setPanelModel(pModel);
  *  } catch (FileNotFoundException fnfe) {
  *    JOptionPane.showMessageDialog(this, "Error openning file",
  *                                  "File Open Error", JOptionPane.ERROR_MESSAGE);
  *  } catch (InvalidObjectException ioe) {
  *    JOptionPane.showMessageDialog(this, "File does not contain a PanelModel",
  *                                  "PanelModel Not Found",
  *                                  JOptionPane.ERROR_MESSAGE);
  *  }
  *
  * </pre>
  *
  * @param is InputStream
  * @return PanelModel object
  * @throws InvalidObjectException
  * @see java.beans.XMLEncoder
  */
 public static PanelModel loadFromXML(InputStream is) throws InvalidObjectException {
   PanelModel pModel = null;
   XMLDecoder xd = new XMLDecoder(is);
   Object obj = xd.readObject();
   xd.close();
   if (obj instanceof PanelModel) {
     pModel = (PanelModel) obj;
     pModel.repair();
   } else {
     throw new InvalidObjectException("XML file does not contain a PanelModel");
   }
   return pModel;
 }
  public void load() {
    // show file chooser dialog
    int r = chooser.showOpenDialog(null);

    // if file selected, open
    if (r == JFileChooser.APPROVE_OPTION) {
      try {
        File file = chooser.getSelectedFile();
        XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
        decoder.readObject();
        decoder.close();
      } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e);
      }
    }
  }