1 package com.sun.org.apache.xml.internal.security.exceptions;
   2 
   3 import java.io.PrintStream;
   4 import java.io.PrintWriter;
   5 import java.text.MessageFormat;
   6 
   7 import com.sun.org.apache.xml.internal.security.utils.Constants;
   8 import com.sun.org.apache.xml.internal.security.utils.I18n;
   9 
  10 /**
  11  * The mother of all runtime Exceptions in this bundle. It allows exceptions to have
  12  * their messages translated to the different locales.
  13  *
  14  * The <code>xmlsecurity_en.properties</code> file contains this line:
  15  * <pre>
  16  * xml.WrongElement = Can't create a {0} from a {1} element
  17  * </pre>
  18  *
  19  * Usage in the Java source is:
  20  * <pre>
  21  * {
  22  *    Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
  23  *
  24  *    throw new XMLSecurityException("xml.WrongElement", exArgs);
  25  * }
  26  * </pre>
  27  *
  28  * Additionally, if another Exception has been caught, we can supply it, too>
  29  * <pre>
  30  * try {
  31  *    ...
  32  * } catch (Exception oldEx) {
  33  *    Object exArgs[] = { Constants._TAG_TRANSFORMS, "BadElement" };
  34  *
  35  *    throw new XMLSecurityException("xml.WrongElement", exArgs, oldEx);
  36  * }
  37  * </pre>
  38  *
  39  *
  40  * @author Christian Geuer-Pollmann
  41  */
  42 public class XMLSecurityRuntimeException
  43         extends RuntimeException {
  44    /**
  45      *
  46      */
  47     private static final long serialVersionUID = 1L;
  48 
  49    /** Field originalException */
  50    protected Exception originalException = null;
  51 
  52    /** Field msgID */
  53    protected String msgID;
  54 
  55    /**
  56     * Constructor XMLSecurityRuntimeException
  57     *
  58     */
  59    public XMLSecurityRuntimeException() {
  60 
  61       super("Missing message string");
  62 
  63       this.msgID = null;
  64       this.originalException = null;
  65    }
  66 
  67    /**
  68     * Constructor XMLSecurityRuntimeException
  69     *
  70     * @param _msgID
  71     */
  72    public XMLSecurityRuntimeException(String _msgID) {
  73 
  74       super(I18n.getExceptionMessage(_msgID));
  75 
  76       this.msgID = _msgID;
  77       this.originalException = null;
  78    }
  79 
  80    /**
  81     * Constructor XMLSecurityRuntimeException
  82     *
  83     * @param _msgID
  84     * @param exArgs
  85     */
  86    public XMLSecurityRuntimeException(String _msgID, Object exArgs[]) {
  87 
  88       super(MessageFormat.format(I18n.getExceptionMessage(_msgID), exArgs));
  89 
  90       this.msgID = _msgID;
  91       this.originalException = null;
  92    }
  93 
  94    /**
  95     * Constructor XMLSecurityRuntimeException
  96     *
  97     * @param _originalException
  98     */
  99    public XMLSecurityRuntimeException(Exception _originalException) {
 100 
 101       super("Missing message ID to locate message string in resource bundle \""
 102             + Constants.exceptionMessagesResourceBundleBase
 103             + "\". Original Exception was a "
 104             + _originalException.getClass().getName() + " and message "
 105             + _originalException.getMessage());
 106 
 107       this.originalException = _originalException;
 108    }
 109 
 110    /**
 111     * Constructor XMLSecurityRuntimeException
 112     *
 113     * @param _msgID
 114     * @param _originalException
 115     */
 116    public XMLSecurityRuntimeException(String _msgID, Exception _originalException) {
 117 
 118       super(I18n.getExceptionMessage(_msgID, _originalException));
 119 
 120       this.msgID = _msgID;
 121       this.originalException = _originalException;
 122    }
 123 
 124    /**
 125     * Constructor XMLSecurityRuntimeException
 126     *
 127     * @param _msgID
 128     * @param exArgs
 129     * @param _originalException
 130     */
 131    public XMLSecurityRuntimeException(String _msgID, Object exArgs[],
 132                                Exception _originalException) {
 133 
 134       super(MessageFormat.format(I18n.getExceptionMessage(_msgID), exArgs));
 135 
 136       this.msgID = _msgID;
 137       this.originalException = _originalException;
 138    }
 139 
 140    /**
 141     * Method getMsgID
 142     *
 143     * @return the messageId
 144     */
 145    public String getMsgID() {
 146 
 147       if (msgID == null) {
 148          return "Missing message ID";
 149       }
 150       return msgID;
 151    }
 152 
 153    /** @inheritDoc */
 154    public String toString() {
 155 
 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 (originalException != null) {
 166          message = message + "\nOriginal Exception was "
 167                    + originalException.toString();
 168       }
 169 
 170       return message;
 171    }
 172 
 173    /**
 174     * Method printStackTrace
 175     *
 176     */
 177    public void printStackTrace() {
 178 
 179       synchronized (System.err) {
 180          super.printStackTrace(System.err);
 181 
 182          if (this.originalException != null) {
 183             this.originalException.printStackTrace(System.err);
 184          }
 185       }
 186    }
 187 
 188    /**
 189     * Method printStackTrace
 190     *
 191     * @param printwriter
 192     */
 193    public void printStackTrace(PrintWriter printwriter) {
 194 
 195       super.printStackTrace(printwriter);
 196 
 197       if (this.originalException != null) {
 198          this.originalException.printStackTrace(printwriter);
 199       }
 200    }
 201 
 202    /**
 203     * Method printStackTrace
 204     *
 205     * @param printstream
 206     */
 207    public void printStackTrace(PrintStream printstream) {
 208 
 209       super.printStackTrace(printstream);
 210 
 211       if (this.originalException != null) {
 212          this.originalException.printStackTrace(printstream);
 213       }
 214    }
 215 
 216    /**
 217     * Method getOriginalException
 218     *
 219     * @return the original exception
 220     */
 221    public Exception getOriginalException() {
 222       return originalException;
 223    }
 224 }