1 7 8 package org.dom4j.util; 9 10 import org.dom4j.DocumentHelper; 11 import org.dom4j.Element; 12 import org.dom4j.QName; 13 14 import org.xml.sax.ErrorHandler ; 15 import org.xml.sax.SAXParseException ; 16 17 25 public class XMLErrorHandler implements ErrorHandler { 26 protected static final QName ERROR_QNAME = QName.get("error"); 27 28 protected static final QName FATALERROR_QNAME = QName.get("fatalError"); 29 30 protected static final QName WARNING_QNAME = QName.get("warning"); 31 32 33 private Element errors; 34 35 36 private QName errorQName = ERROR_QNAME; 37 38 39 private QName fatalErrorQName = FATALERROR_QNAME; 40 41 42 private QName warningQName = WARNING_QNAME; 43 44 public XMLErrorHandler() { 45 this.errors = DocumentHelper.createElement("errors"); 46 } 47 48 public XMLErrorHandler(Element errors) { 49 this.errors = errors; 50 } 51 52 public void error(SAXParseException e) { 53 Element element = errors.addElement(errorQName); 54 addException(element, e); 55 } 56 57 public void fatalError(SAXParseException e) { 58 Element element = errors.addElement(fatalErrorQName); 59 addException(element, e); 60 } 61 62 public void warning(SAXParseException e) { 63 Element element = errors.addElement(warningQName); 64 addException(element, e); 65 } 66 67 public Element getErrors() { 70 return errors; 71 } 72 73 public void setErrors(Element errors) { 74 this.errors = errors; 75 } 76 77 public QName getErrorQName() { 79 return errorQName; 80 } 81 82 public void setErrorQName(QName errorQName) { 83 this.errorQName = errorQName; 84 } 85 86 public QName getFatalErrorQName() { 87 return fatalErrorQName; 88 } 89 90 public void setFatalErrorQName(QName fatalErrorQName) { 91 this.fatalErrorQName = fatalErrorQName; 92 } 93 94 public QName getWarningQName() { 95 return warningQName; 96 } 97 98 public void setWarningQName(QName warningQName) { 99 this.warningQName = warningQName; 100 } 101 102 105 113 protected void addException(Element element, SAXParseException e) { 114 element.addAttribute("column", Integer.toString(e.getColumnNumber())); 115 element.addAttribute("line", Integer.toString(e.getLineNumber())); 116 117 String publicID = e.getPublicId(); 118 119 if ((publicID != null) && (publicID.length() > 0)) { 120 element.addAttribute("publicID", publicID); 121 } 122 123 String systemID = e.getSystemId(); 124 125 if ((systemID != null) && (systemID.length() > 0)) { 126 element.addAttribute("systemID", systemID); 127 } 128 129 element.addText(e.getMessage()); 130 } 131 } 132 133 169 | Popular Tags |