xjc and XSD choice - java

When I run xjc to generate a Java type representing this XSD snippet:
<xs:complexType name="fileUploadRequest">
<xs:choice>
<xs:element name="path" type="xs:string"/>
<xs:element name="file" type="xs:base64Binary"/>
</xs:choice>
</xs:complexType>
I get a class that's indistinguishable from what it would have been if I'd specified a sequence with optional elements instead.
I want a type with a little bit of intelligence, that'll let me have at most 1 element of my choice at a time. If I invoke the generated setFile method for example, it should make the path null. Is there some plugin I can use for what seems like an obvious requirement of a code generator?

binding.xml
You can use the following external binding file to generate the type of property you are looking for:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<globalBindings choiceContentProperty="true"/>
</bindings>
XJC Call
The binding file is referenced using the -b flag.
xjc -b binding.xml schema.xsd
Generated Property
Now the following property will be generated:
#XmlElements({
#XmlElement(name = "path", type = String.class),
#XmlElement(name = "file", type = byte[].class)
})
protected Object pathOrFile;
For More Information
http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html

Related

JAXB maven plugin bindings does not generate classes with interface

I'm trying to generate the java files from XSD with use of jaxb2-maven-plugin. This works without any issues and I can see generated classes in target directory.
Now I decided to let all generated classes implement some interface. So I set up bindings.xjb file where I'm defining the interface. The issue is the plugin can not recognize correct namespace which defines inheritance.
Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/inheritance". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?
I think code-injector is not what I'm looking for, since this allow to define custom pieces of code to add to generated file.
I'm trying to use latest maven plugin:
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
The bindings.xjb file contains following content:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:extensionBindingPrefixes="inheritance"
version="3.0">
<jaxb:bindings schemaLocation="application.xsd" node="/xs:schema">
<jaxb:bindings node="//xs:complexType[#name='applicationType']">
<inheritance:implements>com.example.SomeInterface</inheritance:implements>
<jaxb:property name="inheritance"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
And the application.xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="application" type="applicationType"/>
<xs:complexType name="applicationType">
<xs:sequence>
<xs:element type="xs:string" name="language"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I have found this namespace xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" on the internet.
Also notice plugin requires version 3.0 of bindings.xjb file.
What is the correct definition of namespace to let plugin generate classes with interface?

Make xs and xmls:xsi attributes required in XSD

I have the following schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
...
</xs:element>
</xs:schema>
How I can define the following required attributes, so when adding a new translator node, those attributes are also added?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Translator.xsd"
If I put them in XSD, like this
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
<xs:attribute name="xmlns:xsi" type="xs:string" default="http://www.w3.org/2001/XMLSchema-instance"/>
<xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string" default="Translator.xsd"/>
</xs:element>
</xs:schema>
The following problem is reported by Xerces
[Error] :678:114: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'.
[Error] :678:114: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
[Error] :679:117: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xsi:noNamespaceSchemaLocation' is not a valid value for 'NCName'.
[Error] :679:117: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
A warning first: the XML Schema specification forbids declaring attributes in the XML Schema instance namespace, and explicitly discourages attempts to alter its behavior.
Having said that, the reason for the errors you are getting is that the name attribute only supports definition of new elements in the target namespace (or in this case in no namespace) by providing their local names.
You could technically do something like this, by referencing the xsi:noNamespaceSchemaLocation attribute, which is already defined in the builtin XML Schema instance namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="translator">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xsi:noNamespaceSchemaLocation" default="Translator.xsd"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
However, you cannot change its definition, and since this attribute is builtin and processed in a special way, I am not sure you can much influence its behavior.

How to resolve collision in the ObjectFactory on wsdl2java?

I'm using CXF and wsdl2java to autogenerate webservice classes.
Problem: somehow the webservice I want to connect to has duplicate names for some elements:
Two declarations cause a collision in the ObjectFactory class
The xsd is like:
<xs:schema targetNamespace="http://thenamespace">
<xs:complexType name="ViolatingName">
...
</xs:complexType>
<xs:element name="ViolatingName" nillable="true" type="tns:ViolatingName"/>
</xs:schema>
The xsd itself is imported inside the wsdl that is used to autogenerate the jaxb classes like this:
<wsdl:types>
<xsd:schema targetNamespace="http://imports">
<xsd:import schemaLocation="https://path.to.xsd" namespace="http://thenamespace" />
I'm trying to cover this using jaxb-bindings.xml:
<jaxb:bindings
xmlns:jaxb="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"
jaxb:extensionBindingPrefixes="xjc"
jaxb:version="2.1">
<jaxb:bindings schemalocation="https://path.to.xsd" node="//xs:schema">
<jaxb:bindings node=".//xs:element[#name='ViolatingName']">
<jaxb:property name="ViolatingNameBinding" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Result:
[ERROR] XPath evaluation of "//xs:schema" results in empty target node (org.apache.cxf:cxf-codegen-plugin:3.0.1:wsdl2java:generate-sources:generate-sources)
Why is the node wrong here? The xsd has a xs:schema tag, so why is this failing?
Interesting fact: when I use any xpath tool, download the XSD to my local machine an check the path, then //xs:schema/xs:element[#name='ViolatingName'] evaluates to the proper tag.
It turned out I have to apply a renaming/suffix on all xs:complexType elements like this:
<jaxb:bindings schemaLocation="https://path.to.xsd" node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="Type" />
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
Try jaxb:factoryMethod instead of jaxb:property.
<jaxb:bindings node=".//xs:element[#name='ViolatingName']">
<jaxb:factoryMethod name="ViolatingName1" />
</jaxb:bindings>
An example of binding using jaxb:factoryMethod.
Update:
This might help as well.
Property Binding Declarations
The binding declaration enables you to customize the binding of an XML schema element to its Java representation as a property. The scope of customization can either be at the definition level or component level depending upon where the binding declaration is specified.
The syntax for customizations is:
<property [ name = "propertyName"]
[ collectionType = "propertyCollectionType" ]
[ fixedAttributeAsConstantProperty = "true" | "false" | "1" | "0" ]
[ generateIsSetMethod = "true" | "false" | "1" | "0" ]
[ enableFailFastCheck ="true" | "false" | "1" | "0" ]
[ <baseType> ... </baseType> ]
[ <javadoc> ... </javadoc> ]
</property>
<baseType>
<javaType> ... </javaType>
</baseType>
name defines the customization value propertyName; it must be a legal Java identifier.
collectionType defines the customization value propertyCollectionType, which is the collection type for the property. propertyCollectionType if specified, can be either indexed or any fully-qualified class name that implements java.util.List.
fixedAttributeAsConstantProperty defines the customization value fixedAttributeAsConstantProperty. The value can be either true, false, 1, or 0.
generateIsSetMethod defines the customization value of generateIsSetMethod. The value can be either true, false, 1, or 0.
enableFailFastCheck defines the customization value enableFailFastCheck. The value can be either true, false, 1, or 0. Please note that the JAXB implementation does not support failfast validation.
<javadoc> customizes the Javadoc tool annotations for the property's getter method.
From this link
XSD
<xs:schema targetNamespace="http://thenamespace">
<xs:element name="ViolatingName" type="tns:ViolatingName"/>
<xs:complexType name="ViolatingName">
<xs:all>
<xs:element name="prova" type="xs:string"/>
</xs:all>
</xs:complexType>
<xs:element name="AdditionalInfos" type="AdditionalInfos"/>
<xs:complexType name="AdditionalInfos">
<xs:sequence>
<xs:element minOccurs="1" name="ViolatingName" type="tns:ViolatingName"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Binding
<bindings version="2.0" xmlns="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"
xmlns:annox="http://annox.dev.java.net"
xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
<bindings schemaLocation="../path/of/your.xsd">
<bindings node="//xs:complexType[#name='AdditionalInfos']//xs:sequence//xs:element[#name='ViolatingName']">
<property name="aaa" />
</bindings>
</bindings>
</bindings>
Generated Class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "AdditionalInfos", propOrder = {
"aaa",
})
#XmlRootElement
public class AdditionalInfos
implements Serializable
{
private final static long serialVersionUID = 12343L;
#XmlElement(name = "ViolatingName", required = true)
protected ViolatingName aaa;

Convert xs:string to java.util.UUID in jaxb

In jaxb, how do you convert a string in xsd to java.util.UUID? Is there a built-in data type converter or do I have to create my own custom converter?
This is much easier to do if you start with Java classes and use JAXB annotations. However, to do this using schema you must use a custom bindings file. Here is an example:
Schema: (example.xsd)
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com"
xmlns="http://www.example.com"
elementFormDefault="qualified">
<xs:simpleType name="uuid-type">
<xs:restriction base="xs:string">
<xs:pattern value=".*"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="example-type">
<xs:all>
<xs:element name="uuid" type="uuid-type"/>
</xs:all>
</xs:complexType>
<xs:element name="example" type="example-type"/>
</xs:schema>
Bindings: (bindings.xjb) (Note that for brevity in printMethod and parseMethod I assumed that the UuidConverter class was in the default package. These should be fully qualified in reality. So if UuidConverter where in package com.foo.bar then the values should be like com.foo.bar.UuidConverter.parse and com.foo.bar.UuidConverter.print
<!-- Modify the schema location to be a path or url -->
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
node="/xs:schema"
schemaLocation="example.xsd">
<!-- Modify this XPATH to suit your needs! -->
<jxb:bindings node="//xs:simpleType[#name='uuid-type']">
<jxb:javaType name=" java.util.UUID"
parseMethod="UuidConverter.parse"
printMethod="UuidConverter.print"/>
</jxb:bindings>
</jxb:bindings>
UuidConverter.java:
import java.util.UUID;
public class UuidConverter {
public static UUID parse(String xmlValue) {
return UUID.fromString(xmlValue);
}
public static String print(UUID value) {
return value.toString();
}
}
Sadly I can't point you to a good reference because its really not documented well. There are bits and pieces of how it all works spread out in blog posts. Took me a few hours to make this work the first time. :-/
Create a simple converter yourself:
UUID.fromString(String uuid);
http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html

Java/XSD parsing

I doubt if there is something like this but I thought to ask though:
Does anyone know if there is a library in Java that reads an xsd file and "creates" the defined elements e.g. in a String format to use in the code?
E.g. read in the following schema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="Street" type="xs:string" />
<xs:element name="Town" type="xs:string" />
<xs:element name="Country" type="xs:string" minOccurs="0" />
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And have a String in the following format:
<Address>
<Street></Street>
<Town></Town>
<Country></Country>
</Address>
Automatic tools do something similar, i.e. parse a WSDL and from the types section create for example JAXB classes that can be instances of the elements defined in schema.
Is there any library to do this?
UPDATE:
For example in Eclipse when creating an xml descriptor for a web application it presents a tree table with all the required elements for the users to fill in according to schema. How do they do it? I imagine they parse the xsds included in the jars
Any input is very welcome.
Thank you!
If its a WSDL file with which you want to generate Java classes, then Axis WSDL2Java (based on JAXB) can be used to get classes based on the schema defined in the WSDL.
JAXB also offers binding framework which you might want to look up.
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/twbs_jaxbschema2java.html
Above link should be useful.
oXygen has an XML instance generator that can generate a set of XML document samples based on a given XML Schema.
You can also invoke it from the commandline.

Categories

Resources