1 16 19 package org.apache.xml.utils; 20 21 import java.io.PrintStream ; 22 import java.io.PrintWriter ; 23 24 import javax.xml.transform.ErrorListener ; 25 import javax.xml.transform.SourceLocator ; 26 import javax.xml.transform.TransformerException ; 27 28 import org.apache.xml.res.XMLErrorResources; 29 import org.apache.xml.res.XMLMessages; 30 31 import org.xml.sax.ErrorHandler ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 36 40 public class DefaultErrorHandler implements ErrorHandler , ErrorListener 41 { 42 PrintWriter m_pw; 43 44 47 public DefaultErrorHandler(PrintWriter pw) 48 { 49 m_pw = pw; 50 } 51 52 55 public DefaultErrorHandler(PrintStream pw) 56 { 57 m_pw = new PrintWriter (pw, true); 58 } 59 60 63 public DefaultErrorHandler() 64 { 65 m_pw = new PrintWriter (System.err, true); 66 } 67 68 69 85 public void warning(SAXParseException exception) throws SAXException 86 { 87 printLocation(m_pw, exception); 88 m_pw.println("Parser warning: " + exception.getMessage()); 89 } 90 91 112 public void error(SAXParseException exception) throws SAXException 113 { 114 117 throw exception; 118 } 119 120 139 public void fatalError(SAXParseException exception) throws SAXException 140 { 141 144 throw exception; 145 } 146 147 164 public void warning(TransformerException exception) throws TransformerException 165 { 166 printLocation(m_pw, exception); 167 168 m_pw.println(exception.getMessage()); 169 } 170 171 193 public void error(TransformerException exception) throws TransformerException 194 { 195 198 throw exception; 199 } 200 201 221 public void fatalError(TransformerException exception) throws TransformerException 222 { 223 226 throw exception; 227 } 228 229 public static void ensureLocationSet(TransformerException exception) 230 { 231 SourceLocator locator = null; 233 Throwable cause = exception; 234 235 do 237 { 238 if(cause instanceof SAXParseException ) 239 { 240 locator = new SAXSourceLocator((SAXParseException )cause); 241 } 242 else if (cause instanceof TransformerException ) 243 { 244 SourceLocator causeLocator = ((TransformerException )cause).getLocator(); 245 if(null != causeLocator) 246 locator = causeLocator; 247 } 248 249 if(cause instanceof TransformerException ) 250 cause = ((TransformerException )cause).getCause(); 251 else if(cause instanceof SAXException ) 252 cause = ((SAXException )cause).getException(); 253 else 254 cause = null; 255 } 256 while(null != cause); 257 258 exception.setLocator(locator); 259 } 260 261 public static void printLocation(PrintStream pw, TransformerException exception) 262 { 263 printLocation(new PrintWriter (pw), exception); 264 } 265 266 public static void printLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception) 267 { 268 printLocation(new PrintWriter (pw), exception); 269 } 270 271 public static void printLocation(PrintWriter pw, Throwable exception) 272 { 273 SourceLocator locator = null; 274 Throwable cause = exception; 275 276 do 278 { 279 if(cause instanceof SAXParseException ) 280 { 281 locator = new SAXSourceLocator((SAXParseException )cause); 282 } 283 else if (cause instanceof TransformerException ) 284 { 285 SourceLocator causeLocator = ((TransformerException )cause).getLocator(); 286 if(null != causeLocator) 287 locator = causeLocator; 288 } 289 if(cause instanceof TransformerException ) 290 cause = ((TransformerException )cause).getCause(); 291 else if(cause instanceof WrappedRuntimeException) 292 cause = ((WrappedRuntimeException)cause).getException(); 293 else if(cause instanceof SAXException ) 294 cause = ((SAXException )cause).getException(); 295 else 296 cause = null; 297 } 298 while(null != cause); 299 300 if(null != locator) 301 { 302 String id = (null != locator.getPublicId() ) 304 ? locator.getPublicId() 305 : (null != locator.getSystemId()) 306 ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); 308 pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber() 309 + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; "); 310 } 311 else 312 pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")"); 313 } 314 } 315 | Popular Tags |