3

我想根据属性值将 JAXB bean 解组为子类。

MOXy 可以做到这一点:

@XmlDiscriminatorNode("@type")
public static class ActionDef extends ContainerOfStackableDefs { ... }

@XmlDiscriminatorValue("cli")
public static class CliActionDef extends ActionDef {
    /** CLI command. EL. */
    @XmlAttribute public String command;
}

但是,JAXBContext需要指定所有类。在“根”类之外的其他地方定义这有点令人不快。

我希望有类似的东西:

@XmlDiscriminatorNode("@type")
@XmlSubClasses({ CliActionDef.class, XsltActionDef.class, ... })
public static class ActionDef extends ContainerOfStackableDefs { ... }

我不想进入XmlAdapters,这是一项微不足道的任务,应该有微不足道的解决方案。

有什么东西,无论是规范还是 MOXy 扩展,我都可以用来简单地列出根类中的子类?

否则,我将使用 Jandex 自动查找子类。毕竟,MOXy 也可以做到这一点;-)


更新:为了记录,然后我得到

Descriptor: XMLDescriptor(org.jboss.loom.migrators._ext.MigratorDefinition$ActionDef --> [])
at org.eclipse.persistence.exceptions.DescriptorException.missingClassIndicatorField(DescriptorException.java:957)
    ...

此处描述:异常:“数据库行 [UnmarshalRecordImpl()] 中缺少类指示符字段。” 使用 EclipseLink JAXB (MOXy) 解组 XML 时

简而言之:不要将 MOXy 解组为与@XmlDiscriminatorNode("@name"). 如果你这样做了,那么就拥有它@XmlReadOnly并且没有类抽象。


更新:我仍然无法让它工作。我不断得到基类。由 MOXy 处理,而不是 JDK 的 impl。

我的代码:

    <action type="xslt" var="addAction" ...>
    </action>

    <action type="manual">
        ...
    </action>

豆子:

//@XmlRootElement
@XmlDiscriminatorNode("@type")
@XmlSeeAlso({ CliActionDef.class, ModuleActionDef.class, CopyActionDef.class, XsltActionDef.class })
public static class ActionDef extends ContainerOfStackableDefs {
    //@XmlAttribute
    //@XmlReadOnly
    public String typeVal; // Was type, caused an exception.

    //public List<PropertyBean> properties;
    //@XmlAnyAttribute
    public Map<String, String> attribs = new HashMap();
}

@XmlRootElement
@XmlDiscriminatorValue("manual")
public static class ManualActionDef extends ActionDef {
}

public static class FileBasedActionDef extends ActionDef {
    /** Path mask. Ant-like wildcards, EL. */
    @XmlAttribute public String pathMask;

    /** Where to store the result. May be a dir or a file. EL. */
    @XmlAttribute public String dest;
}

@XmlRootElement
@XmlDiscriminatorValue("xslt")
public static class XsltActionDef extends FileBasedActionDef {
    /** XSLT template path. */
    @XmlAttribute public String xslt;
}

怎么了?

更新:

将 @XmlDiscriminatorNode("@type") 放到最顶层ContainerOfStackableDefs后,我得到:

Exception Description: Missing class for indicator field value [manual] of type [class java.lang.String].
Descriptor: XMLDescriptor(org.jboss.loom.migrators._ext.MigratorDefinition$ActionDef --> [])
at org.eclipse.persistence.exceptions.DescriptorException.missingClassForIndicatorFieldValue(DescriptorException.java:938)
at org.eclipse.persistence.internal.oxm.QNameInheritancePolicy.classFromRow(QNameInheritancePolicy.java:264)

简而言之:缺少java.lang.String. :-o 可以解决吗?


另外,我创建了一个最简单的测试用例:

public class JaxbInheritanceTest {    
    @Test
    public void testUnmarshall() throws JAXBException{
        final Unmarshaller marshaller = XmlUtils.createJaxbContext(Root.class).createUnmarshaller();
        Root root = (Root) marshaller.unmarshal( new StringReader(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><sub disc=\"foo\"/><sub disc=\"bar\"/></root>") );

        boolean rightClass = (DiscFoo.class.isAssignableFrom( root.subs.get(0).getClass() ));
        Assert.assertTrue( "base elements go into subclasses", rightClass );

        rightClass = (DiscBar.class.isAssignableFrom( root.subs.get(1).getClass() ));
        Assert.assertTrue( "base elements go into subclasses", rightClass );
    }

    @XmlRootElement
    public static class Root {
        @XmlElement(name = "sub")
        List<Base> subs;
    }

    @XmlDiscriminatorNode("@disc")
    @XmlSeeAlso({DiscFoo.class, DiscBar.class})
    public static abstract class Base {}

    @XmlRootElement @XmlDiscriminatorValue("foo")
    public static class DiscFoo {}

    @XmlRootElement @XmlDiscriminatorValue("bar")
    public static class DiscBar {}

}

但这也给了我 2x Base。它由 MOXy 处理 - 在调试器中检查。
这里有什么问题?

4

1 回答 1

4

您可以@XmlSeeAlso为此使用 (javax.xml.bind.annotation) 注释。

@XmlDiscriminatorNode("@type")
@XmlSeeAlso({ CliActionDef.class, XsltActionDef.class, ... })
public static class ActionDef extends ContainerOfStackableDefs { ... }

更新

您在最新更新中看到的问题是由于@XmlDescriminatorNode未放置在继承层次结构中的根类上引起的。当我ContainerOfStackableDefs从继承层次结构中删除时,@XmlTransient一切正常(参见: http ://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html )。

演示

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

public class Demo {

    @XmlTransient
    public static class ContainerOfStackableDefs {
    }

    @XmlDiscriminatorNode("@type")
    @XmlSeeAlso({ ManualActionDef.class, FileBasedActionDef.class, XsltActionDef.class })
    public static class ActionDef extends ContainerOfStackableDefs {
        //@XmlAttribute
        //@XmlReadOnly
        public String typeVal; // Was type, caused an exception.

        //public List<PropertyBean> properties;
        //@XmlAnyAttribute
        public Map<String, String> attribs = new HashMap();
    }

    @XmlDiscriminatorValue("manual")
    public static class ManualActionDef extends ActionDef {
    }

    @XmlDiscriminatorValue("fileBased")
    public static class FileBasedActionDef extends ActionDef {
        /** Path mask. Ant-like wildcards, EL. */
        @XmlAttribute public String pathMask;

        /** Where to store the result. May be a dir or a file. EL. */
        @XmlAttribute public String dest;
    }

    @XmlDiscriminatorValue("xslt")
    public static class XsltActionDef extends FileBasedActionDef {
        /** XSLT template path. */
        @XmlAttribute public String xslt;
    }

    @XmlRootElement
    public static class Root {

        @XmlElement(name="actionDef")
        public List<ActionDef> actionDefs;

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17453793/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <actionDef type="manual"/>
   <actionDef pathMask="PATH MASK" dest="DEST" type="fileBased"/>
   <actionDef xslt="XSLT" type="xslt"/>
</root>
于 2013-07-03T16:52:44.643 回答