how use xmlAdapter in maven-jaxb-plugin - java

Hi I have xsd schema with base64Binary. when this plugin genereted this elemen looks like
#XmlElement(name = "element")
protected byte[] element;
but how can I tell to this plugin to use #XmlJavaTypeAdapter(HexBinaryAdapter.class) so I need something like this
#XmlJavaTypeAdapter(HexBinaryAdapter.class)
#XmlElement(name = "element")
protected byte[] element;
I hope that this is possible thx for help
PS: I cant modify classes which were generated by these plugin because they are always rewrited

You should create a JAXB schema bindings file that instructs the JAXB implementation to use the built in javax.xml.bind.DatatypeConverter to perform conversions to/from hexBinary.
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="YourSchema.xsd">
<jxb:bindings node="//xs:element[#name='element']">
<jxb:property>
<jxb:baseType>
<jxb:javaType name="byte[]"
parseMethod="javax.xml.bind.DatatypeConverter.parseHexBinary"
printMethod="javax.xml.bind.DatatypeConverter.printHexBinary"/>
</jxb:baseType>
</jxb:property>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
For More Information
http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html

Related

xpath: prefix must resolve to a namespace:xsd

I am a rookie in using jaxb2. I get this error:"xpath: prefix must resolve to a namespace:xsd" and I have no clue how to fix it.
This appen because I have two xsd files that trying to design the same class but in different packages of my java application
<jxb:bindings schemaLocation="../documentazione/xsd/Global/datatypes_global_v62.xsd">
<jxb:schemaBindings>
<jxb:package name="com.companyname.plugin.entities.global" />
</jxb:schemaBindings>
<jxb:bindings node="//xsd:complexType[#name='Contact']">
<jxb:class name="GlobalContact" />
</jxb:bindings>
</jxb:bindings>
<jxb:bindings schemaLocation="../documentazione/xsd/Global/pickupdatatypes_global-3.0.xsd">
<jxb:schemaBindings>
<jxb:package name="com.companyname.plugin.entities.pickup" />
</jxb:schemaBindings>
<jxb:bindings node="xsd://complexType[#name='contact']" >
<jxb:class name="Contact" />
</jxb:bindings>
</jxb:bindings>
I already tried to run the plugin, but I don't know how to fix the error.
How I fix the problem?
Simple! In the name space of my xjb file I was using:
<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2000/10/XMLSchema-instance"
xs:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
And this was fine for most of my xjb files, but in my case I want to use complexType node. In this case I have to use:
<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2000/10/XMLSchema-instance"
xs:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
the xmlns:xsd="http://www.w3.org/2001/XMLSchema help me to fix all my issue.

JAXB Generating Classes Issue

I am trying to use Eclipse to generate a few Java Classes. But somehow I am running into issues. When I try to do it, I get this error:
parsing a schema...
compiling a schema...
[ERROR] A class/interface with the same name "com.xml.apis.schemas.MPLSVPNListItem" is already in use. Use a class customization to resolve this conflict.
line 9 of file:/C:/Workspace/MyFiles/WebContent/WEB-INF/XSDFiles/jaxb_binding.xml
[ERROR] (Relevant to above error) another "MPLSVPNListItem" is generated from here.
line 3122 of file:/C:/Workspace/MyFiles/WebContent/WEB-INF/XSDFiles/APIcTypes.xsd
[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 9 of file:/C:/Workspace/MyFiles/WebContent/WEB-INF/XSDFiles/jaxb_binding.xml
[ERROR] (Related to above error) This is the other declaration.
line 3122 of file:/C:/Workspace/MyFiles/WebContent/WEB-INF/XSDFiles/APIcTypes.xsd
Failed to produce code.
I am using the JAXB Binding file (jaxb_binding.xml):
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jxb:bindings
schemaLocation="APIcTypes.xsd">
<jxb:bindings
node="//xs:complexType[#name='MPLS-VPNListItem']">
<jxb:class name="MPLS_VPNListItem" />
</jxb:bindings>
</jxb:bindings>
<jxb:bindings
schemaLocation="APIcTypes.xsd">
<jxb:bindings
node="//xs:complexType[#name='MPLSVPNListItem']">
<jxb:class name="MPLSVPNListItem" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Because I think these two tags are causing the problem.
I have to say that APIcTypes.xsd is actually an import in the XSD that I am trying to generate the classes for.
The includes structure is like:
API.xsd --> APIRequests.xsd --> APIcTypes.xsd --> APIsTypes.xsd.
I hope this is understandable. If you need any more info, please do let me know. Just cant get it to work!

How to custom xjc generated enum and field name?

Here is the question, our contract is XSD file. Lately we want it to support Json.But there is some tricky problem we have to solve. When I define xsd like this:
<xs:simpleType name="SomeType">
<xs:restriction base="xs:string">
<xs:enumeration value="SomeSelfDefineType" />
</xs:restriction>
</xs:simpleType>
The generated code is like this:
#XmlType(name = "SomeType")
#XmlEnum
public enum SomeType {
#XmlEnumValue("SomeSelfDefineType")
SOME_SELF_DEFINE_TYPE("SomeSelfDefineType")
}
When using XML, it's fine, because it reads the annotation info, but when we use Json, SomeSelfDefineType will be transfer into SOME_SELF_DEFINE_TYPE. Register a lot of custom Gson serializable/deserializable Interface to resovle this problem is not a good option for me.
I've checked out other's answers about how to custom some name of field or enum, but I really have a lot of enum definations. Is there any plugin or solutions for me to generate code like this:
#XmlType(name = "SomeType")
#XmlEnum
public enum SomeType {
#XmlEnumValue("SomeSelfDefineType")
SomeSelfDefineType("SomeSelfDefineType")
}
I am not familiar with JAXB or its plugins, could anyone give me some xjb settings or plugin for me to save this problem?
you can override the enum values using .xjb as below.
<?xml version="1.0"?>
<jxb:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="yours.xsd">
<jxb:bindings
node="//xs:simpleType[#name='SomeType']/xs:restriction/xs:enumeration[#value='SomeSelfDefineType']">
<jxb:typesafeEnumMember name="SomeSelfDefineType" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>

How to skip complexType in JAXB bindings

I use JAXB bindings to generate java class from an existing xml schema. But I want to skip class generation for types that endswith "Old" or declare an "obsolete" attribute or contains undescore.
I try, in vain, to modify my JAXB bindings file but I don't know what node write to declare these types skipped...
<!-- skip old types -->
<!-- with ie:obsolete attribute -->
<jaxb:bindings schemaLocation="external/insee/*.xsd">
<jaxb:bindings node="//*[#ie:obsolete='true']">
<!-- declare this type skipped -->
</jaxb:bindings>
</jaxb:bindings>
<!-- that endswith Old -->
<!-- that contains "_" underscore -->
Is there a solution?
Assuming none of the types you process reference these "skipped types", then you could use an external bindings file to specify that they correspond to an existing class so that a new one won't be generated. If these skipped types are referenced then references to this fake class will be brought into your model.
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:bindings schemaLocation="beta.xsd">
<jxb:bindings node="//xs:element[#name='person']/complexType">
<jxb:class ref="com.FakeClass"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Full Example
Shared class for child element in JAXB in different xmls/roots

Generating JAXB classes with multiple prefixes using an external binding

I have a group of XSD files, as follows:
http://www.iana.org/assignments/xml-registry/schema/contact-1.0.xsd
http://www.iana.org/assignments/xml-registry/schema/domain-1.0.xsd
http://www.iana.org/assignments/xml-registry/schema/epp-1.0.xsd
http://www.iana.org/assignments/xml-registry/schema/eppcom-1.0.xsd
http://www.iana.org/assignments/xml-registry/schema/host-1.0.xsd
Most of them are dependent on each other, so I created the following catalog file to allow the JAXB compiler (XJC) to generate the classes.
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<public publicId="urn:ietf:params:xml:ns:contact-1.0" uri="contact-1.0.xsd"/>
<public publicId="urn:ietf:params:xml:ns:domain-1.0" uri="domain-1.0.xsd"/>
<public publicId="urn:ietf:params:xml:ns:epp-1.0" uri="epp-1.0.xsd"/>
<public publicId="urn:ietf:params:xml:ns:eppcom-1.0" uri="eppcom-1.0.xsd"/>
<public publicId="urn:ietf:params:xml:ns:host-1.0" uri="host-1.0.xsd"/>
<public publicId="urn:ietf:params:xml:ns:rgp-1.0" uri="rgp-1.0.xsd"/>
</catalog>
Unfortunately, there are some collisions between some of the generated class names, which I must resolve - according to some research I need to create an external bindings file.
I would like to prefix all classes that are directly generated from each XSD.
Imported XSDs should be ignored (e.g. epp-1.0 imports eppcom-1.0.xsd), since they will already be generated if I repeat the process for each XSD. (i.e. prefix by XSD namespace if possible)
Does anyone know how I can solve this issue?
Since the XSD files are maintained by a 3rd party, I do not have the option of modifying the XSDs directly. I have tried the following external binding, but it does not work:
<jxb:bindings version="2.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings scd="x-schema:epp" node="/xs:schema">
<jxb:schemaBindings>
<jxb:nameXmlTransform>
<jxb:typeName prefix="Epp" />
<jxb:elementName prefix="Epp" />
<jxb:modelGroupName prefix="Epp" />
<jxb:anonymousTypeName prefix="Epp" />
</jxb:nameXmlTransform>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings scd="x-schema:eppcom" node="/xs:schema">
<jxb:schemaBindings>
<jxb:nameXmlTransform>
<jxb:typeName prefix="EppCom" />
<jxb:elementName prefix="EppCom" />
<jxb:modelGroupName prefix="EppCom" />
<jxb:anonymousTypeName prefix="EppCom" />
</jxb:nameXmlTransform>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
Output:
parsing a schema...
[ERROR] XPath evaluation of "/xs:schema" results in empty target node
line 4 of file:/C:/Users/Jin%20Kim/workspace/registry/server/src/main/resources/xsd/bindings.xjb
[ERROR] XPath evaluation of "/xs:schema" results in empty target node
line 14 of file:/C:/Users/Jin%20Kim/workspace/registry/server/src/main/resources/xsd/bindings.xjb
Failed to parse a schema.

Categories

Resources