How to disable Java Naming Conventions in xjc?

狂风中的少年 提交于 2019-12-17 19:46:01

问题


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

I tried to use globalBindings enableJavaNamingConventions="false" but it doesn't work.


回答1:


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

}



回答2:


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


来源:https://stackoverflow.com/questions/6267478/how-to-disable-java-naming-conventions-in-xjc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!