1 25 26 package org.webdocwf.util.loader; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.apache.xerces.parsers.DOMParser; 34 import org.xml.sax.ErrorHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 import org.xml.sax.helpers.DefaultHandler ; 39 40 46 public class OctopusValidator extends DefaultHandler { 47 48 49 57 public void validate(ByteArrayInputStream inStream) throws Exception 58 { 59 60 final String JAXP_SCHEMA_LANGUAGE = 61 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 62 final String W3C_XML_SCHEMA = 63 "http://www.w3.org/2001/XMLSchema"; 64 65 DOMParser parser = new DOMParser(); 66 67 try { 68 parser.setFeature("http://xml.org/sax/features/validation",true); 69 parser.setFeature("http://apache.org/xml/features/validation/schema",true); 70 parser.setErrorHandler(new OctopusErrorHandler()); 71 parser.setEntityResolver(new OctopusEntityResolver()); 72 73 parser.parse(new InputSource (inStream)); 74 75 } catch (Exception e) { 76 throw e; 77 } 78 } 79 80 86 private String getLocationString (SAXParseException ex) { 87 StringBuffer str = new StringBuffer (); 88 String systemId = ex.getSystemId(); 89 if (systemId != null) { 90 int index = systemId.lastIndexOf('/'); 91 if (index != -1) 92 systemId = systemId.substring(index + 1); 93 str.append(systemId); 94 } 95 str.append(':'); 96 str.append(ex.getLineNumber()); 97 str.append(':'); 98 str.append(ex.getColumnNumber()); 99 return str.toString(); 100 } 101 102 class OctopusErrorHandler implements ErrorHandler 103 { 104 105 public void fatalError( SAXParseException exception ) throws SAXException 107 { 108 LocationOfException.getLineNumber(exception.getLineNumber()); 110 System.err.println("[Validation : FatalError ] URI : "+LocationOfException.getFileName()); 111 System.err.println("[Validation : FatalError ] Line number : "+LocationOfException.getLineNumber(exception.getLineNumber())); 112 throw new SAXException (exception); 113 } 114 115 public void error( SAXParseException errorException ) throws SAXException 116 { 117 LocationOfException.getLineNumber(errorException.getLineNumber()); 119 System.err.println("[Validation : Error ] URI : "+LocationOfException.getFileName()); 120 System.err.println("[Validation : Error ] Line number : "+LocationOfException.getLineNumber(errorException.getLineNumber())); 121 throw new SAXException (errorException); 122 } 123 124 public void warning( SAXParseException warningError ) throws SAXException 126 { 127 System.err.println("[Validation : Warning] URI : " + getLocationString(warningError) + ": " + warningError.getMessage()); 128 } 129 } 130 131 132 } | Popular Tags |