View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    */
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  /***
18   * <code>XMLErrorHandler</code> is a SAX {@link ErrorHandler}which turns the
19   * SAX parsing errors into XML so that the output can be formatted using XSLT or
20   * the errors can be included in a SOAP message.
21   * 
22   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
23   * @version $Revision: 1.7 $
24   */
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      /*** Stores the errors that occur during a SAX parse */
33      private Element errors;
34  
35      /*** QName used for error elements */
36      private QName errorQName = ERROR_QNAME;
37  
38      /*** QName used for fatalerror elements */
39      private QName fatalErrorQName = FATALERROR_QNAME;
40  
41      /*** QName used for warning elements */
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      // Properties
68      // -------------------------------------------------------------------------
69      public Element getErrors() {
70          return errors;
71      }
72  
73      public void setErrors(Element errors) {
74          this.errors = errors;
75      }
76  
77      // Allow the QNames used to create subelements to be changed
78      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     // Implementation methods
103     // -------------------------------------------------------------------------
104 
105     /***
106      * Adds the given parse exception information to the given element instance
107      * 
108      * @param element
109      *            DOCUMENT ME!
110      * @param e
111      *            DOCUMENT ME!
112      */
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 /*
134  * Redistribution and use of this software and associated documentation
135  * ("Software"), with or without modification, are permitted provided that the
136  * following conditions are met:
137  * 
138  * 1. Redistributions of source code must retain copyright statements and
139  * notices. Redistributions must also contain a copy of this document.
140  * 
141  * 2. Redistributions in binary form must reproduce the above copyright notice,
142  * this list of conditions and the following disclaimer in the documentation
143  * and/or other materials provided with the distribution.
144  * 
145  * 3. The name "DOM4J" must not be used to endorse or promote products derived
146  * from this Software without prior written permission of MetaStuff, Ltd. For
147  * written permission, please contact dom4j-info@metastuff.com.
148  * 
149  * 4. Products derived from this Software may not be called "DOM4J" nor may
150  * "DOM4J" appear in their names without prior written permission of MetaStuff,
151  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
152  * 
153  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
154  * 
155  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
156  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
157  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
158  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
159  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
160  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
161  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
162  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
163  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
164  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
165  * POSSIBILITY OF SUCH DAMAGE.
166  * 
167  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
168  */