001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may 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,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.util;
018:
019: import java.text.MessageFormat;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.ResourceBundle;
023:
024: /**
025: * Manages application resources. <br>
026: */
027: public class ResourceManager {
028:
029: static Map nameToRM = new HashMap();
030:
031: private ResourceBundle bundle;
032:
033: /**
034: * Gets the ResourceManager associated with <code>clazz</code>.
035: * It looks for a ResourceBundle named against the class name plus
036: * the string "RB". For example, for the com.mypackage.Main, the
037: * ResourceBundle com.mypackage.MainRB will be looked up.
038: *
039: * @param clazz
040: * @return the ResourceManager associated with the class
041: */
042: public static ResourceManager get(Class clazz) {
043: String bundleName = clazz.getName() + "RB";
044: return get(bundleName);
045: }
046:
047: /**
048: * Gets the ResourceManager with the given name.
049: *
050: * @param bundleName
051: * @return the ResourceManager with the given name.
052: */
053: public static ResourceManager get(String bundleName) {
054: ResourceManager rm = (ResourceManager) nameToRM.get(bundleName);
055: if (rm == null) {
056: ResourceBundle rb = ResourceBundle.getBundle(bundleName);
057: rm = new ResourceManager(rb);
058: nameToRM.put(bundleName, rm);
059: }
060: return rm;
061: }
062:
063: /**
064: * @param clazz
065: * @return the "AllRB" in the class package
066: */
067: public static ResourceManager all(Class clazz) {
068: return get(getPackage(clazz) + ".AllRB");
069: }
070:
071: /**
072: * Gets the default ResourceManager. This is equivalent to
073: * <code>all(ResourceManager.class)</code>. It returns the
074: * ResourceManager named "AllRB" located in the same package
075: * ResourceManager class (i.e com.l2fprod.common.util.AllRB).
076: *
077: * @return the default ResourceManager
078: */
079: public static ResourceManager common() {
080: return all(ResourceManager.class);
081: }
082:
083: /**
084: * @return the default ResourceManager for ui specific resources.
085: */
086: public static ResourceManager ui() {
087: return get("com.l2fprod.common.swing.AllRB");
088: }
089:
090: /**
091: * Resolves any references to a resource bundle contained in
092: * <code>rbAndProperty</code>. To reference a resource bundle
093: * inside a property use <code>${com.package.FileRB:key}</code>,
094: * this will look for <code>key</code> in the ResourceBundle
095: * <code>com.package.FileRB</code>.
096: *
097: * @param rbAndProperty
098: * @return the resolved resource or rbAndProperty if no resource was
099: * found
100: */
101: public static String resolve(String rbAndProperty) {
102: return common().resolve0(rbAndProperty);
103: }
104:
105: /**
106: * Same as {@link #resolve(String)} but once the value as been
107: * resolved, a MessageFormatter is applied with the given
108: * <code>args</code>.
109: *
110: * @param rbAndProperty
111: * @param args
112: * @return the value for the resource parametrized by args
113: */
114: public static String resolve(String rbAndProperty, Object[] args) {
115: String value = common().resolve0(rbAndProperty);
116: return MessageFormat.format(value, args);
117: }
118:
119: /**
120: * Can't be directly constructed
121: *
122: * @param bundle
123: */
124: private ResourceManager(ResourceBundle bundle) {
125: this .bundle = bundle;
126: }
127:
128: /**
129: * Gets the String associated with <code>key</code> after having
130: * resolved any nested keys ({@link #resolve(String)}).
131: *
132: * @param key the key to lookup
133: * @return the String associated with <code>key</code>
134: */
135: public String getString(String key) {
136: return resolve0(String.valueOf(bundle.getObject(key)));
137: }
138:
139: /**
140: * Gets the String associated with <code>key</code> after having
141: * resolved any nested keys ({@link #resolve(String)}) and applied
142: * a formatter using the given <code>args</code>.
143: *
144: * @param key the key to lookup
145: * @param args the arguments to pass to the formatter
146: * @return the String associated with <code>key</code>
147: */
148: public String getString(String key, Object[] args) {
149: String value = getString(key);
150: return MessageFormat.format(value, args);
151: }
152:
153: /**
154: * Gets the first character of the String associated with
155: * <code>key</code>.
156: *
157: * @param key the key to lookup
158: * @return the first character of the String associated with
159: * <code>key</code>.
160: */
161: public char getChar(String key) {
162: String s = getString(key);
163: if (s == null || s.trim().length() == 0) {
164: return (char) 0;
165: } else {
166: return s.charAt(0);
167: }
168: }
169:
170: private String resolve0(String property) {
171: String result = property;
172: if (property != null) {
173: int index = property.indexOf("${");
174: if (index != -1) {
175: int endIndex = property.indexOf("}", index);
176: String sub = property.substring(index + 2, endIndex);
177: // check if sub contains a reference to another RB, key
178: int colon = sub.indexOf(":");
179: if (colon != -1) {
180: String rbName = sub.substring(0, colon);
181: String keyName = sub.substring(colon + 1);
182: sub = get(rbName).getString(keyName);
183: } else {
184: // it's a regular nested property
185: sub = getString(sub);
186: }
187: result = property.substring(0, index) + sub
188: + resolve0(property.substring(endIndex + 1));
189: }
190: }
191: return result;
192: }
193:
194: private static String getPackage(Class clazz) {
195: String pck = clazz.getName();
196: int index = pck.lastIndexOf('.');
197: if (index != -1) {
198: pck = pck.substring(0, index);
199: } else {
200: pck = "";
201: }
202: return pck;
203: }
204:
205: }
|