19 * limitations under the License. |
18 * limitations under the License. |
20 */ |
19 */ |
21 |
20 |
22 package com.sun.org.apache.xml.internal.dtm; |
21 package com.sun.org.apache.xml.internal.dtm; |
23 |
22 |
24 |
|
25 import java.lang.reflect.InvocationTargetException; |
|
26 import java.lang.reflect.Method; |
|
27 |
|
28 import javax.xml.transform.SourceLocator; |
|
29 |
|
30 import com.sun.org.apache.xml.internal.res.XMLErrorResources; |
|
31 import com.sun.org.apache.xml.internal.res.XMLMessages; |
|
32 |
|
33 |
|
34 /** |
23 /** |
35 * This class specifies an exceptional condition that occured |
24 * This class specifies an exceptional condition that occurred |
36 * in the DTM module. |
25 * in the DTM module. |
37 */ |
26 */ |
38 public class DTMException extends RuntimeException { |
27 public class DTMException extends RuntimeException { |
39 static final long serialVersionUID = -775576419181334734L; |
28 static final long serialVersionUID = -775576419181334734L; |
40 |
|
41 /** Field locator specifies where the error occured. |
|
42 * @serial */ |
|
43 SourceLocator locator; |
|
44 |
|
45 /** |
|
46 * Method getLocator retrieves an instance of a SourceLocator |
|
47 * object that specifies where an error occured. |
|
48 * |
|
49 * @return A SourceLocator object, or null if none was specified. |
|
50 */ |
|
51 public SourceLocator getLocator() { |
|
52 return locator; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Method setLocator sets an instance of a SourceLocator |
|
57 * object that specifies where an error occured. |
|
58 * |
|
59 * @param location A SourceLocator object, or null to clear the location. |
|
60 */ |
|
61 public void setLocator(SourceLocator location) { |
|
62 locator = location; |
|
63 } |
|
64 |
|
65 /** Field containedException specifies a wrapped exception. May be null. |
|
66 * @serial */ |
|
67 Throwable containedException; |
|
68 |
|
69 /** |
|
70 * This method retrieves an exception that this exception wraps. |
|
71 * |
|
72 * @return An Throwable object, or null. |
|
73 * @see #getCause |
|
74 */ |
|
75 public Throwable getException() { |
|
76 return containedException; |
|
77 } |
|
78 |
|
79 /** |
|
80 * Returns the cause of this throwable or <code>null</code> if the |
|
81 * cause is nonexistent or unknown. (The cause is the throwable that |
|
82 * caused this throwable to get thrown.) |
|
83 */ |
|
84 public Throwable getCause() { |
|
85 |
|
86 return ((containedException == this) |
|
87 ? null |
|
88 : containedException); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Initializes the <i>cause</i> of this throwable to the specified value. |
|
93 * (The cause is the throwable that caused this throwable to get thrown.) |
|
94 * |
|
95 * <p>This method can be called at most once. It is generally called from |
|
96 * within the constructor, or immediately after creating the |
|
97 * throwable. If this throwable was created |
|
98 * with {@link #DTMException(Throwable)} or |
|
99 * {@link #DTMException(String,Throwable)}, this method cannot be called |
|
100 * even once. |
|
101 * |
|
102 * @param cause the cause (which is saved for later retrieval by the |
|
103 * {@link #getCause()} method). (A <tt>null</tt> value is |
|
104 * permitted, and indicates that the cause is nonexistent or |
|
105 * unknown.) |
|
106 * @return a reference to this <code>Throwable</code> instance. |
|
107 * @throws IllegalArgumentException if <code>cause</code> is this |
|
108 * throwable. (A throwable cannot |
|
109 * be its own cause.) |
|
110 * @throws IllegalStateException if this throwable was |
|
111 * created with {@link #DTMException(Throwable)} or |
|
112 * {@link #DTMException(String,Throwable)}, or this method has already |
|
113 * been called on this throwable. |
|
114 */ |
|
115 public synchronized Throwable initCause(Throwable cause) { |
|
116 |
|
117 if ((this.containedException == null) && (cause != null)) { |
|
118 throw new IllegalStateException(XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_OVERWRITE_CAUSE, null)); //"Can't overwrite cause"); |
|
119 } |
|
120 |
|
121 if (cause == this) { |
|
122 throw new IllegalArgumentException( |
|
123 XMLMessages.createXMLMessage(XMLErrorResources.ER_SELF_CAUSATION_NOT_PERMITTED, null)); //"Self-causation not permitted"); |
|
124 } |
|
125 |
|
126 this.containedException = cause; |
|
127 |
|
128 return this; |
|
129 } |
|
130 |
29 |
131 /** |
30 /** |
132 * Create a new DTMException. |
31 * Create a new DTMException. |
133 * |
32 * |
134 * @param message The error or warning message. |
33 * @param message The error or warning message. |
135 */ |
34 */ |
136 public DTMException(String message) { |
35 public DTMException(String message) { |
137 |
|
138 super(message); |
36 super(message); |
139 |
|
140 this.containedException = null; |
|
141 this.locator = null; |
|
142 } |
37 } |
143 |
38 |
144 /** |
39 /** |
145 * Create a new DTMException wrapping an existing exception. |
40 * Create a new DTMException wrapping an existing exception. |
146 * |
41 * |
147 * @param e The exception to be wrapped. |
42 * @param e The exception to be wrapped. |
148 */ |
43 */ |
149 public DTMException(Throwable e) { |
44 public DTMException(Throwable e) { |
150 |
45 super(e); |
151 super(e.getMessage()); |
|
152 |
|
153 this.containedException = e; |
|
154 this.locator = null; |
|
155 } |
46 } |
156 |
47 |
157 /** |
48 /** |
158 * Wrap an existing exception in a DTMException. |
49 * Wrap an existing exception in a DTMException. |
159 * |
50 * |
163 * @param message The error or warning message, or null to |
54 * @param message The error or warning message, or null to |
164 * use the message from the embedded exception. |
55 * use the message from the embedded exception. |
165 * @param e Any exception |
56 * @param e Any exception |
166 */ |
57 */ |
167 public DTMException(String message, Throwable e) { |
58 public DTMException(String message, Throwable e) { |
168 |
59 super(message, e); |
169 super(((message == null) || (message.length() == 0)) |
|
170 ? e.getMessage() |
|
171 : message); |
|
172 |
|
173 this.containedException = e; |
|
174 this.locator = null; |
|
175 } |
60 } |
176 |
|
177 /** |
|
178 * Create a new DTMException from a message and a Locator. |
|
179 * |
|
180 * <p>This constructor is especially useful when an application is |
|
181 * creating its own exception from within a DocumentHandler |
|
182 * callback.</p> |
|
183 * |
|
184 * @param message The error or warning message. |
|
185 * @param locator The locator object for the error or warning. |
|
186 */ |
|
187 public DTMException(String message, SourceLocator locator) { |
|
188 |
|
189 super(message); |
|
190 |
|
191 this.containedException = null; |
|
192 this.locator = locator; |
|
193 } |
61 } |
194 |
|
195 /** |
|
196 * Wrap an existing exception in a DTMException. |
|
197 * |
|
198 * @param message The error or warning message, or null to |
|
199 * use the message from the embedded exception. |
|
200 * @param locator The locator object for the error or warning. |
|
201 * @param e Any exception |
|
202 */ |
|
203 public DTMException(String message, SourceLocator locator, |
|
204 Throwable e) { |
|
205 |
|
206 super(message); |
|
207 |
|
208 this.containedException = e; |
|
209 this.locator = locator; |
|
210 } |
|
211 |
|
212 /** |
|
213 * Get the error message with location information |
|
214 * appended. |
|
215 */ |
|
216 public String getMessageAndLocation() { |
|
217 |
|
218 StringBuffer sbuffer = new StringBuffer(); |
|
219 String message = super.getMessage(); |
|
220 |
|
221 if (null != message) { |
|
222 sbuffer.append(message); |
|
223 } |
|
224 |
|
225 if (null != locator) { |
|
226 String systemID = locator.getSystemId(); |
|
227 int line = locator.getLineNumber(); |
|
228 int column = locator.getColumnNumber(); |
|
229 |
|
230 if (null != systemID) { |
|
231 sbuffer.append("; SystemID: "); |
|
232 sbuffer.append(systemID); |
|
233 } |
|
234 |
|
235 if (0 != line) { |
|
236 sbuffer.append("; Line#: "); |
|
237 sbuffer.append(line); |
|
238 } |
|
239 |
|
240 if (0 != column) { |
|
241 sbuffer.append("; Column#: "); |
|
242 sbuffer.append(column); |
|
243 } |
|
244 } |
|
245 |
|
246 return sbuffer.toString(); |
|
247 } |
|
248 |
|
249 /** |
|
250 * Get the location information as a string. |
|
251 * |
|
252 * @return A string with location info, or null |
|
253 * if there is no location information. |
|
254 */ |
|
255 public String getLocationAsString() { |
|
256 |
|
257 if (null != locator) { |
|
258 StringBuffer sbuffer = new StringBuffer(); |
|
259 String systemID = locator.getSystemId(); |
|
260 int line = locator.getLineNumber(); |
|
261 int column = locator.getColumnNumber(); |
|
262 |
|
263 if (null != systemID) { |
|
264 sbuffer.append("; SystemID: "); |
|
265 sbuffer.append(systemID); |
|
266 } |
|
267 |
|
268 if (0 != line) { |
|
269 sbuffer.append("; Line#: "); |
|
270 sbuffer.append(line); |
|
271 } |
|
272 |
|
273 if (0 != column) { |
|
274 sbuffer.append("; Column#: "); |
|
275 sbuffer.append(column); |
|
276 } |
|
277 |
|
278 return sbuffer.toString(); |
|
279 } else { |
|
280 return null; |
|
281 } |
|
282 } |
|
283 |
|
284 /** |
|
285 * Print the the trace of methods from where the error |
|
286 * originated. This will trace all nested exception |
|
287 * objects, as well as this object. |
|
288 */ |
|
289 public void printStackTrace() { |
|
290 printStackTrace(new java.io.PrintWriter(System.err, true)); |
|
291 } |
|
292 |
|
293 /** |
|
294 * Print the the trace of methods from where the error |
|
295 * originated. This will trace all nested exception |
|
296 * objects, as well as this object. |
|
297 * @param s The stream where the dump will be sent to. |
|
298 */ |
|
299 public void printStackTrace(java.io.PrintStream s) { |
|
300 printStackTrace(new java.io.PrintWriter(s)); |
|
301 } |
|
302 |
|
303 /** |
|
304 * Print the the trace of methods from where the error |
|
305 * originated. This will trace all nested exception |
|
306 * objects, as well as this object. |
|
307 * @param s The writer where the dump will be sent to. |
|
308 */ |
|
309 public void printStackTrace(java.io.PrintWriter s) { |
|
310 |
|
311 if (s == null) { |
|
312 s = new java.io.PrintWriter(System.err, true); |
|
313 } |
|
314 |
|
315 try { |
|
316 String locInfo = getLocationAsString(); |
|
317 |
|
318 if (null != locInfo) { |
|
319 s.println(locInfo); |
|
320 } |
|
321 |
|
322 super.printStackTrace(s); |
|
323 } catch (Throwable e) {} |
|
324 |
|
325 } |
|
326 } |
|