001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv;
019:
020: import java.util.ResourceBundle;
021: import java.util.PropertyResourceBundle;
022: import java.util.MissingResourceException;
023:
024: /**
025: * Base class for datatype exceptions. For DTD types, the exception can be
026: * created from an error message. For Schema types, it needs an error code
027: * (as defined in Appendix C of the structure spec), plus an array of arguments,
028: * for error message substitution.
029: *
030: * @xerces.internal
031: *
032: * @author Sandy Gao, IBM
033: *
034: * @version $Id: DatatypeException.java 572086 2007-09-02 18:26:08Z mrglavas $
035: */
036: public class DatatypeException extends Exception {
037:
038: /** Serialization version. */
039: static final long serialVersionUID = 1940805832730465578L;
040:
041: // used to store error code and error substitution arguments
042: protected final String key;
043: protected final Object[] args;
044:
045: /**
046: * Create a new datatype exception by providing an error code and a list
047: * of error message substitution arguments.
048: *
049: * @param key error code
050: * @param args error arguments
051: */
052: public DatatypeException(String key, Object[] args) {
053: super (key);
054: this .key = key;
055: this .args = args;
056: }
057:
058: /**
059: * Return the error code
060: *
061: * @return error code
062: */
063: public String getKey() {
064: return key;
065: }
066:
067: /**
068: * Return the list of error arguments
069: *
070: * @return error arguments
071: */
072: public Object[] getArgs() {
073: return args;
074: }
075:
076: /**
077: * Overrides this method to get the formatted&localized error message.
078: *
079: * REVISIT: the system locale is used to load the property file.
080: * do we want to allow the appilcation to specify a
081: * different locale?
082: */
083: public String getMessage() {
084: ResourceBundle resourceBundle = null;
085: resourceBundle = PropertyResourceBundle
086: .getBundle("org.apache.xerces.impl.msg.XMLSchemaMessages");
087: if (resourceBundle == null)
088: throw new MissingResourceException(
089: "Property file not found!",
090: "org.apache.xerces.impl.msg.XMLSchemaMessages", key);
091:
092: String msg = resourceBundle.getString(key);
093: if (msg == null) {
094: msg = resourceBundle.getString("BadMessageKey");
095: throw new MissingResourceException(msg,
096: "org.apache.xerces.impl.msg.XMLSchemaMessages", key);
097: }
098:
099: if (args != null) {
100: try {
101: msg = java.text.MessageFormat.format(msg, args);
102: } catch (Exception e) {
103: msg = resourceBundle.getString("FormatFailed");
104: msg += " " + resourceBundle.getString(key);
105: }
106: }
107:
108: return msg;
109: }
110: }
|