混合静态MoXy和动态Moxy

时间:2013-08-13 06:55:01

标签: eclipselink moxy

我正在尝试检查混合静态moxy和Dynamic moxy的一个概念。 我有两个模式。

  1. 一个是基本模式(emp.xsd),它不会经常更改,因此我使用静态Moxy并为其生成类,将这些类添加到类路径中,并使用这些具体类型(例如SearchManager) )在我的计划中。

  2. 另一个是派生架构(Manager.xsd),它会经常更改,所以我打算使用Dynamic Moxy动态生成动态类型。我正面临下面提到的一些问题。

  3. 这种静态和动态moxy的混合是否可能?

    代码段:

    emp.xsd(父模式):

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:emp="Employee:2:0" targetNamespace="Employee:2:0"
        elementFormDefault="unqualified" attributeFormDefault="unqualified"
        version="2.0">
    
        <xsd:element name="searchManager" type="emp:SearchManager" />
    
        <xsd:complexType name="SearchManager">
            <xsd:sequence>
                <xsd:element name="CompanyName" type="xsd:string" />
                <xsd:element name="objects" type="emp:Employee" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
    
    
        <xsd:complexType name="Employee">
    
            <xsd:complexContent>
                <xsd:extension base="emp:Organization">
                    <xsd:sequence>
                       <xsd:element name="EmpId" type="xsd:string" minOccurs="0" />
                    </xsd:sequence>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    
        <xsd:complexType name="Projects">
            <xsd:complexContent>
                <xsd:extension base="emp:Organization"/>
            </xsd:complexContent>
        </xsd:complexType>
    
        <xsd:complexType name="Organization">
            <xsd:annotation>
                <xsd:documentation>Abstract base class </xsd:documentation>
            </xsd:annotation>
        </xsd:complexType>
    </xsd:schema>
    

    manager.xsd(子模式):

    <?xml version="1.0" encoding="UTF-8"?>
    
    <xs:schema targetNamespace="Manager:1:0" xmlns:emp="Employee:2:0"
        xmlns:manager="Manager:1:0" xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="unqualified" attributeFormDefault="unqualified"
        version="1.0">
        <!-- schema imports -->
        <xs:import namespace="Employee:2:0" schemaLocation="emp.xsd" />
    
        <xs:complexType name="Manager">
            <xs:annotation>
                <xs:documentation>
                    Definition of class Employee
                </xs:documentation>
            </xs:annotation>
            <xs:complexContent>
                <xs:extension base="emp:Employee">
                    <xs:sequence>
                        <xs:element name="teamSize" type="xsd:int" minOccurs="0" />
                        <xs:element name="project1" type="manager:Project1"
                            minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    
    
        <xs:complexType name="Project1">
            <xs:complexContent>
                <xs:extension base="manager:Developement">
                    <xs:sequence>
                        <xs:element name="type" type="xsd:int" minOccurs="0" />
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="Developement">
            <xs:annotation>
                <xs:documentation>
                    Abstract base class for an Development
                </xs:documentation>
            </xs:annotation>
            <xs:complexContent>
                <xs:extension base="emp:Projects"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:schema>
    

    编组和解组的Java代码:

    public class XMLToJSONUsingBaseClasses {
    
    public static void main(String[] args) {
        FileInputStream xsdInputStream;
        try {
            xsdInputStream = new FileInputStream("Manager.xsd");
            DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, new MyEntityResolver(), null, null);
    
            FileInputStream xmlInputStream = new FileInputStream("manager.xml");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            SearchManager manager = (SearchManager) unmarshaller.unmarshal(xmlInputStream);
    
        }catch (JAXBException e) {
            e.printStackTrace();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    } 
    

    manager.xml

    <emp:searchManager xmlns:emp="Employee:2:0"
        xmlns:manager="Manager:1:0">
        <CompanyName>Test</CompanyName>
        <objects xmlns:ns2="Manager:1:0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Manager">
            <EmpId>123456</EmpId>
            <teamSize>10</teamSize>
            <project1>
                <type>1</type>
            </project1>
        </objects>
    </emp:searchManager>
    

    异常。

    Exception Description: An error occurred unmarshalling the document
    Internal Exception: java.lang.ClassCastException: employee._2._0.SearchManager cannot be cast to org.eclipse.persistence.internal.dynamic.DynamicEntityImpl
        at org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:114)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.convertSAXException(SAXUnmarshaller.java:996)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:903)
        at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:659)
        at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:585)
        ... 2 more
    Caused by: java.lang.ClassCastException: employee._2._0.SearchManager cannot be cast to org.eclipse.persistence.internal.dynamic.DynamicEntityImpl
        at org.eclipse.persistence.internal.dynamic.ValuesAccessor.setAttributeValueInObject(ValuesAccessor.java:60)
        at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.setAttributeValueInObject(XMLDirectMapping.java:408)
        at org.eclipse.persistence.internal.oxm.record.ObjectUnmarshalContext.setAttributeValue(ObjectUnmarshalContext.java:41)
        at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.setAttributeValue(UnmarshalRecordImpl.java:1400)
        at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:193)
        at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endElement(UnmarshalRecordImpl.java:1026)
        at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:154)
        at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:99)
        at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:86)
        at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:895)
        ... 4 more
    

1 个答案:

答案 0 :(得分:0)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

我们目前不支持以这种方式混合静态和动态。您能否使用以下链接为您的用例输入增强请求: