001: /*
002:
003: Derby - Class org.apache.derby.iapi.util.PropertyUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.util;
023:
024: import java.util.Properties;
025: import java.util.Enumeration;
026: import java.io.InputStream;
027: import java.io.IOException;
028:
029: public class PropertyUtil {
030:
031: //////////////////////////////////////////////////////////////////////////////
032: //
033: // SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES
034: //
035: /////////////////////////////////////////////////////////////////////////////
036:
037: /**
038: * Sorts a property list and turns the sorted list into a string.
039: *
040: * @param list property list to sort
041: *
042: * @return a string version of the sorted list
043: */
044: public static String sortProperties(Properties list) {
045: // stringify them with no indentation
046: return sortProperties(list, null);
047: }
048:
049: /**
050: * Sorts property list and print out each key=value pair prepended with
051: * specific indentation. If indent is null, do not prepend with
052: * indentation.
053: *
054: * The output string shows up in two styles, style 1 looks like
055: * { key1=value1, key2=value2, key3=value3 }
056: *
057: * style 2 looks like
058: * key1=value1
059: * key2=value2
060: * key3=value3
061: * where indent goes between the new line and the keys
062: *
063: * To get style 1, pass in a null indent
064: * To get sytle 2, pass in non-null indent (whatever you want to go before
065: * the key value)
066: */
067: public static String sortProperties(Properties list, String indent) {
068: int size = list == null ? 0 : list.size();
069: int count = 0;
070: String[] array = new String[size];
071: String key;
072: String value;
073: StringBuffer buffer;
074:
075: // Calculate the number of properties in the property list and
076: // build an array of all the property names.
077: // We need to go thru the enumeration because Properties has a
078: // recursive list of defaults.
079: if (list != null) {
080: for (Enumeration propertyNames = list.propertyNames(); propertyNames
081: .hasMoreElements();) {
082: if (count == size) {
083: // need to expand the array
084: size = size * 2;
085: String[] expandedArray = new String[size];
086: System.arraycopy(array, 0, expandedArray, 0, count);
087: array = expandedArray;
088: }
089: key = (String) propertyNames.nextElement();
090: array[count++] = key;
091: }
092:
093: // now sort the array
094: java.util.Arrays.sort(array, 0, count);
095: }
096:
097: // now stringify the array
098: buffer = new StringBuffer();
099: if (indent == null)
100: buffer.append("{ ");
101:
102: for (int ictr = 0; ictr < count; ictr++) {
103: if (ictr > 0 && indent == null)
104: buffer.append(", ");
105:
106: key = array[ictr];
107:
108: if (indent != null)
109: buffer.append(indent);
110:
111: buffer.append(key);
112: buffer.append("=");
113:
114: value = list.getProperty(key, "MISSING_VALUE");
115: buffer.append(value);
116:
117: if (indent != null)
118: buffer.append("\n");
119:
120: }
121: if (indent == null)
122: buffer.append(" }");
123:
124: return buffer.toString();
125: }
126:
127: /**
128: * Copy a set of properties from one Property to another.
129: * <p>
130: *
131: * @param src_prop Source set of properties to copy from.
132: * @param dest_prop Dest Properties to copy into.
133: *
134: **/
135: public static void copyProperties(Properties src_prop,
136: Properties dest_prop) {
137: for (Enumeration propertyNames = src_prop.propertyNames(); propertyNames
138: .hasMoreElements();) {
139: Object key = propertyNames.nextElement();
140: dest_prop.put(key, src_prop.get(key));
141: }
142: }
143:
144: /**
145: * Read a set of properties from the received input stream, strip
146: * off any excess white space that exists in those property values,
147: * and then add those newly-read properties to the received
148: * Properties object; not explicitly removing the whitespace here can
149: * lead to problems.
150: *
151: * This method exists because of the manner in which the jvm reads
152: * properties from file--extra spaces are ignored after a _key_, but
153: * if they exist at the _end_ of a property decl line (i.e. as part
154: * of a _value_), they are preserved, as outlined in the Java API:
155: *
156: * "Any whitespace after the key is skipped; if the first non-
157: * whitespace character after the key is = or :, then it is ignored
158: * and any whitespace characters after it are also skipped. All
159: * remaining characters on the line become part of the associated
160: * element string."
161: *
162: * @param iStr An input stream from which the new properties are to be
163: * loaded (should already be initialized).
164: * @param prop A set of properties to which the properties from
165: * iStr will be added (should already be initialized).
166: * properties loaded from 'iStr' (with the extra whitespace (if any)
167: * removed from all values), will be returned via the parameter.
168: *
169: **/
170: public static void loadWithTrimmedValues(InputStream iStr,
171: Properties prop) throws IOException {
172:
173: if ((iStr == null) || (prop == null)) {
174: // shouldn't happen; just ignore this call and return.
175: return;
176: }
177:
178: // Else, load the properties from the received input stream.
179: Properties p = new Properties();
180: p.load(iStr);
181:
182: // Now, trim off any excess whitespace, if any, and then
183: // add the properties from file to the received Properties
184: // set.
185: for (Enumeration propKeys = p.propertyNames(); propKeys
186: .hasMoreElements();) {
187: // get the value, trim off the whitespace, then store it
188: // in the received properties object.
189: String tmpKey = (String) propKeys.nextElement();
190: String tmpValue = p.getProperty(tmpKey);
191: tmpValue = tmpValue.trim();
192: prop.put(tmpKey, tmpValue);
193: }
194:
195: return;
196:
197: }
198: }
|