2012-08-12 3 views
1

jaxb 클래스를 생성하는 xsd가 있습니다. 나는 generic map을위한 XML 어댑터를 만들었다. 이 어댑터는 단위 테스트를 전달하며 생성 된 변수의 유형은 의도 한대로 적절히 맵입니다. 그러나이 시나리오에서 생성 된 객체의 유효성을 검사 할 때 "hashMap '유형을'autoExecuteArguments '요소의 정의로 해석 할 수 없다는 것을 알았습니다. 위의 내용을 약간 변형 해 보았습니다. 오류가 발생했습니다.이 두 바인딩은 함께 사용하지만, 일부 해상도에 오는에 시도를 예시되지 않습니다.xsd에 대한 java.util.Map 유효성 검사

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

내가 마지막이 바인딩에 대한 조롱이라고 확신하고, 같은 나는 그것을 할 수 없었다 많은 양의 tweeking 작업, 나는 '[ERROR] 컴파일러가이 속성을 커스터마이징 할 수 없다는 것을 알게되었습니다. 잘못된 위치에 붙어 있거나 다른 바인딩과 일치하지 않습니다.

다음 conv 오류 :

@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에 익숙하지 않다. 그리고 나는이 모든 것을 이해하려고 노력했지만, 연구하려고 시도 했음에도 불구하고, 나는 조각들을 발견하는 것처럼 엉뚱한 곳을보고 있다고 생각하지만,이 모든 것이 함께 작용한다는 설명은 아닙니다. 여기에 최소한 부품을 모두 넣으려는 나의 주요 리소스 중 하나가 있습니다 : http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html. xml 어댑터 용 javadocs와 함께 여기저기서 브라우징하는 gobs도 있습니다. "적어도 넌 어떻게 생각해?"

답변

0

내가 무엇을 할 것인가 (그리고 완료 했음)는 깨끗한 xsd를 작성하고 xjc를 사용하여 Java 클래스를 생성합니다. 보다 "코더 친화적 인"데이터 구조가 필요하다면 jaxb 객체를 역 직렬화 후 변환하는 코드를 작성하십시오. 데이터 구조가 보이길 원한다고 생각하는 방식에 xml을 강제 적용하려고하면 일반적으로 좌절감이 생기고 프로젝트가 시간이지나면서 변형되기 때문에 삶이 어려워 질 것입니다.

+0

내가 기대했던 것입니다. 빠른 응답을 가져 주셔서 감사합니다. – cAllen

관련 문제