Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

View: New views
7 Messages — Rating Filter:   Alert me  

Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Im using JAXB RI 2.1 to unmarshal my documents, and I'm currently facing the following problem: I have different input XML documents that contain type information via the [i]xsi:type[/i] attribute. The application does not know all possible types used in the input documents. When an unknown type occurs, I want JAXB to report an error. Unfortunately, JAXB just uses the base type and does not report an error.

I've come across <a href="http://weblogs.java.net/blog/kohsuke/archive/2006/04/why_doesnt_jaxb.html" target="_blank">Kohsuke's blog</a> where the use of a ValidationEventHandler is recommended. Unfortunately, no error is reported this way either.

Can somebody help out?
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=354226

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by Glen Mazza :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Perhaps that functionality is intentional, using the base type if a subtype is unknown.  Can you make the base type abstract in your XSD and see if you get error messages that way?  Or is that not something you would wish to do--i.e., sometimes you *do* want the base type instantiated?

Glen

metro-3 wrote:
Hi,

Im using JAXB RI 2.1 to unmarshal my documents, and I'm currently facing the following problem: I have different input XML documents that contain type information via the [i]xsi:type[/i] attribute. The application does not know all possible types used in the input documents. When an unknown type occurs, I want JAXB to report an error. Unfortunately, JAXB just uses the base type and does not report an error.

I've come across Kohsuke's blog where the use of a ValidationEventHandler is recommended. Unfortunately, no error is reported this way either.

Can somebody help out?
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=354226

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net

Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Glen,

thanks for your answer.

> Can you make the base type abstract in your XSD and see if you get error messages that way?

If I remember correctly, an abstract base type would indeed lead to an exception (which would be OK for me). Unfortunately, I cannot change the schema, and the base type is not defined abstract. This was done because the base type is sufficient for the default case. We have a lot of xml documents for the default case. They are not generated, thus 'xsi:type'-attribute definitions have been avoided to reduce complexity and gain readability.
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=354592

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Any other ideas?
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=355418

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm one step further although I didn't solve the problem yet.

I found out that I can use an XMLFilter to get notified of SAX events while unmarshalling. I listen to [i]startElement()[/i] and save the value of the [i]xsi:type[/i] attribute now. Additionally, I register an [i]Unmarshaller.Listener[/i] to get notified about each unmarshalled object. When [i]afterUnmarshal()[/i] is called, I have the [i]xsi:type[/i] value from the XMLFilter and the unmarshalled object's [i]getClass()[/i] value, e.g. [i]xsi:type='ns:Subtype'[/i] and [i]class org.example.Basetype[/i].

What's still missing: can JAXB tell me the XML type for the unmarshalled object somehow (which could be [i]ns:Basetype[/i] in my example)?
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=355751

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I finally found a solution. On [i]dev.java.net[/i] there's project called [i]jaxb2-reflection[/i] which allows access to JAXB metadata. It is possible to retrieve the XML type with a given java type now.

Not quite simple IMHO...
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=356022

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Can JAXB Unmarshaller be configured to fail on missing xsi:type-binding?

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

A combination of Unmarshaller.Listener, XMLFilter and jaxb2-reflection solves the problem.
[Message sent by forum member 'ck1' (ck1)]

http://forums.java.net/jive/thread.jspa?messageID=356024

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...