• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAXB 2.0: Generating attributeGroup as a seperate class

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it is possible to generate classes from complexTypes and simpleTypes using JAXB 2.0. However, I was wondering if it is also possible to generate classes from attributeGroups?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"anon ny",
Please check your private messages regarding an important administrative matter.
-Ben
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anon,

welcome to the JavaRanch ;)

I'm currently using JAXB 2.1 and it works really great for the beginning. Unfortunately I've not yet used attributeGroups but from what I've read it seems that JAXB 2.x supports 100% of XML Schema, so I think it will support attributeGroups as well. It works for all my complexTypes, simpleTypes and elements so I'm pretty sure attributeGroups are not a problem.

Marco
[ June 03, 2008: Message edited by: Marco Ehrentreich ]
 
J Rosetto
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

Unfortunately I've been able to find no information regarding this anywhere on the web. I'm beginning to think its not supported in JAXB 2.0.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your special problem with attribute groups? Think about it. Element and attribute groups are more or less just building blocks which you want to reuse within your schema declaration. They don't have a semantic meaning if you don't reuse them in another place.

On the other hand the classes the JAXB compiler generates from a schema are more or less only complex/simple types and elements (along with their attributes). So your attribute groups will show up in your Java classes as soon as you use them to construct elements in your schema.
In the end the attribute groups are nothing more than a placeholder for the attributes which belong to that group. So it's the same as if you would declare the attributes "by hand" in every place in the schema you use an attribute group at the moment. For this reason I'm really quite sure that the attribute groups are treated like any other attributes by the JAXB compiler.

What exactly do you expect JAXB to do with the attribute groups?


Marco
 
J Rosetto
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take the following example:

I have an attribute group attached to a number of different elements. This attribute group represents a set of attributes representing details e.g. title, age, gender.

I then want to assign this attribute group to some various elements. e.g. author, illustrator etc.

The generated JAXB Author and Illustrator classes will have duplicated setters and getters for each of the attributes in the attribute group. (e.g. getAge(), setAge()). By generating an attribute group Details class would remove this code duplication.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, now I understand your problem

This is easy to solve. What you describe is obviously similar to the concept of inheritance in OOP. I don't know if it's possible to achieve what you want directly with attribute groups.

But I know a clean solution that is working for me without problems. To have JAXB create a common base class to avoid code duplication you have to create a common type for this in your XML schema (usually a complex type). So create a complex type with all common properties which may also include your attribute group(s). Then create new complex types for Author and Illustrator which extend the base type you have declared before.

This will result in having a Java class with the common properties of Author and Illustrator and a separate child class with only specific properties which extends the common base class.

I hope it's more or less understandable. Of course you should be familiar with the concept of inheritance/extension in XML schema, though it's not very complicated for "normal" scenarios.

Marco
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Marco.

I hate to be contrary, but your solution doesn't work in all cases. Consider the case where Author and/or Illustrator already inherit from some other complexType. Having a means to create a class for an attributeGroup would be very handy. I'm rather shocked it doesn't exist already (of course, if it does exist, please point me in the right direction and I'll gladly admit to PEBKAC).

tb
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic