1 22 package org.jboss.util.xml; 23 24 import javax.xml.transform.ErrorListener ; 25 import javax.xml.transform.TransformerException ; 26 27 import org.jboss.logging.Logger; 28 import org.jboss.util.JBossStringBuilder; 29 import org.xml.sax.ErrorHandler ; 30 import org.xml.sax.SAXParseException ; 31 32 public class JBossErrorHandler implements ErrorHandler , ErrorListener 33 { 34 private static final Logger log =Logger.getLogger(JBossErrorHandler.class); 35 36 private String fileName; 38 private JBossEntityResolver resolver; 39 private boolean error; 40 41 public JBossErrorHandler(String fileName, JBossEntityResolver resolver) 42 { 43 this.fileName = fileName; 44 this.resolver = resolver; 45 this.error = false; 46 } 47 48 public void error(SAXParseException e) 49 { 50 if (resolver == null || resolver.isEntityResolved()) 51 { 52 error = true; 53 log.error(formatError("error", e)); 54 } 55 } 56 57 public void fatalError(SAXParseException e) 58 { 59 if (resolver == null || resolver.isEntityResolved()) 60 { 61 error = true; 62 log.error(formatError("fatal", e)); 63 } 64 } 65 66 public void warning(SAXParseException e) 67 { 68 if (resolver == null || resolver.isEntityResolved()) 69 { 70 error = true; 71 log.error(formatError("warning", e)); 72 } 73 } 74 75 public void error(TransformerException e) 76 { 77 if (resolver == null || resolver.isEntityResolved()) 78 { 79 error = true; 80 log.error(formatError("error", e)); 81 } 82 } 83 84 public void fatalError(TransformerException e) 85 { 86 if (resolver == null || resolver.isEntityResolved()) 87 { 88 error = true; 89 log.error(formatError("fatal", e)); 90 } 91 } 92 93 public void warning(TransformerException e) 94 { 95 if (resolver == null || resolver.isEntityResolved()) 96 { 97 error = true; 98 log.error(formatError("warning", e)); 99 } 100 } 101 102 protected String formatError(String context, SAXParseException e) 103 { 104 JBossStringBuilder buffer = new JBossStringBuilder(); 105 buffer.append("File ").append(fileName); 106 buffer.append(" process ").append(context); 107 buffer.append(". Line: ").append(e.getLineNumber()); 108 buffer.append(". Error message: ").append(e.getMessage()); 109 return buffer.toString(); 110 } 111 112 protected String formatError(String context, TransformerException e) 113 { 114 JBossStringBuilder buffer = new JBossStringBuilder(); 115 buffer.append("File ").append(fileName); 116 buffer.append(" process ").append(context); 117 buffer.append(". Location: ").append(e.getLocationAsString()); 118 buffer.append(". Error message: ").append(e.getMessage()); 119 return buffer.toString(); 120 } 121 122 public boolean hadError() 123 { 124 return error; 125 } 126 } 127 | Popular Tags |