isSet methods not generated using jaxb2-basics simplify plugin - java

I have a schema with two elements within a <xs:choice/> block, as per the following fragment:
<xs:complexType name="POCD_MT000040.Component2">
<xs:sequence>
<xs:element name="realmCode" type="CS" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="typeId" type="POCD_MT000040.InfrastructureRoot.typeId" minOccurs="0"/>
<xs:element name="templateId" type="II" minOccurs="0" maxOccurs="unbounded"/>
<xs:choice>
<xs:element name="nonXMLBody" type="POCD_MT000040.NonXMLBody"/>
<xs:element name="structuredBody" type="POCD_MT000040.StructuredBody"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="nullFlavor" type="NullFlavor" use="optional"/>
<xs:attribute name="typeCode" type="ActRelationshipHasComponent" use="optional" fixed="COMP"/>
<xs:attribute name="contextConductionInd" type="bl" use="optional" fixed="true"/>
</xs:complexType>
This is part of the HL7/CDA specification schemas, by the way.
Well, I am using maven-jaxb2-plugin to generate the Java classes, with a touch of the jaxb2-basics plugin to simplify complex elements, like nonXMLBody and structuredBody. Let's say I have a bindings file with the following contents:
<jxb:bindings
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"
xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify"
jxb:extensionBindingPrefixes="xjc simplify"
version="2.1">
<jxb:globalBindings
typesafeEnumMaxMembers="1000"
generateMixedExtensions="true"
generateIsSetMethod="true"
choiceContentProperty="true">
<xjc:serializable uid="1" />
</jxb:globalBindings>
<jxb:bindings schemaLocation="xsd/POCD_MT000040.xsd">
<jxb:bindings node="xs:complexType[#name='POCD_MT000040.Component2']//xs:choice">
<simplify:as-element-property />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
The attributes are generated as expected:
protected POCDMT000040NonXMLBody nonXMLBody;
protected POCDMT000040StructuredBody structuredBody;
However, although I am using generateIsSetMethod="true" in the global bindings, the methods isSetNonXMLBody ( ) and isSetStructuredBody ( ) are not being generated at all. The equivalent methods for the rest of the attributes are present, though, like isSetTypeId ( ).
Am I missing any option I should know of to generate these methods in the aforementioned elements?

Related

XSD "property already defined"

I'm trying to use jaxb to compile some Veracode provided .xsd files
the "detailedreport.xsd" is throwing this error:
[ERROR] Property "Vulnerabilities" is already defined. Use <jaxb:property> to resolve this conflict.
line 930 of file:detailedreport.xsd
[ERROR] The following location is relevant to the above error
line 936 of detailedreport.xsd
when I look at the XSD file, I see that Vulnerabilities is both an attr and a type:
<xs:complexType name="Component">
<xs:annotation>
<xs:documentation>
The element describe the details of vulnerable component.
* file_paths: File paths of the component.
-----> * vulnerabilities : Vulnerabilities of the component.
* violated_policy_rules: Violated policy rules of the component.
* component_id: The id of the component.
* file_name: File name of the component.
-----> * vulnerabilities: Number of vulnerabilities available in the component.
* max_cvss_score: Max cvss_score of the component.
* library: Library name of the component.
* version: Version of the component.
* vendor: Vendor name of the component.
* description: Description about component.
* blacklisted: Component's blacklisted status.
* new: Component added newly.
* added_date: Component's added_date.
* component_affects_policy_compliance: COmponent's policy violation status.
* licenses: Contains license details of the component.
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="file_paths" minOccurs="0" maxOccurs="1" type="tns:FilePathList"/>
<xs:element name="licenses" minOccurs="0" maxOccurs="1" type="tns:LicenseList"/>
-----> <xs:element name="vulnerabilities" minOccurs="0" maxOccurs="1" type="tns:VulnerabilityList" />
<xs:element name="violated_policy_rules" minOccurs="0" maxOccurs="1" type="tns:ViolatedRuleList" />
</xs:sequence>
<xs:attribute name="component_id" type="xs:string" use="required"/>
<xs:attribute name="file_name" type="xs:string" use="required"/>
<xs:attribute name="sha1" type="xs:string" use="required"/>
-----> <xs:attribute name="vulnerabilities" type="xs:integer" use="required"/>
<xs:attribute name="max_cvss_score" type="xs:string" use="required"/>
<xs:attribute name="library" type="xs:string" use="required"/>
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="vendor" type="xs:string" use="required"/>
<xs:attribute name="description" type="xs:string" use="required"/>
<xs:attribute name="blacklisted" type="xs:string"/>
<xs:attribute name="new" type="xs:string"/>
<xs:attribute name="added_date" type="xs:string"/>
<xs:attribute name="component_affects_policy_compliance" type="xs:string"/>
</xs:complexType>
here's the xsd they publish: https://analysiscenter.veracode.com/resource/detailedreport.xsd
from what I can figure out, I need to create a detailedreport.xjb file and (as the output states) setup a <jaxb:property> to convert the vulnerabilities integer attribute into something like vulnerabilityCount.
I created detailedreport.xjb:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<!-- Used to avoid the duplicate element/attribute name conflict -->
<jaxb:bindings node="//xsd:attribute[#name='vulnerabilities']">
<jaxb:property name="vulnerabilityCount"/>
</jaxb:bindings>
</jaxb:bindings>
but my xpath is wrong:
[ERROR] XPath evaluation of "//xsd:attribute[#name='vulnerabilities']" results in empty target node
line 10 of file: detailedreport.xjb
am I even on the right path
any xpath help is appreciated
One more bindins element referencing schema location should be provided:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb"
>
<jxb:bindings schemaLocation="PATH_TO_THE_SCHEMA" node="/xs:schema">
<jxb:bindings node="//xs:attribute[#name='vulnerabilities']">
<jxb:property name="vulnerabilityCount"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Path can be relative to xjb file, e.g.: schemaLocation="../detailedreport.xsd"

Cannot produce top level JAXBs from wsdl file using xjc compiler and binding file

I am trying to generate top level JAXBs (Using binding file) from a local wsdl file.
The binding file (binding.xjb) contents are given here:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
The command I am using is the following:
xjc -wsdl getShops.wsdl -b binding.xjb
and the wsdl file is given here
It successfully creates jaxb classes but they are nested classes in a single file.
Is this an unsupported feature of -wsdl flag of xjc compiler or am I mising something?
Extract the XSD schema from wsdl.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://new.webservice.namespace" elementFormDefault="qualified">
<xs:element name="getShopsRequest">
<xs:complexType>
<xs:attribute name="ui" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="shoplist">
<xs:complexType>
<xs:sequence>
<xs:element name="shop" maxOccurs="unbounded" >
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string" />
<xs:element name="companyname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run the command:
xjc -wsdl getShops.xsd -b binding.xjb
output Console:
parsing a schema...
compiling a schema...
namespace/webservice/_new/GetShopsRequest.java
namespace/webservice/_new/ObjectFactory.java
namespace/webservice/_new/Shop.java
namespace/webservice/_new/Shoplist.java
namespace/webservice/_new/package-info.java

xpath to rename single class in crazy hierarchy

I'm trying to generate classes from wsdl using jaxws-maven-plugin, but I get uncompilable results. The problem is this part of one of the xsds, where you can see that there are nested elements with the same name:
<xs:complexType name="TrafficCountsReplyData" abstract="true">
<xs:sequence>
<xs:element name="effectiveTrafficWindow" type="common:DateTimeMinutePeriod" minOccurs="1" maxOccurs="1"/>
<xs:element name="flows" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="flow:Flow" minOccurs="0" maxOccurs="100"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="counts" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="1440">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="common:DateTimeMinutePeriod" minOccurs="1"
maxOccurs="1"/>
<xs:element name="value" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="item" minOccurs="1" maxOccurs="3">
<xs:complexType>
<xs:sequence>
<xs:element name="key" type="flight:TrafficType"
minOccurs="1" maxOccurs="1"/>
<xs:element name="value" type="flow:Counts" minOccurs="1"
maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
As you can see, there there are multiple elements named item. This result in multiple inner classes with the same name inside of the single class TrafficCountsReplyData
So I'm trying to put together a binding file which would rename one of them. When I try to rename the outer item in the counts element using this:
<jaxb:bindings node="//xs:complexType[#name='TrafficCountsReplyData']//xs:element[#name='counts']//xs:element[#name='item']">
<jaxb:class name="Lulz"/>
</jaxb:bindings>
I get the following error:
[ERROR] XPath evaluation of "//xs:complexType[#name='TrafficCountsReplyData']//xs:element[#name='counts']//xs:element[#name='item']" results in too many (2) target nodes
When I create the binding xpath expression for the inner one like this:
<jaxb:bindings node="//xs:complexType[#name='TrafficCountsReplyData']//xs:element[#name='counts']//xs:element[#name='item']//xs:element[#name='value']//xs:element[#name='item']">
Then I get following error:
java.lang.IllegalArgumentException: Illegal class inheritance loop. Outer class Lulz may not subclass from inner class: Lulz
I can't figure out how to make this work. Where does the inheritance come from? There's another item in the flows element, but that shouldn't be matched by the Xpath. Without the binding file, I can generate the sources. I have correct schema in the binding file, because I can for example rename the class generated by the parent element.
mmm
I had a similar issue, but with properties..
I don't ahve the full xsd to check, but provided you want to change the OUTER item tag into something else, try this one:
<?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="../yourlocation/yourschema.xsd" version="1.0">
<!-- Customise the package name -->
<jxb:schemaBindings>
<jxb:package name="whatyouwant.something"/>
</jxb:schemaBindings>
<!-- rename the value element -->
<jxb:bindings node="//xs:complexType[#name='TrafficCountsReplyData']//xs:sequence//xs:element[#name='counts']//xs:complexType//xs:sequence//xs:element[#name='item']">
<jaxb:class name="Lulz"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
If this seems not to work if you can disclose the xml I should be able to help you furthe (on vacation until monday in case) , but consider that you were giving the wrong "path", jumping away a few "subelement" in your xpath expression.
Let me know.
I've found the solution here: JAXB Customizations With a Poorly Formed WSDL
I had to add /xs:complexType at the end of the Xpath. I guess it's because the root of the class is in fact the complexType and not enclosing xs:element. So it was like this:
<jaxb:bindings node="//xs:complexType[#name='TrafficCountsReplyData']//xs:element[#name='counts']/xs:complexType/xs:sequence/xs:element[#name='item']/xs:complexType">
<jaxb:class name="Lulz"/>
</jaxb:bindings>
Now I can generate classes and compile them.

Shared class for child element in JAXB in different xmls/roots

In JAXB when using automatic class generation via xjc from xsd scheme.
alpha.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="alpha">
<xs:complexType>
<xs:sequence>
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
beta.xml
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
<xs:complexType>
<xs:sequence>
<xs:element name="class">
<xs:complexType>
<xs:sequence>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
As you can see there is Person element which is shared among these two schemes. What I would like to do is:
generate classes using xjc in a way that ObjectFactory class is shared for both schema classes (the output classes will be in one package)
not use nested static classes (with attribute localScoping="toplevel")
use Person class to bind with /alpha/persons/person as with /country/class/person so there are not two Person classes created
The purpose of this is unmarshalling one xml, applying business logic and creating another one as output where some elements (like Person) are same and shared for both xml files. The namespace will be the same for both files.
I would welcome if you can present me with complete .xjb bindings settings file. So far mine contains only:
<jxb:bindings version="1.0"
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:globalBindings localScoping="toplevel"/>
</jxb:bindings>
And of course I get name collision error as I do not know how to set binding compiler to see Person as the same entity/element.
You can use an external binding file to indicate that during class generation we wish to use our existing class for the complex type called Document.
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="alpha.Person"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC Call
xjc -b binding.xml beta.xsd
If namespace of person from A will be equals namespace person from B, that xjc has to generate the correct classes.

How to handle elements & attributes having same name in xsd while generating pojos using jaxb?

I have a xsd that contains something like:
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element ref="HereIsTheProblem"/>
<xs:element ref="blaBla"/>
</xs:sequence>
<xs:attribute name="something" type="xs:string" use="required">
<xs:annotation/>
</xs:attribute>
<xs:attribute name="somethingElse" type="xs:string">
<xs:annotation/>
</xs:attribute>
<xs:attribute name="HereIsTheProblem" type="xs:string">
<xs:annotation/>
</xs:attribute>
</xs:complexType>
now when i try to parse the schema using jaxb to generate java classes it fails:
[ERROR] Element "{http://something.somemorething.com/etc/}HereIsTheProblem" shows up in more than one properties.
how to resolve this without making any modification in the schema?
PS:my jaxb version is 2.1.13
You need to use a binding file indicating jaxB how it should handle this name collision. For example, put something like this in a file named something like bindings.xjb:
<jaxb:bindings version="2.1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:com.fnf="http://www.fnf.com/xes">
<jaxb:bindings schemaLocation="your schema location here" node="/xs:schema">
<jaxb:bindings node="//XPath selector">
<jaxb:property name="HereIsTheProblem2" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Can't provide you a complete solution without a complete schema
Also we can modify the XSD as below.
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element ref="HereIsTheProblem"/>
<xs:element ref="blaBla"/>
</xs:sequence>
<xs:attribute name="something" type="xs:string" use="required">
<xs:annotation/>
</xs:attribute>
<xs:attribute name="somethingElse" type="xs:string">
<xs:annotation/>
</xs:attribute>
<xs:attribute name="HereIsTheProblem" type="xs:string">
<xs:annotation>
<xs:appinfo>
<jaxb:property name="HereIsTheProblemAttr"/>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:complexType>
The main issue is inside the pojo class, if we have the element and the attribute with the same name, it will try to create variables with the same name which is not allowed in simple java principles. So we can define the variable name to be different from one another like above.

Categories