KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > AbstractResourceBundle


1 /*
2  * ====================================================================
3  *
4  * This code is subject to the freebxml License, Version 1.1
5  *
6  * Copyright (c) 2001 - 2005 freebxml.org. All rights reserved.
7  *
8  * $Header: /cvs/fi/FastInfoset/src/com/sun/xml/fastinfoset/AbstractResourceBundle.java,v 1.2 2005/05/17 20:53:19 joehw Exp $
9  * ====================================================================
10  */

11 package com.sun.xml.fastinfoset;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18
19 /**
20  * This class contains methods common to all XXResourceBundle classes
21  *
22  * @author Paul Sterk / Sun Microsystems
23  */

24 public abstract class AbstractResourceBundle extends ResourceBundle JavaDoc {
25         
26     public static final String JavaDoc LOCALE = "com.sun.xml.fastinfoset.locale";
27     static String JavaDoc _bundleName = null;
28     
29     public static String JavaDoc getBundleName() {
30         return _bundleName;
31     }
32     public void setBundleName(String JavaDoc name) {
33         _bundleName = name;
34     }
35     /**
36      * Gets 'key' from ResourceBundle and format mesage using 'args'.
37      *
38      * @param key String key for message.
39      * @param args Array of arguments for message.
40      * @return String formatted message.
41      */

42     public String JavaDoc getString(String JavaDoc key, Object JavaDoc args[]) {
43         String JavaDoc pattern = getBundle().getString(key);
44         return MessageFormat.format(pattern, args);
45     }
46     
47     /**
48      * Gets 'key' from ResourceBundle and format mesage using 'args'.
49      *
50      * @param key String key for message.
51      * @param args Array of arguments for message.
52      * @param locale Locale in which to perform key lookup.
53      * @return String formatted message.
54      */

55     public String JavaDoc getString(String JavaDoc key, Object JavaDoc args[], Locale JavaDoc locale) {
56         String JavaDoc pattern = null;
57         if (locale == null) {
58             pattern = getBundle().getString(key);
59         } else {
60             pattern = getBundle(_bundleName, locale).getString(key);
61         }
62         return MessageFormat.format(pattern, args);
63     }
64
65     /**
66      * Parse a locale string, return corresponding Locale instance.
67      *
68      * @param localeString
69      * Name for the locale of interest. If null, use VM default locale.
70      * @return New Locale instance.
71      */

72     public static Locale JavaDoc parseLocale(String JavaDoc localeString) {
73         Locale JavaDoc locale = null;
74         if (localeString == null) {
75             locale = Locale.getDefault();
76         } else {
77             try {
78                 String JavaDoc[] args = localeString.split("_");
79                 if (args.length == 1) {
80                     locale = new Locale JavaDoc(args[0]);
81                 } else if (args.length == 2) {
82                     locale = new Locale JavaDoc(args[0], args[1]);
83                 } else if (args.length == 3) {
84                     locale = new Locale JavaDoc(args[0], args[1], args[2]);
85                 }
86             } catch (Throwable JavaDoc t) {
87                 locale = Locale.getDefault();
88             }
89         }
90         return locale;
91     }
92     
93     /**
94      * Subclasses of this class must implement this method so that the
95      * correct resource bundle is passed to methods in this class
96      *
97      * @return
98      * A java.util.ResourceBundle from the subsclass. Methods in this class
99      * will use this reference.
100      */

101     public abstract ResourceBundle JavaDoc getBundle();
102     
103
104     /**
105      * Since we are changing the ResourceBundle extension point, must
106      * implement handleGetObject() using delegate getBundle(). Uses
107      * getObject() call to work around protected access to
108      * ResourceBundle.handleGetObject(). Happily, this means parent tree
109      * of delegate bundle is searched for a match.
110      *
111      * Implements java.util.ResourceBundle.handleGetObject; inherits that
112      * javadoc information.
113      *
114      * @see java.util.ResourceBundle.handleGetObject
115      */

116     protected Object JavaDoc handleGetObject(String JavaDoc key) {
117        return getBundle().getObject(key);
118     }
119
120     /**
121      * Since we are changing the ResourceBundle extension point, must
122      * implement getKeys() using delegate getBundle().
123      *
124      * Implements java.util.ResourceBundle.getKeys; inherits that javadoc
125      * information.
126      *
127      * @see java.util.ResourceBundle.getKeys
128      */

129     public final Enumeration JavaDoc getKeys() {
130        return getBundle().getKeys();
131     }
132 }
133
Popular Tags