KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xml > xdbc > XMLDBCException


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xml.xdbc;
24
25 import java.io.PrintStream JavaDoc;
26
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.SAXParseException JavaDoc;
29
30 /**
31  * This class describes a XMLDBC (Xml DataBase Connectivity) exception for
32  * various use in XMLDBC API. <BR>
33  * All other specific XMLDBC exceptions extend this exception. <BR>
34  * <BR>
35  */

36 public class XMLDBCException extends java.lang.Exception JavaDoc {
37
38     //*************************** Constants declaration:
39

40     /** Error code for exception that have no associated error code. */
41     public static final int NO_CODE = 0;
42
43     //*************************** Properties declaration:
44

45     /** The error code of this exception. */
46     protected int errorCode = NO_CODE;
47
48     /** The underlying exception (or error) of this exception. */
49     protected java.lang.Throwable JavaDoc exception;
50
51     //*************************** Constructor declaration:
52

53     /**
54      * Constructor with error code.
55      *
56      * @param code
57      * the code featuring the kind of error (see constants).
58      */

59     public XMLDBCException(int code) {
60         this(code, "", null);
61     }
62
63     /**
64      * Constructor with message.
65      *
66      * @param msg
67      * a message to describe the exception.
68      */

69     public XMLDBCException(java.lang.String JavaDoc msg) {
70         this(XMLDBCException.NO_CODE, msg, null);
71     }
72
73     /**
74      * Constructor with error code and message.
75      *
76      * @param code
77      * the code featuring the kind of error (see constants).
78      * @param msg
79      * a message to describe the exception.
80      */

81     public XMLDBCException(int code, java.lang.String JavaDoc msg) {
82         this(code, msg, null);
83     }
84
85     /**
86      * Constructor with message and underlying exception.
87      *
88      * @param msg
89      * a message to describe the exception.
90      * @param exception
91      * the underlying exception (or error).
92      */

93     public XMLDBCException(java.lang.String JavaDoc msg, java.lang.Throwable JavaDoc exception) {
94         this(XMLDBCException.NO_CODE, msg, exception);
95     }
96
97     /**
98      * Constructor with error code, message and underlying exception.
99      *
100      * @param code
101      * the code featuring the kind of error (see constants).
102      * @param msg
103      * a message to describe the exception.
104      * @param exception
105      * the underlying exception (or error).
106      */

107     public XMLDBCException(int code, java.lang.String JavaDoc msg,
108             java.lang.Throwable JavaDoc exception) {
109         super(msg);
110         this.errorCode = code;
111         this.exception = exception;
112     }
113
114     //*************************** Other methods declaration:
115

116     /**
117      * To get the code of this exception/error.
118      *
119      * @return the error code.
120      */

121     public int getCode() {
122         return this.errorCode;
123     }
124
125     /**
126      * To get the exception of this exception/error.
127      *
128      * @return the underlying exception.
129      */

130     public java.lang.Throwable JavaDoc getException() {
131         return this.exception;
132     }
133
134     /**
135      * Returns the exception message, or the embedded exception message if none
136      * is present
137      *
138      * @return the message
139      */

140     public String JavaDoc getMessage() {
141         String JavaDoc message = super.getMessage();
142         if ((message == null || message.length() == 0) && exception != null)
143             message = this.exception.getMessage();
144
145         return message;
146     }
147
148     /**
149      * Print an error message processing embedded expeptions to the standard
150      * error output.
151      */

152     // TODO: put it in a common tools library ?
153
public static void printError(Throwable JavaDoc e, boolean pst) {
154         printError(e, System.err, pst);
155     }
156     
157     /**
158      * Print an error message processing embedded expeptions.
159      */

160     // TODO: put it in a common tools library ?
161
public static void printError(Throwable JavaDoc e, PrintStream JavaDoc ps, boolean pst) {
162         if (e != null) {
163             if (e.getMessage() != null) {
164                 /* print exception message */
165                 ps.println("Exception: " + e.getMessage());
166             }
167
168             /* printing location info */
169             if (e instanceof SAXParseException JavaDoc) {
170                 SAXParseException JavaDoc spex = (SAXParseException JavaDoc) e;
171                 ps.println("XML error location : " + spex.getSystemId() + " ["
172                         + spex.getLineNumber() + ':' + spex.getColumnNumber()
173                         + ']');
174             }
175             /* Print stack trace */
176             if (pst)
177                 e.printStackTrace(ps);
178
179             /* underlying exceptions */
180             if (e instanceof XMLDBCException)
181                 printError(((XMLDBCException) e).getException(), ps, pst);
182             else if (e instanceof SAXException JavaDoc)
183                 printError(((SAXException JavaDoc) e).getException(), ps, pst);
184         }
185     }
186
187 }
Popular Tags