users@jaxb.java.net

Re: Generating Java bindings in different packages

From: Jean_at_Eastcode <jean.eastcode_at_gmail.com>
Date: Wed, 3 Nov 2010 20:10:38 -0700 (PDT)

Hi Wolfgang,

I've got a new question concerning Java Enum autogeneration, sorry about
that :-(

I need a specific toString() for Enums (basically replacing _ by space).
Since inheritance isn't possible with Java enums, I have to include a custom
toString method in each every Enum classes.

Now that my enum classes are JAXB2 generated, I'm wondering if its at all
possible to control/tweak the generated code to the point of adding a
method.

I fear it won't be, and having found no answer on the www index, I'm turning
back to you again.

cheers
-jean



Wolfgang Laun-2 wrote:
>
> On 27 October 2010 07:17, Jean_at_Eastcode <jean.eastcode_at_gmail.com> wrote:
>>
>> Great! thanks for sharing your knowledge Wolfgang.
>>
>> I had to use this in the header to get things working:
>> xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
>
> This was in my last schema snippet ;-)
>
>>
>> Version was needed apparently.
>> But my IDE can't find any reference with http://java.sun.com/xml/ns/jaxb
>
> This is not a "real" resource identifier; it just makes the JAXB
> namespace unique.
>
>>
>> I tried
>> xmlns:jaxb="http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
>> which is found, but got an error.
>>
>> No way to get a unique ObjectFactory for several namespaces I guess?
>
> The ObjectFactory.java, even when empty, is a marker required by JAXB.
> (There is
> an alternative, but that's *another* file.) Generally, you have many
> packages and
> then, believe me, it's better to have one ObjectFactory per package.
> In your case,
> where there's only an enum class in "model", you just carry it along,
> otherwise you
> can ignore it.
>
> -W
>
>>
>> cheers
>> -jean
>>
>>
>> Wolfgang Laun-2 wrote:
>>>
>>> This customization forces the Java enum name to be whatever is the
>>> value of @name.
>>>
>>> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>>            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
>>>            targetNamespace="model"
>>>            jaxb:version="2.0">
>>> <xs:simpleType name="psType">
>>> <xs:annotation>
>>>     <xs:appinfo>
>>>          <jaxb:typesafeEnumClass>
>>>             <jaxb:typesafeEnumMember name="BETA2" value="beta2"/>
>>>          </jaxb:typesafeEnumClass>
>>>     </xs:appinfo>
>>> </xs:annotation>
>>>    <xs:restriction base="xs:string">
>>>     <xs:enumeration value="one"/>
>>>     <xs:enumeration value="two"/>
>>>     <xs:enumeration value="beta2">
>>> ...
>>>
>>> -W
>>>
>>>
>>> On 26 October 2010 14:23, Jean_at_Eastcode <jean.eastcode_at_gmail.com> wrote:
>>>>
>>>> Hi Wolfgang,
>>>>
>>>> very good piece of example, highly helpful for me.
>>>>
>>>> I finally made it with my enums in a specific "model" package, and with
>>>> one
>>>> invocation of XJC only.
>>>> Somehow, I get two ObjectFactory.java generated, one per
>>>> package/namespace.
>>>> But the one for the "model" is just empty. Isn't there a way to
>>>> generate
>>>> one
>>>> only?
>>>>
>>>> I've got a bigger problem though, hopefully easy to solve through some
>>>> configuration that I can't find:
>>>>
>>>> I've got an Enum that has a value BETA2, containing a number. This
>>>> triggers
>>>> the generation of a specific Java enum class, as follow:
>>>>
>>>> public enum TrainingOrigin {
>>>>
>>>>    EXTRANET("EXTRANET"),
>>>>    INTRANET("INTRANET"),
>>>>    @XmlEnumValue("BETA2")
>>>>    BETA_2("BETA2");
>>>>    private final String value;
>>>>
>>>>    TrainingOrigin(String v) {
>>>>        value = v;
>>>>    }
>>>>
>>>>    public String value() {
>>>>        return value;
>>>>    }
>>>>
>>>>    public static TrainingOrigin fromValue(String v) {
>>>>        for (TrainingOrigin c: TrainingOrigin.values()) {
>>>>            if (c.value.equals(v)) {
>>>>                return c;
>>>>            }
>>>>        }
>>>>        throw new IllegalArgumentException(v);
>>>>    }
>>>> }
>>>>
>>>> I need to keep the value as BETA2, and not BETA_2.
>>>>
>>>> Would you happen to know how to control this in the generation?
>>>>
>>>> cheers
>>>> -jean
>>>>
>>>>
>>>> Wolfgang Laun-2 wrote:
>>>>>
>>>>> You just put the simple type into another schema with another
>>>>> namespace.
>>>>>
>>>>> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>>>>            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
>>>>>            targetNamespace="model"
>>>>>            version="2.0">
>>>>>
>>>>> <xs:simpleType name="psType">
>>>>>    <xs:restriction base="xs:string">
>>>>>     <xs:enumeration value="one"/>
>>>>>     <xs:enumeration value="two"/>
>>>>>   </xs:restriction>
>>>>> </xs:simpleType>
>>>>>
>>>>> </xs:schema>
>>>>>
>>>>> This is imported from the other schema:
>>>>> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>>>>            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
>>>>>            targetNamespace="package"
>>>>>            xmlns:m="model"
>>>>>            xmlns:p="package"
>>>>>             version="2.0">
>>>>>
>>>>> <xs:import namespace="model" schemaLocation="enum.xsd"/>
>>>>>
>>>>> <xs:element name="doc" type="p:DocType"/>
>>>>>
>>>>> <xs:complexType name="DocType">
>>>>>   <xs:sequence>
>>>>>     <xs:element name="Topo" type="m:psType"/>
>>>>>     <xs:element name="Count" type="xs:int" default="1" minOccurs="0"/>
>>>>>   </xs:sequence>
>>>>> </xs:complexType>
>>>>>
>>>>> </xs:schema>
>>>>>
>>>>> -W
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On 26 October 2010 09:37, Jean_at_Eastcode <jean.eastcode_at_gmail.com>
>>>>> wrote:
>>>>>
>>>>>>
>>>>>> Actually, the Enum XJC generates are fine.
>>>>>> I just want to be able to specify their package, as I don't want it
>>>>>> to
>>>>>> be
>>>>>> the same as the other generated classes.
>>>>>>
>>>>>> Is this at all possible?
>>>>>>
>>>>>> -jean
>>>>>>
>>>>>>
>>>>>> Wolfgang Laun-2 wrote:
>>>>>> >
>>>>>> > xjc generates enum types from xs:string with
>>>>>> restriction/enumeration.
>>>>>> It
>>>>>> > should, however, be possible to use an Adapter to map the Schema
>>>>>> type
>>>>>> > to your model enum types.
>>>>>> > -W
>>>>>> >
>>>>>> > On 25 October 2010 12:20, Jean_at_Eastcode <jean.eastcode_at_gmail.com>
>>>>>> wrote:
>>>>>> >
>>>>>> >>
>>>>>> >> Hi,
>>>>>> >>
>>>>>> >> I'm using jaxb2-maven-plugin to get automatic generation of my
>>>>>> Java
>>>>>> >> bindings
>>>>>> >> from XSDs.
>>>>>> >>
>>>>>> >> I'm introducing the use of Enums, hence I have to create
>>>>>> simpleTypes
>>>>>> that
>>>>>> >> maps the Java Enum values.
>>>>>> >>
>>>>>> >> The XSD as well as the generated classes are all in a binding
>>>>>> "package"
>>>>>> >>
>>>>>> >> The enums are in a different package since they are not
>>>>>> autogenerated,
>>>>>> >> called "model".
>>>>>> >>
>>>>>> >> I want the generated classes to use the original Java "model"
>>>>>> enum,
>>>>>> but
>>>>>> >> jaxb
>>>>>> >> keep generating them into "binding".
>>>>>> >>
>>>>>> >> I've split all the simpleType corresponding to the Java enums in a
>>>>>> >> EnumTypes.xsd that I put in "model" but still, corresponding Java
>>>>>> enums
>>>>>> >> are
>>>>>> >> always created in "binding".
>>>>>> >>
>>>>>> >> Is there a way to generate EnumsTypes.xsd classes in "model", and
>>>>>> to
>>>>>> have
>>>>>> >> classes generated into "binding" be linked to them?
>>>>>> >>
>>>>>> >> FYI, here's my plugin config:
>>>>>> >>
>>>>>> >> <plugin>
>>>>>> >>                <groupId>org.codehaus.mojo</groupId>
>>>>>> >>                <artifactId>jaxb2-maven-plugin</artifactId>
>>>>>> >>                <version>1.3</version>
>>>>>> >>                <executions>
>>>>>> >>                    <execution>
>>>>>> >>                        <id>binding</id>
>>>>>> >>                        <goals>
>>>>>> >>                            <goal>xjc</goal>
>>>>>> >>                        </goals>
>>>>>> >>                        <configuration>
>>>>>> >>
>>>>>> >>
>>>>>> >>
>>>>>> <schemaDirectory>src/main/resources/com//comp/project/app/common/binding</schemaDirectory>
>>>>>> >>
>>>>>> >> <packageName>com.comp.project.app.common.binding</packageName>
>>>>>> >>
>>>>>> >> <outputDirectory>src/main/generated</outputDirectory>
>>>>>> >>                            <clearOutputDir>false</clearOutputDir>
>>>>>> >>                        </configuration>
>>>>>> >>                    </execution>
>>>>>> >>                    <execution>
>>>>>> >>                        <id>model</id>
>>>>>> >>                        <goals>
>>>>>> >>                            <goal>xjc</goal>
>>>>>> >>                        </goals>
>>>>>> >>                        <configuration>
>>>>>> >>
>>>>>> >>
>>>>>> >>
>>>>>> <schemaDirectory>src/main/resources/com/comp/project/app/common/model</schemaDirectory>
>>>>>> >>
>>>>>> >> <packageName>com.comp.project.app.common.model</packageName>
>>>>>> >>
>>>>>> >> <outputDirectory>src/main/generated</outputDirectory>
>>>>>> >>                            <clearOutputDir>false</clearOutputDir>
>>>>>> >>                        </configuration>
>>>>>> >>                    </execution>
>>>>>> >>                </executions>
>>>>>> >>
>>>>>> >> cheers
>>>>>> >>
>>>>>> >>
>>>>>> >> --
>>>>>> >> View this message in context:
>>>>>> >>
>>>>>> http://old.nabble.com/Generating-Java-bindings-in-different-packages-tp30046469p30046469.html
>>>>>> >> Sent from the java.net - jaxb users mailing list archive at
>>>>>> Nabble.com.
>>>>>> >>
>>>>>> >>
>>>>>> >>
>>>>>> ---------------------------------------------------------------------
>>>>>> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>>>>> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>>>> >>
>>>>>> >>
>>>>>> >
>>>>>> >
>>>>>>
>>>>>> --
>>>>>> View this message in context:
>>>>>> http://old.nabble.com/Generating-Java-bindings-in-different-packages-tp30046469p30054965.html
>>>>>> Sent from the java.net - jaxb users mailing list archive at
>>>>>> Nabble.com.
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>>>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://old.nabble.com/Generating-Java-bindings-in-different-packages-tp30046469p30056982.html
>>>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>
>>>
>>>
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Generating-Java-bindings-in-different-packages-tp30046469p30064011.html
>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>

-- 
View this message in context: http://old.nabble.com/Generating-Java-bindings-in-different-packages-tp30046469p30128847.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.