This discussion is archived
2 Replies Latest reply: Jun 28, 2011 1:43 PM by 804134 RSS

Exception Management with XMLDecoder

804134 Newbie
Currently Being Moderated
Hi guys,

i have a question related to XMLDecoder, i use this function to create some objects saved to an XML file using XMLEncoder, the problem comes when i select an XML wich hasn´t got in it the information related to de Java Beans i need to load.

I dont have much experience with exception management but this is what i got so far:

public static Repository loadFromXML(String pathXML){
Repository pru = new Repository();

try {
InputStream is = new BufferedInputStream( new FileInputStream(pathXML));

ExceptionListener exception = new ExceptionListener() {

public void exceptionThrown(Exception e) {

System.out.println("EXCEPCION XMLDECODER");
System.out.println(e.getMessage().toString());
//System.out.println(e.initCause(e).toString());

if(e.getMessage().toString().equals("Premature end of file.")){
System.out.println("PREMATURE");
}

if(e.getMessage().toString().equals("Content is not allowed in prolog.")){
System.out.println("CONTENT");
}




if(e.getMessage().toString().equals("NullPointerException")){
System.out.println("EXCEPCION NULL POINTER");
}



System.out.println("FIN");
e.printStackTrace();

}
};

Object owner = new Object();

XMLDecoder d = new XMLDecoder(is,owner,exception);





//probar retornando parameters
System.out.println("antes");
System.out.println(d.readObject().toString());

pru=(Repository)d.readObject();
System.out.println("despues");

if(pru==null){
System.out.println("OJO");
}

if(pru.getDriverPluginVector().isEmpty() && pru.getPluginVector().isEmpty()){
JOptionPane.showMessageDialog(null, "The repository is empty","Information", JOptionPane.INFORMATION_MESSAGE);

}


}
catch (IOException ex)
{ //System.out.println(ex.getMessage());
System.out.println("No existe fichero XML");
}
return pru;
} // end Repository loadFromXML(String pathXML)

I need to somehow check that the XML has the correct format (the Java Beans i need to load) before trying to use pru=(Repository)d.readObject(); because if not i get an exception. How can i know all the XML exception i need to take care off? Im a bit confussed, lets see if someone can give me a hand.

If i pass an empty .xml i get this output:
run:
antes
EXCEPCION XMLDECODER
Premature end of file.
PREMATURE
FIN
org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1059)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:364)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:142)
at java.beans.XMLDecoder.getHandler(XMLDecoder.java:238)
at java.beans.XMLDecoder.readObject(XMLDecoder.java:201)
at model.Repository.loadFromXML(Repository.java:241)
at gui.Open.initComponents(Open.java:82)
at gui.Open.<init>(Open.java:28)
at gui.Gui.openButtonActionPerformed(Gui.java:427)
at gui.Gui.access$200(Gui.java:28)
at gui.Gui$3.actionPerformed(Gui.java:211)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
at java.util.Vector.lastElement(Vector.java:456)
at com.sun.beans.ObjectHandler.lastExp(ObjectHandler.java:134)
at com.sun.beans.ObjectHandler.dequeueResult(ObjectHandler.java:138)
at java.beans.XMLDecoder.readObject(XMLDecoder.java:201)
at model.Repository.loadFromXML(Repository.java:241)
at gui.Open.initComponents(Open.java:82)
at gui.Open.<init>(Open.java:28)
at gui.Gui.openButtonActionPerformed(Gui.java:427)
at gui.Gui.access$200(Gui.java:28)
at gui.Gui$3.actionPerformed(Gui.java:211)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points