2011-02-08 21 views
2

我已經創建了自定義JiBX編組,並驗證了它的工作原理。它的工作原理如下:與自定義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> 

但是我需要爲不同的HashMaps創建多個marshallers。於是,我試着用抽象的映射像這樣引用它:這樣做時

<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(http://jibx.sourceforge.net/api/org/jibx/runtime/IAbstractMarshaller.html)中找到了IAbstractMarshaller接口,但是如果這是我需要實現的,以及它如何工作,文檔對我來說似乎還不清楚。作爲一個例子,我一直無法找到這個接口的實現。

我的問題是,你如何做自定義marshallers抽象映射(如果可能的話)?如果通過IAbstractMarshaller接口完成,它是如何工作的和/或我應該如何實現它?

回答

2

我不確定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>