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 runtime 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 XMLSecurityRuntimeException extends RuntimeException {
65
66    private static final long serialVersionUID = 1L;
67
68    /** Field msgID */
69    protected String msgID;
70
71    /**
72     * Constructor XMLSecurityRuntimeException
73     *
74     */
75    public XMLSecurityRuntimeException() {
76        super("Missing message string");
77
78        this.msgID = null;
79    }
80
81    /**
82     * Constructor XMLSecurityRuntimeException
83     *
84     * @param msgID
85     */
86    public XMLSecurityRuntimeException(String msgID) {
87        super(I18n.getExceptionMessage(msgID));
88
89        this.msgID = msgID;
90    }
91
92    /**
93     * Constructor XMLSecurityRuntimeException
94     *
95     * @param msgID
96     * @param exArgs
97     */
98    public XMLSecurityRuntimeException(String msgID, Object exArgs[]) {
99        super(MessageFormat.format(I18n.getExceptionMessage(msgID), exArgs));
100
101        this.msgID = msgID;
102    }
103
104    /**
105     * Constructor XMLSecurityRuntimeException
106     *
107     * @param originalException
108     */
109    public XMLSecurityRuntimeException(Exception originalException) {
110        super("Missing message ID to locate message string in resource bundle \""
111              + Constants.exceptionMessagesResourceBundleBase
112              + "\". Original Exception was a "
113              + originalException.getClass().getName() + " and message "
114              + originalException.getMessage(), originalException);
115    }
116
117    /**
118     * Constructor XMLSecurityRuntimeException
119     *
120     * @param msgID
121     * @param originalException
122     */
123    public XMLSecurityRuntimeException(String msgID, Exception originalException) {
124        super(I18n.getExceptionMessage(msgID, originalException), originalException);
125
126        this.msgID = msgID;
127    }
128
129    /**
130     * Constructor XMLSecurityRuntimeException
131     *
132     * @param msgID
133     * @param exArgs
134     * @param originalException
135     */
136    public XMLSecurityRuntimeException(String msgID, Object exArgs[], Exception originalException) {
137        super(MessageFormat.format(I18n.getExceptionMessage(msgID), exArgs));
138
139        this.msgID = msgID;
140    }
141
142    /**
143     * Method getMsgID
144     *
145     * @return the messageId
146     */
147    public String getMsgID() {
148        if (msgID == null) {
149            return "Missing message ID";
150        }
151        return msgID;
152    }
153
154    /** @inheritDoc */
155    public String toString() {
156        String s = this.getClass().getName();
157        String message = super.getLocalizedMessage();
158
159        if (message != null) {
160            message = s + ": " + message;
161        } else {
162            message = s;
163        }
164
165        if (this.getCause() != null) {
166            message = message + "\nOriginal Exception was " + this.getCause().toString();
167        }
168
169        return message;
170    }
171
172    /**
173     * Method printStackTrace
174     *
175     */
176    public void printStackTrace() {
177        synchronized (System.err) {
178            super.printStackTrace(System.err);
179        }
180    }
181
182    /**
183     * Method printStackTrace
184     *
185     * @param printwriter
186     */
187    public void printStackTrace(PrintWriter printwriter) {
188        super.printStackTrace(printwriter);
189    }
190
191    /**
192     * Method printStackTrace
193     *
194     * @param printstream
195     */
196    public void printStackTrace(PrintStream printstream) {
197        super.printStackTrace(printstream);
198    }
199
200    /**
201     * Method getOriginalException
202     *
203     * @return the original exception
204     */
205    public Exception getOriginalException() {
206        if (this.getCause() instanceof Exception) {
207            return (Exception)this.getCause();
208        }
209        return null;
210    }
211
212}
213