KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > res > XResourceBundle


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XResourceBundle.java,v 1.7 2004/02/17 04:22:15 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.utils.res;
20
21 import java.util.ListResourceBundle JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 /**
27  * The default (english) resource bundle.
28  * @xsl.usage internal
29  */

30 public class XResourceBundle extends ListResourceBundle JavaDoc
31 {
32
33   /** Error resource constants */
34   public static final String JavaDoc ERROR_RESOURCES =
35     "com.sun.org.apache.xalan.internal.res.XSLTErrorResources", XSLT_RESOURCE =
36     "com.sun.org.apache.xml.internal.utils.res.XResourceBundle", LANG_BUNDLE_NAME =
37     "com.sun.org.apache.xml.internal.utils.res.XResources", MULT_ORDER =
38     "multiplierOrder", MULT_PRECEDES = "precedes", MULT_FOLLOWS =
39     "follows", LANG_ORIENTATION = "orientation", LANG_RIGHTTOLEFT =
40     "rightToLeft", LANG_LEFTTORIGHT = "leftToRight", LANG_NUMBERING =
41     "numbering", LANG_ADDITIVE = "additive", LANG_MULT_ADD =
42     "multiplicative-additive", LANG_MULTIPLIER =
43     "multiplier", LANG_MULTIPLIER_CHAR =
44     "multiplierChar", LANG_NUMBERGROUPS = "numberGroups", LANG_NUM_TABLES =
45     "tables", LANG_ALPHABET = "alphabet", LANG_TRAD_ALPHABET = "tradAlphabet";
46
47   /**
48    * Return a named ResourceBundle for a particular locale. This method mimics the behavior
49    * of ResourceBundle.getBundle().
50    *
51    * @param className Name of local-specific subclass.
52    * @param locale the locale to prefer when searching for the bundle
53    */

54   public static final XResourceBundle loadResourceBundle(
55           String JavaDoc className, Locale JavaDoc locale) throws MissingResourceException JavaDoc
56   {
57
58     String JavaDoc suffix = getResourceSuffix(locale);
59
60     //System.out.println("resource " + className + suffix);
61
try
62     {
63       
64       // first try with the given locale
65
String JavaDoc resourceName = className + suffix;
66       return (XResourceBundle) ResourceBundle.getBundle(resourceName, locale);
67     }
68     catch (MissingResourceException JavaDoc e)
69     {
70       try // try to fall back to en_US if we can't load
71
{
72
73         // Since we can't find the localized property file,
74
// fall back to en_US.
75
return (XResourceBundle) ResourceBundle.getBundle(
76           XSLT_RESOURCE, new Locale JavaDoc("en", "US"));
77       }
78       catch (MissingResourceException JavaDoc e2)
79       {
80
81         // Now we are really in trouble.
82
// very bad, definitely very bad...not going to get very far
83
throw new MissingResourceException JavaDoc(
84           "Could not load any resource bundles.", className, "");
85       }
86     }
87   }
88
89   /**
90    * Return the resource file suffic for the indicated locale
91    * For most locales, this will be based the language code. However
92    * for Chinese, we do distinguish between Taiwan and PRC
93    *
94    * @param locale the locale
95    * @return an String suffix which canbe appended to a resource name
96    */

97   private static final String JavaDoc getResourceSuffix(Locale JavaDoc locale)
98   {
99
100     String JavaDoc lang = locale.getLanguage();
101     String JavaDoc country = locale.getCountry();
102     String JavaDoc variant = locale.getVariant();
103     String JavaDoc suffix = "_" + locale.getLanguage();
104
105     if (lang.equals("zh"))
106       suffix += "_" + country;
107
108     if (country.equals("JP"))
109       suffix += "_" + country + "_" + variant;
110
111     return suffix;
112   }
113
114   /**
115    * Get the association list.
116    *
117    * @return The association list.
118    */

119   protected Object JavaDoc[][] getContents() {
120       // return a copy of contents; in theory we want a deep clone
121
// of contents, but since it only contains (immutable) Strings,
122
// this shallow copy is sufficient
123
Object JavaDoc[][] msgCopy = new Object JavaDoc[contents.length][2];
124       for (int i = 0; i < contents.length; i++) {
125           msgCopy[i][0] = contents[i][0];
126           msgCopy[i][1] = contents[i][1];
127       }
128       return msgCopy;
129   }
130
131   /** The association list. */
132   static final Object JavaDoc[][] contents =
133   {
134     { "ui_language", "en" }, { "help_language", "en" }, { "language", "en" },
135     { "alphabet",
136       new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
137                   'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
138                   'Y', 'Z' } },
139     { "tradAlphabet",
140       new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
141                   'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
142                   'Y', 'Z' } },
143
144     //language orientation
145
{ "orientation", "LeftToRight" },
146
147     //language numbering
148
{ "numbering", "additive" },
149   };
150 }
151
Popular Tags