Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill in fixed values in DefaultValuePlugin #15

Open
dschulten opened this issue Nov 10, 2023 · 3 comments
Open

Fill in fixed values in DefaultValuePlugin #15

dschulten opened this issue Nov 10, 2023 · 3 comments

Comments

@dschulten
Copy link

dschulten commented Nov 10, 2023

When schemas define a fixed value, it would be useful to set those values during code generation too, alongside default values. Not as simple as it first appears, but how about that?

@patrodyne
Copy link
Owner

I'll review XJC plugin code generation for XML Schema attribute and simple element fixed values before the next release.

An example schema for experimentation is ...

schema.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
    jaxb:version="3.0"
>
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings fixedAttributeAsConstantProperty="false">
                <jaxb:serializable uid="20231101" />
            </jaxb:globalBindings>
            <jaxb:schemaBindings>
                <jaxb:package name="org.example.fixed"/>
            </jaxb:schemaBindings>
        </xs:appinfo>
    </xs:annotation>

    <xs:complexType name="USAddress">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="street" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="state" type="xs:string"/>
            <xs:element name="zip" type="xs:decimal"/>
            <xs:element name="country" type="xs:NMTOKEN" fixed="US"/>
        </xs:sequence>
        <xs:attribute name="region" type="xs:NMTOKEN" fixed="NA"/>
        <xs:attribute name="planet" type="xs:NMTOKEN" default="Earth"/>
    </xs:complexType>

</xs:schema>

The XJC command line tool can be invoked to demonstrate the current behavior (no extended plugins).

xjc -no-header schema.xsd

Current Behavior (no plugin)

Currently when fixedAttributeAsConstantProperty="false", XJC generates a getter for the "region" attribute and it assigns the fixed value "NA" as if it were a default value. When the region field is null the getRegion() method returns "NA" as its default value. It also generates a setter method which is consistent with a default value but not consistent with a fixed value.

@XmlAttribute(name = "region")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NMTOKEN")
protected String region;

public String getRegion()
{
    if (region == null)
        return "NA";
    else
        return region;
}

public void setRegion(String value)
{
    this.region = value;
}

Currently when fixedAttributeAsConstantProperty="true", XJC generates an immutable constant field for REGION without getter / setter methods, consistent with a fixed value.

@XmlAttribute(name = "region")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NMTOKEN")
public static final String REGION = "NA";

Currently, regardless of the fixedAttributeAsConstantProperty setting, XJC generates a getter and a setter for the "country" element which declares "US" as its fixed value BUT does not generate code for the "US" value. As the name of the setting implies, it only applies to XML attributes.

@XmlElement(required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NMTOKEN")
protected String country;

public String getCountry()
{
    return country;
}

public void setCountry(String value)
{
    this.country = value;
}

Proposed Plugin Behavior

When fixedAttributeAsConstantProperty="false", a fixed value plugin should generate immutable constant fields for XML schema attributes and elements. And to facilitate bean reflection, include a getter (but exclude any setter) for both.

When fixedAttributeAsConstantProperty="true", a fixed value plugin should preserve the existing XJC behavior for XML attributes with fixed values (i.e. an immutable constant field without getter / setter methods) but is free to handle XML elements with fixed values as proposed above (i.e. an immutable constant field with a getter method).

@patrodyne
Copy link
Owner

I have added FixedValuePlugin to generate public static final constants for XML schema attributes and/or elements that declare fixed=FOO value.

When these constants are generated, their corresponding getter/setter/... methods are removed. I decided that this would be the best way to represent constant values.

The plugin will be included in the next release (no ETA).

@patrodyne
Copy link
Owner

The FixedValuePlugin has been released to Maven Central.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants