Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB 2.0: Generating attributeGroup as a seperate class

 
J Rosetto
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"anon ny",
Please check your private messages regarding an important administrative matter.
-Ben
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
 
Tim Burrell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic