KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > util > PropertyUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.util.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.derby.iapi.util;
23
24 import java.util.Properties JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 public class PropertyUtil {
30     
31
32     //////////////////////////////////////////////////////////////////////////////
33
//
34
// SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES
35
//
36
/////////////////////////////////////////////////////////////////////////////
37

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

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

69     public static String JavaDoc sortProperties( Properties JavaDoc list, String JavaDoc indent )
70     {
71         int size = list == null ? 0 : list.size();
72         int count = 0;
73         String JavaDoc[] array = new String JavaDoc[size];
74         String JavaDoc key;
75         String JavaDoc value;
76         StringBuffer JavaDoc buffer;
77
78         // Calculate the number of properties in the property list and
79
// build an array of all the property names.
80
// We need to go thru the enumeration because Properties has a
81
// recursive list of defaults.
82
if (list != null)
83         {
84             for (Enumeration JavaDoc propertyNames = list.propertyNames();
85                  propertyNames.hasMoreElements(); )
86             {
87                 if (count == size)
88                 {
89                     // need to expand the array
90
size = size*2;
91                     String JavaDoc[] expandedArray = new String JavaDoc[size];
92                     System.arraycopy(array, 0, expandedArray, 0, count);
93                     array = expandedArray;
94                 }
95                 key = (String JavaDoc) propertyNames.nextElement();
96                 array[ count++ ] = key;
97             }
98
99             // now sort the array
100
java.util.Arrays.sort(array, 0, count);
101         }
102
103         // now stringify the array
104
buffer = new StringBuffer JavaDoc();
105         if (indent == null)
106             buffer.append( "{ " );
107
108         for ( int ictr = 0; ictr < count; ictr++ )
109         {
110             if ( ictr > 0 && indent == null)
111                 buffer.append( ", " );
112
113             key = array[ ictr ];
114
115             if (indent != null)
116                 buffer.append( indent );
117
118             buffer.append( key ); buffer.append( "=" );
119
120             value = list.getProperty( key, "MISSING_VALUE" );
121             buffer.append( value );
122
123             if (indent != null)
124                 buffer.append( "\n" );
125
126         }
127         if (indent == null)
128             buffer.append( " }" );
129
130         return buffer.toString();
131     }
132
133     /**
134      * Copy a set of properties from one Property to another.
135      * <p>
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             Object JavaDoc key = propertyNames.nextElement();
147             dest_prop.put(key, src_prop.get(key));
148         }
149     }
150
151     /**
152      * Read a set of properties from the received input stream, strip
153      * off any excess white space that exists in those property values,
154      * and then add those newly-read properties to the received
155      * Properties object; not explicitly removing the whitespace here can
156      * lead to problems.
157      *
158      * This method exists because of the manner in which the jvm reads
159      * properties from file--extra spaces are ignored after a _key_, but
160      * if they exist at the _end_ of a property decl line (i.e. as part
161      * of a _value_), they are preserved, as outlined in the Java API:
162      *
163      * "Any whitespace after the key is skipped; if the first non-
164      * whitespace character after the key is = or :, then it is ignored
165      * and any whitespace characters after it are also skipped. All
166      * remaining characters on the line become part of the associated
167      * element string."
168      *
169      * @param iStr An input stream from which the new properties are to be
170      * loaded (should already be initialized).
171      * @param prop A set of properties to which the properties from
172      * iStr will be added (should already be initialized).
173      * properties loaded from 'iStr' (with the extra whitespace (if any)
174      * removed from all values), will be returned via the parameter.
175      *
176      **/

177     public static void loadWithTrimmedValues(InputStream JavaDoc iStr,
178         Properties JavaDoc prop) throws IOException JavaDoc {
179
180         if ((iStr == null) || (prop == null)) {
181         // shouldn't happen; just ignore this call and return.
182
return;
183         }
184
185         // Else, load the properties from the received input stream.
186
Properties JavaDoc p = new Properties JavaDoc();
187         p.load(iStr);
188
189         // Now, trim off any excess whitespace, if any, and then
190
// add the properties from file to the received Properties
191
// set.
192
for (Enumeration JavaDoc propKeys = p.propertyNames();
193           propKeys.hasMoreElements();) {
194         // get the value, trim off the whitespace, then store it
195
// in the received properties object.
196
String JavaDoc tmpKey = (String JavaDoc)propKeys.nextElement();
197             String JavaDoc tmpValue = p.getProperty(tmpKey);
198             tmpValue = tmpValue.trim();
199             prop.put(tmpKey, tmpValue);
200         }
201
202         return;
203
204     }
205 }
206
207
Popular Tags