JAXB-ElipseLink:Marshaller没有验证

时间:2012-01-19 10:15:17

标签: validation jaxb schema eclipselink moxy

我希望我的Eclipselink 2.3 Marshaller在编组时执行验证。 我已确保Schema正确创建SchemaFactory,我将其传递给Marshaller.setSchema,并通过Marshaller.setEventHandler()注册了处理程序。

元帅的结果显然无效。对于它的Schema(在Eclipse中验证),但我可以看到handleEvent(ValidationEvent event)中的断点从未被击中。

我使用marshal(Object, XMLStreamWriter)编组XML片段,并希望Marshaller根据我传递的Schema对这些片段进行验证。

有人知道为什么没有发生这种情况吗?

修改

应该发生的验证错误:2元素上缺少属性。

该元素对应于List<>中包含的Java对象。我使用:

编组列表
<xml-element java-attribute="listInstance" xml-path="ListWrapperElement/ListElement" type="foo.ElementType" container-type="java.util.ArrayList"/>

元素本身的映射:

<java-type name="foo.ElementType" xml-accessor-type="PROPERTY">
    <java-attributes>
        // just <xml-attribute> elements here
    </java-attributes>
</java-type>

因此,所有属性都被编组到ListWrapperElement / ListElement / @属性。 其中2个丢失,未通过验证检测到。

1 个答案:

答案 0 :(得分:1)

我无法重现您所看到的问题。以下是我的尝试(改编自以下博客文章):

MarshalDemo(改编自博客文章)

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.eclipse.persistence.Version;

public class MarshalDemo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("src/blog/jaxb/validation/customer.xsd"));

        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        System.out.println(jc.getClass());
        System.out.println(Version.getVersion());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setSchema(schema);
        marshaller.setEventHandler(new MyValidationEventHandler());
        XMLStreamWriter xsw = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
        marshaller.marshal(customer, xsw);
    }

}

<强>输出

class org.eclipse.persistence.jaxb.JAXBContext
2.3.0

EVENT
SEVERITY:  1
MESSAGE:  cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringWithMaxSize5'.
LINKED EXCEPTION:  org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringWithMaxSize5'.
LOCATOR
    LINE NUMBER:  -1
    COLUMN NUMBER:  -1
    OFFSET:  -1
    OBJECT:  forum8924293.Customer@ef2c60
    NODE:  null
    URL:  null

EVENT
SEVERITY:  1
MESSAGE:  cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
LINKED EXCEPTION:  org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
LOCATOR
    LINE NUMBER:  -1
    COLUMN NUMBER:  -1
    OFFSET:  -1
    OBJECT:  forum8924293.Customer@ef2c60
    NODE:  null
    URL:  null

EVENT
SEVERITY:  1
MESSAGE:  cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
LINKED EXCEPTION:  org.eclipse.persistence.oxm.record.ValidatingMarshalRecord$MarshalSAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
LOCATOR
    LINE NUMBER:  -1
    COLUMN NUMBER:  -1
    OFFSET:  -1
    OBJECT:  forum8924293.Customer@ef2c60
    NODE:  null
    URL:  null
<?xml version="1.0"?><customer><name>Jane Doe</name><phone-number></phone-number><phone-number></phone-number><phone-number></phone-number></customer>