16161

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

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 <strong>jaxb2-maven-plugin (org.codehaus.mojo) version 1.5</strong>:

<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?

Answer1:

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

<strong>output:</strong>

@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; } }

<strong>plugin configuration:</strong>

<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> <hr>

Using jaxb2-maven-plugin

<strong>plugin configuration:</strong>

<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>

<strong>output</strong>

@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; } }

Recommend

  • Reading Element attribute value using XStream
  • Use of Multiple Inheritance in EclipseLink MOXy
  • Unmarshalling works incorrectly: javax.xml.bind.UnmarshalException: unexpected element
  • Specify datetime format for WCF Data Service
  • How can I export a gbm model in R?
  • Spring RestTemplate slow response time
  • Controlling volume of running applications in Mac OS X via Objective-C
  • Selecting multiple elements with Selenium
  • How create references between elements in XML
  • Entity Framework Core 2.0 add-migration not generating anything
  • Root element minOccurs or maxOccurs
  • Filtering SPARQL results by day and month
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • How to convert SOAP response with xsi values to json in WSO2esb
  • Web.config system.webserver errors
  • Jetty 9 HashLoginService
  • Checking free space on FTP server
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Change Inet root folder for iis 7
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Counter field in MS Access, how to generate?
  • How to set/get protobuf's extension field in Go?
  • Cross-Platform Protobuf Serialization
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • Updated Ionic CLI but shows previous version (Windows)
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • Importing jscolor library in angular 2
  • Websockets service method fails during R startup
  • How to format a variable of double type
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • coudnt use logback because of log4j
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF