Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 313249

Summary: XMLResource cannot save an empty resource
Product: [Modeling] EMF Reporter: Bryan Hunt <bhunt>
Component: XML/XMIAssignee: Ed Merks <Ed.Merks>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 2.5.0   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Bryan Hunt CLA 2010-05-17 17:51:41 EDT
If you create an XMLResource and then try to save it without adding an object to its contents, you will get a BasicEList$BasicIndexOutOfBoundsException.  It appears the problem is that in XMLSaveImpl line 592 calls wreiteTopObjects(contents) when contents.size() != 1 which then calls contents.get(0) which throws the exception because contents.size() == 0.
Comment 1 Ed Merks CLA 2010-05-17 18:04:21 EDT
SAX can't parse an empty file so it's good for save to fail one way or another.
Comment 2 Bryan Hunt CLA 2010-05-17 18:07:37 EDT
In this case, it doesn't make sense to write a header with empty content?  If that's the case, it might be nice to have a better exception ... something like:

if(contents.isEmpty())
  throw new IOException("Cannot save a resource with no contents");
Comment 3 Ed Merks CLA 2010-05-17 18:16:24 EDT
No, an XML resource must have a single root element.  For all I know, someone already catches an index out of bounds exception so while I agree that a descriptive message would be a marginal improvement, the current failure is pretty obvious as well.  In fact, this is really a smaller problem than the fact that if you have more than one element, only the first is saved and the rest are silently ignored...