1

我希望我的 Eclipselink 2.3 Marshaller 在编组时执行验证。我已确保Schema由 a 正确创建SchemaFactory,我将其传递给Marshaller.setSchema并且我已通过Marshaller.setEventHandler().

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

我正在编组 XML 片段marshal(Object, XMLStreamWriter),并希望编组器根据我通过的模式对这些片段执行验证。

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

编辑

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

该元素对应于 List<> 中包含的 Java-Object。我正在使用以下方法编组列表:

<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/@attribute。其中 2 个缺失且未被验证检测到。

4

1 回答 1

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>
于 2012-01-19T10:45:47.053 回答