KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > MetaData


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 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 package org.apache.jetspeed.util;
18
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.apache.jetspeed.services.resources.JetspeedResources;
25 import java.io.Serializable JavaDoc;
26
27 /**
28 A class for storing MetaData about an object.
29
30 @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 */

33 public class MetaData implements Serializable JavaDoc
34 {
35
36     public static final String JavaDoc DEFAULT_TITLE =
37         JetspeedResources.getString(JetspeedResources.DEFAULT_TITLE_KEY);
38
39     public static final String JavaDoc DEFAULT_DESCRIPTION =
40         JetspeedResources.getString(JetspeedResources.DEFAULT_DESCRIPTION_KEY);
41
42     public static final String JavaDoc DEFAULT_IMAGE =
43         JetspeedResources.getString(JetspeedResources.DEFAULT_IMAGE_KEY);
44
45     /**
46     Hashtable to store all the properties
47     */

48     private Hashtable JavaDoc data = new Hashtable JavaDoc();
49
50     /**
51     Sets a title in the default locale
52     */

53     public void setTitle(String JavaDoc title)
54     {
55         setTitle(title, null);
56     }
57
58     /**
59     Sets a title for the given locale
60     */

61     public void setTitle(String JavaDoc title, Locale JavaDoc locale)
62     {
63         setProperty("title", locale, title);
64     }
65
66     /**
67     Returns the title for the default locale
68     */

69     public String JavaDoc getTitle()
70     {
71         return getTitle(null);
72     }
73
74     /**
75     Returns the title for the given locale, if the title isn't defined
76     in this locale or the locale, return the default title
77     */

78     public String JavaDoc getTitle(Locale JavaDoc locale)
79     {
80         String JavaDoc title = (String JavaDoc) getProperty("title", locale);
81
82         if ((title == null) && (locale != null))
83         {
84             title = (String JavaDoc) getProperty("title", null);
85         }
86
87         if (title == null)
88         {
89             title = DEFAULT_TITLE;
90         }
91
92         return title;
93     }
94
95     /**
96     Sets a descriptive image in the default locale
97     */

98     public void setImage(String JavaDoc image)
99     {
100         setImage(image, null);
101     }
102
103     /**
104     Sets a descriptive image for the given locale
105     */

106     public void setImage(String JavaDoc image, Locale JavaDoc locale)
107     {
108         setProperty("image", locale, image);
109     }
110
111     /**
112     Returns the descriptive image for the default locale
113     */

114     public String JavaDoc getImage()
115     {
116         return getImage(null);
117     }
118
119     /**
120     Returns the descriptive image for the given locale, if the image isn't defined
121     in this locale or the locale, return the default image
122     */

123     public String JavaDoc getImage(Locale JavaDoc locale)
124     {
125         String JavaDoc image = (String JavaDoc) getProperty("image", locale);
126
127         if ((image == null) && (locale != null))
128         {
129             image = (String JavaDoc) getProperty("image", null);
130         }
131
132         if (image == null)
133         {
134             image = DEFAULT_IMAGE;
135         }
136
137         return image;
138     }
139
140     /**
141     Sets a description in the default locale
142     */

143     public void setDescription(String JavaDoc description)
144     {
145         setDescription(description, null);
146     }
147
148     /**
149     Sets a description for the given locale
150     */

151     public void setDescription(String JavaDoc description, Locale JavaDoc locale)
152     {
153         setProperty("description", locale, description);
154     }
155
156     /**
157     Returns the description for the default locale
158     */

159     public String JavaDoc getDescription()
160     {
161         return getDescription(null);
162     }
163
164     /**
165     Returns the title for the given locale, if the title isn't defined
166     in this locale or the locale, return the default title
167     */

168     public String JavaDoc getDescription(Locale JavaDoc locale)
169     {
170         String JavaDoc desc = (String JavaDoc) getProperty("description", locale);
171
172         if ((desc == null) && (locale != null))
173         {
174             desc = (String JavaDoc) getProperty ("description", null);
175         }
176
177         if (desc == null)
178         {
179             desc = DEFAULT_DESCRIPTION;
180         }
181
182         return desc;
183     }
184
185     /**
186      Stores a property for later retrieval, uses the notation
187      propertyName.localeName for locale distinction
188
189     <p>For example, title.fr_FR will store the French title, while
190     title.sp will keep the Spanich one. title will keep the value
191     which can be used when defaulting because en entry is not present
192     for the queried locale</p>
193     */

194     private void setProperty(String JavaDoc name, Locale JavaDoc locale, Object JavaDoc value)
195     {
196         if (name == null)
197         {
198             return;
199         }
200
201         if (locale != null)
202         {
203             name = name + "." + locale.toString();
204         }
205
206         if (value == null)
207         {
208             data.remove(name);
209         }
210         else
211         {
212             data.put(name, value);
213         }
214     }
215
216     /**
217     Retrieves a property by name for a given locale
218     */

219     private Object JavaDoc getProperty(String JavaDoc name, Locale JavaDoc locale)
220     {
221         if (name == null)
222         {
223             return null;
224         }
225
226         String JavaDoc extname = null;
227         if (locale != null)
228         {
229             extname = name + "." + locale.toString();
230         }
231
232         Object JavaDoc obj = null;
233
234         if (extname != null)
235         {
236             obj = data.get(extname);
237         }
238         if (obj == null)
239         {
240             obj = data.get(name);
241         }
242
243         return obj;
244     }
245
246     /**
247     Retrieves a property by name for a given locale
248     */

249     private Map JavaDoc getProperties()
250     {
251         return data;
252     }
253
254     /**
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 one
258     */

259     public void merge(MetaData meta)
260     {
261         Map JavaDoc map = meta.getProperties();
262         Hashtable JavaDoc params = (Hashtable JavaDoc) map;
263         Enumeration JavaDoc en = params.keys();
264
265         while (en.hasMoreElements())
266         {
267             Object JavaDoc key = en.nextElement();
268             String JavaDoc value = (String JavaDoc) params.get(key);
269             
270             if (value != null)
271             {
272                 this.data.put(key, value);
273             }
274         }
275     }
276 }
277
Popular Tags