41 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
|
|
16 |
import java.io.BufferedInputStream;
|
|
|
17 |
import java.io.File;
|
|
|
18 |
import java.io.FileInputStream;
|
|
|
19 |
import java.io.IOException;
|
|
|
20 |
import java.io.InputStream;
|
149 |
ilm |
21 |
import java.util.HashMap;
|
41 |
ilm |
22 |
import java.util.Map;
|
|
|
23 |
import java.util.Map.Entry;
|
|
|
24 |
import java.util.Properties;
|
|
|
25 |
|
|
|
26 |
public class PropertiesUtils {
|
|
|
27 |
|
|
|
28 |
public static final Properties createFromMap(final Map<String, String> map) {
|
149 |
ilm |
29 |
return loadFromMap(new Properties(), map);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public static final Properties loadFromMap(final Properties res, final Map<String, String> map) {
|
41 |
ilm |
33 |
for (final Entry<String, String> e : map.entrySet()) {
|
149 |
ilm |
34 |
final String key = e.getKey();
|
|
|
35 |
if (key == null)
|
|
|
36 |
throw new NullPointerException("Null key : " + e);
|
|
|
37 |
final String val = e.getValue();
|
|
|
38 |
// Properties doesn't support nulls
|
|
|
39 |
if (val != null)
|
|
|
40 |
res.setProperty(key, val);
|
41 |
ilm |
41 |
}
|
|
|
42 |
return res;
|
|
|
43 |
}
|
|
|
44 |
|
149 |
ilm |
45 |
public static final Map<String, String> toMap(final Properties props) {
|
|
|
46 |
final Map<String, String> res = new HashMap<>();
|
|
|
47 |
for (final String key : props.stringPropertyNames()) {
|
|
|
48 |
final String value = props.getProperty(key);
|
|
|
49 |
assert value != null;
|
|
|
50 |
res.put(key, value);
|
|
|
51 |
}
|
|
|
52 |
return res;
|
|
|
53 |
}
|
|
|
54 |
|
41 |
ilm |
55 |
public static final Properties createFromFile(final File f) throws IOException {
|
180 |
ilm |
56 |
try (final InputStream stream = new BufferedInputStream(new FileInputStream(f))) {
|
|
|
57 |
return create(stream);
|
|
|
58 |
}
|
41 |
ilm |
59 |
}
|
|
|
60 |
|
|
|
61 |
public static final Properties createFromResource(final Class<?> ctxt, final String rsrc) throws IOException {
|
180 |
ilm |
62 |
try (final InputStream stream = ctxt.getResourceAsStream(rsrc)) {
|
|
|
63 |
return create(stream);
|
|
|
64 |
}
|
41 |
ilm |
65 |
}
|
|
|
66 |
|
180 |
ilm |
67 |
public static final Properties create(final InputStream stream) throws IOException {
|
41 |
ilm |
68 |
if (stream != null) {
|
180 |
ilm |
69 |
final Properties res = new Properties();
|
|
|
70 |
res.load(stream);
|
|
|
71 |
return res;
|
41 |
ilm |
72 |
} else {
|
|
|
73 |
return null;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
80 |
ilm |
77 |
public static final void load(final Properties props, final Properties toLoad) {
|
41 |
ilm |
78 |
for (final String key : toLoad.stringPropertyNames()) {
|
|
|
79 |
final String value = toLoad.getProperty(key);
|
|
|
80 |
assert value != null;
|
|
|
81 |
props.setProperty(key, value);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|