JAXB class ref prevent enum generation - xsd

I've found this QA already and was able to prevent the generation of classes, but enums are still generated.
I've tried to prevent it with custom binding:
<jxb:bindings node="//xs:simpleType[#name='myEnum']">
<jxb:class ref="com.myapp.enums.MyEnum" />
</jxb:bindings>
but it is not working for me.
Is there any possibility to prevent the generation of enums or maybe another customization?

You must use following snippet
<jxb:bindings node="//xs:simpleType[#name='myEnum']">
<jxb:typesafeEnumClass ref="com.myapp.enums.MyEnum"/>
</jxb:bindings>

Related

Generating JAXB classes for XML having same parent tag

I am getting xml input where parent tag is same but other tags are different.
every parent tag has attribute type which tells what content you should read.
i have created jaxb classes under different packages based on xml content ,
now problem is as parent class name is same for all ,so while making instance of JAXBContext ,which parent class should i passed ?
while marshelling we can specify if this is my tag then renamed to abc tag by passing annotation {name="abc"} , in same fashion what if i changed parent class name and based on annotation jaxb will unmarshel the xml
I got answer of this. i used binding file. where we can mentioned what should be name of generated class for particular tag.
<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="attribute.xsd">
<jxb:bindings node="//xs:element[#name='component']">
<jxb:class name="MyOwnClass"/>
</jxb:bindings>
</jxb:bindings>

how to add static property in XSD file

I have one Java interface for example as following
public interface A
public static final String SD_A= "SD-A";
My question is how write the above property in XSD file ? Because I have blaze advisor project where I used XML BOM getting error that- A is not defined in current scope. So when I am adding that interface as following
I am getting error like :- is not a constant or static property defined by 'A'
Any suggestion to resolve the issue?
If I got you right, you want to add a static field to your schema-derived classes.
The simplest thing you can do is to use the code injector plugin:
Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "
Something like:
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="/xs:schema/xs:complexType[#name='SomeExtension']">
<ci:code>
public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
</ci:code>
</jxb:bindings>
</jxb:bindings>
I have created XSD file through eclipse i.e File->New->Other->JAXB->Scema from JAXB Classes
from Java application of Java classes. But now I required to make a XSD of Java interface. so when I am following the above process to create a XSD it is showing error "
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.test.IClassproduct is an interface, and JAXB can't handle interfaces.
So I have added that interface in another XSD file as below format
<xs:complexType name="IClassProducts"> <xs:sequence> <xs:element name="AHS" type="static" fixed="AHS" /> </xs:sequence> </xs:complexType>
But now it is showing error
'AHS' is not a constant or static property defined by 'IClassProducts'
so you have suggested to use below format .
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="/xs:schema/xs:complexType[#name='SomeExtension']">
<ci:code>
public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
</ci:code>
</jxb:bindings>
</jxb:bindings>
My question is where I will add this and how to use . My purpose is anyhow I have use the above static property 'AHS' in XSD file So that when I will load that XSD file it can identify that property and can resolve the below error
'AHS' is not a constant or static property defined by 'IClassProducts'

Is there a way to ignore empty nodes in JAXB bindings file?

I am trying to create a generic .xjb bindings file to provide consistent Java classes generation from WSDLs / XSDs across multiple projects.
We generate the code via maven-jaxb2-plugin (Made by #lexicore).
The problem lies in the multiple projects part. If a particular binding instruction matches nothing in the provided XSD or WSDL, the class generation fails with
XPath evaluation of "<some_xpath_expression>" results in empty target node
How can I tell JAXB to ignore these cases so the bindings file can be used on any project without fine-tuning, regardless of the elements types used?
Here is a (stripped down) version showcasing the problem I have:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="path/to/the/schema" node="/xs:schema">
<jxb:bindings multiple="true" node="//*[#type='xs:dateTime']">
<xjc:javaType name="java.time.LocalDateTime" adapter="a.b.c.LocalDateTimeAdapter" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
If I try to generate classes from a schema with no dateTime element, it will fail.
The objective is, in the end, to create something all projects of various teams could reuse without changing anything but the schemaLocation.
You need to set the required="no" attribute on the binding that you would like to allow not to match any node, i.e.
<jxb:bindings required="no" multiple="true" node="//*[#type='xs:dateTime']">
Depending on your context you may choose to say required="false" or required="0" as well.

How can I specify JAXB customizations externally to a WSDL file?

I have a WSDL file for a service implemented in .NET. I also have the same file with some "customizations" made by a 3rd-party to make the file tolerable to wsimport, mostly of the form:
<s:annotation>
<s:appinfo>
<jaxb:property name="result"/>
</s:appinfo>
</s:annotation>
I'd like to be able to process the original WSDL from the vendor plus these overrides, but I'd like to specify them externally. I see that I can use the -b option for wsimport for "binding files" and I've tried to write an override file that currently looks like this:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings node="//xs:element[#name='MyElementResult']">
<jxb:property name="result"/>
</jxb:bindings>
</jxb:bindings>
I've verified that "MyElementName" does in fact exist, in an element found here:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="vendor-uri"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:s2="http://microsoft.com/wsdl/types/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="vendor-namespace"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
[...]
<s:element name="MyElementResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MyElementResult" type="tns:Result" />
I'm getting this warning (and therefore no changes) from wsimport:
[ERROR] XPath evaluation of "//xs:element[#name='MyElementResult']" results in empty target node
line 4 of file:/Users/..../wsdl/custom-bindings.xjb
Am I missing something in my declaration(s)? Do I have my XPath expression incorrect? If I get my XPath/overrides working, is it formatted correctly in order to achieve the same result as if I had edited the original WSDL?
Is this even possible using external files, or will I have to re-modify any future versions of the WSDL with these same changes?
You need to give the schema location in your bindings XML file.
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings
schemaLocation="PATH_TO_YOUR_WSDL#types?schema1"
node="//xs:schema[#targetNamespace='SCHEMA_NAMESPACE']">
<jxb:bindings node="//xs:element[#name='MyElementResult']">
<jxb:property name="result"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
The #types?schema1 after the WSDL file name will specify which schema in the WSDL you are binding, starting at 1.

How can I tell wsimport that separate WSDL files are referring to the same object classes?

I have three different JAX-WS services which use the same classes on the server (e.g. ServiceA, ServiceB, and ServiceC, all of which use MyCommonClass as a parameter). Another module we are developing is using wsimport to create a client for these services, however the problem is that wsimport creates separate instances of MyCommonClass for each service:
com.company.servicea.endpoint.MyCommonClass
com.company.serviceb.endpoint.MyCommonClass
etc.
I know that I could use the wsimport -p option to specify a common package for each endpoint, however I'd like to keep most of the classes in separate packages, but just to share certain common ones. From what I have read it sounds like a JAXB bindings file(s) might be able to help, but I haven't yet figured out the exact syntax to achieve the desired result. I think I'll need a separate bindings file for each service (as I call wsimport once for each one), which looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1" xmlns:tns="http://endpoint.servicea.company.com/">
<bindings node="//xsd:complexType[#name='myCommonClass']">
<class name="com.company.model.MyCommonClass"/>
</bindings>
</bindings>
Am I on the right track? Or do you have any alternative solutions to the problem?
Define your common classes in an xsd and import it in to the service WSDL's.
Then use the schema customization to generate definitions in this schema in to a separate package like "com.company.model"
<jxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="1.0">
<jxb:bindings schemaLocation="model.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.company.model"/>
</jxb:schemaBindings>
</jxb:bindings>
...
ref: http://jax-ws.java.net/jax-ws-21-ea1/docs/customizations.html#2.6_Class_Customization

Resources