2012-01-19 42 views
1

我希望我的Eclipselink 2.3 Marshaller在编组时执行验证。 我确定Schema是由SchemaFactory正确创建的,我将它传递给Marshaller.setSchema,并且我通过Marshaller.setEventHandler()注册了一个处理程序。JAXB-ElipseLink:Marshaller未验证

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

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

任何人有任何想法,为什么这不会发生?

EDIT

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

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

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

因此,所有的属性都整理到ListWrapperElement/ListElement/@属性:

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

的元素本身的映射。 其中2个缺失,未通过验证检测到。

回答

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: [email protected] 
    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: [email protected] 
    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: [email protected] 
    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> 
+0

谢谢布莱斯,我检查了你的博客文章,但无法找到任何不同的东西,我在做什么。在marshal()调用完成后,Marshaller是否执行验证?在我的具体用例2中,一个元素中缺少了必需的属性,这并不是Marshaller检测到的。 验证有任何限制吗? – quaylar

+0

@quaylar - 你的对象的XML模式的哪一部分对应于:global/anomymous,element/type?它是用XmlRootElememt注解还是用JAXBElement包装? –

+0

已更新我的帖子以提供更多详细信息! – quaylar