|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LocalizedProperties.java | 0% | 27.8% | 33.3% | 25.8% |
|
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; | |
18 | import java.io.InputStream; | |
19 | import java.io.Reader; | |
20 | import java.io.UnsupportedEncodingException; | |
21 | import java.util.HashMap; | |
22 | import java.util.Map; | |
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 _propertyMap; | |
39 | ||
40 | /** | |
41 | * Create a new object with an empty property storage | |
42 | */ | |
43 | 0 | public LocalizedProperties() |
44 | { | |
45 | 0 | this(new HashMap()); |
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 | 176 | public LocalizedProperties(Map propertyMap) |
57 | { | |
58 | 176 | _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 | 0 | public String getProperty(String key) |
69 | { | |
70 | 0 | Object value = _propertyMap.get(key); |
71 | 0 | if (value instanceof String) |
72 | 0 | return (String) value; |
73 | 0 | 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 | 0 | public String getProperty(String key, String defaultValue) |
85 | { | |
86 | 0 | String value = getProperty(key); |
87 | 0 | if (value != null) |
88 | 0 | return value; |
89 | 0 | 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 | 0 | public void setProperty(String key, String value) |
99 | { | |
100 | 0 | _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 | 0 | public Map getPropertyMap() |
110 | { | |
111 | 0 | 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 | 156 | public void load(InputStream ins) throws IOException |
123 | { | |
124 | 156 | LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins); |
125 | 156 | 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 | 20 | public void load(InputStream ins, String encoding) throws UnsupportedEncodingException, IOException |
138 | { | |
139 | 20 | LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins, encoding); |
140 | 20 | 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 | 0 | public void load(Reader reader) throws IOException |
152 | { | |
153 | 0 | LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(reader); |
154 | 0 | loader.load(_propertyMap); |
155 | } | |
156 | } |
|