[英]jaxb override package of a specific generated set of classes
I have a large third party xsd file that pulls in a whole bunch of other xsd files through imports. 我有一个大型第三方xsd文件,该文件通过导入引入了一大堆其他xsd文件。 The whole thing generates over 1000 classes. 整个过程产生了1000多个类。 When I tell the xjc (jaxb) processor to generate everything in a specific package I get all kinds of naming conflicts. 当我告诉xjc(jaxb)处理器在特定程序包中生成所有内容时,我会遇到各种命名冲突。 If I don't specify a package, then the processor creates java packages using the target attributes in the xsd files, and the whole thing generates without any errors. 如果我未指定包,则处理器将使用xsd文件中的target属性创建java包,并且整个过程不会产生任何错误。
The problem is that the package structure is awful because the target names are chosen really poorly. 问题在于包结构很糟糕,因为目标名称的选择确实很差。 Other developers hate it. 其他开发人员讨厌它。 They only need a few classes. 他们只需要几节课。
So what I've been trying to do is to give the processor a bindings.xml file where I want to specify certain classes to be generated in a given package name. 因此,我一直在尝试为处理器提供一个bindings.xml文件,在该文件中我想指定要在给定程序包名称中生成的某些类。 I've gone back and forth between documentation on Oracle's site, forums, samples. 我在Oracle网站的文档,论坛和示例之间来回走动。 I can't quote every single iteration of my bindings file. 我无法引用绑定文件的每个迭代。 My current incarnation is this: 我当前的化身是这样的:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1"
schemaLocation="heavy.xsd"
node="//xsd:element[@name='Error']"
>
<jaxb:bindings node="//xsd:element[@name='Error']">
<jaxb:package name="ABC"/>
</jaxb:bindings>
<jaxb:globalBindings
underscoreBinding="asCharInWord"
localScoping="toplevel"
typesafeEnumMaxMembers="10000"
generateElementClass="true"
>
</jaxb:globalBindings>
<!--
<jaxb:package name="ABC"/>
-->
<!--
<jaxb:schemaBindings>
<jaxb:package name="ABC"/>
</jaxb:schemaBindings>
-->
<!--
<jaxb:schemaBindings>
<jaxb:package name="ABC"/>
<jaxb:nameXmlTransform>
<jaxb:elementName prefix="Error"/>
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
-->
</jaxb:bindings>
No matter what I try, I have not yet seen it generate any classes in the ABC package. 不管我尝试什么,我都还没有看到它会在ABC包中生成任何类。 In this case I want Error to get generated in ABC. 在这种情况下,我希望在ABC中生成错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。
Unfortunately it is not possible to do what you want. 不幸的是,不可能做您想做的事。 I want the same thing and I'm in the same boat. 我想要同一件事,而且我在同一条船上。 But straight from Oracles mouth: 但直接从甲骨文的口中:
http://docs.oracle.com/cd/E19316-01/819-3669/bnbbt/index.html http://docs.oracle.com/cd/E19316-01/819-3669/bnbbt/index.html
Relevant Info: 相关资料:
name - is the name of the derived Java interface. name-是派生的Java接口的名称。 It must be a legal Java interface name and must not contain a package prefix. 它必须是合法的Java接口名称,并且不能包含程序包前缀。 The package prefix is inherited from the current value of package. 包前缀是从包的当前值继承的。
So there is simply no way on the element to specify a different package its inherited per xsd. 因此,根本没有办法在元素上指定其每个xsd继承的不同包。
The way I would do it is more like this: 我这样做的方式更像是这样:
<?xml version="1.0" encoding="utf-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1" jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="XSD1.xsd" node="/xs:schema">
<jxb:globalBindings>
<!-- -->
</jxb:globalBindings>
<jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:package name="com.something">
</jxb:package>
</jxb:schemaBindings>
<jxb:bindings node="//xs:element[@name='Error']">
<jxb:class xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="MyError"/>
<jxb:property xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="Whatever"/>
</jxb:bindings>
</jxb:bindings>
<jxb:bindings schemaLocation="XSD2.xsd" node="/xs:schema">
<jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:package name="com.somethingelse">
</jxb:package>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
Here is an example of bindings.xjb file where java source is generated in primer.myPo package. 这是bindings.xjb文件的示例,其中java源代码是在primary.myPo软件包中生成的。
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="po.xsd" node="/xs:schema">
<jxb:globalBindings
fixedAttributeAsConstantProperty="false"
collectionType="java.util.Vector"
typesafeEnumBase="xs:NCName"
choiceContentProperty="false"
typesafeEnumMemberName="generateError"
enableFailFastCheck="false"
generateIsSetMethod="false"
underscoreBinding="asCharInWord"/>
<jxb:schemaBindings>
<jxb:package name="primer.myPo">
<jxb:javadoc><![CDATA[<body>Package level documentation for generated package primer.myPo.</body>]]>
</jxb:javadoc>
</jxb:package>
<jxb:nameXmlTransform>
<jxb:elementName suffix="Element"/>
</jxb:nameXmlTransform>
</jxb:schemaBindings>
<jxb:bindings node="//xs:complexType[@name='PurchaseOrderType']">
<jxb:class name="POType">
<jxb:javadoc>A <b>Purchase Order</b> consists of addresses and items.</jxb:javadoc>
</jxb:class>
</jxb:bindings>
<jxb:bindings node="//xs:complexType[@name='USAddress']">
<jxb:class>
<jxb:javadoc><![CDATA[First line of documentation for a <b>USAddress</b>.]]></jxb:javadoc>
</jxb:class>
<jxb:bindings node=".//xs:element[@name='name']">
<jxb:property name="toName"/>
</jxb:bindings>
<jxb:bindings node=".//xs:element[@name='zip']">
<jxb:property name="zipCode"/>
</jxb:bindings>
</jxb:bindings> <!-- node="//xs:complexType[@name='USAddress']" -->
<jxb:bindings node="//xs:complexType[@name='Items']">
<jxb:bindings node=".//xs:element[@name='item']//xs:element[@name='quantity']">
<jxb:property generateIsSetMethod="true"/>
<jxb:bindings node="./xs:simpleType">
<jxb:javaType name="short"
parseMethod="javax.xml.bind.DatatypeConverter.parseShort"
printMethod="javax.xml.bind.DatatypeConverter.printShort" />
</jxb:bindings>
</jxb:bindings> <!-- node="//xs:complexType[@name='Items']" -->
<jxb:bindings node=".//xs:element[@name='USPrice']">
<jxb:property name="Price"/>
</jxb:bindings>
<jxb:bindings node=".//xs:attribute[@name='partNum']">
<jxb:property name="partNumber"/>
</jxb:bindings>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='USState']">
<jxb:typesafeEnumClass/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='ZipCodeType']">
<jxb:javaType name="int"
parseMethod="javax.xml.bind.DatatypeConverter.parseInt"
printMethod="javax.xml.bind.DatatypeConverter.printInt"/>
</jxb:bindings>
</jxb:bindings> <!-- schemaLocation="po.xsd" node="/xs:schema" -->
</jxb:bindings>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.