当前位置: 首页 > 面试题库 >

如何在运行时以编程方式添加xjc插件?

东郭和光
2023-03-14
问题内容

我正在使用XJC在运行时从xsd生成JAXB类。但是默认情况下,maxLengthxsd中的限制没有注释。

我找到了一个用于处理此问题的插件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);

我究竟做错了什么?


问题答案:

这是该#generateCode()方法的实际实现在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();
}

如您所见,参数只是被吞没了。

要添加插件,您要访问中Options找到的插件,然后在其中SchemaCompiler添加插件。另请注意,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);


 类似资料:
  • 我试图实例化插件,配置它并将其传递给我的模型上的方法,但似乎没有任何效果。下面是我的代码: 我做错了什么?

  • 问题内容: 是否可以使用XML配置中的规范以编程方式添加Log4J2附加程序? 我计划在log4j2.xml中定义所有内容,然后按情况选择追加器(不会编译): 问题答案: 编辑:有关log4j2的最新版本,请参阅下方答案 我给他们留下了不希望您这样做的印象,但这对我有用:

  • 我有一个RecyclerView,里面有这样的项目: 我使用用于侦听滑动,OnChildRaw()用于在滑动项目时绘制画布: 再刷一点: 我想在项目列表中的第一个项目上模拟(在运行时)滑动;我需要第一个项目去(或多或少)-100像素的X轴,然后回到原来的位置。如何做到这一点?

  • 我正在使用SpringDoc,并试图以编程方式向OpenApi添加一个模式,但没有成功。 mySchema的描述没有添加到我在生成的YAML文件中看到的模式列表中,如果我试图引用它:

  • 我正在尝试在Android上添加Wifi网络,我想知道如何连接到不广播其SSID的Wifi网络(它是否有空SSID或带有\0s的清晰SSID)。 这是我目前用于广播其SSID的Wifi网络的内容:

  • Spring非常出色地为注释提供了一个属性。我希望以编程方式提供带有时区的方法。 例如,假设我想在当地时间午夜打开洛杉矶和芝加哥大楼的灯。我想从我的存储库中获取建筑物。 时区可以在运行时提供还是以其他方式注入? 比如: 我知道方法不能接受任何参数,所以以这种方式提供时区是不行的。