001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.harness.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.derbyTesting.functionTests.harness;
023:
024: import java.util.Properties;
025: import java.util.Enumeration;
026:
027: public class PropertyUtil {
028:
029: //////////////////////////////////////////////////////////////////////////////
030: //
031: // SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES
032: //
033: /////////////////////////////////////////////////////////////////////////////
034:
035: /**
036: * Sorts a property list and turns the sorted list into a string.
037: *
038: * @param list property list to sort
039: *
040: * @return a string version of the sorted list
041: */
042: public static String sortProperties(Properties list) {
043: // stringify them with no indentation
044: return sortProperties(list, null);
045: }
046:
047: /**
048: * Sorts property list and print out each key=value pair prepended with
049: * specific indentation. If indent is null, do not prepend with
050: * indentation.
051: *
052: * The output string shows up in two styles, style 1 looks like
053: * { key1=value1, key2=value2, key3=value3 }
054: *
055: * style 2 looks like
056: * key1=value1
057: * key2=value2
058: * key3=value3
059: * where indent goes between the new line and the keys
060: *
061: * To get style 1, pass in a null indent
062: * To get sytle 2, pass in non-null indent (whatever you want to go before
063: * the key value)
064: */
065: public static String sortProperties(Properties list, char[] indent) {
066: int size = list == null ? 0 : list.size();
067: int count = 0;
068: String[] array = new String[size];
069: String key;
070: String value;
071: StringBuffer buffer;
072:
073: // Calculate the number of properties in the property list and
074: // build an array of all the property names.
075: // We need to go thru the enumeration because Properties has a
076: // recursive list of defaults.
077: if (list != null) {
078: for (Enumeration propertyNames = list.propertyNames(); propertyNames
079: .hasMoreElements();) {
080: if (count == size) {
081: // need to expand the array
082: size = size * 2;
083: String[] expandedArray = new String[size];
084: System.arraycopy(array, 0, expandedArray, 0, count);
085: array = expandedArray;
086: }
087: key = (String) propertyNames.nextElement();
088: array[count++] = key;
089: }
090: // now sort the array
091: java.util.Arrays.sort(array, 0, count);
092: }
093:
094: // now stringify the array
095: buffer = new StringBuffer();
096: if (indent == null)
097: buffer.append("{ ");
098:
099: for (int ictr = 0; ictr < count; ictr++) {
100: if (ictr > 0 && indent == null)
101: buffer.append(", ");
102:
103: key = array[ictr];
104:
105: if (indent != null)
106: buffer.append(indent);
107:
108: buffer.append(key);
109: buffer.append("=");
110:
111: value = list.getProperty(key, "MISSING_VALUE");
112: buffer.append(value);
113:
114: if (indent != null)
115: buffer.append("\n");
116:
117: }
118: if (indent == null)
119: buffer.append(" }");
120:
121: return buffer.toString();
122: }
123:
124: /**
125: * Copy a set of properties from one Property to another.
126: * <p>
127: *
128: * @return The identifier to be used to open the conglomerate later.
129: *
130: * @param src_prop Source set of properties to copy from.
131: * @param dest_prop Dest Properties to copy into.
132: *
133: **/
134: public static void copyProperties(Properties src_prop,
135: Properties dest_prop) {
136: for (Enumeration propertyNames = src_prop.propertyNames(); propertyNames
137: .hasMoreElements();) {
138: String key = (String) propertyNames.nextElement();
139: dest_prop.put(key, src_prop.getProperty(key));
140: }
141: }
142: }
|