[英]JAXB convert non-ASCII characters to ASCII characters
我有一些xsd模式,元素名称包含非ASCII字符。 当我使用Eclipse Kepler使用Generate JAXB Classes命令生成java类时,生成的类和变量包含非ASCII字符。 我想将这个非ASCII字符转换为ASCII字符。
我已经在JAVA_TOOL_OPTIONS设置了语言环境
-Duser.country=GB -Duser.language=en
例如
İ -> I
Ç -> C
Ş -> S
Ö -> O
Ğ -> G
Ü -> U
ı -> i
ö -> o
ü -> u
ç -> c
ğ -> g
ş -> s
编辑: 由于要求是通用解决方案而不使用外部绑定文件 ,我提供了以下2个选项:
通用解决方案有效:
com.sun.tools.xjc.Plugin
抽象类并覆盖JAXB
用于命名工件的方法 - 基本创建插件 jar
内META-INF
文件夹的services
目录中的实现名称后,将此实现打包在jar中 jaxb
libs并通过ANT运行它(下面提供build.xml
,继续阅读) 你的目的,我已经创造了它,你可以从下载JAR插件这里 ,下载ant脚本( build.xml
从) 这里 。 将jar放入eclipse中的构建路径并编辑ant文件以提供JAXB库的位置,生成的类的目标包,项目名称和模式位置并运行它。 而已!
说明:
我创建了一个带有额外命令行选项-normalize
的自定义XJC
插件,用你的ASCII
等价替换你创建的Java类,方法,变量,属性和接口中的重音字符。
XJC
具有自定义插件创建功能,可以控制生成的类,变量等的名称,注释和其他属性。 这篇博文虽然陈旧,但可以让您了解这些插件实现的基础知识。
简而言之,我创建了一个扩展抽象com.sun.tools.xjc.Plugin
类的类,重写其重要的一个onActivated
方法。
在这个方法中,我将com.sun.tools.xjc.Option#setNameConverter
设置为一个自定义类,它负责覆盖获取类,方法等名称所需的方法。我已将源代码提交到我的git repo这里同样,下面是它的详细用法:
import java.text.Normalizer;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.Outline;
import com.sun.xml.bind.api.impl.NameConverter;
/**
* {@link Plugin} that normalized the names of JAXB generated artifacts
*
* @author popofibo
*/
public class NormalizeElements extends Plugin {
/**
* Set the command line option
*/
@Override
public String getOptionName() {
return "normalize";
}
/**
* Usage content of the option
*/
@Override
public String getUsage() {
return " -normalize : normalize the classes and method names generated by removing the accented characters";
}
/**
* Set the name converted option to a delegated custom implementation of
* NameConverter.Standard
*/
@Override
public void onActivated(Options opts) throws BadCommandLineException {
opts.setNameConverter(new NonAsciiConverter(), this);
}
/**
* Always return true
*/
@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler)
throws SAXException {
return true;
}
}
/**
*
* @author popofibo
*
*/
class NonAsciiConverter extends NameConverter.Standard {
/**
* Override the generated class name
*/
@Override
public String toClassName(String s) {
String origStr = super.toClassName(s);
return normalize(origStr);
}
/**
* Override the generated property name
*/
@Override
public String toPropertyName(String s) {
String origStr = super.toPropertyName(s);
return normalize(origStr);
}
/**
* Override the generated variable name
*/
@Override
public String toVariableName(String s) {
String origStr = super.toVariableName(s);
return normalize(origStr);
}
/**
* Override the generated interface name
*/
@Override
public String toInterfaceName(String s) {
String origStr = super.toInterfaceName(s);
return normalize(origStr);
}
/**
* Match the accented characters within a String choosing Canonical
* Decomposition option of the Normalizer, regex replaceAll using non POSIX
* character classes for ASCII
*
* @param accented
* @return normalized String
*/
private String normalize(String accented) {
String normalized = Normalizer.normalize(accented, Normalizer.Form.NFD);
normalized = normalized.replaceAll("[^\\p{ASCII}]", "");
return normalized;
}
}
要使用正常的jaxb
解组启用此插件,请将这些类打包到jar中,在jar中添加/META-INF/services/com.sun.tools.xjc.Plugin
文件并将其放入构建路径中。
jar中的/META-INF/services/com.sun.tools.xjc.Plugin
文件:
该文件为:
com.popofibo.plugins.jaxb.NormalizeElements
如前所述,我将它打包在一个jar
,在我的eclipse构建路径中部署它,现在我遇到的问题是运行eclipse kepler with JDK 1.7
我得到了这个异常(消息):
com.sun.tools.xjc.plugin Provider <my class> not a subtype
因此,最好使用ANT生成类,以下build.xml
对目前为止完成的工作做出了正确的判断:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project name="SomeProject" default="createClasses">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJC2Task">
<classpath>
<pathelement
path="C:/Workspace/jaxb-ri-2.2.7/jaxb-ri-2.2.7/lib/jaxb-xjc.jar" />
<pathelement
path="C:/Workspace/jaxb-ri-2.2.7/jaxb-ri-2.2.7/lib/jaxb-impl.jar" />
<pathelement
path="C:/Workspace/jaxb-ri-2.2.7/jaxb-ri-2.2.7/lib/jaxb2-value-constructor.jar" />
<pathelement path="C:/Workspace/normalizeplugin_xjc_v0.4.jar" />
</classpath>
</taskdef>
<target name="clean">
<delete dir="src/com/popofibo/jaxb" />
</target>
<target name="createClasses" depends="clean">
<xjc schema="res/some.xsd" destdir="src" package="com.popofibo.jaxb"
encoding="UTF-8">
<arg value="-normalize" />
</xjc>
</target>
</project>
展示我选择的规范化过程的模式是:
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="Örderperson" type="xs:string"/>
<xs:element name="Şhİpto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="Çity" type="xs:string"/>
<xs:element name="ÇoÜntry" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="İtem" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="qÜantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
正如您所看到的,我已经设置了参数和包,以确定我希望生成类的位置,并且瞧 - 生成的工件中的类,方法,变量的ASCII名称(我看到的唯一差距是XML注释)这不会影响原因,但也很容易克服):
上面的屏幕截图显示了名称已经标准化并被其ASCII
对应物替换(要查看它没有替换的样子,请参阅选项2中的屏幕截图)。
要删除重音字符,您可以创建自定义绑定文件,并在生成类时使用它绑定类和属性名称。 请参阅: 使用JAXB绑定声明创建外部绑定声明文件
我使用了选项1中已经提到的xsd,其元素名称包含“重音”(非ASCII)字符:
如果我在没有指定外部绑定的情况下生成类,则会得到以下输出:
!
现在,如果我更改绑定以生成我选择的类名和变量,我将binding.xml
写为:
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jxb:globalBindings localScoping="toplevel" />
<jxb:bindings schemaLocation="some.xsd">
<jxb:bindings node="//xs:element[@name='Şhİpto']">
<jxb:class name="ShipTo" />
</jxb:bindings>
<jxb:bindings node="//xs:element[@name='Örderperson']">
<jxb:property name="OrderPerson" />
</jxb:bindings>
<jxb:bindings node="//xs:element[@name='Şhİpto']//xs:complexType">
<jxb:class name="ShipToo" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
现在,当我通过指定绑定文件通过eclipse生成我的类时:
在接下来的步骤中,我选择包和我得到的绑定文件,
注意:如果您没有使用eclipse生成类,则可能需要检查xjc绑定编译器以利用外部绑定文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.