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:
com.sun.tools.xjc.Plugin
abstract class and override methods thatJAXB
uses to name the artifacts - create a plugin basciallyjar
after specifically calling out the name of the implementation within theservices
directory of theMETA-INF
folder inside the jarjaxb
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 theirASCII
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 beingonActivated
.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: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:
As mentioned before, I pack it in a
jar
, deploy it in my eclipse build path, now the problem I ran in to with runningeclipse kepler with JDK 1.7
is I get this exception (message):Hence, it's better to generate the classes using ANT, the following
build.xml
does justice to the work done so far:The schema to showcase this normalization process I chose was:
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: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.