1 16 package org.apache.juddi.util.xml; 17 18 21 import java.io.IOException ; 22 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.SAXParseException ; 25 import org.xml.sax.XMLReader ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 import org.xml.sax.helpers.XMLReaderFactory ; 28 29 public class SchemaValidator 30 { 31 public static void main(String [] args) 32 { 33 String parserClass = "org.apache.xerces.parsers.SAXParser"; 34 String validationFeature = "http://xml.org/sax/features/validation"; 35 String schemaFeature = "http://apache.org/xml/features/validation/schema"; 36 try 37 { 38 String x = args[0]; 39 XMLReader r = XMLReaderFactory.createXMLReader(parserClass); 40 r.setFeature(validationFeature, true); 41 r.setFeature(schemaFeature, true); 42 r.setErrorHandler(new MyErrorHandler()); 43 r.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","C:/Projects/sample/xsd/dictionary.xsd"); 44 r.parse(x); 45 } 46 catch (SAXException e) 47 { 48 System.out.println(e.toString()); 49 } 50 catch (IOException e) 51 { 52 System.out.println(e.toString()); 53 } 54 } 55 private static class MyErrorHandler extends DefaultHandler 56 { 57 public void warning(SAXParseException e) throws SAXException 58 { 59 System.out.println("Warning: "); 60 printInfo(e); 61 } 62 public void error(SAXParseException e) throws SAXException 63 { 64 System.out.println("Error: "); 65 printInfo(e); 66 } 67 public void fatalError(SAXParseException e) throws SAXException 68 { 69 System.out.println("Fattal error: "); 70 printInfo(e); 71 } 72 private void printInfo(SAXParseException e) 73 { 74 System.out.println(" Public ID: " + e.getPublicId()); 75 System.out.println(" System ID: " + e.getSystemId()); 76 System.out.println(" Line number: " + e.getLineNumber()); 77 System.out.println(" Column number: " + e.getColumnNumber()); 78 System.out.println(" Message: " + e.getMessage()); 79 } 80 } 81 } 82 | Popular Tags |