From version 0.9.14.0 of JCAM a new interface based on the JAXP API was introduced. The specific part of the JAXP APi implemented is the javax.xml.validation api. This involves using the SchemaFactory, Schema and Validator abstract classes.
... public String[] validate(String templateFileName, String XmlFileName, String[] params) throws Exception{ SchemaFactory sf = new SchemaFactoryImpl(); // instantiate a factory Schema cam = sf.newSchema(new File(templateFileName)); // associate a CAM template with a Schema object Validator validator = cam.newValidator(); // get the Validator for (int i = 0; i < params.length;i++) // Set any parameters validator.setProperty("param", params[i]); Document xml = new DocumentFactory().createDocument(XmlFileName); // read in teh XML file JDOMSource source = new JDOMSource(xml); //Wrap the JDOM in a JDOMSource JDOMResult result = new JDOMResult(); // Create the output result object validator.validate(source, result); // validate return ((ValidatorImpl)validator).getErrors(); //check and return errors. } ...
JCam JAXP API provides three implementation classes - SchemaFactoryImpl, SchemaImpl, and ValidatorImpl. The API uses the validator.setProperty with the property called "param" to set parameters. More than one parameter can be set by repeatedly calling the setproperty with the "param" name. The API also uses the SetFeature to change the behaviour of the validator. The options are 'debug','verbose','trace' and 'eTrace'. A boolean value is supplied to swicth on or off the option.
The ValidatorImpl API slightly extends the JAXP API to allow a set of errors to be extracted from the resulting file.
The second example is similar to the first except it takes imput using javax.xml.transform.Source instances.
... public String[] validate(Source template, Source Xml, String[] params) throws Exception{ ... } ...
This last example shows how to use the ErrorHandler to allow errors to be handled outside of the normal flow of control.
... // A very basic ErrorHandler public class ErrorHandlerTest implements ErrorHandler { public void error(SAXParseException e) throws SAXException { error.add(e.getMessage()); } public void fatalError(SAXParseException e) throws SAXException { error.add(e.getMessage()); } public void warning(SAXParseException e) throws SAXException { error.add(e.getMessage()); } } public List<String> error = new ArrayList<String>(); //Example showing validation with only an error handler public void validateWithErrorHandler(Source template, Source Xml, String[] params) throws Exception{ SchemaFactory sf = new SchemaFactoryImpl(); Schema cam = sf.newSchema(template); Validator validator = cam.newValidator(); for (int i = 0; i < params.length;i++) validator.setProperty("param", params[i]); Document xml = new DocumentFactory().createDocument(Xml); JDOMSource source = new JDOMSource(xml); validator.setErrorHandler(new ErrorHandlerTest()); validator.validate(source); } public boolean hasErrors(){ return error.size() > 0; } ...
A simple call to the hasErrors() method after the validation has taken place will show if the XML file is valid.
JCAM using JDOM. This means that there are some operations that can only work is the Source and Result are of the JDOM variety. The Validator and Schema can handle SAXSource objects.