[jboss-dev-forums] [Design of JBossXB] - Re: How to force strict validation?

rob.stryker@jboss.com do-not-reply at jboss.com
Thu Feb 28 20:43:43 EST 2008


Thanks for the replies:

Just setting validation and schema validation to true did not accomplish what I needed it to do. I still was unable to access the parser to set the properties needed. I ended up cloning UnmarshallerImpl and making the getParser() method public (rather than package private) to do what I needed it to.


  | public class ParseTest extends TestCase {
  | 	private static final String SCHEMA = "/home/rob/apps/eclipse/workspaces/work/garbage/src/schema.xsd";
  | 	private static final String DATA = "/home/rob/apps/eclipse/workspaces/work/garbage/data/test.xml";
  | 	public void testABC() {
  | 		try {
  | 			File f = new File(SCHEMA);
  | 			InputStream stream = new FileInputStream(f);
  | 			SchemaBinding binding = XsdBinder.bind(stream, "UTF-8", (String)null);
  | 			stream.close();
  | 			binding.setStrictSchema(true);
  | 			UnmarshallerImpl unmarshaller = new UnmarshallerImpl();
  | 			String schemaLocation = new File("/home/rob/apps/eclipse/workspaces/work/garbage/src/schema.xsd").toURI().toURL().toExternalForm();
  | 			unmarshaller.setValidation(true);
  | 			unmarshaller.getParser().setFeature("http://apache.org/xml/features/validation/schema", true);
  | 			unmarshaller.getParser().setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
  |             schemaLocation);
  | 			FileInputStream dataStream = new FileInputStream(new File(DATA));
  | 			Object xmlObject = unmarshaller.unmarshal(dataStream, binding);
  | 			System.out.println("object is " + xmlObject);
  | 		} catch( Exception e ) {
  | 			// GOOD! I want XB to fail!
  | 			return;
  | 		}
  | 		fail("data file should not have validated!");
  | 	}
  | }
  | 


Now I'm just looking via APIs to find a similar way to enforce strict validation when writing / marshalling the files as well ;) 

Again, thanks for the help. 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4133030#4133030

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4133030



More information about the jboss-dev-forums mailing list