KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > exceptions > XMLSecurityException


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.exceptions;
18
19
20
21 import java.io.PrintStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24
25 import com.sun.org.apache.xml.internal.security.utils.Constants;
26 import com.sun.org.apache.xml.internal.security.utils.I18n;
27
28
29 /**
30  * The mother of all Exceptions in this bundle. It allows exceptions to have
31  * their messages translated to the different locales.
32  *
33  * The <code>xmlsecurity_en.properties</code> file contains this line:
34  * <pre>
35  * xml.WrongElement = Can't create a {0} from a {1} element
36  * </pre>
37  *
38  * Usage in the Java source is:
39  * <pre>
40  * {
41  * Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
42  *
43  * throw new XMLSecurityException("xml.WrongElement", exArgs);
44  * }
45  * </pre>
46  *
47  * Additionally, if another Exception has been caught, we can supply it, too>
48  * <pre>
49  * try {
50  * ...
51  * } catch (Exception oldEx) {
52  * Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
53  *
54  * throw new XMLSecurityException("xml.WrongElement", exArgs, oldEx);
55  * }
56  * </pre>
57  *
58  *
59  * @author Christian Geuer-Pollmann
60  */

61 public class XMLSecurityException extends Exception JavaDoc {
62
63     
64     
65    /**
66      *
67      */

68     private static final long serialVersionUID = 1L;
69
70    /** Field originalException */
71    protected Exception JavaDoc originalException = null;
72
73    /** Field msgID */
74    protected String JavaDoc msgID;
75
76    /**
77     * Constructor XMLSecurityException
78     *
79     */

80    public XMLSecurityException() {
81
82       super("Missing message string");
83
84       this.msgID = null;
85       this.originalException = null;
86    }
87
88    /**
89     * Constructor XMLSecurityException
90     *
91     * @param _msgID
92     */

93    public XMLSecurityException(String JavaDoc _msgID) {
94
95       super(I18n.getExceptionMessage(_msgID));
96
97       this.msgID = _msgID;
98       this.originalException = null;
99    }
100
101    /**
102     * Constructor XMLSecurityException
103     *
104     * @param _msgID
105     * @param exArgs
106     */

107    public XMLSecurityException(String JavaDoc _msgID, Object JavaDoc exArgs[]) {
108
109       super(MessageFormat.format(I18n.getExceptionMessage(_msgID), exArgs));
110
111       this.msgID = _msgID;
112       this.originalException = null;
113    }
114
115    /**
116     * Constructor XMLSecurityException
117     *
118     * @param _originalException
119     */

120    public XMLSecurityException(Exception JavaDoc _originalException) {
121
122       super("Missing message ID to locate message string in resource bundle \""
123             + Constants.exceptionMessagesResourceBundleBase
124             + "\". Original Exception was a "
125             + _originalException.getClass().getName() + " and message "
126             + _originalException.getMessage());
127
128       this.originalException = _originalException;
129    }
130
131    /**
132     * Constructor XMLSecurityException
133     *
134     * @param _msgID
135     * @param _originalException
136     */

137    public XMLSecurityException(String JavaDoc _msgID, Exception JavaDoc _originalException) {
138
139       super(I18n.getExceptionMessage(_msgID, _originalException));
140
141       this.msgID = _msgID;
142       this.originalException = _originalException;
143    }
144
145    /**
146     * Constructor XMLSecurityException
147     *
148     * @param _msgID
149     * @param exArgs
150     * @param _originalException
151     */

152    public XMLSecurityException(String JavaDoc _msgID, Object JavaDoc exArgs[],
153                                Exception JavaDoc _originalException) {
154
155       super(MessageFormat.format(I18n.getExceptionMessage(_msgID), exArgs));
156
157       this.msgID = _msgID;
158       this.originalException = _originalException;
159    }
160
161    /**
162     * Method getMsgID
163     *
164     * @return the messageId
165     */

166    public String JavaDoc getMsgID() {
167
168       if (msgID == null) {
169          return "Missing message ID";
170       }
171       return msgID;
172    }
173
174    /** @inheritDoc */
175    public String JavaDoc toString() {
176
177       String JavaDoc s = this.getClass().getName();
178       String JavaDoc message = super.getLocalizedMessage();
179
180       if (message != null) {
181          message = s + ": " + message;
182       } else {
183          message = s;
184       }
185
186       if (originalException != null) {
187          message = message + "\nOriginal Exception was "
188                    + originalException.toString();
189       }
190
191       return message;
192    }
193
194    /**
195     * Method printStackTrace
196     *
197     */

198    public void printStackTrace() {
199
200       synchronized (System.err) {
201          super.printStackTrace(System.err);
202
203          if (this.originalException != null) {
204             this.originalException.printStackTrace(System.err);
205          }
206       }
207    }
208
209    /**
210     * Method printStackTrace
211     *
212     * @param printwriter
213     */

214    public void printStackTrace(PrintWriter JavaDoc printwriter) {
215
216       super.printStackTrace(printwriter);
217
218       if (this.originalException != null) {
219          this.originalException.printStackTrace(printwriter);
220       }
221    }
222
223    /**
224     * Method printStackTrace
225     *
226     * @param printstream
227     */

228    public void printStackTrace(PrintStream JavaDoc printstream) {
229
230       super.printStackTrace(printstream);
231
232       if (this.originalException != null) {
233          this.originalException.printStackTrace(printstream);
234       }
235    }
236
237    /**
238     * Method getOriginalException
239     *
240     * @return the original exception
241     */

242    public Exception JavaDoc getOriginalException() {
243       return originalException;
244    }
245 }
246
Popular Tags