Osw*_*Osw 6 schema class jaxb extend
需要以下情况的帮助:用户可以生成自己的数据结构,这些数据结构存储为JAXB-ready XSD源,如下所示:
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Group" type="Group"/>
<xs:element name="Parameter" type="Parameter"/>
<xs:complexType name="Group">
<xs:sequence>
<xs:element name="caption" type="xs:string" minOccurs="0"/>
<xs:element name="parameters" type="Parameter" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Parameter">
<xs:sequence>
<xs:element name="key" type="xs:string" minOccurs="0"/>
<xs:element name="group" type="Group" minOccurs="0"/>
<xs:element name="value" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
在出现新的或修改的模式之后,它将由Schema编译器自动解析,生成,编译和打包到用户jar的java源:
SchemaCompiler sc = XJC.createSchemaCompiler();
// Input source for schema
InputSource is = new InputSource(new StringInputStream(objectPackage.getObjectSchema()));
// Parse
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
// Generate source
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(packageSourceDirectory);
// Compile and package
// ......
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到确定所有用户生成的类必须扩展一个特定的已知类,比如说UserRootObject:
package user.abc;
public class Group extends com.mycompany.xml.UserRootObject {
//
}
Run Code Online (Sandbox Code Playgroud)
和
package user.abc;
public class Parameter extends com.mycompany.xml.UserRootObject {
//
}
Run Code Online (Sandbox Code Playgroud)
一切都在飞行中,我不能强迫用户修改他们的模式文件,但我可以在代码生成之前对其进行转换.看起来我有两个选择来介绍它UserRootObject:JCodeModel在构建Java源代码之前以某种方式通过或以某种方式转换模式文件.
XJC为此目的有一个扩展
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
jaxb:version="2.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<xjc:superClass name="com.mycompany.xml.UserRootObject"/>
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
.
.
.
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅
模式注释也可以通过外部绑定文件提供.有关示例,请参阅:
非常感谢D.Shawley指出JSR 222中的正确部分.这是最终解决方案,可能对其他人有帮助并节省时间.原始模式必须转换如下:
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0" >
<xs:element name="Group" type="Group"/>
<xs:element name="Parameter" type="Parameter"/>
<xs:complexType name="Group">
<xs:complexContent>
<xs:extension base="UserRootObject">
<xs:sequence>
<!-- params -->
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Parameter">
<xs:complexContent>
<xs:extension base="UserRootObject">
<xs:sequence>
<!-- params -->
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="UserRootObject">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="UserRootObject" implClass="com.mycompany.xml.UserRootObject"/>
</xs:appinfo>
</xs:annotation>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
可以通过org.w3c.dom.Document inteface轻松执行转换.
我不认为使用 JAXB 本身有一种简单的方法可以做到这一点。有许多不为人所知的可用定制选项 - 请阅读JSR222 第 7 节了解详细信息。
如果您对输入模式有一定的控制,那么您可能需要考虑使用 XSLT 来转换模式。我相信这可以通过使用javax.xml.transform.dom.DOMResult实例作为转换的目标并使用输出作为 DOM 树(例如,调用getNode()结果)作为parseSchema. 一个基本的转变是替换:
<xs:complexType name="foo">
<!-- CONTENTS -->
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
类似的东西:
<xs:complexType name="foo">
<xs:complexContent>
<xs:extension base="UserRootObject">
<!-- CONTENTS -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
当然这仅适用于简单的情况。如果您的模式文件中已经具有继承,那么您将必须在 XSLT 中进行一些过滤,仅将此转换应用于尚未扩展的类型。
| 归档时间: |
|
| 查看次数: |
11254 次 |
| 最近记录: |