2

I have a XSD scheme but I cannot change the Scheme 

<xs:attribute name="zbpName" type="Zbp_NC" use="required"/>
<xs:attribute name="zbpType" type="ZBPTYP_CL" use="required"/

Generation of Java-Classes works, but I want to ignore the Attribute use="required”. Is there a way to ignore this?

I want to get this result when I marshal.

<protectionPoint zbpName="Protection Point - 0">

But in the moment I get this result….

<protectionPoint zbpNotes="" zbpStation="" zbpInterlockingName="" zbpType="" zbpName="Protection Point - 0">

It is because the generated Clasess have this Annotation.

@XmlAttribute(name = "zbpStation", required = true)

But the should look like this…

@XmlAttribute(name = "zbpStation")

Thanks for your help ;-)

lexicore
  • 42,748
  • 17
  • 132
  • 221
user985203
  • 23
  • 4

2 Answers2

2

So you want to required="false" but can't change schema? You can use JAXB2-Basics Annotate Plugin version 0.6.3 and higher to achieve this. The customization would look like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">

    <!-- org.example.TFreeForm @XmlRootElement -->
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='MyType']/xs:attribute[@name='test']">
            <annox:annotate target="field">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlAttribute" required="false"/>
            </annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

The 0.6.3 version is not released yet. A snapshot is available here.

lexicore
  • 42,748
  • 17
  • 132
  • 221
0

I think you can just create your own class with correct annotations and use it instead of old one, e.g.:

public interface TestInterface {
    Integer getField();     
}

public class TestClass implements TestInterface{

    @Attribute(required = true)
    private Integer field;

    public TestClass() {
    }

    public TestClass(Integer field) {
        this.field = field;
    }

    public Integer getField() {
        return field;
    }

    public void setField(Integer field) {
        this.field = field;
    }
}

public class NewTestClass implements TestInterface{

    @Attribute
    private Integer field;

    public NewTestClass() {
    }

    public NewTestClass(Integer field) {
        this.field = field;
    }

    public Integer getField() {
        return field;
    }

    public void setField(Integer field) {
        this.field = field;
    }
}

Actually, it depends on what access you have to target class.

se.solovyev
  • 1,901
  • 1
  • 16
  • 20
  • This "use" is not easy, as JAXB classes should be in one package. I would say, one needs to correct the Java class after it is being generated. – dma_k Oct 08 '11 at 10:36
  • You cannot remove annotations in runtime, so it must be done manually BEFORE compilation – se.solovyev Oct 08 '11 at 10:37
  • But I must do this for many Classes so that is not really an option for me… Thank you. – user985203 Oct 08 '11 at 10:49
  • You can search & replaces in .java files [based on regex](http://stackoverflow.com/questions/186096). But I agree, that using JAXB plugin (as @lexicore suggested) is best idea. – dma_k Oct 08 '11 at 11:13
  • @dma_k The only problem I see is that its is still a lot of work for many classes. A better option might be to write a plugin which changes the `required` setting for attribute fields. – lexicore Oct 08 '11 at 15:13