users@jaxb.java.net

Re : Need help - I need to overwrite the "required=true" in one element

From: Edson Richter <edsonrichter_at_hotmail.com>
Date: Sun, 10 Aug 2014 21:00:58 +0000

Perfect understandable explanation, this would go into wiki.

 

Thank you very much, you not only explained concepts I couldn't find in official docs, but also provided more than one viable alternative. 

 

Atenciosamente,

Edson Richter 


------ Mensagem original ------
De: Wolfgang Laun
Data: 10/08/2014 13h47
Para: users@jaxb.java.net;
Assunto:Re: Need help - I need to overwrite the "required=true" in one element

Well, it isn't possible to spirit the "required=true" away, but even
if it were, this wouldn't help you as you don't want to change the XML
schema.

(1) Try this: edit the generated Java file, remove the required=true
and run your program as before. You'll get an exception/validation
error.

(2) Leave required=true, but remove the schema validation. You'll see
that JAXB tolerates the absent element; the field remains null.

Here's one way (rather inconvenient) but if the gov. threatens you
with terrible punishment:

Write a class like this, replacing Xyz with the name of the parent
element where Signature can be omitted:

class Cheater  implements ValidationEventHandler{
    public boolean handleEvent(ValidationEvent event){
	ValidationEventLocator vel = event.getLocator();
	System.out.println( "******* being called on: " + event );
	System.out.println( "<" +event.getMessage() + ">"   );
	if( "cvc-complex-type.2.4.a: Invalid content was found starting with
element 'Xyz'. One of '{Signature}' is expected.".equals(
event.getMessage() ) || "cvc-complex-type.2.4.b: The content of
element 'Xyz' is not complete. One of '{Signature}' is
expected.".equals( event.getMessage() ) ){
	    return true;
	} else {
	    return false;
	}
    }

Register with the unmarshaller:

    Unmarshaller m = ...;
    m.setEventHandler( new Cheater() );

You might use a more robust technique for matching the error message.
Notice that "One of '{Signature}'" may not be what *you* get - adapt
as required.

After all this rigmarole, note that what you'll be doing is indeed *a
change of the XML schema* - if not in its text but in its intended
effect. It would be far better to edit the XML schema, insert a
minOccurrence="0" and use *stable* code throughout. You can do this on
the fly using an XSLT transformation to insert just this attribute.

-W





On 09/08/2014, edsonrichter@hotmail.com  wrote:
> It is possible to overwrite the "required=true" in one element?
>
> Todays generated code (using NetBeans 8) is like:
>
> [code]
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "TNFe", propOrder = {
>     "infNFe",
>     "signature"
> })
> public class TNFe {
>
>     @XmlElement(required = true)
>     protected TNFe.InfNFe infNFe;
>     @XmlElement(name = "Signature", namespace =
> "http://www.w3.org/2000/09/xmldsig#", required = true)
>     protected SignatureType signature;
> [/code]
>
> As you can see, the "Signature" element is required=true.
> I need to provide binding customatization so the generated code points
> to
>
> [code]
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "TNFe", propOrder = {
>     "infNFe",
>     "signature"
> })
> public class TNFe {
>
>     @XmlElement(required = true)
>     protected TNFe.InfNFe infNFe;
>     @XmlElement(required = false)
>     protected SignatureType signature;
> [/code]
>
> How to accomplish that?
> I can't change the XSD file because it comes from goverment standards.
>
> Thanks,
>
> Edson
>