Skip to content

Initializes BigDecimal fields in classes generated by maven-jaxb2-plugin (XJC)

License

Notifications You must be signed in to change notification settings

digitalfog/jaxb2-init-bigdecimal-fields-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugin: init-bigdecimal-fields

Plugin extends functionality of artifact

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

It processes all java.math.BigDecimal fields in classes generated by XJC and initializes them using provided customizations.

Status

Build Status Download

Usage

  1. Add this plugin to plugins section of maven-jaxb2-plugin in your pom.xml.

  2. Enable it by providing -Xinit-bigdecimal-fields arg

  3. Add customizations for xs:decimal fields in your *.xsd file

    • Add namespace used by plugin xmlns:bd="http://init.bigdecimal"
    • Enable processing of it's customizations jaxb:extensionBindingPrefixes="bd"

    Example of *.xsd header:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
             xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
             jaxb:extensionBindingPrefixes="bd"
             jaxb:version="2.1"
             xmlns:bd="http://init.bigdecimal">
    • Add init-field customizations.

    Example:

    <xs:element name="amount" type="xs:decimal">
        <xs:annotation>
            <xs:appinfo>
                <bd:setStaticValue>ZERO</bd:setStaticValue>
                <bd:executeMethod>
                    <bd:name>setScale</bd:name>
                    <bd:param type="int">2</bd:param>
                    <bd:param type="static" class="java.math.BigDecimal">ROUND_HALF_UP</bd:param>
                </bd:executeMethod>
            </xs:appinfo>
        </xs:annotation>
    </xs:element>

Available customizations in *.xsd file:

Customizations are defined in xmlns:bd="http://init.bigdecimal" namespace and processed in the order, they are written in *.xsd.

  1. <bd:setStaticValue> - initializes field with static value from java.math.BigDecimal class.
    For example, this customization:

    <xs:element name="amount" type="xs:decimal">
        <xs:annotation>
            <xs:appinfo>
                <bd:setStaticValue>ZERO</bd:setStaticValue>
            </xs:appinfo>
        </xs:annotation>
    </xs:element>

    will generate the following code:

    BigDecimal amount = BigDecimal.ZERO;
  2. <bd:executeMethod> - executes method on previously initialized value, so it must be placed after <setStaticValue> tag.

    • <name> - method name
    • <param type="" class=""> - parameter that will be passed in method
      Attribute type accepts values:
      • int - param will be qualified as java int
      • static - param will be qualified as static field in class defined in additional attribute class Attribute class is required only with type="static".

    For example, this customization:

    <xs:element name="amount" type="xs:decimal">
        <xs:annotation>
            <xs:appinfo>
                <bd:setStaticValue>ZERO</bd:setStaticValue>
                <bd:executeMethod>
                    <bd:name>setScale</bd:name>
                    <bd:param type="int">2</bd:param>
                    <bd:param type="static" class="java.math.BigDecimal">ROUND_HALF_UP</bd:param>
                </bd:executeMethod>
            </xs:appinfo>
        </xs:annotation>
    </xs:element>

    will generate the following code:

    BigDecimal amount = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
Example of config in pom.xml:

Below configuration assumes:

  • *.xsd source files placed in /src/main/resources/foo
  • generated classes will be placed in ${project.build.directory}/generated-sources/xjc-foo
  • classes will be placed in package com.foo.bar.package
<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.13.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaLanguage>AUTODETECT</schemaLanguage>
                <episode>false</episode>
                <generatePackage>com.foo.bar.package</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/xjc-foo</generateDirectory>
                <schemaDirectory>${basedir}/src/main/resources/foo</schemaDirectory>
                <schemaIncludes>
                    <include>**/*.xsd</include>
                </schemaIncludes>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <args>
            <arg>-Xinit-bigdecimal-fields</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>jaxb2-init-bigdecimal-fields</groupId>
                <artifactId>jaxb2-init-bigdecimal-fields</artifactId>
                <version>1.0.2</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

About

Initializes BigDecimal fields in classes generated by maven-jaxb2-plugin (XJC)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages