繁体   English   中英

JAXB和使用外部自定义绑定的继承

[英]JAXB and Inheritance with external custom bindings

我目前正在尝试在XML模式中对Spring LDAP过滤器进行建模。 这涉及多态类型,可以任意嵌套:

<andFilter>
  <notFilter>
     <equalsFilter name="mail" value="asfd@example.com" />
  </notFilter>
  <likeFilter name="mail" value="asdf*" />
</andFilter>

这就是我在我的xsd中定义上述过滤器的方式(我实际上定义了一些中间抽象类型,但为了简单起见将它们排除在外):

<xsd:complexType name="baseFilterType" abstract="true" />
<xsd:element name="filter" type="tns:baseFilterType" />

<xsd:complexType name="andFilterType">
  <xsd:sequence>
    <xsd:element ref="tns:filter" minOccurs="1" maxOccurs="unbounded" />
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="andFilter" type="tns:andFilterType" substitutionGroup="tns:filter" />

<xsd:complexType name="notFilterType">
  <xsd:sequence>
    <xsd:element ref="tns:filter" />
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="notFilter" type="tns:notFilterType" substitutionGroup="tns:filter" />

<xsd:complexType name="likeFilterType">
  <xsd:sequence>
    <xsd:element ref="tns:attributeName" />
    <xsd:element ref="tns:value" />
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="likeFilter" type="tns:likeFilterType" substitutionGroup="tns:filter" />

<xsd:complexType name="equalsFilterType">
  <xsd:sequence>
    <xsd:element ref="tns:attributeName" />
    <xsd:element ref="tns:value" />
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="equalsFilter" type="tns:equalsFilterType" substitutionGroup="tns:filter" />

我已经使用一些自定义行为扩展了每个过滤器类(以实现访问者模式),但我无法弄清楚如何将JAXB绑定到那些而不是JAXBElement<? extends BaseFilterType> JAXBElement<? extends BaseFilterType>在整个生成的类中使用的JAXBElement<? extends BaseFilterType>

JAXB模型类是在构建时生成的,因此我无法直接修改它们 - 我必须使用外部自定义绑定进行任何自定义 我已经尝试过implClassbaseClass自定义,但还是无法弄清楚如何做到这一点。

有人可以帮忙吗?

编辑:我正在使用Spring的Jaxb2Marshaller其中contextPaths设置用于编组Jaxb2Marshaller组。 我希望这可以利用ObjectFactory进行对象实例化,但似乎并非如此。

编辑2:过滤器元素由顶级元素以及彼此引用,如:

<xsd:element name="GetAttributesRequest">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="filter:filter" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

因此, GetAttributesRequest对象将使用@XmlRootElement标记,而过滤器类本身则不会。 但是,即使是顶级的GetAttributesRequest对象也没有被ObjectFactory实例化......

编辑:

问题作者使用的解决方案:

我最终使用JAXB绑定自定义来使ObjectFactory生成我的类的实例,然后在Spring的Jaxb2Marshaller上设置com.sun.xml.internal.bind.ObjectFactory(注意内部)unmarshaller属性以强制它使用工厂。


原始答案:

是的,可以向生成的类添加行为。 您想查看相关文档

以下是文档中的一个简单示例:

JAXB生成的代码

package org.acme.foo;

@XmlRootElement
class Person {
  private String name;

  public String getName() { return name; }
  public void setName(String) { this.name=name; }
}

@XmlRegistry
class ObjectFactory {
  Person createPerson() { ... }
}

扩展课程

包org.acme.foo.impl;

class PersonEx extends Person {
  @Override
  public void setName(String name) {
    if(name.length()<3) throw new IllegalArgumentException();
    super.setName(name);
  }
}

@XmlRegistry
class ObjectFactoryEx extends ObjectFactory {
  @Override
  Person createPerson() {
    return new PersonEx();
  }
}

解组的例子

Unmarshaller u = context.createUnmarshaller();
u.setProperty("com.sun.xml.bind.ObjectFactory",new ObjectFactoryEx());
PersonEx p = (PersonEx)u.unmarshal(new StringReader("<person />"));

更多信息在这里

暂无
暂无

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

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