1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.util; |
7 | |
8 | import java.io.BufferedWriter; |
9 | import java.io.ByteArrayInputStream; |
10 | import java.io.ByteArrayOutputStream; |
11 | import java.io.IOException; |
12 | import java.io.InputStream; |
13 | import java.io.InputStreamReader; |
14 | import java.io.LineNumberReader; |
15 | import java.io.OutputStreamWriter; |
16 | import java.io.PrintWriter; |
17 | import java.io.Writer; |
18 | import java.util.Collections; |
19 | import java.util.Enumeration; |
20 | import java.util.Properties; |
21 | import java.util.TreeMap; |
22 | import java.util.Vector; |
23 | import java.util.Map.Entry; |
24 | import org.h2.store.fs.FileUtils; |
25 | |
26 | /** |
27 | * Sorted properties file. |
28 | * This implementation requires that store() internally calls keys(). |
29 | */ |
30 | public class SortedProperties extends Properties { |
31 | |
32 | private static final long serialVersionUID = 1L; |
33 | |
34 | @Override |
35 | public synchronized Enumeration<Object> keys() { |
36 | Vector<String> v = new Vector<String>(); |
37 | for (Object o : keySet()) { |
38 | v.add(o.toString()); |
39 | } |
40 | Collections.sort(v); |
41 | return new Vector<Object>(v).elements(); |
42 | } |
43 | |
44 | /** |
45 | * Get a boolean property value from a properties object. |
46 | * |
47 | * @param prop the properties object |
48 | * @param key the key |
49 | * @param def the default value |
50 | * @return the value if set, or the default value if not |
51 | */ |
52 | public static boolean getBooleanProperty(Properties prop, String key, |
53 | boolean def) { |
54 | String value = prop.getProperty(key, "" + def); |
55 | try { |
56 | return Boolean.parseBoolean(value); |
57 | } catch (Exception e) { |
58 | e.printStackTrace(); |
59 | return def; |
60 | } |
61 | } |
62 | |
63 | /** |
64 | * Get an int property value from a properties object. |
65 | * |
66 | * @param prop the properties object |
67 | * @param key the key |
68 | * @param def the default value |
69 | * @return the value if set, or the default value if not |
70 | */ |
71 | public static int getIntProperty(Properties prop, String key, int def) { |
72 | String value = prop.getProperty(key, "" + def); |
73 | try { |
74 | return Integer.decode(value); |
75 | } catch (Exception e) { |
76 | e.printStackTrace(); |
77 | return def; |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * Load a properties object from a file. |
83 | * |
84 | * @param fileName the name of the properties file |
85 | * @return the properties object |
86 | */ |
87 | public static synchronized SortedProperties loadProperties(String fileName) |
88 | throws IOException { |
89 | SortedProperties prop = new SortedProperties(); |
90 | if (FileUtils.exists(fileName)) { |
91 | InputStream in = null; |
92 | try { |
93 | in = FileUtils.newInputStream(fileName); |
94 | prop.load(in); |
95 | } finally { |
96 | if (in != null) { |
97 | in.close(); |
98 | } |
99 | } |
100 | } |
101 | return prop; |
102 | } |
103 | |
104 | /** |
105 | * Store a properties file. The header and the date is not written. |
106 | * |
107 | * @param fileName the target file name |
108 | */ |
109 | public synchronized void store(String fileName) throws IOException { |
110 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
111 | store(out, null); |
112 | ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
113 | InputStreamReader reader = new InputStreamReader(in, "ISO8859-1"); |
114 | LineNumberReader r = new LineNumberReader(reader); |
115 | Writer w; |
116 | try { |
117 | w = new OutputStreamWriter(FileUtils.newOutputStream(fileName, false)); |
118 | } catch (Exception e) { |
119 | throw new IOException(e.toString(), e); |
120 | } |
121 | PrintWriter writer = new PrintWriter(new BufferedWriter(w)); |
122 | while (true) { |
123 | String line = r.readLine(); |
124 | if (line == null) { |
125 | break; |
126 | } |
127 | if (!line.startsWith("#")) { |
128 | writer.print(line + "\n"); |
129 | } |
130 | } |
131 | writer.close(); |
132 | } |
133 | |
134 | /** |
135 | * Convert the map to a list of line in the form key=value. |
136 | * |
137 | * @return the lines |
138 | */ |
139 | public synchronized String toLines() { |
140 | StringBuilder buff = new StringBuilder(); |
141 | for (Entry<Object, Object> e : new TreeMap<Object, Object>(this).entrySet()) { |
142 | buff.append(e.getKey()).append('=').append(e.getValue()).append('\n'); |
143 | } |
144 | return buff.toString(); |
145 | } |
146 | |
147 | /** |
148 | * Convert a String to a map. |
149 | * |
150 | * @param s the string |
151 | * @return the map |
152 | */ |
153 | public static SortedProperties fromLines(String s) { |
154 | SortedProperties p = new SortedProperties(); |
155 | for (String line : StringUtils.arraySplit(s, '\n', true)) { |
156 | int idx = line.indexOf('='); |
157 | if (idx > 0) { |
158 | p.put(line.substring(0, idx), line.substring(idx + 1)); |
159 | } |
160 | } |
161 | return p; |
162 | } |
163 | |
164 | } |