JAXB SchemaGen与Enum在不同的包中

时间:2012-08-11 03:00:09

标签: xsd jaxb schemagen

如何从引用不同包中的枚举的JAXB对象生成模式?

以下是一个例子:

枚举等级

package com.example.one

public enum EnumExample {
    VALUE_ONE
    VALUE_TWO
}

JAXB Class

package com.example.two

public class Example {

private EnumExample enumExample;

    @XMLElement(name="enumExample", required=true)
    public EnumExample getEnumExample() {
        return enumExample
    }

    public void setEnumExample() {
        this.enumExample = enumExample;
    }
}

package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://example.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package com.example.two;

Ant Target

<path id="glassfish.schemagen.classpath">
    <pathelement location="/apps/glassfish/lib/webservices-tools.jar" />
</path>

<taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask">
    <classpath refid="glassfish.schemagen.classpath" />
</taskdef>

<schemagen srcdir="${src.dir}/com/example/two" destdir="xsd">
    <schema namespace="http://exmaple.com" file="example.xsd" />
</schemagen>

当我运行上面的例子时,我得到以下内容:

  

[schemagen]异常的package-info.java路径:package-info.java   [schemagen]从7个源文件生成模式   [schemagen]注释处理过程中遇到的问题;   [schemagen]请参阅下面的stacktrace以获取更多信息。   [schemagen] java.lang.NullPointerException   [schemagen] at com.sun.tools.jxc.model.nav.APTNavigator $ 2.onDeclaredType(APTNavigator.java:428)   [schemagen] at com.sun.tools.jxc.model.nav.APTNavigator $ 2.onClassType(APTNavigator.java:402)   [schemagen] at com.sun.tools.jxc.model.nav.APTNavigator $ 2.onClassType(APTNavigator.java:456)   [schemagen] at com.sun.istack.tools.APTTypeVisitor.apply(APTTypeVisitor.java:27)   [schemagen] at com.sun.tools.jxc.model.nav.APTNavigator.getBaseClass(APTNavigator.java:109)   [schemagen] at com.sun.tools.jxc.model.nav.APTNavigator.getBaseClass(APTNavigator.java:85)   [schemagen] at com.sun.xml.bind.v2.model.impl.PropertyInfoImpl.getIndividualType(PropertyInfoImpl.java:190)   [schemagen] at com.sun.xml.bind.v2.model.impl.PropertyInfoImpl。(PropertyInfoImpl.java:132)   [schemagen] at com.sun.xml.bind.v2.model.impl.ERPropertyInfoImpl。(ERPropertyInfoImpl.java:53)   [schemagen] at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl。(ElementPropertyInfoImpl.java:96)   [schemagen] at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.createElementProperty(ClassInfoImpl.java:886)   [schemagen] at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.addProperty(ClassInfoImpl.java:841)   [schemagen] at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.findGetterSetterProperties(ClassInfoImpl.java:966)   [schemagen] at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getProperties(ClassInfoImpl.java:298)   [schemagen] at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:243)   [schemagen] at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)   [schemagen] at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)   [schemagen] at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:330)   [schemagen] at com.sun.tools.xjc.api.impl.j2s.JavaCompilerImpl.bind(JavaCompilerImpl.java:90)   [schemagen] at com.sun.tools.jxc.apt.SchemaGenerator $ 1.process(SchemaGenerator.java:115)   [schemagen] at com.sun.mirror.apt.AnnotationProcessors $ CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)   [schemagen] at com.sun.tools.apt.comp.Apt.main(Apt.java:454)   [schemagen] at com.sun.tools.apt.main.JavaCompiler.compile(JavaCompiler.java:258)   [schemagen] at com.sun.tools.apt.main.Main.compile(Main.java:1102)   [schemagen] at com.sun.tools.apt.main.Main.compile(Main.java:964)   [schemagen] at com.sun.tools.apt.Main.processing(Main.java:95)   [schemagen] at com.sun.tools.apt.Main.process(Main.java:85)   [schemagen] at com.sun.tools.apt.Main.process(Main.java:67)   [schemagen] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   [schemagen] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)   [schemagen] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)   [schemagen] at java.lang.reflect.Method.invoke(Method.java:597)   [schemagen] at com.sun.tools.jxc.AptBasedTask $ InternalAptAdapter.execute(AptBasedTask.java:97)   [schemagen] at com.sun.tools.jxc.AptBasedTask.compile(AptBasedTask.java:144)   [schemagen] at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:906)   [schemagen] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)   [schemagen] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)   [schemagen] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)   [schemagen] at java.lang.reflect.Method.invoke(Method.java:597)   [schemagen] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)   [schemagen] at org.apache.tools.ant.Task.perform(Task.java:348)   [schemagen] at org.apache.tools.ant.Target.execute(Target.java:390)   [schemagen] at org.apache.tools.ant.Target.performTasks(Target.java:411)   [schemagen] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1397)   [schemagen] at org.apache.tools.ant.Project.executeTarget(Project.java:1366)   [schemagen] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)   [schemagen]在org.apache.tools.ant.Project.executeTargets(Project.java:1249)   [schemagen]在org.apache.tools.ant.Main.runBuild(Main.java:801)   [schemagen]在org.apache.tools.ant.Main.startAnt(Main.java:218)   [schemagen] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)   [schemagen]在org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

我知道它导致错误的枚举因为如果我切换到一个字符串就会消失。如何使用上面的示例生成一个模式,其中枚举位于同一名称空间或不同的名称空间中?

1 个答案:

答案 0 :(得分:0)

  

schemagen支持javac task

定义的大部分属性

您是否尝试过使用包含或排除元素(未经测试)?

<schemagen srcdir="${src.dir}/com/example" destdir="xsd">
    <schema namespace="http://exmaple.com" file="example.xsd" />
    <include name="one/EnumExample.java"/>
    <include name="two/Example.java"/>
</schemagen>