001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038: /*
039: *
040: * This code is subject to the freebxml License, Version 1.1
041: *
042: * Copyright (c) 2001 - 2005 freebxml.org. All rights reserved.
043: *
044: * $Header: /cvs/fi/FastInfoset/src/com/sun/xml/fastinfoset/AbstractResourceBundle.java,v 1.3.2.2 2006/10/03 13:38:07 sandoz Exp $
045: */
046: package com.sun.xml.fastinfoset;
047:
048: import java.text.MessageFormat;
049: import java.util.Enumeration;
050: import java.util.Locale;
051: import java.util.ResourceBundle;
052:
053: /**
054: * This class contains methods common to all *ResourceBundle classes
055: *
056: * @author Paul Sterk / Sun Microsystems
057: */
058: public abstract class AbstractResourceBundle extends ResourceBundle {
059:
060: public static final String LOCALE = "com.sun.xml.fastinfoset.locale";
061: static String _bundleName = null;
062:
063: public static String getBundleName() {
064: return _bundleName;
065: }
066:
067: public static void setBundleName(String name) {
068: _bundleName = name;
069: }
070:
071: /**
072: * Gets 'key' from ResourceBundle and format mesage using 'args'.
073: *
074: * @param key String key for message.
075: * @param args Array of arguments for message.
076: * @return String formatted message.
077: */
078: public String getString(String key, Object args[]) {
079: String pattern = getBundle().getString(key);
080: return MessageFormat.format(pattern, args);
081: }
082:
083: /**
084: * Gets 'key' from ResourceBundle and format mesage using 'args'.
085: *
086: * @param key String key for message.
087: * @param args Array of arguments for message.
088: * @param locale Locale in which to perform key lookup.
089: * @return String formatted message.
090: */
091: public String getString(String key, Object args[], Locale locale) {
092: String pattern = null;
093: if (locale == null) {
094: pattern = getBundle().getString(key);
095: } else {
096: pattern = getBundle(_bundleName, locale).getString(key);
097: }
098: return MessageFormat.format(pattern, args);
099: }
100:
101: /**
102: * Parse a locale string, return corresponding Locale instance.
103: *
104: * @param localeString
105: * Name for the locale of interest. If null, use VM default locale.
106: * @return New Locale instance.
107: */
108: public static Locale parseLocale(String localeString) {
109: Locale locale = null;
110: if (localeString == null) {
111: locale = Locale.getDefault();
112: } else {
113: try {
114: String[] args = localeString.split("_");
115: if (args.length == 1) {
116: locale = new Locale(args[0]);
117: } else if (args.length == 2) {
118: locale = new Locale(args[0], args[1]);
119: } else if (args.length == 3) {
120: locale = new Locale(args[0], args[1], args[2]);
121: }
122: } catch (Throwable t) {
123: locale = Locale.getDefault();
124: }
125: }
126: return locale;
127: }
128:
129: /**
130: * Subclasses of this class must implement this method so that the
131: * correct resource bundle is passed to methods in this class
132: *
133: * @return
134: * A java.util.ResourceBundle from the subsclass. Methods in this class
135: * will use this reference.
136: */
137: public abstract ResourceBundle getBundle();
138:
139: /**
140: * Since we are changing the ResourceBundle extension point, must
141: * implement handleGetObject() using delegate getBundle(). Uses
142: * getObject() call to work around protected access to
143: * ResourceBundle.handleGetObject(). Happily, this means parent tree
144: * of delegate bundle is searched for a match.
145: *
146: * Implements java.util.ResourceBundle.handleGetObject; inherits that
147: * javadoc information.
148: *
149: * @see java.util.ResourceBundle#handleGetObject(String)
150: */
151: protected Object handleGetObject(String key) {
152: return getBundle().getObject(key);
153: }
154:
155: /**
156: * Since we are changing the ResourceBundle extension point, must
157: * implement getKeys() using delegate getBundle().
158: *
159: * Implements java.util.ResourceBundle.getKeys; inherits that javadoc
160: * information.
161: *
162: * @see java.util.ResourceBundle#getKeys()
163: */
164: public final Enumeration getKeys() {
165: return getBundle().getKeys();
166: }
167: }
|