How to disable Java Naming Conventions in xjc?

前端 未结 2 567
醉酒成梦
醉酒成梦 2020-12-06 13:18

For example sOmE_PROPerty in xsd must be sOmE_PROPerty in java class not someProperty.

I tried to use globalBindings enableJavaNamingConventions=\"false\" but it doe

相关标签:
2条回答
  • 2020-12-06 13:56

    You will need to use underscoreBinding="asCharInWord" instead of enableJavaNamingConventions="false":

    customer.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema 
        targetNamespace="http://www.example.org/customer" 
        xmlns="http://www.example.org/customer"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"> 
    
        <xsd:complexType name="customer">
                    <xsd:sequence>
                        <xsd:element name="sOmE_PROPerty" type="xsd:string"/>
                    </xsd:sequence>
        </xsd:complexType>
    
    </xsd:schema>
    

    binding.xml

    A JAXB binding file is used to customize the schema to Java conversion:

    <jaxb:bindings 
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="2.1">
        <jaxb:globalBindings underscoreBinding="asCharInWord"/>
    </jaxb:bindings>
    

    XJC Call

    xjc -d out -b binding.xml customer.xsd
    

    Customer

    The generated property names now include the underscore character:

    package org.example.customer;
    
    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 = "customer", propOrder = {
        "sOmEPROPerty"
    })
    public class Customer {
    
        @XmlElement(name = "sOmE_PROPerty", required = true)
        protected String sOmEPROPerty;
    
        public String getSOmE_PROPerty() {
            return sOmEPROPerty;
        }
    
        public void setSOmE_PROPerty(String value) {
            this.sOmEPROPerty = value;
        }
    
    }
    

    Without Using binding.xml

    If you instead make the following XJC call:

    xjc -d out -customer.xsd
    

    You will see that the generated properties do not include the underscore:

    package org.example.customer;
    
    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 = "customer", propOrder = {
        "sOmEPROPerty"
    })
    public class Customer {
    
        @XmlElement(name = "sOmE_PROPerty", required = true)
        protected String sOmEPROPerty;
    
        public String getSOmEPROPerty() {
            return sOmEPROPerty;
        }
    
        public void setSOmEPROPerty(String value) {
            this.sOmEPROPerty = value;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-06 14:09

    Solved by changing source code of jaxb in class com.sun.xml.bind.api.impl.NameConverter like this:

    public static final NameConverter standard = new Standard();
    
    static class Standard extends NameUtil implements NameConverter {
        public String toClassName(String s) {
            return s;//toMixedCaseName(toWordList(s), true);
        }
        public String toVariableName(String s) {
            return s;//toMixedCaseName(toWordList(s), false);
        }
        public String toInterfaceName( String token ) {
            return token;//toClassName(token);
        }
        public String toPropertyName(String s) {
            String prop = s;//toClassName(s);
            // property name "Class" with collide with Object.getClass,
            // so escape this.
            if(prop.equals("Class"))
                prop = "Clazz";
            return prop;
        }
    
    0 讨论(0)
提交回复
热议问题