简体   繁体   English

用JAXB注释应用Composite模式的最佳方法?

[英]Best way to apply Composite pattern with JAXB annotation?

Consider building this xml msg using JAXB: 考虑使用JAXB构建此xml消息:

<msg>
  <database id="111">
     <table name="t1" pkCol="id">
        <row op="update">
            <col name="id" value="12345"/>
            <col name="age" value="30"/>
            <col name="name" value="John"/>
        </row>
        <row ...>
        </row>
     </table>
     <table ...>
       :
     </table>
   </database>
   <database ...>
     <table ...>
     </table>
   </database>
</msg>

Our existing legacy implementation is having an individual class for each tag, namely Msg, Database, Table, Row and Column. 我们现有的旧版实现为每个标签都有一个单独的类,即消息,数据库,表,行和列。 No common base class. 没有共同的基类。 Each class has a List/ArrayList, a no-arg constructor, plus other setters for @XmlAttribute and List adding. 每个类都有一个List / ArrayList,一个无参数构造函数以及用于@XmlAttribute和List添加的其他设置方法。

Trying to apply the Composite pattern (other pattern suggestion?) since all these classes are pretty much nodes (with child nodes) and yet with their own corresponding attributes. 尝试应用Composite模式(其他模式建议?),因为所有这些类都是相当多的节点(带有子节点),但是具有它们自己的对应属性。 Coming up with a common base class 提出一个共同的基类

class Node<X> extends ArrayList<X>

but don't know howto apply the @XmlElement(name ="depends_on_subclass") on the base class from within a subclass. 但不知道如何从子类中将@XmlElement(name =“ depends_on_subclass”)应用于基类。 Unless from within the subclass and add @XmlElement on a dummy getSelf() method, eg in Table, 除非在子类中,并且在虚拟getSelf()方法上(例如在Table中)添加@XmlElement,

class Table extends Node<Row>
{

    @XmlElement(name="row")
    public Table getSelf()
    {
        return this;
    }
:
:

Any other or better suggestion? 还有其他更好的建议吗? Thanks in advance. 提前致谢。

I assume your using xjc for this, in which case you can use the jaxb plugin 我假设您为此使用xjc,在这种情况下,您可以使用jaxb插件

I used this plugin http://confluence.highsource.org/display/J2B/Inheritance+plugin 我使用了这个插件http://confluence.highsource.org/display/J2B/Inheritance+plugin

then add something like this to your xml. 然后将类似这样的内容添加到您的xml中。 and thats it. 就是这样。 they will extend the given base class. 他们将扩展给定的基类。

<xsd:element name="attributesRequest">
    <xsd:annotation>
        <xsd:appinfo>
            <inheritance:extends>com.work.autofill.common.Filter</inheritance:extends>
        </xsd:appinfo>
    </xsd:annotation>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM