Debian Codegraph @

src/main/org/h2/util/SortedProperties.java

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