001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.om.common;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.ListResourceBundle;
025: import java.util.Locale;
026: import java.util.ResourceBundle;
027: import java.util.StringTokenizer;
028:
029: import org.apache.pluto.om.common.Language;
030: import org.apache.pluto.util.Enumerator;
031: import org.apache.pluto.util.StringUtils;
032:
033: /**
034: *
035: *
036: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
037: *
038: * @version CVS $Id: LanguageImpl.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class LanguageImpl implements Language, java.io.Serializable {
041: // ResourceBundle creation part
042:
043: /*private static class Resources extends ListResourceBundle {
044: private Object [][] resources = null;
045: private Language source = null;
046:
047: public Resources(Language source)
048: {
049: this.source = source;
050: Vector v = new Vector();
051: Iterator it = source.getKeywords();
052: while(it != null && it.hasNext())
053: v.add(it.next());
054:
055: resources = new Object [][] {
056: { "javax.portlet.title", source.getTitle()==null? "": source.getTitle()},
057: { "javax.portlet.short-title", source.getShortTitle()==null? "": source.getShortTitle()},
058: { "javax.portlet.keywords", v.size() > 0 ? v.toArray().toString() : ""}};
059: }
060:
061: public Object[][] getContents()
062: {
063: return resources;
064: }
065: }*/
066:
067: private static class DefaultsResourceBundle extends
068: ListResourceBundle {
069: private Object[][] resources;
070:
071: public DefaultsResourceBundle(String defaultTitle,
072: String defaultShortTitle, String defaultKeyWords) {
073: resources = new Object[][] {
074: { "javax.portlet.title", defaultTitle },
075: { "javax.portlet.short-title", defaultShortTitle },
076: { "javax.portlet.keywords", defaultKeyWords } };
077: }
078:
079: protected Object[][] getContents() {
080: return resources;
081: }
082: }
083:
084: private static class ResourceBundleImpl extends ResourceBundle {
085: private HashMap data;
086:
087: public ResourceBundleImpl(ResourceBundle bundle,
088: ResourceBundle defaults) {
089: data = new HashMap();
090:
091: importData(defaults);
092: importData(bundle);
093: }
094:
095: private void importData(ResourceBundle bundle) {
096: if (bundle != null) {
097: for (Enumeration enumeration = bundle.getKeys(); enumeration
098: .hasMoreElements();) {
099: String key = (String) enumeration.nextElement();
100: Object value = bundle.getObject(key);
101:
102: data.put(key, value);
103: }
104: }
105: }
106:
107: protected Object handleGetObject(String key) {
108: return data.get(key);
109: }
110:
111: public Enumeration getKeys() {
112: return new Enumerator(data.keySet());
113: }
114: }
115:
116: private Locale locale;
117: private String title;
118: private String shortTitle;
119: private ResourceBundle bundle;
120: private ArrayList keywords;
121:
122: public LanguageImpl(Locale locale, ResourceBundle bundle,
123: String defaultTitle, String defaultShortTitle,
124: String defaultKeyWords) {
125: this .bundle = new ResourceBundleImpl(bundle,
126: new DefaultsResourceBundle(defaultTitle,
127: defaultShortTitle, defaultKeyWords));
128:
129: this .locale = locale;
130: title = this .bundle.getString("javax.portlet.title");
131: shortTitle = this .bundle.getString("javax.portlet.short-title");
132: keywords = toList(this .bundle
133: .getString("javax.portlet.keywords"));
134: }
135:
136: // Language implementation.
137:
138: public Locale getLocale() {
139: return locale;
140: }
141:
142: public String getTitle() {
143: return title;
144: }
145:
146: public String getShortTitle() {
147: return shortTitle;
148: }
149:
150: public Iterator getKeywords() {
151: return keywords.iterator();
152: }
153:
154: public ResourceBundle getResourceBundle() {
155: return bundle;
156: }
157:
158: // internal methods.
159: private ArrayList toList(String value) {
160: ArrayList keywords = new ArrayList();
161:
162: for (StringTokenizer st = new StringTokenizer(value, ","); st
163: .hasMoreTokens();) {
164: keywords.add(st.nextToken().trim());
165: }
166:
167: return keywords;
168: }
169:
170: public String toString() {
171: return toString(0);
172: }
173:
174: public String toString(final int indent) {
175: StringBuffer buffer = new StringBuffer(50);
176: StringUtils.newLine(buffer, indent);
177: buffer.append(getClass().toString());
178: buffer.append(":");
179: StringUtils.newLine(buffer, indent);
180: buffer.append("{");
181: StringUtils.newLine(buffer, indent);
182: buffer.append("locale='");
183: buffer.append(locale);
184: buffer.append("'");
185: StringUtils.newLine(buffer, indent);
186: buffer.append("title='");
187: buffer.append(title);
188: buffer.append("'");
189: StringUtils.newLine(buffer, indent);
190: buffer.append("shortTitle='");
191: buffer.append(shortTitle);
192: buffer.append("'");
193: Iterator iterator = keywords.iterator();
194: if (iterator.hasNext()) {
195: StringUtils.newLine(buffer, indent);
196: buffer.append("Keywords:");
197: }
198: while (iterator.hasNext()) {
199: buffer.append(iterator.next());
200: buffer.append(',');
201: }
202: StringUtils.newLine(buffer, indent);
203: buffer.append("}");
204: return buffer.toString();
205: }
206:
207: // additional methods.
208:
209: /* (non-Javadoc)
210: * @see java.lang.Object#equals(java.lang.Object)
211: * used for element equality according collection implementations
212: */
213: public boolean equals(Object o) {
214: return o == null ? false : ((LanguageImpl) o).getLocale()
215: .equals(this .locale);
216: }
217:
218: /* (non-Javadoc)
219: * @see java.lang.Object#hashCode()
220: */
221: public int hashCode() {
222: return locale.hashCode();
223: }
224:
225: public void setKeywords(Collection keywords) {
226: this .keywords.clear();
227: this .keywords.addAll(keywords);
228: }
229:
230: public void setLocale(Locale locale) {
231: this .locale = locale;
232: }
233:
234: public void setShortTitle(String shortTitle) {
235: this .shortTitle = shortTitle;
236: }
237:
238: public void setTitle(String title) {
239: this.title = title;
240: }
241:
242: }
|