001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.impl;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.LinkedHashSet;
021: import java.util.Locale;
022: import java.util.Map;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025: import java.util.Set;
026:
027: /**
028: * Represents a Resource that includes its configuration. The Resource
029: * handles resource bundle names and is able to load them dynamically
030: * for different locales. In addition, it can have a "parent" reference to
031: * another ConfiguredResource so that instances can be chained together.
032: * When an instance refers to another "parent" resource, it overrides
033: * any resources defined in the reference resource.
034: *
035: * @author Shellman, Dan
036: */
037: public class ConfiguredResource implements Resource {
038: private Map messageMap = new HashMap();
039: private Set resources = new LinkedHashSet();
040: private ConfiguredResource ref;
041: private String id;
042:
043: /**
044: * Default constructor.
045: */
046: public ConfiguredResource() {
047: } //end ConfiguredResource()
048:
049: /**
050: * Adds an individual message as a resource, available via its
051: * key.
052: *
053: * @param key The key to the message resource
054: * @param value The value of the message
055: */
056: public void addMessage(String key, String value) {
057: messageMap.put(key, value);
058: } //end addMessage()
059:
060: /**
061: * Adds a resource bundle based upon its classpath location.
062: *
063: * @param location The classpath location of the resource bundle.
064: */
065: public void addResourceFile(String location) {
066: resources.add(location);
067: } //end addResourceFile()
068:
069: /**
070: * Adds a Set of resource bundle locations (Set of String objects).
071: *
072: * @param fileSet A Set of Strings representing resource bundles.
073: */
074: public void addResourceFiles(Set fileSet) {
075: resources.addAll(fileSet);
076: } //end addResourceFiles()
077:
078: /**
079: * Sets a reference to another ConfiguredResource. If a key value
080: * isn't found in this resource, the referenced one will be checked.
081: *
082: * @param resource The "parent" resource.
083: */
084: public void setRef(ConfiguredResource resource) {
085: ref = resource;
086: } //end setRef()
087:
088: /**
089: * Sets the id of the resource.
090: *
091: * @param theId The id of the resource.
092: */
093: public void setId(String theId) {
094: id = theId;
095: } //end setId()
096:
097: /**
098: * Gets the value of the message key.
099: *
100: * @param key The message key
101: * @param locale The locale to use in retrieving the value.
102: *
103: * @return Returns the value of the message key.
104: */
105: public String getValue(String key, Locale locale) {
106: String value;
107: Iterator it;
108:
109: value = (String) messageMap.get(key);
110: if (value != null) {
111: return value;
112: }
113:
114: it = resources.iterator();
115: while (it.hasNext()) {
116: String bundleName;
117: ResourceBundle bundle;
118:
119: bundleName = (String) it.next();
120: bundle = ResourceBundle.getBundle(bundleName, locale,
121: Thread.currentThread().getContextClassLoader());
122: try {
123: value = bundle.getString(key);
124: } catch (MissingResourceException e) {
125: continue;
126: } catch (ClassCastException e) {
127: value = bundle.getObject(key).toString();
128: break;
129: }
130: }
131:
132: if (value == null && ref != null) {
133: return ref.getValue(key, locale);
134: }
135:
136: return value;
137: } //end getValue()
138:
139: /**
140: * For debug info.
141: *
142: * @return Returns debug info.
143: */
144: public String toString() {
145: return id;
146: } //end toString()
147: } //end ConfiguredResource
|