1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
181920import java.util.Hashtable;
21import java.util.Locale;
22import java.util.Enumeration;
23import java.util.Map;
24import org.apache.jetspeed.services.resources.JetspeedResources;
25import java.io.Serializable;
2627/***28A class for storing MetaData about an object.2930@author <a href="mailto:raphael@apache.org">Raphaël Luta</a>31@version $Id: MetaData.java,v 1.11 2004/02/23 03:23:42 jford Exp $32*/33publicclassMetaData implements Serializable
34 {
3536publicstaticfinal String DEFAULT_TITLE =
37 JetspeedResources.getString(JetspeedResources.DEFAULT_TITLE_KEY);
3839publicstaticfinal String DEFAULT_DESCRIPTION =
40 JetspeedResources.getString(JetspeedResources.DEFAULT_DESCRIPTION_KEY);
4142publicstaticfinal String DEFAULT_IMAGE =
43 JetspeedResources.getString(JetspeedResources.DEFAULT_IMAGE_KEY);
4445/***46 Hashtable to store all the properties47 */48private Hashtable data = new Hashtable();
4950/***51 Sets a title in the default locale52 */53publicvoid setTitle(String title)
54 {
55 setTitle(title, null);
56 }
5758/***59 Sets a title for the given locale60 */61publicvoid setTitle(String title, Locale locale)
62 {
63 setProperty("title", locale, title);
64 }
6566/***67 Returns the title for the default locale68 */69public String getTitle()
70 {
71return getTitle(null);
72 }
7374/***75 Returns the title for the given locale, if the title isn't defined76 in this locale or the locale, return the default title77 */78public String getTitle(Locale locale)
79 {
80 String title = (String) getProperty("title", locale);
8182if ((title == null) && (locale != null))
83 {
84 title = (String) getProperty("title", null);
85 }
8687if (title == null)
88 {
89 title = DEFAULT_TITLE;
90 }
9192return title;
93 }
9495/***96 Sets a descriptive image in the default locale97 */98publicvoid setImage(String image)
99 {
100 setImage(image, null);
101 }
102103/***104 Sets a descriptive image for the given locale105 */106publicvoid setImage(String image, Locale locale)
107 {
108 setProperty("image", locale, image);
109 }
110111/***112 Returns the descriptive image for the default locale113 */114public String getImage()
115 {
116return getImage(null);
117 }
118119/***120 Returns the descriptive image for the given locale, if the image isn't defined121 in this locale or the locale, return the default image122 */123public String getImage(Locale locale)
124 {
125 String image = (String) getProperty("image", locale);
126127if ((image == null) && (locale != null))
128 {
129 image = (String) getProperty("image", null);
130 }
131132if (image == null)
133 {
134 image = DEFAULT_IMAGE;
135 }
136137return image;
138 }
139140/***141 Sets a description in the default locale142 */143publicvoid setDescription(String description)
144 {
145 setDescription(description, null);
146 }
147148/***149 Sets a description for the given locale150 */151publicvoid setDescription(String description, Locale locale)
152 {
153 setProperty("description", locale, description);
154 }
155156/***157 Returns the description for the default locale158 */159public String getDescription()
160 {
161return getDescription(null);
162 }
163164/***165 Returns the title for the given locale, if the title isn't defined166 in this locale or the locale, return the default title167 */168public String getDescription(Locale locale)
169 {
170 String desc = (String) getProperty("description", locale);
171172if ((desc == null) && (locale != null))
173 {
174 desc = (String) getProperty ("description", null);
175 }
176177if (desc == null)
178 {
179 desc = DEFAULT_DESCRIPTION;
180 }
181182return desc;
183 }
184185/***186 Stores a property for later retrieval, uses the notation187 propertyName.localeName for locale distinction188189 <p>For example, title.fr_FR will store the French title, while190 title.sp will keep the Spanich one. title will keep the value191 which can be used when defaulting because en entry is not present192 for the queried locale</p>193 */194privatevoid setProperty(String name, Locale locale, Object value)
195 {
196if (name == null)
197 {
198return;
199 }
200201if (locale != null)
202 {
203 name = name + "." + locale.toString();
204 }
205206if (value == null)
207 {
208 data.remove(name);
209 }
210else211 {
212 data.put(name, value);
213 }
214 }
215216/***217 Retrieves a property by name for a given locale218 */219private Object getProperty(String name, Locale locale)
220 {
221if (name == null)
222 {
223returnnull;
224 }
225226 String extname = null;
227if (locale != null)
228 {
229 extname = name + "." + locale.toString();
230 }
231232 Object obj = null;
233234if (extname != null)
235 {
236 obj = data.get(extname);
237 }
238if (obj == null)
239 {
240 obj = data.get(name);
241 }
242243return obj;
244 }
245246/***247 Retrieves a property by name for a given locale248 */249private Map getProperties()
250 {
251return data;
252 }
253254/***255 Merges the properties defined in the param MetaData into the current one.256 If values are defined in both object for the same key,the one passed as 257 parameter updates the previous one258 */259publicvoid merge(MetaData meta)
260 {
261 Map map = meta.getProperties();
262 Hashtable params = (Hashtable) map;
263 Enumeration en = params.keys();
264265while (en.hasMoreElements())
266 {
267 Object key = en.nextElement();
268 String value = (String) params.get(key);
269270if (value != null)
271 {
272this.data.put(key, value);
273 }
274 }
275 }
276 }