简体   繁体   中英

Best way to apply Composite pattern with JAXB annotation?

Consider building this xml msg using JAXB:

<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.

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. 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. Unless from within the subclass and add @XmlElement on a dummy getSelf() method, eg in Table,

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

I used this plugin http://confluence.highsource.org/display/J2B/Inheritance+plugin

then add something like this to your 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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