1 57 58 package dom.wrappers; 59 60 import dom.DOMParserWrapper; 61 62 import org.apache.xerces.dom.TextImpl; 63 import org.apache.xerces.parsers.DOMParser; 64 65 import org.w3c.dom.Document ; 66 import org.w3c.dom.Text ; 67 68 import org.xml.sax.InputSource ; 69 import org.xml.sax.ErrorHandler ; 70 import org.xml.sax.SAXException ; 71 import org.xml.sax.SAXParseException ; 72 import org.xml.sax.SAXNotRecognizedException ; 73 import org.xml.sax.SAXNotSupportedException ; 74 75 80 public class Xerces 81 implements DOMParserWrapper, DOMParserWrapper.DocumentInfo, ErrorHandler { 82 83 87 88 protected DOMParser parser = new DOMParser(); 89 90 94 95 public Xerces() { 96 parser.setErrorHandler(this); 97 } 99 103 104 public Document parse(String uri) throws Exception { 105 parser.parse(uri); 106 return parser.getDocument(); 107 } 109 110 public Document parse(InputSource src) throws Exception { 111 parser.parse(src); 112 return parser.getDocument(); 113 } 115 116 public void setFeature(String featureId, boolean state) 117 throws SAXNotRecognizedException , SAXNotSupportedException { 118 parser.setFeature(featureId, state); 119 } 121 122 public DOMParserWrapper.DocumentInfo getDocumentInfo() { 123 return this; 124 } 126 130 133 public boolean isIgnorableWhitespace(Text text) { 134 return ((TextImpl)text).isIgnorableWhitespace(); 135 } 136 137 141 142 public void warning(SAXParseException ex) throws SAXException { 143 printError("Warning", ex); 144 } 146 147 public void error(SAXParseException ex) throws SAXException { 148 printError("Error", ex); 149 } 151 152 public void fatalError(SAXParseException ex) throws SAXException { 153 printError("Fatal Error", ex); 154 throw ex; 155 } 157 161 162 protected void printError(String type, SAXParseException ex) { 163 164 System.err.print("["); 165 System.err.print(type); 166 System.err.print("] "); 167 String systemId = ex.getSystemId(); 168 if (systemId != null) { 169 int index = systemId.lastIndexOf('/'); 170 if (index != -1) 171 systemId = systemId.substring(index + 1); 172 System.err.print(systemId); 173 } 174 System.err.print(':'); 175 System.err.print(ex.getLineNumber()); 176 System.err.print(':'); 177 System.err.print(ex.getColumnNumber()); 178 System.err.print(": "); 179 System.err.print(ex.getMessage()); 180 System.err.println(); 181 System.err.flush(); 182 183 } 185 } | Popular Tags |