1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/**
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
23package com.sun.org.apache.xml.internal.security.exceptions;
24
25import java.io.PrintStream;
26import java.io.PrintWriter;
27import java.text.MessageFormat;
28
29import com.sun.org.apache.xml.internal.security.utils.Constants;
30import com.sun.org.apache.xml.internal.security.utils.I18n;
31
32/**
33 * The mother of all Exceptions in this bundle. It allows exceptions to have
34 * their messages translated to the different locales.
35 *
36 * The <code>xmlsecurity_en.properties</code> file contains this line:
37 * <pre>
38 * xml.WrongElement = Can't create a {0} from a {1} element
39 * </pre>
40 *
41 * Usage in the Java source is:
42 * <pre>
43 * {
44 *    Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
45 *
46 *    throw new XMLSecurityException("xml.WrongElement", exArgs);
47 * }
48 * </pre>
49 *
50 * Additionally, if another Exception has been caught, we can supply it, too>
51 * <pre>
52 * try {
53 *    ...
54 * } catch (Exception oldEx) {
55 *    Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
56 *
57 *    throw new XMLSecurityException("xml.WrongElement", exArgs, oldEx);
58 * }
59 * </pre>
60 *
61 *
62 * @author Christian Geuer-Pollmann
63 */
64public class XMLSecurityException extends Exception {
65
66    /**
67     *
68     */
69    private static final long serialVersionUID = 1L;
70
71    /** Field msgID */
72    protected String msgID;
73
74    /**
75     * Constructor XMLSecurityException
76     *
77     */
78    public XMLSecurityException() {
79        super("Missing message string");
80
81        this.msgID = null;
82    }
83
84    /**
85     * Constructor XMLSecurityException
86     *
87     * @param msgID
88     */
89    public XMLSecurityException(String msgID) {
90        super(I18n.getExceptionMessage(msgID));
91
92        this.msgID = msgID;
93    }
94
95    /**
96     * Constructor XMLSecurityException
97     *
98     * @param msgID
99     * @param exArgs
100     */
101    public XMLSecurityException(String msgID, Object exArgs[]) {
102
103        super(MessageFormat.format(I18n.getExceptionMessage(msgID), exArgs));
104
105        this.msgID = msgID;
106    }
107
108    /**
109     * Constructor XMLSecurityException
110     *
111     * @param originalException
112     */
113    public XMLSecurityException(Exception originalException) {
114
115        super("Missing message ID to locate message string in resource bundle \""
116              + Constants.exceptionMessagesResourceBundleBase
117              + "\". Original Exception was a "
118              + originalException.getClass().getName() + " and message "
119              + originalException.getMessage(), originalException);
120    }
121
122    /**
123     * Constructor XMLSecurityException
124     *
125     * @param msgID
126     * @param originalException
127     */
128    public XMLSecurityException(String msgID, Exception originalException) {
129        super(I18n.getExceptionMessage(msgID, originalException), originalException);
130
131        this.msgID = msgID;
132    }
133
134    /**
135     * Constructor XMLSecurityException
136     *
137     * @param msgID
138     * @param exArgs
139     * @param originalException
140     */
141    public XMLSecurityException(String msgID, Object exArgs[], Exception originalException) {
142        super(MessageFormat.format(I18n.getExceptionMessage(msgID), exArgs), originalException);
143
144        this.msgID = msgID;
145    }
146
147    /**
148     * Method getMsgID
149     *
150     * @return the messageId
151     */
152    public String getMsgID() {
153        if (msgID == null) {
154            return "Missing message ID";
155        }
156        return msgID;
157    }
158
159    /** @inheritDoc */
160    public String toString() {
161        String s = this.getClass().getName();
162        String message = super.getLocalizedMessage();
163
164        if (message != null) {
165            message = s + ": " + message;
166        } else {
167            message = s;
168        }
169
170        if (super.getCause() != null) {
171            message = message + "\nOriginal Exception was " + super.getCause().toString();
172        }
173
174        return message;
175    }
176
177    /**
178     * Method printStackTrace
179     *
180     */
181    public void printStackTrace() {
182        synchronized (System.err) {
183            super.printStackTrace(System.err);
184        }
185    }
186
187    /**
188     * Method printStackTrace
189     *
190     * @param printwriter
191     */
192    public void printStackTrace(PrintWriter printwriter) {
193        super.printStackTrace(printwriter);
194    }
195
196    /**
197     * Method printStackTrace
198     *
199     * @param printstream
200     */
201    public void printStackTrace(PrintStream printstream) {
202        super.printStackTrace(printstream);
203    }
204
205    /**
206     * Method getOriginalException
207     *
208     * @return the original exception
209     */
210    public Exception getOriginalException() {
211        if (this.getCause() instanceof Exception) {
212            return (Exception)this.getCause();
213        }
214        return null;
215    }
216}
217