Details
Description
I am using Castor to unmarshal a very simple XML document and I wish to have Castor indicate when it encounters anything in the XML that it cannot unmarshal. I am not using a mapping file as the XML to Java conversion is simple and straight-forward.
I have searched high and low and googled and looked in the javadocs and done all I can to find out how to do this, but to no success. Castor continues to silently ignore the error in the XML.
Take the following class:
public class Library {
private List<String> books;
public Library() { books = new LinkedList<String>(); }
public void addBook(String title) { books.add(title); }
public String toString() {
return "Books = " + books;
}
}
with this XML file "library.xml" (the misspelled "boook" is intentional):
<library>
<book>War And Peace</book>
<book>Ulysses</book>
<boook>Crime And Punishment</boook>
</library>
and with this Castor application:
public class CastorTest {
public static void main(String[] args) {
try {
Unmarshaller unmarshaller = new Unmarshaller(Library.class);
unmarshaller.setIgnoreExtraAttributes(false);
unmarshaller.setIgnoreExtraElements(false);
unmarshaller.setValidation(true);
Reader reader = new FileReader(new File("library.xml"));
Library library = (Library)unmarshaller.unmarshal(reader);
System.out.println(library);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The 3rd book in the library file is misspelled "boook". I want Castor to tell me somehow that this is something it does not understand.
No matter what I do, the program runs without throwing an Exception or printing an error message. The output of the above is:
Books = [War And Peace, Ulysses]
The problem is with the following line of code taken from UnmarshalHandler:
//-- If we are skipping elements that have appeared in the XML but for //-- which we have no mapping, increase the ignore depth counter and return if (! _strictElements) { ++_ignoreElementDepth; //-- remove the StateInfo we just added _stateInfo.pop(); if (LOG.isDebugEnabled()) { LOG.debug(mesg + " - ignoring extra element."); } return; } //if we have no field descriptor and //the class descriptor was introspected //just log it else if (Introspector.introspected(classDesc)) { LOG.warn(mesg); return; } //-- otherwise report error since we cannot find a suitable //-- descriptor else { throw new SAXException(mesg); }It can be clearly seen that if the class descriptor was 'created' by introspection rather than a class and/or field mapping, just a warning is emitted, but no exception will be thrown.
//-- If we are skipping elements that have appeared in the XML but for //-- which we have no mapping, increase the ignore depth counter and return if (! _strictElements) { ++_ignoreElementDepth; //-- remove the StateInfo we just added _stateInfo.pop(); if (LOG.isDebugEnabled()) { LOG.debug(mesg + " - ignoring extra element."); } return; } //if we have no field descriptor and //the class descriptor was introspected //just log it else if (Introspector.introspected(classDesc)) { LOG.warn(mesg); return; } //-- otherwise report error since we cannot find a suitable //-- descriptor else { throw new SAXException(mesg); }