1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.util.PropertyUtil |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.util; |
22 | |
23 | import java.util.Properties; |
24 | import java.util.Enumeration; |
25 | import java.io.InputStream; |
26 | import java.io.IOException; |
27 | |
28 | public class PropertyUtil { |
29 | |
30 | |
31 | ////////////////////////////////////////////////////////////////////////////// |
32 | // |
33 | // SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES |
34 | // |
35 | ///////////////////////////////////////////////////////////////////////////// |
36 | |
37 | /** |
38 | * Sorts a property list and turns the sorted list into a string. |
39 | * |
40 | * @param list property list to sort |
41 | * |
42 | * @return a string version of the sorted list |
43 | */ |
44 | public static String sortProperties( Properties list ) |
45 | { |
46 | // stringify them with no indentation |
47 | return sortProperties(list, null); |
48 | } |
49 | |
50 | /** |
51 | * Sorts property list and print out each key=value pair prepended with |
52 | * specific indentation. If indent is null, do not prepend with |
53 | * indentation. |
54 | * |
55 | * The output string shows up in two styles, style 1 looks like |
56 | * { key1=value1, key2=value2, key3=value3 } |
57 | * |
58 | * style 2 looks like |
59 | * key1=value1 |
60 | * key2=value2 |
61 | * key3=value3 |
62 | * where indent goes between the new line and the keys |
63 | * |
64 | * To get style 1, pass in a null indent |
65 | * To get sytle 2, pass in non-null indent (whatever you want to go before |
66 | * the key value) |
67 | */ |
68 | public static String sortProperties( Properties list, String indent ) |
69 | { |
70 | int size = list == null ? 0 : list.size(); |
71 | int count = 0; |
72 | String[] array = new String[size]; |
73 | String key; |
74 | String value; |
75 | StringBuffer buffer; |
76 | |
77 | // Calculate the number of properties in the property list and |
78 | // build an array of all the property names. |
79 | // We need to go thru the enumeration because Properties has a |
80 | // recursive list of defaults. |
81 | if (list != null) |
82 | { |
83 | for (Enumeration propertyNames = list.propertyNames(); |
84 | propertyNames.hasMoreElements(); ) |
85 | { |
86 | if (count == size) |
87 | { |
88 | // need to expand the array |
89 | size = size*2; |
90 | String[] expandedArray = new String[size]; |
91 | System.arraycopy(array, 0, expandedArray, 0, count); |
92 | array = expandedArray; |
93 | } |
94 | key = (String) propertyNames.nextElement(); |
95 | array[ count++ ] = key; |
96 | } |
97 | |
98 | // now sort the array |
99 | java.util.Arrays.sort(array, 0, count); |
100 | } |
101 | |
102 | // now stringify the array |
103 | buffer = new StringBuffer(); |
104 | if (indent == null) |
105 | buffer.append( "{ " ); |
106 | |
107 | for ( int ictr = 0; ictr < count; ictr++ ) |
108 | { |
109 | if ( ictr > 0 && indent == null) |
110 | buffer.append( ", " ); |
111 | |
112 | key = array[ ictr ]; |
113 | |
114 | if (indent != null) |
115 | buffer.append( indent ); |
116 | |
117 | buffer.append( key ); buffer.append( "=" ); |
118 | |
119 | value = list.getProperty( key, "MISSING_VALUE" ); |
120 | buffer.append( value ); |
121 | |
122 | if (indent != null) |
123 | buffer.append( "\n" ); |
124 | |
125 | } |
126 | if (indent == null) |
127 | buffer.append( " }" ); |
128 | |
129 | return buffer.toString(); |
130 | } |
131 | |
132 | /** |
133 | * Copy a set of properties from one Property to another. |
134 | * <p> |
135 | * |
136 | * @param src_prop Source set of properties to copy from. |
137 | * @param dest_prop Dest Properties to copy into. |
138 | * |
139 | **/ |
140 | public static void copyProperties(Properties src_prop, Properties dest_prop) |
141 | { |
142 | for (Enumeration propertyNames = src_prop.propertyNames(); |
143 | propertyNames.hasMoreElements(); ) |
144 | { |
145 | Object key = propertyNames.nextElement(); |
146 | dest_prop.put(key, src_prop.get(key)); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * Read a set of properties from the received input stream, strip |
152 | * off any excess white space that exists in those property values, |
153 | * and then add those newly-read properties to the received |
154 | * Properties object; not explicitly removing the whitespace here can |
155 | * lead to problems. |
156 | * |
157 | * This method exists because of the manner in which the jvm reads |
158 | * properties from file--extra spaces are ignored after a _key_, but |
159 | * if they exist at the _end_ of a property decl line (i.e. as part |
160 | * of a _value_), they are preserved, as outlined in the Java API: |
161 | * |
162 | * "Any whitespace after the key is skipped; if the first non- |
163 | * whitespace character after the key is = or :, then it is ignored |
164 | * and any whitespace characters after it are also skipped. All |
165 | * remaining characters on the line become part of the associated |
166 | * element string." |
167 | * |
168 | * @param iStr An input stream from which the new properties are to be |
169 | * loaded (should already be initialized). |
170 | * @param prop A set of properties to which the properties from |
171 | * iStr will be added (should already be initialized). |
172 | * properties loaded from 'iStr' (with the extra whitespace (if any) |
173 | * removed from all values), will be returned via the parameter. |
174 | * |
175 | **/ |
176 | public static void loadWithTrimmedValues(InputStream iStr, |
177 | Properties prop) throws IOException { |
178 | |
179 | if ((iStr == null) || (prop == null)) { |
180 | // shouldn't happen; just ignore this call and return. |
181 | return; |
182 | } |
183 | |
184 | // Else, load the properties from the received input stream. |
185 | Properties p = new Properties(); |
186 | p.load(iStr); |
187 | |
188 | // Now, trim off any excess whitespace, if any, and then |
189 | // add the properties from file to the received Properties |
190 | // set. |
191 | for (Enumeration propKeys = p.propertyNames(); |
192 | propKeys.hasMoreElements();) { |
193 | // get the value, trim off the whitespace, then store it |
194 | // in the received properties object. |
195 | String tmpKey = (String)propKeys.nextElement(); |
196 | String tmpValue = p.getProperty(tmpKey); |
197 | tmpValue = tmpValue.trim(); |
198 | prop.put(tmpKey, tmpValue); |
199 | } |
200 | |
201 | return; |
202 | |
203 | } |
204 | } |
205 | |