View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: XMLErrorHandler.java,v 1.5 2004/06/25 08:03:42 maartenc Exp $
8    */
9   
10  package org.dom4j.util;
11  
12  import org.dom4j.DocumentHelper;
13  import org.dom4j.Element;
14  import org.dom4j.QName;
15  import org.xml.sax.ErrorHandler;
16  import org.xml.sax.SAXParseException;
17  
18  /*** <code>XMLErrorHandler</code> is a SAX {@link ErrorHandler} which 
19    * turns the SAX parsing errors into XML so that the output can be formatted
20    * using XSLT or 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.5 $
24    */
25  public class XMLErrorHandler implements ErrorHandler {
26       
27      protected static final QName ERROR_QNAME = QName.get( "error" );
28      protected static final QName FATALERROR_QNAME = QName.get( "fatalError" );
29      protected static final QName WARNING_QNAME = QName.get( "warning" );
30      
31      /*** Stores the errors that occur during a SAX parse */
32      private Element errors;
33  
34      /*** QName used for error elements */
35      private QName errorQName = ERROR_QNAME;
36      /*** QName used for fatalerror elements */
37      private QName fatalErrorQName = FATALERROR_QNAME;
38      /*** QName used for warning elements */
39      private QName warningQName = WARNING_QNAME;
40  
41      
42      public XMLErrorHandler() {
43          this.errors = DocumentHelper.createElement( "errors" );
44      }
45  
46      public XMLErrorHandler(Element errors) {
47          this.errors = errors;
48      }
49  
50      public void error(SAXParseException e) {
51          Element element = errors.addElement( errorQName );
52          addException( element, e );
53      }
54  
55      public void fatalError(SAXParseException e) {
56          Element element = errors.addElement( fatalErrorQName );
57          addException( element, e );
58      }
59  
60      public void warning(SAXParseException e) {
61          Element element = errors.addElement( warningQName );
62          addException( element, e );
63      }
64  
65      // Properties
66      //-------------------------------------------------------------------------
67      public Element getErrors() {
68          return errors;
69      }
70      
71      public void setErrors(Element errors) {
72          this.errors = errors;
73      }
74  
75      // Allow the QNames used to create subelements to be changed
76      
77      public QName getErrorQName() {
78          return errorQName;
79      }
80  
81      public void setErrorQName(QName errorQName) {
82          this.errorQName = errorQName;
83      }
84  
85      public QName getFatalErrorQName() {
86          return fatalErrorQName;
87      }
88  
89      public void setFatalErrorQName(QName fatalErrorQName) {
90          this.fatalErrorQName = fatalErrorQName;
91      }
92  
93      public QName getWarningQName() {
94          return warningQName;
95      }
96  
97      public void setWarningQName(QName warningQName) {
98          this.warningQName = warningQName;
99      }
100 
101     // Implementation methods
102     //-------------------------------------------------------------------------
103     
104     /*** Adds the given parse exception information to the given element instance */
105     protected void addException(Element element, SAXParseException e) {
106         element.addAttribute( "column", Integer.toString( e.getColumnNumber() ) );
107         element.addAttribute( "line", Integer.toString( e.getLineNumber() ) );
108 
109         String publicID = e.getPublicId();
110         if ( publicID != null && publicID.length() > 0 ) {
111             element.addAttribute( "publicID", publicID );
112         }
113         String systemID = e.getSystemId();
114         if ( systemID != null && systemID.length() > 0 ) {
115             element.addAttribute( "systemID", systemID );
116         }
117 
118         element.addText( e.getMessage() );
119     }
120 }    
121 
122 
123 
124 /*
125  * Redistribution and use of this software and associated documentation
126  * ("Software"), with or without modification, are permitted provided
127  * that the following conditions are met:
128  *
129  * 1. Redistributions of source code must retain copyright
130  *    statements and notices.  Redistributions must also contain a
131  *    copy of this document.
132  *
133  * 2. Redistributions in binary form must reproduce the
134  *    above copyright notice, this list of conditions and the
135  *    following disclaimer in the documentation and/or other
136  *    materials provided with the distribution.
137  *
138  * 3. The name "DOM4J" must not be used to endorse or promote
139  *    products derived from this Software without prior written
140  *    permission of MetaStuff, Ltd.  For written permission,
141  *    please contact dom4j-info@metastuff.com.
142  *
143  * 4. Products derived from this Software may not be called "DOM4J"
144  *    nor may "DOM4J" appear in their names without prior written
145  *    permission of MetaStuff, Ltd. DOM4J is a registered
146  *    trademark of MetaStuff, Ltd.
147  *
148  * 5. Due credit should be given to the DOM4J Project - 
149  *    http://www.dom4j.org
150  *
151  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
152  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
153  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
154  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
155  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
156  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
157  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
158  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
159  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
160  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
161  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
162  * OF THE POSSIBILITY OF SUCH DAMAGE.
163  *
164  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
165  *
166  * $Id: XMLErrorHandler.java,v 1.5 2004/06/25 08:03:42 maartenc Exp $
167  */