KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > PropertyUtil


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.PropertyUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.util.Properties JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 public class PropertyUtil {
28
29
30     //////////////////////////////////////////////////////////////////////////////
31
//
32
// SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES
33
//
34
/////////////////////////////////////////////////////////////////////////////
35

36     /**
37       * Sorts a property list and turns the sorted list into a string.
38       *
39       * @param list property list to sort
40       *
41       * @return a string version of the sorted list
42       */

43     public static String JavaDoc sortProperties( Properties JavaDoc list )
44     {
45         // stringify them with no indentation
46
return sortProperties(list, null);
47     }
48
49     /**
50      * Sorts property list and print out each key=value pair prepended with
51      * specific indentation. If indent is null, do not prepend with
52      * indentation.
53      *
54      * The output string shows up in two styles, style 1 looks like
55      * { key1=value1, key2=value2, key3=value3 }
56      *
57      * style 2 looks like
58      * key1=value1
59      * key2=value2
60      * key3=value3
61      * where indent goes between the new line and the keys
62      *
63      * To get style 1, pass in a null indent
64      * To get sytle 2, pass in non-null indent (whatever you want to go before
65      * the key value)
66      */

67     public static String JavaDoc sortProperties( Properties JavaDoc list, char[] indent )
68     {
69         int size = list == null ? 0 : list.size();
70         int count = 0;
71         String JavaDoc[] array = new String JavaDoc[size];
72         String JavaDoc key;
73         String JavaDoc value;
74         StringBuffer JavaDoc buffer;
75
76         // Calculate the number of properties in the property list and
77
// build an array of all the property names.
78
// We need to go thru the enumeration because Properties has a
79
// recursive list of defaults.
80
if (list != null)
81         {
82             for (Enumeration JavaDoc propertyNames = list.propertyNames();
83                  propertyNames.hasMoreElements(); )
84             {
85                 if (count == size)
86                 {
87                     // need to expand the array
88
size = size*2;
89                     String JavaDoc[] expandedArray = new String JavaDoc[size];
90                     System.arraycopy(array, 0, expandedArray, 0, count);
91                     array = expandedArray;
92                 }
93                 key = (String JavaDoc) propertyNames.nextElement();
94                 array[ count++ ] = key;
95             }
96             // now sort the array
97
java.util.Arrays.sort( array, 0, count );
98         }
99
100
101         // now stringify the array
102
buffer = new StringBuffer JavaDoc();
103         if (indent == null)
104             buffer.append( "{ " );
105
106         for ( int ictr = 0; ictr < count; ictr++ )
107         {
108             if ( ictr > 0 && indent == null)
109                 buffer.append( ", " );
110
111             key = array[ ictr ];
112
113             if (indent != null)
114                 buffer.append( indent );
115
116             buffer.append( key ); buffer.append( "=" );
117
118             value = list.getProperty( key, "MISSING_VALUE" );
119             buffer.append( value );
120
121             if (indent != null)
122                 buffer.append( "\n" );
123
124         }
125         if (indent == null)
126             buffer.append( " }" );
127
128         return buffer.toString();
129     }
130
131     /**
132      * Copy a set of properties from one Property to another.
133      * <p>
134      *
135      * @return The identifier to be used to open the conglomerate later.
136      *
137      * @param src_prop Source set of properties to copy from.
138      * @param dest_prop Dest Properties to copy into.
139      *
140      **/

141     public static void copyProperties(Properties JavaDoc src_prop, Properties JavaDoc dest_prop)
142     {
143         for (Enumeration JavaDoc propertyNames = src_prop.propertyNames();
144              propertyNames.hasMoreElements(); )
145         {
146             String JavaDoc key = (String JavaDoc) propertyNames.nextElement();
147             dest_prop.put(key, src_prop.getProperty(key));
148         }
149     }
150 }
151
152
Popular Tags