1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.io.Writer ; 29 import org.iso_relax.verifier.Verifier; 30 import org.iso_relax.verifier.VerifierConfigurationException; 31 import org.iso_relax.verifier.VerifierFactory; 32 import org.xml.sax.ErrorHandler ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 import org.xml.sax.XMLReader ; 37 import java.io.InputStream ; 38 import org.xml.sax.EntityResolver ; 39 40 import java.io.OutputStreamWriter ; 41 42 43 44 class RNGValidator 45 { 46 void validate(InputSource schema, InputSource src, Writer w) throws VerifierConfigurationException, SAXException , IOException { 47 validate(schema, src, new MyErrorHandler(w)); 48 } 49 50 void validate(InputSource schema, InputSource src, XMLReader rdr, Writer w) throws VerifierConfigurationException, SAXException , IOException { 51 validate(schema, src, rdr, new MyErrorHandler(w) ); 52 } 53 54 55 void validate(InputSource schema, InputSource src, ErrorHandler eh) throws VerifierConfigurationException, SAXException , IOException { 56 getVerifier(schema, eh).verify(src); 57 } 58 59 75 void validate(InputSource schema, InputSource src, XMLReader rdr, ErrorHandler errorHandler) throws VerifierConfigurationException, SAXException , IOException { 76 rdr.setContentHandler(getVerifier(schema, rdr.getEntityResolver(), errorHandler).getVerifierHandler()); 77 rdr.parse(src); 78 } 79 80 private Verifier getVerifier(InputSource schema, ErrorHandler eh) 81 throws VerifierConfigurationException, SAXException , IOException { 82 return getVerifier(schema, null, eh); 83 } 84 private Verifier getVerifier(InputSource schema, EntityResolver er, ErrorHandler eh) 85 throws VerifierConfigurationException, SAXException , IOException { 86 final VerifierFactory f = new com.sun.msv.verifier.jarv.TheFactoryImpl(); 92 final Verifier verifier = f.newVerifier(schema); 93 verifier.setErrorHandler(eh); 94 verifier.setEntityResolver(er); 95 return verifier; 96 } 97 98 99 private static class MyErrorHandler implements ErrorHandler 100 { 101 private PrintWriter out; 102 MyErrorHandler(Writer out) { 103 this.out = new PrintWriter (out); 104 } 105 106 109 private String getParseExceptionInfo(SAXParseException spe) { 110 String systemId = spe.getSystemId(); 111 if (systemId == null) { 112 systemId = "null"; 113 } 114 String info = "URI=" + systemId + 115 " Line=" + spe.getLineNumber() + 116 ": " + spe.getMessage(); 117 return info; 118 } 119 120 123 public void warning(SAXParseException spe) throws SAXException { 124 out.println("Warning: " + getParseExceptionInfo(spe)); 125 } 126 127 public void error(SAXParseException spe) throws SAXException { 128 String message = "Error: " + getParseExceptionInfo(spe); 129 out.println(message); 130 } 131 132 public void fatalError(SAXParseException spe) throws SAXException { 133 String message = "Fatal Error: " + getParseExceptionInfo(spe); 134 throw new SAXException (message); 135 } 136 } 137 138 139 } 140 | Popular Tags |