1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.util;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Properties;
31
32
33
34
35
36
37
38 public class ExtendedProperties extends Properties
39 {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static final String LINK_SUFFIX="@link";
56
57
58
59
60
61
62
63 public static final String DELIMITER = ".";
64
65
66
67
68 public ExtendedProperties()
69 {
70 super();
71 }
72
73
74
75
76
77
78 public ExtendedProperties(Properties defaults)
79 {
80 super(defaults);
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94 public int getIntProperty(String key) throws NumberFormatException
95 {
96 String value = getProperty(key);
97
98 if (value == null) {
99 throw new NumberFormatException("Cannot create int from null.");
100 }
101
102 return Integer.parseInt(value);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public double getDoubleProperty(String key) throws NumberFormatException
117 {
118 String value = getProperty(key);
119
120 if (value == null) {
121 throw new NumberFormatException("Cannot create double from null.");
122 }
123
124 return Double.parseDouble(value);
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public Class getClassProperty(String key)
138 {
139 String value = getProperty(key);
140
141 if (value == null) {
142 return null;
143 }
144
145 Class c;
146
147 try {
148 c = Class.forName(value);
149 } catch (ClassNotFoundException e) {
150 return null;
151 }
152
153 return c;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public ExtendedProperties getProperties(String mementoName)
169 {
170 assert (mementoName != null);
171
172
173 int stripLen = mementoName.length() + DELIMITER.length();
174 ExtendedProperties ep = new ExtendedProperties();
175
176 for (Enumeration e = keys(); e.hasMoreElements();) {
177 String key = (String)e.nextElement();
178
179 if (!key.startsWith(mementoName)) {
180 continue;
181 }
182
183 ep.put(key.substring(stripLen), get(key));
184 }
185
186 return ep;
187 }
188
189
190
191
192
193
194
195
196 public void setProperties(String mementoName, ExtendedProperties ep)
197 {
198 assert (ep != null);
199
200 String prefix=mementoName+DELIMITER;
201 if (mementoName==null || mementoName.length()<1) {
202 prefix="";
203 }
204
205 for (Enumeration e = ep.keys(); e.hasMoreElements();) {
206 String key = (String)e.nextElement();
207 String newKey = prefix + key;
208 put(newKey, ep.get(key));
209 }
210 }
211
212
213
214
215 public synchronized void load(URL resource) throws IOException, MalformedURLException {
216 InputStream stream =resource.openStream();
217 super.load(stream);
218 checkForLinks(resource);
219 stream.close();
220 }
221
222
223
224
225 public synchronized void load(InputStream inStream) throws IOException {
226 super.load(inStream);
227 }
228
229 public synchronized void checkForLinks(URL resource) throws IOException, MalformedURLException{
230 Map linksToAdd=null;
231 Iterator iter=entrySet().iterator();
232 while (iter.hasNext()) {
233 Map.Entry entry = (Map.Entry) iter.next();
234 String key=(String)entry.getKey();
235 int lastDelIdx=key.lastIndexOf(ExtendedProperties.DELIMITER);
236 String lastPart=key;
237 if (lastDelIdx>=0) {
238 lastPart=key.substring(lastDelIdx+1);
239 }
240 if (lastPart.startsWith(LINK_SUFFIX)) {
241 String prefix="";
242 if (lastDelIdx>0) {
243 prefix=key.substring(0, lastDelIdx);
244 }
245
246 String linkPath=(String)entry.getValue();
247 URL childURL=new URL(resource,linkPath);
248 ExtendedProperties childProps=new ExtendedProperties();
249 childProps.load(childURL);
250 if (linksToAdd==null) {
251 linksToAdd=new HashMap();
252 }
253 linksToAdd.put(prefix,childProps);
254 }
255 }
256 if (linksToAdd!=null) {
257 iter=linksToAdd.keySet().iterator();
258 while(iter.hasNext()) {
259 String prefix=(String)iter.next();
260 ExtendedProperties childProps=(ExtendedProperties)linksToAdd.get(prefix);
261 setProperties(prefix, childProps);
262 }
263 }
264 }
265 }
266
267