Hi,
I want to use ValidationEventCollector for reporting validation errors.
I build a little demo of my problem. Here is the xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="
http://www.lstelcom.com/Schema/demo1" xmlns="
http://www.lstelcom.com/Schema/demo1" xmlns:xs="
http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="A_ROOT">
<xs:complexType>
<xs:sequence>
<xs:element ref="ELEMENT_1" minOccurs="0"/>
<xs:element ref="ELEMENT_2" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ELEMENT_2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ELEMENT_1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
I create a xml object from the scratch (no marshal/unmarshal) and validate
it (see below).
When the string-length constrain of <ELEMENT_1> or <ELEMENT_2> violated,
the ValidationEventCollector report that errors correct.
But the getLocator().getObject() points to AROOTImpl.
In bigger structures always the (complex) parent is returned as validation
error source.
Why that? The constraints are defined on the ELEMENT_x tags and not on its
parent <AROOT>.
Is there something I do completly wrong?
Thanks,
Michael
package de.lsgermany.Demo;
import javax.xml.bind.*;
import de.lsgermany.Demo.xml.*;
import javax.xml.bind.util.*;
public class Main
{
public Main()
{
}
public static void main(String[] args)
throws Exception
{
JAXBContext jc = JAXBContext.newInstance( "de.lsgermany.Demo.xml"
);
ObjectFactory factory = new ObjectFactory();
AROOT root = factory.createAROOT();
root.setELEMENT1("too long");
root.setELEMENT2("too long, too");
ValidationEventCollector validationEventCollector = new
ValidationEventCollector();
Validator validator = jc.createValidator();
validator.setEventHandler( validationEventCollector );
validator.validateRoot(root);
//--- show validation errors
ValidationEvent[] events = validationEventCollector.getEvents();
for (int i=0; i<events.length; i++)
{
System.out.print("Error at ");
System.out.print(
events[i].getLocator().getObject().getClass().getName() );
System.out.print(": ");
System.out.println( events[i].getMessage() );
}
}
}
GIVES OUTPUT:
Error at de.lsgermany.Demo.xml.impl.AROOTImpl: the length of the value is
8, but the required maximum is 1.
Error at de.lsgermany.Demo.xml.impl.AROOTImpl: the length of the value is
13, but the required maximum is 2.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net