001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: XMLErrorHandler.java,v 1.1 2001/08/20 14:57:20 jstrachan Exp $ 008 */ 009 010package org.dom4j.util; 011 012import org.dom4j.DocumentHelper; 013import org.dom4j.Element; 014import org.dom4j.QName; 015 016import org.xml.sax.ErrorHandler; 017import org.xml.sax.SAXParseException; 018 019/** <code>XMLErrorHandler</code> is a SAX {@link ErrorHandler} which 020 * turns the SAX parsing errors into XML so that the output can be formatted 021 * using XSLT or the errors can be included in a SOAP message. 022 * 023 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 024 * @version $Revision: 1.1 $ 025 */ 026public class XMLErrorHandler implements ErrorHandler { 027 028 protected static final QName ERROR_QNAME = QName.get( "error" ); 029 protected static final QName FATALERROR_QNAME = QName.get( "fatalError" ); 030 protected static final QName WARNING_QNAME = QName.get( "warning" ); 031 032 /** Stores the errors that occur during a SAX parse */ 033 private Element errors; 034 035 /** QName used for error elements */ 036 private QName errorQName = ERROR_QNAME; 037 /** QName used for fatalerror elements */ 038 private QName fatalErrorQName = FATALERROR_QNAME; 039 /** QName used for warning elements */ 040 private QName warningQName = WARNING_QNAME; 041 042 043 public XMLErrorHandler() { 044 this.errors = DocumentHelper.createElement( "errors" ); 045 } 046 047 public XMLErrorHandler(Element errors) { 048 this.errors = errors; 049 } 050 051 public void error(SAXParseException e) { 052 Element element = errors.addElement( errorQName ); 053 addException( element, e ); 054 } 055 056 public void fatalError(SAXParseException e) { 057 Element element = errors.addElement( fatalErrorQName ); 058 addException( element, e ); 059 } 060 061 public void warning(SAXParseException e) { 062 Element element = errors.addElement( warningQName ); 063 addException( element, e ); 064 } 065 066 // Properties 067 //------------------------------------------------------------------------- 068 public Element getErrors() { 069 return errors; 070 } 071 072 public void setErrors(Element errors) { 073 this.errors = errors; 074 } 075 076 // Allow the QNames used to create subelements to be changed 077 078 public QName getErrorQName() { 079 return errorQName; 080 } 081 082 public void setErrorQName(QName errorQName) { 083 this.errorQName = errorQName; 084 } 085 086 public QName getFatalErrorQName() { 087 return fatalErrorQName; 088 } 089 090 public void setFatalErrorQName(QName fatalErrorQName) { 091 this.fatalErrorQName = fatalErrorQName; 092 } 093 094 public QName getWarningQName() { 095 return warningQName; 096 } 097 098 public void setWarningQName(QName warningQName) { 099 this.warningQName = warningQName; 100 } 101 102 // Implementation methods 103 //------------------------------------------------------------------------- 104 105 /** Adds the given parse exception information to the given element instance */ 106 protected void addException(Element element, SAXParseException e) { 107 element.addAttribute( "column", Integer.toString( e.getColumnNumber() ) ); 108 element.addAttribute( "line", Integer.toString( e.getLineNumber() ) ); 109 110 String publicID = e.getPublicId(); 111 if ( publicID != null && publicID.length() > 0 ) { 112 element.addAttribute( "publicID", publicID ); 113 } 114 String systemID = e.getSystemId(); 115 if ( systemID != null && systemID.length() > 0 ) { 116 element.addAttribute( "systemID", systemID ); 117 } 118 119 element.addText( e.getMessage() ); 120 } 121} 122 123 124 125/* 126 * Redistribution and use of this software and associated documentation 127 * ("Software"), with or without modification, are permitted provided 128 * that the following conditions are met: 129 * 130 * 1. Redistributions of source code must retain copyright 131 * statements and notices. Redistributions must also contain a 132 * copy of this document. 133 * 134 * 2. Redistributions in binary form must reproduce the 135 * above copyright notice, this list of conditions and the 136 * following disclaimer in the documentation and/or other 137 * materials provided with the distribution. 138 * 139 * 3. The name "DOM4J" must not be used to endorse or promote 140 * products derived from this Software without prior written 141 * permission of MetaStuff, Ltd. For written permission, 142 * please contact dom4j-info@metastuff.com. 143 * 144 * 4. Products derived from this Software may not be called "DOM4J" 145 * nor may "DOM4J" appear in their names without prior written 146 * permission of MetaStuff, Ltd. DOM4J is a registered 147 * trademark of MetaStuff, Ltd. 148 * 149 * 5. Due credit should be given to the DOM4J Project 150 * (http://dom4j.org/). 151 * 152 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 153 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 154 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 155 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 156 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 157 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 158 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 159 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 160 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 161 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 162 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 163 * OF THE POSSIBILITY OF SUCH DAMAGE. 164 * 165 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 166 * 167 * $Id: XMLErrorHandler.java,v 1.1 2001/08/20 14:57:20 jstrachan Exp $ 168 */