Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add JAXB customization to a particular element of an xsd?

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We have a bunch of XSD's where they are bound by JAXB and we are using a global binding as



This means, default bindings are applied to all xsds.

However, I need to apply a particular customization to a particular element of one of those xsd, which is of type xs:dateTime. This customization is applicable only to that particular element in that particular xsd. But all the current global bindings are still to be applied. The new customization is an additional one.

I could not find any examples in doing element specific customization in an external customization file. Could some one, provide me a pointer of such examples.

Thanks
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any thoughts on this.

I can provide more information, if needed.
 
g tsuji
Ranch Hand
Posts: 666
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where you can find an example of customization of xs:date (or xs:dateTime with slight change for that matter). It is a global customization.
http://jaxb.java.net/guide/Using_different_datatypes.html

You can change it to customize a specific schema component. Something which looks like this as an external xjb customization file (with the xpath node attribute pointing to the specific targetted node).

amendment note
I had tried to be figurative and informative in the node attribute expression, but hastily put name() instead of @name in the predicates.
It sure is thereby lack of realism.
As it is not an issue in the followup posts, I change it back directly in the code section.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you TSuji,

Can I actually include the Global customization and specific schema customization in one single .xjb file? or am I required to keep them separated.

Thanks
 
g tsuji
Ranch Hand
Posts: 666
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Can I actually include the Global customization and specific schema customization in one single .xjb file?
You sure can and you should.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I'm trying to set the timezone of the data that is being parsed to GMT and I'm doing like below.



The reason behind this customization is we are getting this date from one of the application and they send us the data is UTC, but no explicit indication of the timezone and when we forward that to anothre application, they are treating it as local... so we did some hacking such that we put "z" in the last. But now we wanted to change this by doing the customization.

So to accomplish this, I see two approaches, but both of them are stuck with other issues.

1) as mentioned above, creating XMLGregorian object and setting TimeZone.. but I don't know what would be the offset, such that it gets me "Z" when converted to GregorianCalendar.
2) Creating XMLGregorianCalendar from GregorianCalendar, whose Timezone is set to "GMT".. but I do not want to use SimpleDateFormat to build date object from String, due to Thread safety issues and FastDateFormat does not have an option to parse string to date...

So what is my best approach here.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I was able to straighten this out, by checking if TimeZone is not set on XMLGregorianCalendar object, by checking for "DatatypeConstants.FIELD_UNDEFINED" and setting the timezone to "0".


Everything works great, if I'm dealing with just one schema and with only customization..

But applying this concept to my real problem at hand, I'm facing trouble

when the required objects are generated, it is also creating org.w3._2001.xmlschema.Adapter1.java

I have two issues with this class and looking for some customization.

1) I should be able to specify a package for this class, instead of org.w3.2001.xmlschema.

2)



Apparently CustomDateTimeBinder is neither XMLGregorianCalendar nor String. So I need to change these methods, such that appropriate methods of CustomDateTimeBinder class are called when Marshalling and Unmarshalling is being done.


All this is fine, if I'm working on some coding and testing locally, but as usual build process is automated and which means, changing the methods of unmarshall and marshall is not that easy. So, how do we handle this situation.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic