jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • castor
  • CASTOR-2015

Castor will not complain or generate a warning when encountering unknowns during unmarshalling without a mapping file

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.1.1
  • Fix Version/s: 1.2
  • Component/s: XML
  • Labels:
    None
  • Testcase included:
    yes

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]

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. XML File
    Books.xml
    11/Jun/07 5:40 PM
    0.1 kB
    Reginals Ratherfurp
  2. Java Source File
    CastorTest.java
    11/Jun/07 5:39 PM
    0.6 kB
    Reginals Ratherfurp
  3. Java Source File
    Library.java
    11/Jun/07 5:38 PM
    0.3 kB
    Reginals Ratherfurp
  4. Text File
    patch.c2015.20070913.txt
    13/Sep/07 10:06 AM
    5 kB
    Werner Guttmann

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Werner Guttmann added a comment - 13/Sep/07 9:46 AM

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.

Show
Werner Guttmann added a comment - 13/Sep/07 9:46 AM 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.
Hide
Permalink
Werner Guttmann added a comment - 13/Sep/07 9:48 AM

My gut feeling is that this should be changed to throw an exception for 'introspected' cases as well, bu as other folks may rely on the current behavior, I might as well introduce et another lenient property. Any objections ?

Show
Werner Guttmann added a comment - 13/Sep/07 9:48 AM My gut feeling is that this should be changed to throw an exception for 'introspected' cases as well, bu as other folks may rely on the current behavior, I might as well introduce et another lenient property. Any objections ?
Hide
Permalink
Werner Guttmann added a comment - 13/Sep/07 10:06 AM

Initial patch for review. This patch adds a new property to configure leniency for element strictness for introspected classes/members. Default is true; in other words, in order to change the current behaviour, set the value of the new property to false in a custom castor.properties file.

Show
Werner Guttmann added a comment - 13/Sep/07 10:06 AM Initial patch for review. This patch adds a new property to configure leniency for element strictness for introspected classes/members. Default is true; in other words, in order to change the current behaviour, set the value of the new property to false in a custom castor.properties file.
Hide
Permalink
Werner Guttmann added a comment - 13/Sep/07 10:07 AM

Just forgot to say that I am open to discuss what the default behavior really should be .. .

Show
Werner Guttmann added a comment - 13/Sep/07 10:07 AM Just forgot to say that I am open to discuss what the default behavior really should be .. .
Hide
Permalink
Werner Guttmann added a comment - 14/Sep/07 2:57 AM

For any change requests to the default behavior, please file a follow-up issue

Show
Werner Guttmann added a comment - 14/Sep/07 2:57 AM For any change requests to the default behavior, please file a follow-up issue

People

  • Assignee:
    Werner Guttmann
    Reporter:
    Reginals Ratherfurp
Vote (0)
Watch (0)

Dates

  • Created:
    11/Jun/07 5:37 PM
    Updated:
    14/Mar/08 6:41 AM
    Resolved:
    14/Sep/07 2:57 AM

Time Tracking

Estimated:
Not Specified
Original Estimate - Not Specified
Remaining:
0m
Remaining Estimate - 0 minutes
Logged:
20m
Time Spent - 20 minutes
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.