Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

java - How do you customize how JAXB generates plural method names?

We are using JAXB to generate Java classes and have encountered a few cases where generated plural method names are not correct. For example, where we expect getPhysicians we are getting getPhysicien. How would we customize how JAXB pluralizes specific methods?

The schema:

<xs:complexType name="physician">
    <xs:sequence>
       ...
    </xs:sequence>
</xs:complexType>

<xs:complexType name="physicianList">
    <xs:sequence>
        <xs:element name="Physician"
                    type="physician"
                    minOccurs="0"
                    maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

The generated Java code:

...
public class PhysicianList {
...

    @XmlElement(name = "Physician")
    protected List<Physician> physicien;
    ...

    public List<Physician> getPhysicien() {
        if (physicien == null) {
            physicien = new ArrayList<Physician>();
        }
        return this.physicien;
    }

Update

This has been answered by Blaise. However, I prefer not mixing concerns such as JAXB customizations in an XML schema. So for those of you with the same preference, here is a JAXB binding file that achieves the same thing as what Blaise suggested, keeping JAXB customization out of the schema:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               version="2.0">

    <jaxb:bindings schemaLocation="myschema.xsd">
        <jaxb:bindings node="//xs:complexType[@name='physicianList']//xs:element[@name='Physician']">
            <jaxb:property name="physicians"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default the following is generated for your schema fragment:

    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "physicianList", propOrder = {
        "physician"
    })
    public class PhysicianList {

        @XmlElement(name = "Physician")
        protected List<Physician> physician;

        public List<Physician> getPhysician() {
            if (physician == null) {
                physician = new ArrayList<Physician>();
            }
            return this.physician;
        }

    }

If you annotate your XML schema:

    <xs:schema
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        jaxb:version="2.1">

        <xs:complexType name="physician">
            <xs:sequence>
            </xs:sequence>
        </xs:complexType>

        <xs:complexType name="physicianList">
            <xs:sequence>
                <xs:element name="Physician"
                            type="physician"
                            minOccurs="0"
                            maxOccurs="unbounded">
                      <xs:annotation>
                          <xs:appinfo>
                              <jaxb:property name="physicians"/>
                          </xs:appinfo>
                      </xs:annotation>
                 </xs:element>
            </xs:sequence>
        </xs:complexType>

    </xs:schema>

Then you can generate the desired class:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "physicianList", propOrder = {
    "physicians"
})
public class PhysicianList {

    @XmlElement(name = "Physician")
    protected List<Physician> physicians;

    public List<Physician> getPhysicians() {
        if (physicians == null) {
            physicians = new ArrayList<Physician>();
        }
        return this.physicians;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...