1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.PrintStream ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.XMLReader ; 34 35 36 37 class XMLReaderFactory 38 { 39 static XMLReader newInstance(PrintStream out) throws ParserConfigurationException , SAXException { 40 SAXParserFactory spf = SAXParserFactory.newInstance(); 41 spf.setNamespaceAware(true); 42 spf.setValidating(false); 43 SAXParser saxParser = spf.newSAXParser(); 44 XMLReader r= saxParser.getXMLReader(); 45 r.setErrorHandler(new MyErrorHandler(out)); 46 return r; 47 } 48 49 50 private static class MyErrorHandler implements ErrorHandler 51 { 52 private PrintStream out; 53 MyErrorHandler(PrintStream out) { 54 this.out = out; 55 } 56 57 60 private String getParseExceptionInfo(SAXParseException spe) { 61 String systemId = spe.getSystemId(); 62 if (systemId == null) { 63 systemId = "null"; 64 } 65 String info = "URI=" + systemId + 66 " Line=" + spe.getLineNumber() + 67 ": " + spe.getMessage(); 68 return info; 69 } 70 71 74 public void warning(SAXParseException spe) throws SAXException { 75 out.println("Warning: " + getParseExceptionInfo(spe)); 76 } 77 78 public void error(SAXParseException spe) throws SAXException { 79 String message = "Error: " + getParseExceptionInfo(spe); 80 out.println(message); 81 } 83 84 public void fatalError(SAXParseException spe) throws SAXException { 85 String message = "Fatal Error: " + getParseExceptionInfo(spe); 86 throw new SAXException (message); 87 } 88 } 89 90 91 } 92 | Popular Tags |