使用自定义 JiBX 编组器进行抽象映射

发布于 2024-10-16 11:57:00 字数 1779 浏览 6 评论 0原文

我创建了一个自定义 JiBX 编组器并验证了它的工作原理。它的工作原理如下:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping name="context" class="com.foobar.Context">
    <structure field="configuration"/>
  </mapping>
</binding>

但是我需要为不同的 HashMap 创建多个编组器。所以我尝试用这样的抽象映射来引用它:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping abstract="true" type-name="configuration" class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping abstract="true" type-name="overrides" class="java.util.HashMap" marshaller="com.foobar.Marshaller2"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration"/>
    <structure map-as="overrides" field="overrides"/>
  </mapping>
</binding>

但是,当我这样做时,当我尝试构建绑定时,我收到以下信息:

Error during code generation for file "E:\project\src\main\jibx\foo.jibx" - this may be due to an error in your binding or classpath, or to an error in the JiBX code

我的猜测是,要么我缺少一些我需要实现的东西来启用我的自定义编组器抽象映射,或自定义编组器不支持抽象映射。

我在 JiBX API 中找到了 IAbstractMarshaller 接口 (http://jibx. sourceforge.net/api/org/jibx/runtime/IAbstractMarshaller.html),但是我似乎不清楚文档是否这就是我需要实现的,以及如果是的话它是如何工作的。我还没有找到这个接口的实现来作为示例。

我的问题是,如何使用自定义编组器进行抽象映射(如果可能的话)?如果它是通过 IAbstractMarshaller 接口完成的,它是如何工作的和/或我应该如何实现它?

I have created a custom JiBX marshaller and verified it works. It works by doing something like the following:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping name="context" class="com.foobar.Context">
    <structure field="configuration"/>
  </mapping>
</binding>

However I need to create multiple marshallers for different HashMaps. So I tried to reference it with abstract mapping like this:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping abstract="true" type-name="configuration" class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping abstract="true" type-name="overrides" class="java.util.HashMap" marshaller="com.foobar.Marshaller2"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration"/>
    <structure map-as="overrides" field="overrides"/>
  </mapping>
</binding>

However when doing so, when I attempt to build the binding, I receive the following:

Error during code generation for file "E:\project\src\main\jibx\foo.jibx" - this may be due to an error in your binding or classpath, or to an error in the JiBX code

My guess is that either I'm missing something I need to implement to enable my custom marshaller for abstract mapping, or custom marshallers do not support abstract mapping.

I have found the IAbstractMarshaller interface in the JiBX API (http://jibx.sourceforge.net/api/org/jibx/runtime/IAbstractMarshaller.html), however the documentation seems unclear to me on if this is what I need to implement, as well as how it works if so. I have not been able to find an implementation of this interface to work off of as an example.

My question is, how do you do abstract mapping with custom marshallers (if it's possible)? If it is done via the IAbstractMarshaller interface, how does it work and/or how should I implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-10-23 11:57:00

我不确定 IAbstractMarshaller 接口是否是您正在寻找的;文档有点不清楚。如果您不介意稍微重复,您可以直接在结构映射上指定编组器,这应该会获得所需的结果(“配置”和“覆盖”的单独映射):

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration" marshaller="com.foobar.Marshaller1"/>
    <structure map-as="overrides" field="overrides" marshaller="com.foobar.Marshaller2"/>
  </mapping>
</binding>

I'm not sure whether the IAbstractMarshaller interface is what you're looking for; the docs are a bit unclear. If you don't mind a little repetition, you can specify the marshaller directly on your structure mapping, which should get the desired result (separate mappings for 'configuration' and 'overrides'):

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration" marshaller="com.foobar.Marshaller1"/>
    <structure map-as="overrides" field="overrides" marshaller="com.foobar.Marshaller2"/>
  </mapping>
</binding>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文