jaxb multiple class generated for same type incase of external bindings - jaxb

I have following schema and corresponding bindings file. I use jaxb2 maven plugin to generate JAXB classes.
person.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="pType" type="pType" minOccurs="0" />
<xs:element name="sex" type="xs:string"
minOccurs="1" />
<xs:element name="dob" type="xs:string"
minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- some more elements ignored for clarity.................
.......................... -->
<xs:complexType name="pType">
<xs:sequence>
<xs:element name="category" type="xs:string"
minOccurs="0" />
<xs:element name="blahh" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
jaxb bindings
<jxb:bindings schemaLocation="person.xsd">
<jxb:bindings node="//xs:complexType[#name='pType']">
<jxb:class name="PersonType" />
</jxb:bindings>
</jxb:bindings>
<jxb:bindings schemaLocation="person.xsd">
<jxb:bindings
node="//xs:element[#name='person']//xs:element[#name='pType']">
<jxb:class ref="PersonType" />
</jxb:bindings>
</jxb:bindings>
I have defined bindings to override the name for the <xs:complexType name="pType"> as PersonType. On XJC generation, it generates PersonType.class and PType.class.
If I define <xs:complexType name="pType"> internally inside the element <xs:element name="pType" > then it did not generate PType.class.
But I have to declare <xs:complexType name="pType"> at the root level in schema, because this xs:complexType is referenced by other schemas as well.
I tried to override for both <xs:complexType name="pType"> and <xs:element name="pType" > in bindings and yet the PType.class gets generated.
How could I instruct it not to generate PType.class ?

The problem was that I have 2 executions for each of the schemas (dependent schema person2.xsd and person.xsd) in maven-jaxb2-plugin. So I had manually written the episode file to reference the already created <xs:complexType name="pType"> and resolved the issue.
For the benefit of someone who has same problem, here are the plugin execution details and episode file. Please note that I did not use namespaces hence you find the scd is simple without namespace.
person-episode
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
<jaxb:bindings scd="x-schema::">
<jaxb:bindings scd="/type::pType">
<jaxb:class ref="org.wipo.pct.test.PersonType"/>
<jaxb:package name="org.wipo.pct.test" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
pom.xml
<execution>
<id>person</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaIncludes>
<include>person.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>pbindings.xjb</include>
</bindingIncludes>
<verbose>true</verbose>
</configuration>
</execution>
<execution>
<id>person2</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaIncludes>
<include>person2.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>pbindings2.xjb</include>
</bindingIncludes>
<verbose>true</verbose>
<args>
<arg>-b</arg>
<arg>src/main/resources/person-episode</arg>
<arg>-Xsimplify</arg>
<arg>-Xinheritance</arg>
</args>
</configuration>
</execution>

Related

Refining external bindings XML for xjc

Hi I'm trying to create an external bindings XML for xjc but I'm getting 2 errors:
[ERROR] A class/interface with the same name "x.x.DaysOfWeek" is already in use. Use a class customization to resolve this conflict.
[ERROR] (Relevant to above error) another "DaysOfWeek" is generated from here.
Am I missing something major? I need a second pair of eyes. Thanks
<!-- data.xsd -->
<xs:complexType name="DaysOfWeek">
<xs:sequence>
<xs:element ref="DaysOfWeek"/>
</xs:sequence>
</xs:complexType>
<xs:element name="DaysOfWeek">
<xs:complexType mixed="true">
<xs:attribute name="Type" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
and I have bindings.xml right now
<jaxb:bindings version="1.0" 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:bindings schemaLocation="data.xsd">
<jaxb:schemaBindings>
<jaxb:package name="net.opengis.kml.v_2_2_0"/>
</jaxb:schemaBindings>
<jaxb:bindings node="xs:complexType[#name='DaysOfWeek']//xs:element[#ref='DaysOfWeek']">
<jaxb:property name="ComplexDaysOfWeek"/>
</jaxb:bindings>
<jaxb:bindings node="xs:element[#name='DaysOfWeek']">
<jaxb:factoryMethod name="ComplexDaysOfWeek"/>
</jaxb:bindings>
</jaxb:bindings>
You have a collision on generated classes. Try adding:
<jaxb:bindings node="xs:element[#name='DaysOfWeek']//xs:complexType">
<jaxb:class name="ComplexDaysOfWeekType"/>
</jaxb:bindings>
(Maybe without //xs:complexType, not quite sure.)
The issue has been solved using
<jaxb:bindings schemaLocation="data.xsd" node="/xs:schema">
<jaxb:bindings node="xs:element[#name='DaysOfWeek']">
<jaxb:class name="ComplexDaysOfWeek"/>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[#name='DaysOfWeek']//xs:element[#ref='DaysOfWeek']">
<jaxb:property name="ComplexDaysOfWeek"/>
</jaxb:bindings>
</jaxb:bindings>

Define Document Type in plain XML for Nuxeo

I try to grasp the mechanics of Nuxeo and as such started with all tutorials and documents around. I put together a bundle that seemed quite right for me and tried now for several hours to make it work in any flavor - without success.
Using Nuxeo 8.3.
Anything i am missing?
The manifest at META_INF/MANIFEST.MF
Manifest-Version: 1.0
Bundle-SymbolicName: de.foo.nuxeo.tutorial.tutorial-demo1;singlet
on=true
Bundle-Version: 1.0.0
Bundle-Name: tutorial-demo1
Bundle-ClassPath: .
Bundle-ManifestVersion: 2
Bundle-Vendor: de.foo.nuxeo.tutorial
Nuxeo-Component: OSGI-INF/foo-demo-component-core-types.xml,OSGI-INF/foo-demo-component-ui-types.xml
A core document type at OSG-INF/foo-demo-component-core-types.xml
<?xml version="1.0"?>
<component name="de.foo.demo.component.core.types" version="1.0">
<extension target="org.nuxeo.ecm.core.schema.TypeService" point="schema">
<schema name="foo" src="schemas/foo-demo-foo.xsd"
prefix="isfoo" />
</extension>
<extension target="org.nuxeo.ecm.core.schema.TypeService" point="doctype" >
<doctype extends="File" name="FooFile">n
<schema name="foo" />
</doctype>
</extension>
</component>
A ECM document type at OSGI-INF/foo-demo-component-ui-types.xml
<?xml version="1.0"?>
<component name="de.foo.demo.component.ui.types" version="1.0">
<require>org.nuxeo.ecm.platform.types</require>
<extension target="org.nuxeo.ecm.platform.types.TypeService"
point="types">
<type id="FooFile">
<label>Foo File</label>
<icon>/icons/file.gif</icon>
<bigIcon>/icons/file_100.png</bigIcon>
<category>SimpleDocument</category>
<description>A foo file</description>
<default-view>view_documents</default-view>
<layouts mode="any">
<layout>heading</layout>
<layout>file</layout>
</layouts>
</type>
<type id="Folder">
<subtypes>
<type>FooFile</type>
</subtypes>
</type>
</extension>
</component>
A schema at schemas/foo-demo-foo.xsd
<?xml version="1.0"?>
<xs:schema
targetNamespace="http://www.foo.de/nuxeo/schemas/foo/"
xmlns="http://www.foo.de/nuxeo/schemas/foo/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="gnu" type="xs:string" />
<xs:element name="gnat" type="xs:string" />
</xs:schema>
Packed in a jar and deployed to nxserver/bundles. After a restart i see the bundle loaded in the administrator view, no errors in the log file. When i create a new document, i do not see the new type...
What am i missing?

jaxb2 simplify plugin binding issue

I tried to use JAXB2 simplify plugin using the XJC 2.2.4.
D:\>xjc -d . -extension -p org.my.space sample.xsd parsing a schema... [ERROR] Unsupported binding namespace
"http://jaxb2-commons.dev.java.net/basic/simplify". Perhaps you meant
"http://jaxb.dev.java.net/plugin/code-injector"? line 7 of file:/D:/sample.xsd
Failed to parse a schema.
sample.xsd has the declared simplify
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
jaxb:extensionBindingPrefixes="simplify"
xmlns="http://www.example.org/sampleXML" targetNamespace="http://www.example.org/sampleXML"
elementFormDefault="qualified">
any thoughts?
EDIT:
I tried other way around with bindings file xjb and still the same error. Does it mean my XJC version(2.2) does not support JAXB2 simplify ?
xjc -d . -extension -b bindings.xjb sample.xsd
<bindings version="2.1"
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
extensionBindingPrefixes="simplify">
<bindings schemaLocation="sample.xsd" node="/xs:schema">
<bindings
node="/xs:schema/xs:complexType[#name='dlist']/xs:choice/xs:element[1]">
<simplify:as-element-property />
</bindings>
</bindings>
</bindings>
I switched to maven jaxb2 plugin and with some more efforts it worked fine. For people who have trouble in future. This setup works just fine in JDK 1.6 (for those still cant migrate).
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- plugin to compile schemas to JAXB classes -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb21-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<id>generate-JAXB</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>sample.xsd</include>
</schemaIncludes>
<cleanPackageDirectories>true</cleanPackageDirectories>
</configuration>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xsimplify</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.11.0</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
I did not use binding.xjb as I have included the simplify directly in the schema (as i have access).
sample.xsd
<xs:complexType name="doclist">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property />
</xs:appinfo>
</xs:annotation>
<xs:element name="document1" type="type1" />
<xs:element name="document2" type="type2" />
</xs:choice>
</xs:sequence>
<xs:attribute name="heading" type="xs:string" />
</xs:complexType>

Create a common xsd generated class to be used by other packages

I am trying to use the same generated class but in separate packages. So the structure should look something like this:
com.test.common
-commonType.java
com.test.A
-objectA.java
com.test.B
-objectB.java
But i keep getting this:
com.test.common
-commonType.java
com.test.A
-objectA.java
-commonType.java
com.test.B
-objectB.java
-commonType.java
My common.xsd looks like this:
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/common"
xmlns="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="CommonType">
<xs:sequence>
<xs:element name="name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
the objectA.xsd looks like
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/objectA"
xmlns:common="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="ObjectA">
<xs:sequence>
<xs:element name="size" type="xs:string" />
<xs:element name="commonA" type="common:CommonType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
And objectB.xsd looks like:
<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
targetNamespace="http://test.com/magic/objectB"
xmlns:common="http://test.com/magic/common"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xs:complexType name="ObjectB">
<xs:sequence>
<xs:element name="version" type="xs:string" />
<xs:element name="commonB" type="common:CommonType" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I have a common binding file common.xjb which looks like this:
<jxb:bindings schemaLocation="../xsd/common.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="com.test.common"/>
</jxb:schemaBindings>
</jxb:bindings>
And finally my maven job looks like this:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<args>
<arg>-Xequals</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.3</version>
</plugin>
</plugins>
<episode>true</episode>
<extension>true</extension>
<verbose>true</verbose>
<generateDirectory>src/main/java</generateDirectory>
</configuration>
<executions>
<execution>
<id>common</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.common</generatePackage>
<schemaIncludes>
<includeSchema>xsd/common.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>login</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.A</generatePackage>
<bindingIncludes>
<includeBinding>xjb/commons.xjb</includeBinding>
</bindingIncludes>
<schemaIncludes>
<includeSchema>xsd/objectA.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>alert</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.test.B</generatePackage>
<bindingIncludes>
<includeBinding>xjb/commons.xjb</includeBinding>
</bindingIncludes>
<schemaIncludes>
<includeSchema>xsd/objectB.xsd</includeSchema>
</schemaIncludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
There is a part in the maven-jaxb2-plugin documentation dedicated specifically to the separate schema compilation. But you can also achieve your goal with usual XJC bindings.
Do not use generatePackage in the plugin config, use jaxb:package in bindings, ex.:
<jaxb:schemaBindings>
<jaxb:package name="generatePackage"/>
</jaxb:schemaBindings>
Use <jaxb:class ref="com.test.common.CommonType"/> on commonType in xjb/commons.xjb.
SO Disclaimer: I'm the author of the maven-jaxb2-plugin.
You can use the -episode extension in the JAXB XJC to handle this use case:
XJC call for common.xsd
xjc -d out -episode common.episode common.xsd
XJC call for objectA.xsd
The episode file produced from the first step is really a JAXB external bindings file that contains links between schema types and existing classes. This prevents XJC from regenerating these classes.
xjc -d out objectA.xsd -extension -b common.episode
For a Detailed Example
Can Castor handle class generation from multiple XSDs importing from a base XSD?

JAXB External Custom Binding XJC Issue - Parsing results in empty node

Forgive me if this is a duplicate. Here is my binding.xjb file. But now i am getting the regular error that the complex type target "AddBankVaultRplyType" is not found. I don't see any issue. Can somebody help me with this? I am listing the xsd that i am trying to customize
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:pd="http://chubb.com/cpi/polsvc/xmlobj"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="inheritance"
jxb:version="2.1"
>
<jxb:bindings node="/xs:schema/xs:ServiceReply/xs:complexType[#name='AddBankVaultRplyType']">
<inheritance:extends>com.print.poc.AddressTypeHelper</inheritance:extends>
</jxb:bindings>
Here is the piece of XSD that i am trying to customize
<xs:schema xmlns:pd="http://com/polsvc/xmlobj" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://com/polsvc/xmlobj" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="AddBankVaultRplyType">
</xs:complexType>
<xs:element name="ServiceReply">
<xs:complexType>
<xs:sequence>
<xs:element name="ReplyHeader" type="pd:MsgHeaderType"/>
<xs:element name="RequestHeader" type="pd:MsgHeaderType"/>
<xs:choice>
<xs:element name="AddBankVaultReply" type="pd:AddBankVaultRplyType"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Now if i run XJC it is saying me that the target "/xs:schema/xs:ServiceReply/xs:complexType[#name='AddBankVaultRplyType']" results in empty node. What is the mistake i am doing here
You will need to wrap in a bindings that has the schema location set. It should be something like:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:pd="http://chubb.com/cpi/polsvc/xmlobj"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="inheritance"
version="2.1">
<jxb:bindings schemaLocation="your-schema.xsd">
<jxb:bindings node="//xs:complexType[#name='AddBankVaultRplyType']">
<inheritance:extends>com.print.poc.AddressTypeHelper</inheritance:extends>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
For more information:
http://jaxb.java.net/guide/Dealing_with_errors.html
I finally got mine workign with subclassing as well as adding #XmlRootElement to those dang complexTypes that are used by a root element(I don't get why JAXB doesn't add it for me, but this does the trick of doing that since JAXB doesn't)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
http://annox.dev.java.net "
jaxb:extensionBindingPrefixes="xjc annox"
version="2.1">
<jaxb:globalBindings>
<jaxb:serializable uid="1"/>
<!-- All generated classes must have MySignature interface (supplied in dependencies) -->
<xjc:superClass name="com.cigna.framework.DataObject"/>
<xjc:superInterface name="com.cigna.framework.InterfaceTest"/>
<!-- All temporal fields are implemented as Joda DateTime and use DateUtils as an adapter -->
<jaxb:javaType
name="org.joda.time.DateTime"
xmlType="xs:time"
parseMethod="com.cigna.framework.util.DateUtil.stringToDateTime"
printMethod="com.cigna.framework.util.DateUtil.dateTimeToString"
/>
</jaxb:globalBindings>
<!-- Application of annotations to selected classes within schemas -->
<!-- org.example.SomeRootType #XmlRootElement -->
<jaxb:bindings schemaLocation="../schemas/externalaction_2012_03.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.action"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../schemas/common_2012_04.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.common"></jaxb:package>
</jaxb:schemaBindings>
<jaxb:bindings node="xs:complexType[#name='PersonNameType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="SomeRootType"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../schemas/utilities_2012_03.xsd" node="/xs:schema">
<jaxb:schemaBindings >
<jaxb:package name="com.framework.util"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Of course I struggled with the pom.xml alot but finally came to this solution which worked for me.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>process-xsd</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<schemaIncludes>
<include>schemas/*.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>schemas/*.xjb.xml</include>
</bindingIncludes>
<generateDirectory>${project.build.directory}/generated-sources</generateDirectory>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.3</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.3</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
later,
Dean

Resources