Jaxb POJOs generation with Inheritance - java

I want to generate the POJOs using the xml-binding with a different hierarchy that I have right now.
Now I have an xsd like this one:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://manueldoncel.com" xmlns:tns="http://manueldoncel.com" elementFormDefault="qualified">
<!-- Base element that any element uses / extends -->
<complexType name="baseElement" abstract="true">
<sequence>
<element name="attributes" type="anyType" minOccurs="0" />
</sequence>
<attribute name="id" type="string" />
</complexType>
<complexType name="Square">
<complexContent><extension base="tns:baseElement" /></complexContent>
</complexType>
<complexType name="Triangle">
<complexContent><extension base="tns:baseElement" /></complexContent>
</complexType>
</schema>
An a xjb like this;
<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="figures.xsd" node="/xs:schema">
<jxb:globalBindings>
<xjc:simple />
<xjc:superClass name="org.manuel.metadata.Figure"/>
</jxb:globalBindings>
</jxb:bindings>
</jxb:bindings>
But, I would like to have a better hierarchy with this approach
public abstract class Figure {
// some methods for all the figures
}
public abstract class ThreeSideFigure extends Figure {
.... // some methods for only the Three side Figures
}
So, the Triangle POJO generated from the XSD should extend from ThreeSideFigure rather than from Figure.
In this particular xsd I only put 2 figures, but I can have much more. I would like to be able to specify in the xjb that all the complexType should extends from Figure but only a few of them, should extends from ThreeSideFigure.
Do you know how the xjb should look like?

I don't think the Metro JAXB RI extensions will let you do that.
From the 2.2.7 documentation:
3.1.3. Extending a Common Super Class
The <xjc:superClass> customization allows you to specify the fully
qualified name of the Java class that is to be used as the super class
of all the generated implementation classes. The <xjc:superClass>
customization can only occur within your <jaxb:globalBindings>
customization on the <xs:schema> element
That said, the answer to XJC superinterface and superclass only for all classes? suggests that you may be able to do it with 3rd party extensions. There are some details about plugins in the documentation.

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?

How do I convert the following xml into java objects using JAXB

What is the best way to convert this XML into Java objects?
<entity>
<customers id=2 other="data">
<customer name="john">testData1</customer>
<customer name="jenny">testData2</customer>
<customer name="joe">testData3</customer>
<customer name="joanna">testData4</customer>
</customers>
</entity>
Is it best to use a custom XMLAdapter with a HashMap to convert multiple xml rows of <customer>? I'm not sure if the XMLAdapter is the proper use case for this scenario. Any ideas would be appreciated.
Since the nesting isn't very deep, you could just have Entity, Customer classes and then use these annotations for the mapping in the entity class:
#XmlElementWrapper(name="customers")
#XmlElement(name="customer")
public void setCustomers(List<Customer> customers) {
this.customers= customers;
}
References:
XmlElementWrapper
Best approach, in my opinion, would be to write an xsd file to validate against your xml. You can use that to generate your java classes using xjc which comes bundled with Java. This should get you there.
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:NameOfNamespace="http://enter.your.namespace.here"
targetNamespace="http://enter.your.namespace.here"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<complexType name="customer">
<simpleContent>
<extension base="string">
<attribute name="name"/>
</extension>
</simpleContent>
</complexType>
<complexType name="customers">
<sequence>
<element name="customer" type="NameOfNamespace:customer"/>
</sequence>
<attribute name="id" type="positiveInteger"/>
<attribute name="other"/>
</complexType>
<complexType name="entity" >
<sequence>
<element name="customers" type="NameOfNamespace:customers" minOccurs="1" maxOccurs="1"/>
</sequence>
</complexType>
<element name="entity" type="NameOfNamespace:entity"/>
</schema>
Open a command prompt to the folder where you put your xsd file, and then generate java code you'll just need to type:
$ xjc nameOfSchemaFile.xsd
assuming your java 'bin' folder is in your path. The classes generated will be created in folder with the same name as your targetNamespace.
Using these you can follow the instructions in Naimish's example JAXB Hello World Example

How to merge more than one XSD file to one XSD file?

I am not master in XML and XSD.
Just want to know how I can merge more than one XSD file to one XSD file?
Thanks in Advance.
You can use import (different namespace) and include (same namespace) multiple times. redefine can also be used multiple times. It depends on what you mean by "merge."
See also http://www.herongyang.com/XML-Schema/Multiple-XSD-Schema-Document-Include-Redefine-Import.html or http://msdn.microsoft.com/en-us/library/ee254473%28v=bts.10%29.aspx.
Edit: redefine can be used multiple times (similar to include).
Examples (validated in Eclipse) follow. I used different namespace (as the "merging" target namespace) and element names where necessary:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/m"
xmlns:tns="http://www.example.org/m" elementFormDefault="qualified">
<!-- import: different (i.e. not target) namespace -->
<import namespace="http://www.example.org/a" schemaLocation="so20046640a.xsd"/>
<import namespace="http://www.example.org/b" schemaLocation="so20046640b.xsd"/>
<!-- include: same namespace -->
<include schemaLocation="so20046640c.xsd"/>
<include schemaLocation="so20046640d.xsd"/>
<!-- redefine: same namespace -->
<redefine schemaLocation="so20046640e.xsd"/>
<redefine schemaLocation="so20046640f.xsd"/>
</schema>
...a.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/a"
xmlns:tns="http://www.example.org/a" elementFormDefault="qualified">
<element name="a" type="int"/>
</schema>
...b.xsd: Same as ...a.xsd but target namespace .../b
...c.xsd: Same as ...a.xsd but target namespace .../m
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/m"
xmlns:tns="http://www.example.org/m" elementFormDefault="qualified">
<element name="a" type="int"/>
</schema>
...d.xsd: Same as ...c.xsd but element name b.
...e.xsd: Same as ...c.xsd but element name e.
...f.xsd: Same as ...c.xsd but element name f.

Insert custom annotation in java 'field' using annotate plugin + JAXB (upon xsd -> java)

Use case:
Wanna insert custom annotation to fields in java class generated by JAXB
Problem:
Using Annotate plugin + JAXB [1], am able to successfully insert custom annotations but they are getting inserted at getter method rather than field. Morphia (mongo DB) annotations (that i actually want to insert) however can annotate only java fields [2].
My test xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
<xsd:element name="hoo" type="External" />
<xsd:complexType name="External">
<xsd:sequence>
<xsd:element name="bar" type="xsd:string" />
<xsd:element name="hoobar" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
My test binding xjb:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
<jaxb:bindings schemaLocation="external.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[#name='External']/xs:sequence/xs:element[#name='bar']">
<annox:annotate>
<annox:annotate
annox:class="java.lang.SuppressWarnings"
impl="com.acme.foo.MyFieldBridge">
</annox:annotate>
</annox:annotate>
</jaxb:bindings>
My generated java snippet:
#XmlElement(required = true)
protected String bar;
#XmlElement(required = true)
protected String hoobar;
/**
* Gets the value of the bar property.
*
* #return
* possible object is
* {#link String }
*
*/
#SuppressWarnings({
})
public String getBar() {
return bar;
}
As you can see, i want to annotate "bar" field. Please advise. Ask for more if needed.
[1] Generate #Indexed annotation using Jaxb or HyperJaxb
[2] For sample see #Id annotation of Morphia
Ok, you figured it out yourself. Use <annox:annotate target="field"> to annotate a field. Other options are:
setter
setter-parameter
getter
field
class
See the documentation.
Just one more thing: you need to put the field attribute to the outer <annox:annotate> tag:
<annox:annotate target="field">
<annox:annotate annox:class="java.lang.SuppressWarnings"/>
</annox:annotate>
Putting it to the same tag as the annox:class attribute resides might not work. That happend to me.

JAXB XmlID and XmlIDREF annotations (Schema to Java)

I am exposing a web service using CXF. I am using the #XmlID and #XmlIDREF JAXB annotations to maintain referential integrity of my object graph during marshalling/unmarshalling.
The WSDL rightly contains elements with the xs:id and xs:idref attributes to represent this.
On the server side, everything works really nicely. Instances of Types annotated with #XmlIDREF are the same instances (as in ==) to those annotated with the #XmlID annotation.
However, when I generate a client with WSDLToJava, the references (those annotated with #XmlIDREF) are of type java.lang.Object.
Is there any way that I can customise the JAXB bindings such that the types of references are either java.lang.String (to match the ID of the referenced type) or the same as the referenced type itself?
Use the inline JAXB bindings to indicate the type to be used. Then JAXB generated code will have correct type.
<complexType name="Column">
<sequence>
<element name="name" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="referencedColumn" type="IDREF" maxOccurs="1" minOccurs="0">
<annotation>
<appinfo>
<jaxb:property>
<jaxb:baseType name="Column"/>
</jaxb:property>
</appinfo>
</annotation>
</element>
</sequence>
<attribute name="id" type="ID" use="required"></attribute>
</complexType>
Also note that you have to declare the jaxb namespace and JAXB version in the schema element.
<schema targetNamespace="http://example.com/schema"
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0">
The following seems to at least create string properties for elements/attributes of type xs:IDREF. A good start, but ideally JAXB would generate properties of the same type as the element being referenced. I'll report back if/when I find out how to do that. This result may indicate that I need to write my own converters which would be a shame.
<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings>
<jxb:javaType name="java.lang.String" xmlType="xs:IDREF" parseMethod="javax.xml.bind.DatatypeConverter.parseString" printMethod="javax.xml.bind.DatatypeConverter.printString" />
</jxb:globalBindings>
</jxb:bindings>
OK, so this isn't going to work. It's not possible for JAXB to generate code with the correct types for the IDREFs because the schema can't specify the types of references and there may be IDREF's pointing to different complex types. How would JAXB know what are the types of the references? An extension to XML Schema would do it! :)

Categories

Resources