2015-10-26 71 views
3

我使用XJC在運行時從xsd生成JAXB類。但默認情況下,xsd中的maxLength限制不會被註釋。如何在運行時以編程方式添加xjc插件?

我發現了一個插件來處理這個問題,krasa-jaxb-tools。我已經添加了依賴到我的POM,但我似乎無法將該插件添加到XJC過程。

我使用的是jaxb-xjc工具的2.2.11版本。這裏是我的依賴關係:

<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> 

我試圖實例化插件,配置它,並把它傳遞給generateCode(Plugins[] extensions, ErrorListener errorListener)方法對我S2JJAXBModel模型,但它似乎沒有任何效果。這裏是我的代碼:

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); 

我在做什麼錯?

回答

6

generateCode(Plugins[] extensions, ErrorListener errorListener)方法的實現不會影響插件,如版本2.2.11中使用的實現類中的here所示。

要添加插件,您需要訪問在SchemaCompiler中找到的Options並在其中添加插件。還要注意,parseArgument()呼叫應該在選項上進行,而不是插件。

這裏是你如何實現這一目標:

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); 
相關問題