1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.impl.dv;
  23 
  24 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  25 import java.util.ResourceBundle;
  26 import java.util.PropertyResourceBundle;
  27 import java.util.MissingResourceException;
  28 
  29 /**
  30  * Base class for datatype exceptions. For DTD types, the exception can be
  31  * created from an error message. For Schema types, it needs an error code
  32  * (as defined in Appendix C of the structure spec), plus an array of arguents,
  33  * for error message substitution.
  34  *
  35  * @xerces.internal
  36  *
  37  * @author Sandy Gao, IBM
  38  *
  39  * @version $Id: DatatypeException.java,v 1.6 2010-11-01 04:39:43 joehw Exp $
  40  */
  41 public class DatatypeException extends Exception {
  42 
  43     /** Serialization version. */
  44     static final long serialVersionUID = 1940805832730465578L;
  45 
  46     // used to store error code and error substitution arguments
  47     protected String key;
  48     protected Object[] args;
  49 
  50     /**
  51      * Create a new datatype exception by providing an error code and a list
  52      * of error message substitution arguments.
  53      *
  54      * @param key  error code
  55      * @param args error arguments
  56      */
  57     public DatatypeException(String key, Object[] args) {
  58         super(key);
  59         this.key = key;
  60         this.args = args;
  61     }
  62 
  63     /**
  64      * Return the error code
  65      *
  66      * @return  error code
  67      */
  68     public String getKey() {
  69         return key;
  70     }
  71 
  72     /**
  73      * Return the list of error arguments
  74      *
  75      * @return  error arguments
  76      */
  77     public Object[] getArgs() {
  78         return args;
  79     }
  80 
  81     /**
  82      * Overrides this method to get the formatted&localized error message.
  83      *
  84      * REVISIT: the system locale is used to load the property file.
  85      *          do we want to allow the appilcation to specify a
  86      *          different locale?
  87      */
  88     public String getMessage() {
  89         ResourceBundle resourceBundle = null;
  90         resourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages");
  91         if (resourceBundle == null)
  92             throw new MissingResourceException("Property file not found!", "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
  93 
  94         String msg = resourceBundle.getString(key);
  95         if (msg == null) {
  96             msg = resourceBundle.getString("BadMessageKey");
  97             throw new MissingResourceException(msg, "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
  98         }
  99 
 100         if (args != null) {
 101             try {
 102                 msg = java.text.MessageFormat.format(msg, args);
 103             } catch (Exception e) {
 104                 msg = resourceBundle.getString("FormatFailed");
 105                 msg += " " + resourceBundle.getString(key);
 106             }
 107         }
 108 
 109         return msg;
 110     }
 111 }