1

jaxb クラスを生成する xsd があります。そのために、汎用マップ用の XML アダプターを作成しました。このアダプターは単体テストに合格し、生成された変数の型は適切に意図したとおりのマップになります。ただし、このシナリオから作成されたオブジェクトを検証しようとすると、「「hashMap」タイプを要素「autoExecuteArguments」の定義に解決できません。かなりいじりましたが、常に上記のバリエーションで終了します。エラー. これら 2 つのバインディングは一緒に使用されませんが、何らかの解決に向けた試みを例示しています。

      <xjc:javaType name="java.util.Map" xmlType="mp:MapTypeEntry"
        adapter="mil.dod.th.ose.core.impl.mp.UtilMapConverter" />

 <jaxb:bindings schemaLocation="resources/missionProgramSchema/MissionProgram.xsd">
    <jaxb:bindings node="//xs:element[@name='MissionProgramInstance']">
        <jaxb:bindings node="//xs:element[@name='autoExecuteArguments']">
            <jaxb:property>
                <jaxb:baseType name="java.util.HashMap">
                    <xjc:javaType name="java.util.Map"
                    adapter="//core.impl.mp.UtilMapConverter" />
                </jaxb:baseType>
            </jaxb:property>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings> 

最後の 1 つはバインドの嘲笑であると確信しており、そのため、大量の微調整で動作させることができませんでした。「[エラー] コンパイラは、このプロパティのカスタマイズを尊重できませんでした。間違った場所に取り付けられているか、他のバインディングと矛盾しています。

次にコンバーター:

@XmlSeeAlso(UtilMapConverter.MapType.class)
public class UtilMapConverter<K, V> extends XmlAdapter<UtilMapConverter.MapType<K,V>, Map<K, V>> {

@Override
public UtilMapConverter.MapType<K, V> marshal(final Map<K, V> map) 
{
    final MapType<K, V> mapType = new MapType<K, V>();
    for (Map.Entry<K, V> entry : map.entrySet()) 
    {
        final MapTypeEntry<K, V> mapEntryType = new MapTypeEntry<K, V>(); 
        mapEntryType.setKey(entry.getKey());
        mapEntryType.setValue(entry.getValue());
        mapType.getEntry().add(mapEntryType);
    }
    return mapType;
}

@Override
public Map<K, V> unmarshal(final UtilMapConverter.MapType<K, V> genericMap) 
{
    final Map<K, V> map = new HashMap<K, V>();

    for (MapTypeEntry<K, V> mapEntryType : genericMap.getEntry()) 
    {
        map.put(mapEntryType.getKey(), mapEntryType.getValue());
    }
    return map;
}

@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapType<K, V> 
{
    private List<MapTypeEntry<K, V>> m_Entry = new ArrayList<MapTypeEntry<K, V>>();

    public MapType()
    {  
    }

    public MapType(final Map<K, V> map) 
    {
        for (Map.Entry<K, V> e : map.entrySet()) 
        {
            m_Entry.add(new MapTypeEntry<K, V>(e));
        }
    }
    @XmlElement
    @XmlSchemaType(name = "mapType")
    public List<MapTypeEntry<K, V>> getEntry() 
    {
        return m_Entry;
    }
    public void setEntry(final List<MapTypeEntry<K, V>> entry) 
    {
        this.m_Entry = entry;
    }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapTypeEntry<K, V> 
{

    @XmlElement(required = true)
    @XmlSchemaType(name = "key")
    protected K m_Key;


    @XmlElement(required = true)
    @XmlSchemaType(name = "value")
    protected V m_Value;


    public MapTypeEntry() 
    {
    }


    public MapTypeEntry(final Map.Entry<K, V> entry) 
    {
        m_Key = entry.getKey();
        m_Value = entry.getValue();
    }


    @XmlElement
    public K getKey() 
    {
        return m_Key;
    }

    @XmlElement
    public V getValue() 
    {
        return m_Value;
    }
}

私は混乱していることを認識しています。私は自分のそばで、何が助けになっているのか、何が傷ついているのか、何がただばかげているのかを理解しようとしています. 最後に xsd:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="http://core/mp/model" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://core/mp/model"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" elementFormDefault="qualified"
id="MissionProgramInstance" jaxb:version="2.1"
jaxb:extensionBindingPrefixes="xjc">
<xs:include schemaLocation="MissionProgram.xsd" />
<xs:element name="MissionProgramInstance">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="program" type="MissionProgram"
                minOccurs="1" />
            <xs:element name="autoExecuteArguments" minOccurs="0">
                <xs:annotation>
                    <xs:appinfo>
                        <jaxb:property>
                            <jaxb:baseType name="java.util.Map" />
                        </jaxb:property>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="autoExecuteEnabled" type="xs:boolean"
            use="required" />
        <xs:attribute name="assetDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="physicalLinkDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="linkLayerDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="transportLayerDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="imageStreamDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="programDep" use="optional">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>
<xs:complexType name="MissionProgram">
    <xs:sequence>
        <!--This field holds source data for a mission program -->
        <xs:element name="source" type="xs:string" minOccurs="1" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="MapType">
    <xs:sequence>
        <xs:element name="autoExecuteArgument" type="MapTypeEntry">
            <xs:annotation>
                <xs:appinfo>
                    <jaxb:property>
                        <jaxb:baseType name="//core.mp.impl.UtilMapConverter.MapType" />
                    </jaxb:property>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>
<xs:simpleType name="MapTypeEntry">
    <xs:list itemType="xs:anySimpleType" />
</xs:simpleType>
    </xs:schema>

この現在のプレゼンテーションには少し冗長性があります。私はxsdとjaxbが初めてです。そして、私はこれらすべてを理解しようとしましたが、調査を試みたにもかかわらず、私は間違った場所を見ていると思います. これは、少なくとも一部をまとめようとする私の主なリソースの1つです: http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html . また、xml アダプターの javadoc と、あちこちでブラウジングするゴブも含まれています。少しでも「自分ならどうする?」と考えていただければ幸いです。

4

1 に答える 1

0

私がする (そして行った) ことは、クリーンな xsd を作成し、xjc を使用して Java クラスを生成することです。より「コーダーフレンドリー」なデータ構造が必要な場合は、独自のコードを記述して、デシリアライゼーション後に jaxb オブジェクトを変換します。xml を強制的にデータ構造の外観に合わせようとすることは、通常、フラストレーションの練習であり、プロジェクトが時間の経過とともに変化するにつれて、あなたの人生を困難にします.

于 2012-08-12T20:52:13.543 に答える