001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.validators.datatype;
059:
060: import java.util.ListResourceBundle;
061: import java.util.Locale;
062: import java.util.ResourceBundle;
063: import org.apache.xerces.utils.XMLMessageProvider;
064:
065: /**
066: *
067: * @author Jeffrey Rodriguez
068: * @version $Id: DatatypeMessageProvider.java,v 1.10 2001/07/19 22:36:15 sandygao Exp $
069: */
070: public class DatatypeMessageProvider implements XMLMessageProvider {
071: /**
072: * The domain of messages concerning the XML Schema: Datatypes specification.
073: */
074: public static final String DATATYPE_DOMAIN = "http://www.w3.org/TR/xml-schema-2";
075:
076: /**
077: * Set the locale used for error messages
078: *
079: * @param locale the new locale
080: */
081: public void setLocale(Locale locale) {
082: fLocale = locale;
083: }
084:
085: /**
086: * get the local used for error messages
087: *
088: * @return the locale
089: */
090: public Locale getLocale() {
091: return fLocale;
092: }
093:
094: /**
095: * Creates a message from the specified key and replacement
096: * arguments, localized to the given locale.
097: *
098: * @param locale The requested locale of the message to be
099: * created.
100: * @param key The key for the message text.
101: * @param args The arguments to be used as replacement text
102: * in the message created.
103: */
104: public String createMessage(Locale locale, int majorCode,
105: int minorCode, Object args[]) {
106: boolean throwex = false;
107: if (fResourceBundle == null || locale != fLocale) {
108: if (locale != null)
109: fResourceBundle = ListResourceBundle.getBundle(
110: "org.apache.xerces.msg.DatatypeMessages",
111: locale);
112: if (fResourceBundle == null)
113: fResourceBundle = ListResourceBundle
114: .getBundle("org.apache.xerces.msg.DatatypeMessages");
115: }
116: if (majorCode < 0 || majorCode >= fgMessageKeys.length) {
117: majorCode = MSG_BAD_MAJORCODE;
118: throwex = true;
119: }
120: String msgKey = fgMessageKeys[majorCode];
121: String msg = fResourceBundle.getString(msgKey);
122: if (args != null) {
123: try {
124: msg = java.text.MessageFormat.format(msg, args);
125: } catch (Exception e) {
126: msg = fResourceBundle
127: .getString(fgMessageKeys[MSG_FORMAT_FAILURE]);
128: msg += " " + fResourceBundle.getString(msgKey);
129: }
130: }
131:
132: if (throwex) {
133: throw new RuntimeException(msg);
134: }
135: return msg;
136: }
137:
138: //
139: //
140: //
141: private Locale fLocale = null;
142: private ResourceBundle fResourceBundle = null;
143: //
144: // Major Codes
145: //
146: private static int counter = 0;
147: public static final int MSG_BAD_MAJORCODE = counter++, // majorCode parameter to createMessage was out of bounds
148: MSG_FORMAT_FAILURE = counter++, // exception thrown during messageFormat call
149: NOT_BOOLEAN = counter++,
150: NOT_DECIMAL = counter++,
151: NOT_FLOAT = counter++,
152: NOT_DOUBLE = counter++,
153: INVALID_ENUM_VALUE = counter++,
154: OUT_OF_BOUNDS = counter++,
155: NOT_ENUM_VALUE = counter++,
156: FRACTION_GREATER_TOTALDIGITS = counter++,
157: FRACTION_EXCEEDED = counter++,
158: TOTALDIGITS_EXCEEDED = counter++,
159: ILLEGAL_FACET_VALUE = counter++,
160: ILLEGAL_ANYURI_FACET = counter++,
161: ILLEGAL_BOOLEAN_FACET = counter++,
162: ILLEGAL_BASE64_FACET = counter++,
163: ILLEGAL_DATETIME_FACET = counter++,
164: ILLEGAL_DECIMAL_FACET = counter++,
165: ILLEGAL_DOUBLE_FACET = counter++,
166: ILLEGAL_FLOAT_FACET = counter++,
167: ILLEGAL_HEXBINARY_FACET = counter++,
168: ILLEGAL_NOTATION_FACET = counter++,
169: ILLEGAL_QNAME_FACET = counter++,
170: ILLEGAL_STRING_FACET = counter++,
171: ILLEGAL_LIST_FACET = counter++,
172: ILLEGAL_UNION_FACET = counter++,
173: ILLEGAL_ANYSIMPLETYPE_FACET = counter++,
174:
175: MSG_MAX_CODE = counter;
176:
177: //
178: // Minor Codes
179: //
180: public static final int MSG_NONE = 0;
181:
182: public static final String[] fgMessageKeys = { "BadMajorCode",
183: "FormatFailed", "NotBoolean", "NotDecimal", "NotFloat",
184: "NotDouble", "InvalidEnumValue", "OutOfBounds",
185: "NotAnEnumValue", "FractionDigitsLargerThanTotalDigits",
186: "FractionDigitsExceeded", "TotalDigitsExceeded",
187: "IllegalFacetValue", "IllegalAnyURIFacet",
188: "IllegalBooleanFacet", "IllegalBase64Facet",
189: "IllegalDateTimeFacet", "IllegalDecimalFacet",
190: "IllegalDoubleFacet", "IllegalFloatFacet",
191: "IllegalHexBinaryFacet", "IllegalNotationFacet",
192: "IllegalQNameFacet", "IllegalStringFacet",
193: "IllegalListFacet", "IllegalUnionFacet",
194: "IllegalAnySimpleTypeFacet" };
195:
196: }
|