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