File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\SortedProperties.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import java.io.BufferedWriter;
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.io.LineNumberReader;
16 import java.io.OutputStreamWriter;
17 import java.io.PrintWriter;
18 import java.io.Writer;
19 import java.util.Collections;
20 import java.util.Enumeration;
21 import java.util.Properties;
22 import java.util.TreeMap;
23 import java.util.Vector;
24 import java.util.Map.Entry;
25 import org.h2.message.DbException;
26 import org.h2.message.TraceSystem;
27 import org.h2.store.fs.FileUtils;
28
29 /**
30 * Sorted properties file.
31 * This implementation requires that store() internally calls keys().
32 */
33 public class SortedProperties extends Properties {
34
35 private static final long serialVersionUID = 1L;
36
37 public synchronized Enumeration<Object> keys() {
38 Vector<String> v = new Vector<String>();
39 for (Object o : keySet()) {
40 v.add(o.toString());
41 }
42 Collections.sort(v);
43 return new Vector<Object>(v).elements();
44 }
45
46 /**
47 * Get a boolean property value from a properties object.
48 *
49 * @param prop the properties object
50 * @param key the key
51 * @param def the default value
52 * @return the value if set, or the default value if not
53 */
54 public static boolean getBooleanProperty(Properties prop, String key, boolean def) {
55 String value = prop.getProperty(key, "" + def);
56 try {
57 return Boolean.parseBoolean(value);
58 } catch (Exception e) {
59 TraceSystem.traceThrowable(e);
60 return def;
61 }
62 }
63
64 /**
65 * Get an int property value from a properties object.
66 *
67 * @param prop the properties object
68 * @param key the key
69 * @param def the default value
70 * @return the value if set, or the default value if not
71 */
72 public static int getIntProperty(Properties prop, String key, int def) {
73 String value = prop.getProperty(key, "" + def);
74 try {
75 return Integer.decode(value);
76 } catch (Exception e) {
77 TraceSystem.traceThrowable(e);
78 return def;
79 }
80 }
81
82 /**
83 * Load a properties object from a file.
84 *
85 * @param fileName the name of the properties file
86 * @return the properties object
87 */
88 public static synchronized SortedProperties loadProperties(String fileName) 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 DbException.convertToIOException(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 }
165