如何在 xjc 中禁用 Java 命名约定?

发布于 2024-11-14 10:27:11 字数 148 浏览 5 评论 0原文

例如,xsd 中的 sOmE_PROPerty 必须是 java 类中的 sOmE_PROPerty,而不是 someProperty。

我尝试使用 globalBindings enableJavaNamingConventions="false" 但它不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2024-11-21 10:27:11

您需要使用 underscoreBinding="asCharInWord" 而不是 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

JAXB 绑定文件用于自定义模式到 Java 的转换:

<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

生成的属性名称现在包含下划线字符:

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

}

不使用binding.xml

如果您改为进行以下 XJC 调用:

xjc -d out -customer.xsd

您将看到生成的属性不包含下划线:

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

}

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

}
痴情换悲伤 2024-11-21 10:27:11

通过更改 com.sun.xml.bind.api.impl.NameConverter 类中 jaxb 的源代码来解决,如下所示:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文