users@jaxb.java.net

Re: Supplying a custom CodeWriter

From: Peter Hendry <peter.hendry_at_capeclear.com>
Date: Wed, 05 Dec 2007 00:49:16 +1300


Aleksei Valikov wrote:
Hi.

  
I find the need to control where/how things are written by the
CodeWriter so I'd like to be able to supply my own class that implements
the CodeWriter interface. Looking at the Driver class (and OptionsEx) I
can't see how I could hook in a custom CodeWriter. OptionsEx is created
by Driver with no way to override it, and Driver calls
OptionsEx.createCodeWriter() which has a hard-coded codewriter
implementation it creates.

This means I can't supply a custom OptionsEx to override
createCodeWriter() and I can't set some property/argument to have
OptionsEx use a different class.

Any pointers? It seems like a deliberate (good) decision to pull out the
functionality for controlling where/how an OutputStream/Writer is
created for each class but seems odd that this functionality cannot be
customized with a custom implementation. Is this just a configuration
oversight?

Is there another class other than Driver that I should be looking at for
doing Schema to Java?
    


First of all, two questions. What JAXB version (1 or 2) and which
build tool/plugin (ant, maven-jaxb1-plugin, maven-jaxb2-plugin) do you
use?

  
Sorry, version 2.0.5. I am building with from within a custom application which is creating a Driver instance and then wanting to customize it with a custom CodeWriter.

In maven-jaxb2-plugin we don't use the driver. We create the code
model and then "build" it using an own code writer:

			if (getWriteCode()) {
				if (getVerbose())
					getLog().info("Writing output to: " + xjcOpts.targetDir);

				model.codeModel.build(new JaxbCodeWriter4Mvn(xjcOpts
						.createCodeWriter()));
			} else {
				if (getVerbose())
					getLog().info("Code will not be written.");
			}


  

I believe I have a workaround although it's not that pretty. By passing in an XJCListener I can write out the code in the "compiled(Outline)" method and by setting the mode to DRYRUN the Driver does not output any code.

Thanks for your quick response.

Pete