[jboss-dev-forums] [Design of JBossXB] - Re: How to force strict validation?
alex.loubyansky@jboss.com
do-not-reply at jboss.com
Fri Feb 29 09:41:53 EST 2008
W/o changing APIs you could do something like this:
final String xsd =
| "<xsd:schema" +
| " xmlns:jbxb='http://www.jboss.org/xml/ns/jbxb'" +
| " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
| " <xsd:element name='root' type='root-type'/>" +
| " <xsd:complexType name='root-type'>" +
| " <xsd:annotation>" +
| " <xsd:appinfo>" +
| " <jbxb:class impl='" + E.class.getName() + "'/>" +
| " </xsd:appinfo>" +
| " </xsd:annotation>" +
| " <xsd:attribute name='unqualified' type='xsd:string' use='required'/>" +
| " </xsd:complexType>" +
| "</xsd:schema>";
|
| String xml = "<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='testns'/>";
|
| org.xml.sax.EntityResolver resolver = new org.xml.sax.EntityResolver()
| {
| public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
| {
| if(systemId != null && systemId.endsWith("testns"))
| return new org.xml.sax.InputSource(new StringReader(xsd));
| return null;
| }
| };
|
| /*
| Validator.assertValidXml(xml, new org.xml.sax.EntityResolver()
| {
| public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
| {
| if(systemId != null && systemId.endsWith("testns"))
| return new org.xml.sax.InputSource(new StringReader(xsd));
| return null;
| }
| });
| */
|
| SchemaBinding schema = XsdBinder.bind(new StringReader(xsd), null);
| Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
| unmarshaller.setEntityResolver(resolver);
| unmarshaller.setSchemaValidation(true);
|
| Object o = unmarshaller.unmarshal(new StringReader(xml), schema);
| assertTrue(o instanceof E);
This will throw the exception you expect. The difference is that I had to add noNamespaceSchemaLocation into the xml instead of setting it on the parser.
It might still be useful to expose setProperty() or the parser itself though.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4133243#4133243
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4133243
More information about the jboss-dev-forums
mailing list