Schema API

Does anyone know a good XML Schema API?

I had a quick look at W3 XML Schema API Web pages, also XERCES/PSVI to see 
if it could offer the sort of features I was after, and I have some questions:

How would I load a just a schema (no instance) to create some schema 
objects in Java?

How do I serialize parts of the schema (for example one type def) to create 
a self contained schema declaration?  This is problematic when a type 
definition refers to another root level type/element definition elsewhere 
in the document, or in another document.

If I have a schema object how can I validate instances against it?

Out of interest I developed a program in Java that uses Xerces to validate 
a schema against the schema specification and it gives line number reports 
etc.  It's a bit of a hack.  It takes schemas as strings, which suits my 
purposes, and if they are valid returns an empty string.    Is there a 
cleaner way of doing that too?



import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.jdom.input.SAXBuilder;

import java.io.StringReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;


public class SchemaValidator extends DefaultHandler {
     StringBuffer _errorsString = new StringBuffer("");
     private static SchemaValidator _myInstance;
     private SAXBuilder _builder;
     private File _tempSchema;

     /**
      * If the string is of 0 length then you know it has passed the test.
      * @return the validation failures as a string.
      */
     public String validateSchema(String schema) {
         File userDir = new File(System.getProperty("user.dir"));
         _tempSchema = new File(userDir, "_tempSchema.xsd");
         try {
             FileWriter writer = new FileWriter(_tempSchema);
             writer.write(schema);
             writer.flush();
             writer.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
         String instance =
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                 "<document 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                 "</document>";
         String result = checkSchema(new InputSource(new 
StringReader(instance)));
         _tempSchema.delete();
         return result;
     }

     public static SchemaValidator getInstance() {
         if (_myInstance == null) {
             _myInstance = new SchemaValidator();
         }
         return _myInstance;
     }

     private SchemaValidator() {
         _builder = new SAXBuilder();
         _builder.setValidation(false);
     }


     public void warning(SAXParseException ex) {
         addMessage(ex, "Warning");
     }


     public void error(SAXParseException ex) {
         addMessage(ex, "Invalid");
     }


     public void fatalError(SAXParseException ex) throws SAXException {
         addMessage(ex, "Error");
     }


     private void addMessage(SAXParseException e, String errType) {
         String lineNum = getLineNumber(e);
         if (lineNum != null) {
             _errorsString.append(errType + "#" + lineNum + "# " + 
e.getMessage() + '\n');
         }
     }


     private String getLineNumber(SAXParseException e) {
         String fileURL = e.getSystemId();
         if (fileURL != null) {
             return
                     "[ln: " + e.getLineNumber() + " col: " + 
e.getColumnNumber() + "]";
         }
         return null;
     }


     private String checkSchema(InputSource input) {
         _errorsString.delete(0, _errorsString.length());
         try {
             XMLReader parser = setUpChecker();
             parser.parse(input);
         } catch (SAXParseException e) {
         } catch (Exception e) {
             e.printStackTrace();
         }
         return _errorsString.toString();
     }


     private XMLReader setUpChecker() throws SAXException {
         XMLReader parser = 
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
         parser.setContentHandler(this);
         parser.setErrorHandler(this);
         parser.setFeature("http://xml.org/sax/features/validation", true);
         parser.setFeature("http://apache.org/xml/features/validation/schema", 
true);
         parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", 
true);
         try {
             URL schemaURL = _tempSchema.toURL();
             parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
                 "" + schemaURL);
         } catch (MalformedURLException e) {
             e.printStackTrace();
         }
         return parser;
     }
}



Regards,

Lachlan



Lachlan Aldred
Queensland University of Technology
Australia

Received on Sunday, 20 June 2004 22:46:29 UTC