JAXB Documentation Annotation - java

I have the following java class with the JAXB #XMLRootElement annotation
#XmlRootElement(name="ClientData")
public class ClientData {
/**
* The first address field of the person
*/
private String address1 = null;
}
which produces this xml fragment when i generate the xsd schema
<xs:complexType name="clientData">
<xs:sequence>
<xs:element minOccurs="0" name="address1" type="xs:string"/>
Is it possible to use a JAXB annotation so that the documentation details on the address1 field will be included as a xs:annotation/xs:documentention element in my final schema?
<xs:complexType name="clientData">
<xs:sequence>
<xs:element minOccurs="0" name="address1" type="xs:string">
<xs:annotation>
<xs:documentation>The first address field of the person</xs:documentation>
</xs:annotation>
</xs:element>

Simple answer: no it's not possible with builtin JAXB.

I don't know if it's possible, since I've never used it. But as far as I can tell the API doesn't support the documentation element. However, you could use the #XMLElement annotation to give your member a more descriptive name.
//Example: Code fragment
public class USPrice {
#XmlElement(name="itemprice")
public java.math.BigDecimal price;
}
<!-- Example: Local XML Schema element -->
<xs:complexType name="USPrice"/>
<xs:sequence>
<xs:element name="itemprice" type="xs:decimal" minOccurs="0"/>
</sequence>
</xs:complexType>

Related

How to annotate a JAXB element with type IDREFS?

I use JAXB to convert an XML to a Java class. I have two Java classes: Milestone and Issue. Both have a unique ID and the ID fields are annotated with "#XMLID".
One milestone consists of several issues, so that it has a field variable "issues" holding a List. Issues and Milestones are both contained in parent class, so that Issues are no children of Milestones, but rather siblings. Using schemagen, I generated an XMLSchema to validate the XML file. The schema works well on everything except the "issues"-field of Milestone. Here, I had to manually change this line:
<xs:element name="issue" type="xs:IDREF" minOccurs="0" maxOccurs="unbounded"/>
to be of type IDREFS instead:
<xs:element name="issue" type="xs:IDREFS" minOccurs="0" maxOccurs="unbounded"/>
Now, it works perfectly. My question is: How do I have to annotate my field "issues" in the Milestone class so that I don't have to change the XMLSchema manually afterwards? I tried #XMLIDREFS but this does not seem to be valid.
Here are relevant parts of my code:
XMLSchema of Issue and Milestone
<xs:complexType name="issue">
<xs:element name="id" type="xs:ID" minOccurs="0"/>
</xs:complexType>
<xs:complexType name="milestone">
<xs:sequence>
<xs:element name="id" type="xs:ID" minOccurs="0"/>
<xs:element name="issue" type="xs:IDREFS" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
Java class Milestone with current annotation
public class Milestone {
private String id;
private List<Issue> issues = new LinkedList<>();
#XmlElement(name = "issue")
public List<Issue> getIssues() {
return issues;
}
Example XML
<RootElement>
<issue>
<id>IssueID1</id>
</issue>
<issue>
<id>IssueID2</id>
</issue>
<milestone>
<id>MilestoneID1</id>
<issue>IssueID1</issue>
<issue>IssueID2</issue>
</milestone>
</parent>

JiBX generates binding that ignores attribute namespace

I have the following schema (fragment):
<xs:complexType name="partnerPaymentsItemType">
<xs:sequence>
<xs:element name="changeTime" type="dateTime"/>
<xs:element name="statusId" type="shortId"/>
<xs:element name="paymentPointId" type="shortString"/>
<xs:element name="money" type="currency"/>
<xs:element name="paymentDestination" type="shortString"/>
<xs:element name="paymentDestinationType" type="shortId"/>
<xs:element name="subagentId" type="shortId" minOccurs="0"/>
<xs:element name="discountCardNumber" type="xs:string" minOccurs="0"/>
<xs:element name="amountAll" type="currency" minOccurs="0"/>
<xs:element name="rewardPercent" type="percentAmount" minOccurs="0"/>
<xs:element name="rewardPercentValue" type="percentAmount" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="paymentTime" use="required" type="dateTime"/>
<xs:attribute name="externalId" use="required" type="id"/>
<xs:attribute name="registeredId" use="required" type="id"/>
</xs:complexType>
I use JibX Codegen tool to generate sources from it, and then to compile binding that should allow me to unmarshal XML to Java objects. Here is my codegen settings:
<schema-set xmlns:xs="http://www.w3.org/2001/XMLSchema"
delete-annotations="true"
prefer-inline="false"
generate-all="true"
show-schema="true"
type-substitutions="xs:date xs:string"
package="here.lays.my.package">
<class-decorator class="org.jibx.schema.codegen.extend.SerializableDecorator"/>
</schema-set>
Later, I try to parse an XML document which has namespace prefixes for both tags and attributes, which results an exception saying
Missing required attribute "paymentTime"
Debugging through JiBX sources shown that it tries to look for attribute with no namespace and name "paymentTime" in a document, while a document has an attribute with a namespace mapped to URL, and not able to find it of course.
I have decompiled binding with JAD and it searches for attribute with null namespace:
public static PartnerPaymentsItemType JiBX_beeline_binding_unmarshalAttr_1_93(PartnerPaymentsItemType arg1, UnmarshallingContext arg2)
throws JiBXException
{
arg2.pushTrackedObject(arg1);
arg1;
arg1.setPaymentTime(arg2.attributeText(null, "paymentTime"));
arg1.setExternalId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "externalId"))));
arg1.setRegisteredId(Utility.parseLong(WhitespaceConversions.trim(arg2.attributeText(null, "registeredId"))));
arg2.popObject();
return arg1;
}
I would be grateful for any advise that could help resolving the issue - like, why JiBX generated such mapping, how to make it respect attribute namespace, and so on.
JiBX behavior was actually correct, namespace prefixes for attributes were not intended based on xsd.
Actual solution was to replace JiBX with XJC/JAXB and separate schema for the entity where I needed namespace prefixes for attributes.

specify type for multiple IDREF attributes in JAXB bindings

Apologies if this is a duplicate question, but I could not find anything for my situation. So here goes:
I have the following in an xsd file:
<xs:complexType name="Allocation">
<xs:annotation>
<xs:documentation>Links its owner to an xs:id.</xs:documentation>
</xs:annotation>
<xs:attribute name="idRef" type="xs:IDREF"/>
</xs:complexType>
<xs:complexType name="XYZ">
<xs:sequence>
<xs:element name="SomeAllocation" type="Allocation"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ABC">
<xs:sequence>
<xs:element name="SomeAllocation" type="Allocation"/>
</xs:sequence>
</xs:complexType>
I am try to generate fields/getters and setters in java that are of specific types for example XYZ.getSomeAllocation() should return type Object1 and ABC.getSomeAllocation() should return type Object2. The problem I am facing is that xjc is generating one Allocation class and the XYZ and ABC classes with the methods mentioned below returning java.lang.Object types.
Obviously creating to different Allocation types which are then used in the different objects solves the problem but I would like to reuse the object holding the xs:IDREF.
Your help is highly appreciated!
Thanks!

Set XmlType during xjc class generation

I'm using xjc via maven to generate sources. I'm using an XSD and a bindings file. I would like my generated classes to have the annotation #XmlType(name = ""). I can't see how to set the name to be blank.
I've tried (amongst other ideas) annotating using annox:annotate("http://annox.dev.java.net") with annox:class="javax.xml.bind.annotation.XmlType" but this adds another #XmlType annotation rather than replacing/overwriting the existing one.
Is there a way to set the #XmlType's name to be blank?
The name is left blank if the Type is an anonymous type. Check here (Section "Mapping a Class").
To do that, you need to declare your type inside an <element> tag. The following schema shows an example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Container">
<xs:complexType>
<xs:sequence>
<xs:element ref="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Item">
<xs:complexType >
</xs:complexType>
</xs:element>
</xs:schema>
Here, element Item is of an anonymous type, and here's the generated class:
#XmlType(name = "")
#XmlRootElement(name = "Item")
public class Item {
}

Insert Logic code into generated JAXB java files by XSD def

Question is , for some reason. the xsd doesn't/can't define all logic vars other than basic properties and setters and getters, so we've tried to 'inject code' by xsd definition which are actually discussed by other folks couple of times. I have no problem with 'simple injection' with 'simple java method' which won't need any 'import' statement on top of class def.
yet somehow if we want to use it. it looks to me there is no way we could take or import any packages other than setter or getters. , see below for details
xsd definition test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://company.com/schema/response"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://company.com/schema/response"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
jaxb:extensionBindingPrefixes="ci">
<xs:element name="client">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<ci:code>
<![CDATA[
private String str;
public String returnStr() {
Locations locationCls =this.getLocations();
List<String> locationids = new ArrayList<String>();
// get a list of locationid into locationids (list)
List<Location> locationList = locationCls.getLocation();
for (Location loc : locationList) {
locationids.add(String.valueOf(loc.getId()));
}
// return string like loc1,loc2,loc3
return StringUtils.join(locationids, ',');
}
]]>
</ci:code>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="name" type="xs:NCName" />
<xs:element name="pass" type="xs:NCName" />
<xs:element ref="test:locations" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="locations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="test:location" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="location">
<xs:complexType>
<xs:attribute name="id" use="required" type="xs:string" />
<xs:attribute name="address" use="required" type="xs:string" />
<xs:attribute name="biz" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
run jaxb ri command :
xjc.bat test.xsd -Xinject-code -extension
observe below code snippet in the Client.java successfully
private String str;
public String returnStr() {
Locations locationCls =this.getLocations();
List<String> locationids = new ArrayList<String>();
// get a list of locationid into locationids (list)
List<Location> locationList = locationCls.getLocation();
for (Location loc : locationList) {
locationids.add(String.valueOf(loc.getId()));
}
// return string like loc1,loc2,loc3
return StringUtils.join(locationids, ',');
}
As a consequence, we know jdk complains the compilation error as StringUtils in Apache commons ( or other 3rd part util tools like google collections to help in other scenarios) are not imported in generated file. understand there are some google projects which use jaxb plugin to insert or invoke method into generated java files. just want to spend day or so to see if we could make it by xsd itself only without any plugin. any idea would be appreciated .
You could specify the fully classified class name within the code that you want to inject, e.g.:
return org.apache.commons.lang.StringUtils.join(locationids, ',');

Categories

Resources