Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

java - How do I add an xjc plugin programmatically at runtime?

I'm generating JAXB classes from an xsd at runtime using XJC. But by default the maxLength restriction in the xsd isn't annotated.

I found a plugin to deal with this, krasa-jaxb-tools. I've added the dependencies to my POM, but I can't seem to add the plugin to the XJC process.

I'm using version 2.2.11 of the jaxb-xjc tools. Here are my dependencies:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-xjc</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-jxc</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.github.krasa</groupId>
    <artifactId>krasa-jaxb-tools</artifactId>
    <version>1.3</version>
</dependency>

I'm trying to instantiate the plugin, configuring it and passing it to the generateCode(Plugins[] extensions, ErrorListener errorListener) method on my S2JJAXBModel model, but it doesn't seem to have any effect. Here's my code:

SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
schemaCompiler.forcePackageName(packageRoot);

// JAXB Plugin used to get the proper @Size annotations on all fields.
JaxbValidationsPlugins jaxbValidationPlugin = new JaxbValidationsPlugins();

// Build up list of options for the plugin.
// First option must be the name of the plugin itself.
// Options must be prefixed with dashes
String[] args = new String[] { "-" + JaxbValidationsPlugins.PLUGIN_OPTION_NAME };

try {
    // Activate plugin
    jaxbValidationPlugin.parseArgument(new Options(), args, 0);
} catch (BadCommandLineException | IOException e1) {
    e1.printStackTrace();
}

InputSource inputSource = new InputSource(schemaFile.toURI().toString());

schemaCompiler.parseSchema(inputSource);
S2JJAXBModel model = schemaCompiler.bind();

// Passing the plugin to the method
JCodeModel jCodeModel = model.generateCode(new Plugin[] {jaxbValidationPlugin}, null);

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's how the actual implementation of the #generateCode() method looks in version 2.2.11

// com.sun.tools.xjc.api.impl.s2j.JAXBModelImpl.java

public JCodeModel generateCode(Plugin[] extensions,ErrorListener errorListener) {
    // we no longer do any code generation
    return outline.getCodeModel();
}

As you can see, the params are just swallowed.

To add the plugin, you want to access the Options found in the SchemaCompiler and add your plugin there. Note also that the parseArgument() call should be made on the options, not the plugin.

Here's how you accomplish that:

SchemaCompiler schemaCompiler = XJC.createSchemaCompiler();
schemaCompiler.forcePackageName(packageRoot);

// JAXB Plugin used to get the proper @Size annotations on all fields.
JaxbValidationsPlugins jaxbValidationPlugin = new JaxbValidationsPlugins();

// Build up list of options for the plugin.
// First option must be the name of the plugin itself.
// Options must be prefixed with dashes
String[] args = new String[] { "-" + JaxbValidationsPlugins.PLUGIN_OPTION_NAME };

// Get the options for the schema compiler, this is where we add plugins.
Options schemaCompilerOptions = ((SchemaCompilerImpl) schemaCompiler).getOptions();
schemaCompilerOptions.getAllPlugins().add(jaxbValidationPlugin);

// Call the parseArgument method on the options, not the plugin
// Passing in zero because we want to parse the first argument in the array
try {
    schemaCompilerOptions.parseArgument(args, 0);
} catch (BadCommandLineException e1) {
    e1.printStackTrace();
}

InputSource inputSource = new InputSource(schemaFile.toURI().toString());
schemaCompiler.parseSchema(inputSource);
S2JJAXBModel model = schemaCompiler.bind();

JCodeModel jCodeModel = model.generateCode(null, null);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...