KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > text > LocalizedProperties


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.util.text;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.Reader JavaDoc;
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * A version of java.util.Properties that can read the properties from files
26  * using an encoding other than ISO-8859-1. All non-latin characters are read
27  * correctly using the given encoding and no longer need to be quoted using native2ascii.
28  *
29  * In addition, the properties may be stored in an arbitrary map, rather than
30  * only in Properties. For example, using LinkedHashMap will preserve the order
31  * of the properties as defined in the file.
32  *
33  * @author mb
34  * @since 4.0
35  */

36 public class LocalizedProperties
37 {
38     private Map JavaDoc _propertyMap;
39     
40     /**
41      * Create a new object with an empty property storage
42      */

43     public LocalizedProperties()
44     {
45         this(new HashMap JavaDoc());
46     }
47
48     /**
49      * Use the provided argument as the storage location for the properties managed
50      * by this object. This allows different types of Map implementations to be used,
51      * such as a LinkedHashMap to preserve the order of the keys, for example.
52      * The provided map may contain the default property values as well.
53      *
54      * @param propertyMap the map where properties are to be stored
55      */

56     public LocalizedProperties(Map JavaDoc propertyMap)
57     {
58         _propertyMap = propertyMap;
59     }
60     
61     /**
62      * Returns the property value corresponding the provided key. If there is no such property,
63      * or the value in the provided map is not of type String, null is returned.
64      *
65      * @param key the property key
66      * @return the value of the property, or null if there is no such property
67      */

68     public String JavaDoc getProperty(String JavaDoc key)
69     {
70        Object JavaDoc value = _propertyMap.get(key);
71        if (value instanceof String JavaDoc)
72            return (String JavaDoc) value;
73        return null;
74     }
75     
76     /**
77      * Returns the property value corresponding to the provided key,
78      * or the provided default value if no such property exists.
79      *
80      * @param key the property key
81      * @param defaultValue the default value of the property
82      * @return the value of the property, or the default value if there is no such property
83      */

84     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue)
85     {
86         String JavaDoc value = getProperty(key);
87         if (value != null)
88             return value;
89         return defaultValue;
90     }
91     
92     /**
93      * Stores a property value
94      *
95      * @param key the property key
96      * @param value the property value
97      */

98     public void setProperty(String JavaDoc key, String JavaDoc value)
99     {
100         _propertyMap.put(key, value);
101     }
102     
103     /**
104      * Returns the map containing all properties. The map can be used to enumerate the
105      * properties or their keys.
106      *
107      * @return a map containing the properties
108      */

109     public Map JavaDoc getPropertyMap()
110     {
111         return _propertyMap;
112     }
113     
114     /**
115      * Loads the properties from the given stream using the default character encoding.
116      * This method operates in the same way as the equivalent method in {@link java.util.Properties},
117      * but it also handles non-ascii symbols.
118      *
119      * @param ins the stream to load the properties from
120      * @throws IOException
121      */

122     public void load(InputStream JavaDoc ins) throws IOException JavaDoc
123     {
124         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins);
125         loader.load(_propertyMap);
126     }
127     
128     /**
129      * Loads the properties from the given stream using the provided character encoding.
130      * This method operates in the same way as the equivalent method in {@link java.util.Properties},
131      * but it also handles non-ascii symbols.
132      *
133      * @param ins the stream to load the properties from
134      * @param encoding the encoding the use when parsing the stream
135      * @throws IOException
136      */

137     public void load(InputStream JavaDoc ins, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc, IOException JavaDoc
138     {
139         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins, encoding);
140         loader.load(_propertyMap);
141     }
142     
143     /**
144      * Loads the properties from the given reader.
145      * This method operates in the same way as the equivalent method in {@link java.util.Properties},
146      * but it also handles non-ascii symbols.
147      *
148      * @param reader the reader to load the properties from
149      * @throws IOException
150      */

151     public void load(Reader JavaDoc reader) throws IOException JavaDoc
152     {
153         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(reader);
154         loader.load(_propertyMap);
155     }
156 }
157
Popular Tags