1 16 19 package com.sun.org.apache.xml.internal.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 com.sun.org.apache.xml.internal.res.XMLErrorResources; 29 import com.sun.org.apache.xml.internal.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 printLocation(m_pw, exception); 199 m_pw.println(exception.getMessage()); 200 201 } 203 204 224 public void fatalError(TransformerException exception) throws TransformerException 225 { 226 229 throw exception; 230 } 231 232 public static void ensureLocationSet(TransformerException exception) 233 { 234 SourceLocator locator = null; 236 Throwable cause = exception; 237 238 do 240 { 241 if(cause instanceof SAXParseException ) 242 { 243 locator = new SAXSourceLocator((SAXParseException )cause); 244 } 245 else if (cause instanceof TransformerException ) 246 { 247 SourceLocator causeLocator = ((TransformerException )cause).getLocator(); 248 if(null != causeLocator) 249 locator = causeLocator; 250 } 251 252 if(cause instanceof TransformerException ) 253 cause = ((TransformerException )cause).getCause(); 254 else if(cause instanceof SAXException ) 255 cause = ((SAXException )cause).getException(); 256 else 257 cause = null; 258 } 259 while(null != cause); 260 261 exception.setLocator(locator); 262 } 263 264 public static void printLocation(PrintStream pw, TransformerException exception) 265 { 266 printLocation(new PrintWriter (pw), exception); 267 } 268 269 public static void printLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception) 270 { 271 printLocation(new PrintWriter (pw), exception); 272 } 273 274 public static void printLocation(PrintWriter pw, Throwable exception) 275 { 276 SourceLocator locator = null; 277 Throwable cause = exception; 278 279 do 281 { 282 if(cause instanceof SAXParseException ) 283 { 284 locator = new SAXSourceLocator((SAXParseException )cause); 285 } 286 else if (cause instanceof TransformerException ) 287 { 288 SourceLocator causeLocator = ((TransformerException )cause).getLocator(); 289 if(null != causeLocator) 290 locator = causeLocator; 291 } 292 if(cause instanceof TransformerException ) 293 cause = ((TransformerException )cause).getCause(); 294 else if(cause instanceof WrappedRuntimeException) 295 cause = ((WrappedRuntimeException)cause).getException(); 296 else if(cause instanceof SAXException ) 297 cause = ((SAXException )cause).getException(); 298 else 299 cause = null; 300 } 301 while(null != cause); 302 303 if(null != locator) 304 { 305 String id = (null != locator.getPublicId() ) 307 ? locator.getPublicId() 308 : (null != locator.getSystemId()) 309 ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); 311 pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber() 312 + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; "); 313 } 314 else 315 pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")"); 316 } 317 } 318 | Popular Tags |