/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.xml; import java.io.File; import java.io.FileReader; import java.io.PrintWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.xalan.xslt.EnvironmentCheck; import org.w3c.dom.Document; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; public class TestSchema { private File testFile; public boolean validate() { System.out.println("Article location:" + testFile.getAbsolutePath()); try { boolean environmentOK = (new EnvironmentCheck()).checkEnvironment (new PrintWriter(System.out)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema" ); String[] schemaSources = {"file:///Users/jb/pegadi/trunk/xml/schema/pegadi.xsd", "file:///Users/jb/pegadi/trunk/xml/schema/ud1.xsd"}; factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSources); DocumentBuilder builder = factory.newDocumentBuilder(); InternalErrorHandler handler =new InternalErrorHandler(); builder.setErrorHandler(handler); FileReader in = new FileReader(testFile); Document myDom = builder.parse(new InputSource(in)); if (handler.isValidationError() ) { System.out.println("Error valiating article."); System.out.println("Article location:" + testFile.getAbsolutePath()); return false; } System.out.println("Namespace URI of Document is:" + myDom.getNamespaceURI()); System.out.println("Namespace URI of DocumentElement is: " + myDom.getDocumentElement().getNamespaceURI()); return true; } catch (Exception e) { System.out.print("ERROR: " + e.getMessage()); return false; } } public void setTestFilename(String filename) { this.testFile = new File(filename); if (!testFile.exists()) { System.out.println("ERROR: file does not exist."); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestSchema myTest = new TestSchema(); if (args.length == 0) { myTest.setTestFilename("test/pegadi2images.xml"); // run relative to trunk/xml } else { myTest.setTestFilename(args[0]); } if (myTest.validate()) System.out.println("SUCCESS!!!!"); } private class InternalErrorHandler extends DefaultHandler { private SAXParseException saxe; private boolean validationError = false; /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException) */ public void error(SAXParseException arg0) throws SAXException { System.out.println("Error validating XML: " + arg0.getMessage()); printInfo(arg0); this.saxe = arg0; setValidationError(true); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#fatalError(org.xml.sax.SAXParseException) */ public void fatalError(SAXParseException arg0) throws SAXException { System.out.println("Fatal error validating XML: "+ arg0.getMessage()); printInfo(arg0); this.saxe = arg0; setValidationError(true); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#warning(org.xml.sax.SAXParseException) */ public void warning(SAXParseException arg0) throws SAXException { System.out.println("Warning when validating article:"+ arg0.getMessage() ); printInfo(arg0); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { System.out.println("Start element: " + arg0); super.startElement(arg0, arg1, arg2, arg3); } /** * @return Returns the validationError. */ public boolean isValidationError() { return validationError; } /** * @param validationError The validationError to set. */ public void setValidationError(boolean validationError) { this.validationError = validationError; } private void printInfo(SAXParseException e) { System.out.println(" Public ID: "+e.getPublicId()); System.out.println(" System ID: "+e.getSystemId()); System.out.println(" Line number: "+e.getLineNumber()); System.out.println(" Column number: "+e.getColumnNumber()); } } }