久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

JAXB 將非 ASCII 字符轉換為 ASCII 字符

JAXB convert non-ASCII characters to ASCII characters(JAXB 將非 ASCII 字符轉換為 ASCII 字符)
本文介紹了JAXB 將非 ASCII 字符轉換為 ASCII 字符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一些元素名稱包含非 ASCII 字符的 xsd 模式.當我使用 Eclipse Kepler 使用 Generate JAXB Classes 命令生成 java 類時,生成的類和它們的變量包含非 ASCII 字符.我想把這個非 ASCII 字符轉換成 ASCII 字符.

我已經在 J??AVA_TOOL_OPTIONS 設置了語言環境

-Duser.country=GB -Duser.language=en

例如

? ->一世?->C?->小號?->○? ->Gü ->ü我->一世? ->○ü->你?->C?->GΣ ->s

解決方案

因為要求是通用解決方案并且不使用外部綁定文件,我在下面提供了 2 個選項:

選項 1 - 通用解決方案 - 創建自定義 XJC 插件以進行規范化

通用解決方案是有效的:

  1. 擴展 com.sun.tools.xjc.Plugin 抽象類并覆蓋 JAXB 用于命名工件的方法 - 基本創建插件
  2. META-INF文件夾的services目錄內具體調出實現名稱后,將這個實現打包到一個jar中罐子
  3. 將這個新創建的 jar 與 jaxb 庫一起部署并通過 ANT 運行它(build.xml 下面提供,請繼續閱讀)

為了您的目的,我創建了插件,您可以從

我使用了 選項 1 中已經提到的 xsd,其元素名稱包含重音"(非 ASCII)字符:

如果我在沒有指定外部綁定的情況下生成類,我會得到以下輸出:

!

現在,如果我稍微更改綁定以生成我選擇的類名和變量,我將 binding.xml 編寫為:

<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 版本="2.1"><jxb:globalBindings localScoping="toplevel"/><jxb:bindings schemaLocation="some.xsd"><jxb:bindings node="http://xs:element[@name='?h?pto']"><jxb:class name="ShipTo"/></jxb:綁定><jxb:bindings node="http://xs:element[@name='?rderperson']"><jxb:property name="OrderPerson"/></jxb:綁定><jxb:bindings node="http://xs:element[@name='?h?pto']//xs:complexType"><jxb:class name="ShipToo"/></jxb:綁定></jxb:綁定></jxb:綁定>

現在當我通過 eclipse 通過指定綁定文件生成我的類時:

在接下來的步驟中,我選擇我得到的包和綁定文件,

注意:如果您不使用 eclipse 生成類,您可能需要檢查 xjc 綁定編譯器 來利用你的外部綁定文件.

I have some xsd schemas that element names contains non-ASCII characters. When I generate java classes using Generate JAXB Classes command using Eclipse Kepler, generated classes and variables of them contains non-ASCII characters. I want to transform this non-ASCII characters to ASCII characters.

I already set locale at JAVA_TOOL_OPTIONS

-Duser.country=GB -Duser.language=en

For example

? -> I
? -> C
? -> S
? -> O
? -> G
ü -> U
? -> i
? -> o
ü -> u
? -> c
? -> g
? -> s

解決方案

EDIT: Since the requirement is of a generic solution and not using the external binding files, I have offered 2 options below:

Option 1 - A Generic Solution - Create a Custom XJC plugin to normalize

The generic solution is effectively:

  1. Extend com.sun.tools.xjc.Plugin abstract class and override methods that JAXB uses to name the artifacts - create a plugin bascially
  2. Pack this implementation in a jar after specifically calling out the name of the implementation within the services directory of the META-INF folder inside the jar
  3. Deploy this newly created jar along with jaxb libs and run it through ANT (build.xml provided below, read on)

For your purpose, I have created the plugin for which you can download the jar from here, download the ant script (build.xml) from here. Put the jar to your build path in eclipse and edit the ant file to provide your locations of your JAXB libs, target package of the generated classes, project name and schema location and run it. That's it!

Explanation:

I created a custom XJC plugin with an extra command line option -normalize to replace the accented characters in your created Java classes, methods, variables, properties and interfaces with their ASCII equivalents.

XJC has the capability of custom plugins creation to control the names, annotations and other attributes of the generated classes, variables and so on. This blog post though old can get you started with the basics of such plugin implementations.

Long story short, I created a class extending the abstract com.sun.tools.xjc.Plugin class, overriding its methods important one being onActivated.

In this method, I have set com.sun.tools.xjc.Option#setNameConverter to a custom class which takes care of overriding the required methods of acquiring names of the class, methods etc. I have committed the source to my git repo here as well, below is the detailed usage of it:

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

To enable this plugin with the normal jaxb unmarshalling is to pack these class in a jar, add /META-INF/services/com.sun.tools.xjc.Plugin file within the jar and put it in your build path.

/META-INF/services/com.sun.tools.xjc.Plugin file within the jar:

This file reads:

com.popofibo.plugins.jaxb.NormalizeElements

As mentioned before, I pack it in a jar, deploy it in my eclipse build path, now the problem I ran in to with running eclipse kepler with JDK 1.7 is I get this exception (message):

com.sun.tools.xjc.plugin Provider <my class> not a subtype

Hence, it's better to generate the classes using ANT, the following build.xml does justice to the work done so far:

<?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>

The schema to showcase this normalization process I chose was:

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

As you can see, I have set the argument and package as to where I want to have my classes generated, and voila - the ASCII names for classes, methods, variables in the generated artifacts (the only gap I see is with the XML annotations which would not affect the cause but also easy to overcome):

The above screenshot shows the names were normalized and are replaced by their ASCII counterparts (to check how it would look without the replacement, please refer to the screenshots in option 2).

Option 2 - Using External binding file

To remove accented characters, you can create a custom binding file and use it to bind your class and property names while generating your classes. Refer to: Creating an External Binding Declarations File Using JAXB Binding Declarations

I took the xsd already mentioned in Option 1 with element names containing "accented" (Non-ASCII) characters:

If I generate the classes without specifying the external binding, I get the following outputs:

!

Now if I change the binding a bit to generate class names and variables of my choice, I write my binding.xml as:

<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="http://xs:element[@name='?h?pto']">
            <jxb:class name="ShipTo" />
        </jxb:bindings>
        <jxb:bindings node="http://xs:element[@name='?rderperson']">
            <jxb:property name="OrderPerson" />
        </jxb:bindings>
        <jxb:bindings node="http://xs:element[@name='?h?pto']//xs:complexType">
            <jxb:class name="ShipToo" />
        </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

Now when I generate my class through eclipse by specifying the binding file:

In the next steps, I choose the package and the binding file I get,

Note: If you are not using eclipse to generate your classes, you might want to check xjc binding compiler out to utilize your external binding file.

這篇關于JAXB 將非 ASCII 字符轉換為 ASCII 字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产一区二区三区免费观看视频 | 国产成人免费视频网站高清观看视频 | 看片网站在线 | 中文在线播放 | www免费视频| 成人福利网 | 国产精品日韩一区 | 久操国产 | 日韩在线精品 | www.黄网| 国产在线精品一区二区 | 日韩一区二区黄色片 | 日韩理论电影在线观看 | 免费小视频在线观看 | 亚洲一二三在线观看 | 久久久久久久久91 | 欧美精品久久久久 | 另类二区| 午夜一区 | 国产精品毛片一区二区在线看 | 精品91视频 | 色香婷婷 | 久久综合久色欧美综合狠狠 | 日韩一区二区精品 | 国产精品视频在线播放 | 国产精品一区二区三区四区五区 | 久久机热 | 欧美成人激情视频 | 51ⅴ精品国产91久久久久久 | 欧美日产国产成人免费图片 | 国产日韩欧美中文字幕 | 欧美一卡二卡在线观看 | 亚洲精品第一页 | 国产电影一区二区三区爱妃记 | 日韩二区 | 精品久久一区二区 | 精品国产一区二区三区性色 | 久久精品国产久精国产 | 久久99精品久久久久久 | 久久国产一区二区 | 91九色porny首页最多播放 |