jaxb2-maven-plugin generates invalid source for doubles with default value =INF
Asked Answered
T

1

2

I have a problem where the jaxb2-maven-plugin generates invalid source code when the XSD file contains default values for doubles.

I use the jaxb2-maven-plugin (org.codehaus.mojo) version 1.5:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
    </configuration>
    <executions>
        <execution>
            <id>analysis_jaxb</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <clearOutputDir>false</clearOutputDir>
                <schemaFiles>Analysis.xsd</schemaFiles>
                <packageName>xx.xx.xx.analysis</packageName>
                <generateDirectory>${project.build.directory}/generated-sources/jaxb/analysis</generateDirectory>
                <verbose>true</verbose>
            </configuration>
        </execution>
    </executions>
</plugin>

to generate Java Source from the following XSD file:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="MinMax" type="MinMaxType"/>

    <xs:complexType name="MinMaxType">
        <xs:attribute name="min" type="xs:double" default="-INF" />
        <xs:attribute name="max" type="xs:double" default="INF" />
    </xs:complexType>

</xs:schema> 

The resulting Java file contains this method:

public double getMin() {
    if (min == null) {
        return -InfinityD; //UNDEFINED
    } else {
        return min;
    }
}

The field -InfinityD is not defined anywhere.

When using booleans (e.g. <xs:attribute name="minInclusive" type="xs:boolean" default="false" />) the default values work as expected.

In contrast to this, the plugin org.jvnet.jaxb2.maven2 (maven-jaxb2-plugin) would write Double.POSITIVE_INFINITY on that problematic line.

Is this simply not supported? Am I missing a parameter?

Trager answered 13/10, 2014 at 9:48 Comment(6)
Have you tested the most up-to-date version 1.6 of jaxb2-maven-plugin ?Deboradeborah
Thanks! Tried that, unfortunately with no luck. Added the stacktrace to the question.Trager
@reto: I am the author of the maven-jaxb2-plugin. Out of curiosity, why don't you just use the maven-jaxb2-plugin? Anything you're missing in the plugin? I don't want to persuade you to use my thing, just looking for the constructive feedback.Twenty
@Twenty I like the initiative! We had some occasional cases where the maven-jaxb2-plugin would fail to generate Java files during the build, unfortunately I don't have all the data/stacktraces here to point you in any concrete direction. This lead us to look into alternatives, but the story isn't over yet and we might stick with the jaxb2-maven-plugin. I'll make sure to file a proper report if we can repeat our issues.Trager
@Deboradeborah switching to 1.6 seemed to resolve the issue.Trager
@Trager Please file your issues here: github.com/highsource/maven-jaxb2-plugin/issues, I'd be grateful for that.Twenty
G
6

Using this XSD...

<xs:schema attributeFormDefault="unqualified"
    targetNamespace="yourNameSpace"
    xmlns:a="yourNameSpace"
    elementFormDefault="qualified" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="MinMax" type="a:MinMaxType"/>

    <xs:complexType name="MinMaxType">
        <xs:attribute name="min" type="xs:double" default="-INF" />
        <xs:attribute name="max" type="xs:double" default="INF" />
    </xs:complexType>
</xs:schema>

If you use

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>

works fine

output:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MinMaxType")
public class MinMaxType {

    @XmlAttribute(name = "min")
    protected Double min;
    @XmlAttribute(name = "max")
    protected Double max;

    /**
     * Recupera il valore della proprietà min.
     * 
     * @return
     *     possible object is
     *     {@link Double }
     *     
     */
    public double getMin() {
        if (min == null) {
            return java.lang.Double.NEGATIVE_INFINITY;
        } else {
            return min;
        }
    }

    /**
     * Imposta il valore della proprietà min.
     * 
     * @param value
     *     allowed object is
     *     {@link Double }
     *     
     */
    public void setMin(Double value) {
        this.min = value;
    }

    /**
     * Recupera il valore della proprietà max.
     * 
     * @return
     *     possible object is
     *     {@link Double }
     *     
     */
    public double getMax() {
        if (max == null) {
            return java.lang.Double.POSITIVE_INFINITY;
        } else {
            return max;
        }
    }

    /**
     * Imposta il valore della proprietà max.
     * 
     * @param value
     *     allowed object is
     *     {@link Double }
     *     
     */
    public void setMax(Double value) {
        this.max = value;
    }

}

plugin configuration:

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <id>commun-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                            <schemaDirectory>${basedir}/src/main/resources/schema/xsd</schemaDirectory>
                            <strict>true</strict>
                            <extension>true</extension>
                            <verbose>true</verbose>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Using jaxb2-maven-plugin

plugin configuration:

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                </configuration>
                <executions>
                    <execution>
                        <id>analysis_jaxb</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <clearOutputDir>false</clearOutputDir>
                            <schemaFiles>your.xsd</schemaFiles>
                            <packageName>xx.xx.xx.analysis</packageName>
                            <generateDirectory>generated-sources/jaxb/analysis</generateDirectory>
                            <verbose>true</verbose>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

output

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MinMaxType")
public class MinMaxType {

    @XmlAttribute(name = "min")
    protected Double min;
    @XmlAttribute(name = "max")
    protected Double max;

    /**
     * Recupera il valore della proprietà min.
     * 
     * @return
     *     possible object is
     *     {@link Double }
     *     
     */
    public double getMin() {
        if (min == null) {
            return java.lang.Double.NEGATIVE_INFINITY;
        } else {
            return min;
        }
    }

    /**
     * Imposta il valore della proprietà min.
     * 
     * @param value
     *     allowed object is
     *     {@link Double }
     *     
     */
    public void setMin(Double value) {
        this.min = value;
    }

    /**
     * Recupera il valore della proprietà max.
     * 
     * @return
     *     possible object is
     *     {@link Double }
     *     
     */
    public double getMax() {
        if (max == null) {
            return java.lang.Double.POSITIVE_INFINITY;
        } else {
            return max;
        }
    }

    /**
     * Imposta il valore della proprietà max.
     * 
     * @param value
     *     allowed object is
     *     {@link Double }
     *     
     */
    public void setMax(Double value) {
        this.max = value;
    }

}
Group answered 13/10, 2014 at 9:55 Comment(3)
Thanks for this, but I already know that org.jvnet.jaxb2.maven2 does what I'd expect it to do. I'd like to know how I could achieve the same using jaxb2-maven-pluginTrager
@Trager I updated my answer using jaxb2-maven-pluginGroup
I just got to try this again using 1.6 and it works now.Trager

© 2022 - 2024 — McMap. All rights reserved.