I am trying to generate java classes from sample xsd using jdk 1.6, xjc command.
But I don't want to use annotations i.e I don't want to generated java classes to contain annotations. How can I do it?
In JAXB (JSR-222) the only standard representation of the XML-binding metadata defined in the standard is JAXB annotations. Therefore an option to generate the classes without these annotations is of limited use.
I'm the EclipseLink JAXB (MOXy) lead an we do offer an XML (and JSON) representation of the JAXB metadata as an extension:
XML - http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
JSON - http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
If you are interested in having the ability to generate this metadata instead of annotations please request an enhancement in our bug database against the MOXy component:
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink
This question has two parts:
In the compile time you have to generate your code differently. You can write an XJC plugin to do this.
In the runtime you have to use the JAXB implementation which works without annotations (see Blaise's answer on this part).
See this question on XJC extensibility:
XJC - is it extensible?
You can write an XJC plugin which completely replaces the code generation. So I can imagine an XJC plugin which generates XML mappings for MOXy instead of annotations in classes. You can also do this with JAXB RI using an extra annotation reader, but it is much trickier as simply using MOXy.
Be warned, however, that writing advanced XJC plugins my be quite complex.
Related
I'm trying to enhance a module from using JaxB to JiBX. I was able to produce the java classes through the jiBX maven plugin in pom.xml although I'm having issues on how I can generate an Objectfactory just like the one in JaxB's. Is there like a counterpart in JiBX because I've been searching through the net for hours but I can't find one. I'm new to marshalling and unmarshalling mechanisms so any help will be much appreciated. Thanks!
Rocky,
JiBX has a similar mechanism. You need to create a JiBX Binding Factory then create a Marshalling or Marshalling context. The best way to see this is to look at one of our examples. Here's a link to a simple program that creates a java object, marshalls it to XML, then unmarshals the XML back to java objects.
https://github.com/jibx/maven-plugin/blob/master/test-suite/base-binding-test/org.jibx.schema.test.person/src/main/java/org/jibx/schema/test/person/PersonTest.java
Enjoy!
Don
I was having difficulty with XJCTask so am using XJC2Task to generate Jaxb classes from xsd. What is the difference? Will the generated classes be different?
XJCTask delegates to JAXB1 or JAXB2 depending on what you have on your classpath.
If you are already using JAXB2 then you wont see any difference as it ends up calling the same thing.
You are unlikely to be using JAXB1 so you can just use XJC2Task.
See a related answer with more details https://stackoverflow.com/a/8863287/1000011
Is there any way, xsd tags for instance, to unmarshall xsd files to get private fields with accessor methods instead of protected fields which is the default behaviour? I have tried to search in the documentation but no avail.
I am aware of the plugin plugin solution here on stackoverflow but I would like to have a xsd-tag or a built-in solution on existing technologies, not extra things to handle like writing a plugins that requires extra maintenance cost =)
First of all, excuse me if I say something obvious because I did not research the answer to this question, it's just something that came to mind and couldn't find an answer to with a quick google search (I also don't know Groovy well).
I'm reading Groovy in action (1st ed) and in section 7.6 it states that "If you want a usual Java class to be recognized as a Groovy class, you only have to implement the GroovyObject interface. For convenience, you can also subclass the abstract class GroovyObjectSupport, which provides default implementations."
So I'm trying out some Java/Groovy (trying to find ways to make coding in Java faster/easier), and I'm instantiating this object Person, which is a Java object and the Person class does not have anything to do with Groovy (i.e. doesn't implement GroovyObject or anything). Person has a protected field, name, with getters/setters. I am able to access this like so in a groovy test case (extends GroovyTestCase), which btw is in a different package and should not be accessible like this:
Person person = new Person('joe')
println "Name: ${person.name}"
Isn't that using the meta info which a Groovy object would have i.e. Groovy would intercept, get the "name" part of "person.name" and call the getName() method or something like that? But person is a Java object... i.e. it shouldn't have that logic available.
I have not implemented GroovyObject or extended GroovyObjectSupport, yet it seems like this Java object has Groovy features I can use.
My question is, what can I assume from Java objects used in Groovy? There is obviously something being done behind the scenes to augment Java defined classes with some Groovy features... but, which features?
So, I guess my question boils down to: "What is done to Java classes when used within a Groovy context?"
Any info regarding the subject would be appreciated, thanks :)
Groovy indeed enhances your Java classes. The problem is when you want to use these enhanced Java classes from Java. Then you need to go for the GroovyObject and GroovyObjectSupport strategy.
About what you can expect, you can check the groovy docs:
the groovy-jdk shows the Groovy enhancements to the JDK
the groovy-api shows the stuff specific from the Groovy library
Also the eclipse plugin works great in autocompleting your Java classes from Groovy.
Java objects are enhanced through Groovy's Meta Object Protocol (MOP), which intercepts and makes its own dispatch to methods and attributes. That is why you can intercept stuff and that's how new methods are added to Java's final classes.
The Groovy's MOP uses cached strategy and reflector classes to achieve a good performance.
You can also read Groovy objects via ObjectInputStream by overriding the resolveClass method and changing the class loader.
See http://www.feiteira.org/2013/04/reading-groovy-objects-from-java.html
In JaxB, we need to move from one schema form to another and we would love to be able to deprecated stuff in the xsd with some simple note in the xsd:documentation element or something. Is there a way to get JAXB to mark these generated classes and methods with #Deprecated so developers can easily see which cold still needs to change?
thanks,
Dean
I would recommend the Annotate plugin: Annotate plugin(edit: the original link is no longer valid).
You'll see a couple of examples, including deprecation, warning suppression, etc.
It is usually recommended to do this stuff using special markup under appinfo as opposed to documentation.
I finally got all of mine working quite nicely...thanks!!!! The complete code is found here
JAXB External Custom Binding XJC Issue - Parsing results in empty node