JAXB typeSafeEnumBinding在剧集中不与fixedAttributeAsConstantProperty一起使用

时间:2013-12-04 20:42:11

标签: xml maven enums jaxb xjc

我正在尝试用XSD制作一些(合同优先)java库我无法改变。

因此,假设有2个XSD Maven项目:

1)定义AttributeGroup的项目A,属性条目(enum)设置为固定值

2)使用AttributeGroup的项目B

项目B使用项目A作为剧集。现在,项目A在编译时创建一集。本集将枚举类型映射到typeSafeEnum。

B的编译结果是忽略了绑定'fixedAttributeAsConstantProperty'。我原以为:

@XmlAttribute(name = "type", namespace = "http://test.org/attrib")
public final static TypeType TYPE = TypeType.SIMPLE;

相反,我得到了:

@XmlAttribute(name = "type", namespace = "http://test.org/attrib")
protected TypeType type; 

如果在一个编译单元中构建A和B,它就可以工作。


我已将问题减少到项目A中生成的剧集装订:

  <bindings scd="x-schema::tns" xmlns:tns="http://test.org/attrib">
    <schemaBindings map="false">
      <package name="org.test.attrib"/>
    </schemaBindings>
    <bindings scd="~tns:typeType">
      <typesafeEnumClass ref="org.test.attrib.TypeType"/>
    </bindings>
  </bindings>
</bindings>

我的假设:目前XJC编译器看到了一个到typeSafeEnumClass的映射,它似乎没有构建枚举类型的内部表示。因此,它不知道如何将项目B中TypeType的固定值设置为常量。我试过使用绑定。这会生成一个字符串映射i.s.o一个枚举。在这种情况下,项目B确实将“固定”值设置为(字符串)常量。但是,它会产生其他问题。

任何人都知道如何解决这个问题?我在看XJC的错误吗?


这里要完成的是示例项目的源代码:

1)项目A,它在组中定义一个固定属性的AttributeGroup。

<xs:attribute name="type" type="attrib:typeType"/>

<xs:simpleType name="typeType">
    <xs:restriction base="xs:token">
        <xs:enumeration value="simple"/>
        <xs:enumeration value="extended"/>
    </xs:restriction>
</xs:simpleType>

<xs:attributeGroup name="attribGrp">
    <xs:attribute ref="attrib:type" fixed="simple"/>
</xs:attributeGroup>

binding.xjb:

<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
                   xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
                   jaxb:extensionBindingPrefixes="xjc">

        <jaxb:globalBindings fixedAttributeAsConstantProperty="true"/>


</jaxb:bindings>

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>attrib</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaIncludes>
                        <include>attrib.xsd</include>
                    </schemaIncludes>
                    <bindingIncludes>
                        <include>binding.xjb</include>
                    </bindingIncludes>
                    <target>2.1</target> 
                    <verbose>true</verbose>                 
                </configuration>
            </plugin>
        </plugins>  
    </build>
</project> 

2)使用此AttributeGroup的项目B

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="http://test.org/attribUser"
           xmlns:attrib="http://test.org/attrib"
           xmlns:attribUser="http://test.org/attribUser">

    <xs:import namespace="http://test.org/attrib" schemaLocation="http://test.org/attrib/attrib.xsd"/>


    <xs:complexType name="userOfTst">
        <xs:sequence>
            <xs:element name="dontCare" type="xs:string"/>   
        </xs:sequence>
        <xs:attributeGroup ref="attrib:attribGrp"/>
    </xs:complexType>

</xs:schema>

binding.xjb:

<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
               jaxb:extensionBindingPrefixes="xjc">

    <jaxb:globalBindings fixedAttributeAsConstantProperty="true"/>


</jaxb:bindings>

的catalog.xml:

<?xml version="1.0" encoding="windows-1252"?>
<catalog  xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">  
    <group>  
        <systemSuffix systemIdSuffix="http://test.org/attrib/attrib.xsd"  uri="maven:test:attrib!/attrib.xsd"/>
    </group>
</catalog>

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>test</groupId>
        <artifactId>attribUser</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>

        <dependencies>          
            <dependency>
                <groupId>test</groupId>
                <artifactId>attrib</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.8.3</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <schemaIncludes>
                            <include>attribUser.xsd</include>
                        </schemaIncludes>
                        <bindingIncludes>
                            <include>binding.xjb</include>
                        </bindingIncludes>
                        <catalog>${project.build.resources[0].directory}/catalog.xml</catalog>
                        <target>2.1</target> 
                        <verbose>true</verbose>
                        <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
                    </configuration>
                </plugin>
            </plugins>  
        </build>

    </project> 

0 个答案:

没有答案