1 57 58 package dom.wrappers; 59 60 import org.w3c.dom.Document ; 61 import org.xml.sax.ErrorHandler ; 62 import org.xml.sax.SAXException ; 63 import org.xml.sax.SAXNotRecognizedException ; 64 import org.xml.sax.SAXNotSupportedException ; 65 import org.xml.sax.SAXParseException ; 66 67 import dom.DOMParserWrapper; 68 69 74 public class DOMParser 75 implements DOMParserWrapper, ErrorHandler { 76 77 81 82 83 84 org.enhydra.apache.xerces.parsers.DOMParser parser = 85 new org.enhydra.apache.xerces.parsers.DOMParser(); 86 87 91 92 public DOMParser() { 93 try { 94 parser.setFeature( "http://xml.org/sax/features/validation", true); 95 96 } catch (SAXException e) { 97 System.out.println("error in setting up parser feature"); 98 } 99 100 parser.setErrorHandler(this); 101 102 } 103 104 108 109 public Document parse(String uri) throws Exception { 110 parser.parse(uri); 111 return parser.getDocument(); 112 113 } 115 116 public void setFeature(String featureId, boolean state) 117 throws SAXNotRecognizedException , SAXNotSupportedException { 118 parser.setFeature( featureId, state ); 119 } 120 121 122 123 127 128 public void warning(SAXParseException ex) { 129 System.err.println("[Warning] "+ 130 getLocationString(ex)+": "+ 131 ex.getMessage()); 132 } 133 134 135 public void error(SAXParseException ex) { 136 System.err.println("[Error] "+ 137 getLocationString(ex)+": "+ 138 ex.getMessage()); 139 } 140 141 142 public void fatalError(SAXParseException ex) throws SAXException { 143 System.err.println("[Fatal Error] "+ 144 getLocationString(ex)+": "+ 145 ex.getMessage()); 146 throw ex; 147 } 148 149 153 154 private String getLocationString(SAXParseException ex) { 155 StringBuffer str = new StringBuffer (); 156 157 String systemId = ex.getSystemId(); 158 if (systemId != null) { 159 int index = systemId.lastIndexOf('/'); 160 if (index != -1) 161 systemId = systemId.substring(index + 1); 162 str.append(systemId); 163 } 164 str.append(':'); 165 str.append(ex.getLineNumber()); 166 str.append(':'); 167 str.append(ex.getColumnNumber()); 168 169 return str.toString(); 170 171 } 173 } | Popular Tags |