1 21 22 package org.apache.derby.iapi.util; 23 24 import java.util.Properties ; 25 import java.util.Enumeration ; 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 29 public class PropertyUtil { 30 31 32 38 45 public static String sortProperties( Properties list ) 46 { 47 return sortProperties(list, null); 49 } 50 51 69 public static String sortProperties( Properties list, String indent ) 70 { 71 int size = list == null ? 0 : list.size(); 72 int count = 0; 73 String [] array = new String [size]; 74 String key; 75 String value; 76 StringBuffer buffer; 77 78 if (list != null) 83 { 84 for (Enumeration propertyNames = list.propertyNames(); 85 propertyNames.hasMoreElements(); ) 86 { 87 if (count == size) 88 { 89 size = size*2; 91 String [] expandedArray = new String [size]; 92 System.arraycopy(array, 0, expandedArray, 0, count); 93 array = expandedArray; 94 } 95 key = (String ) propertyNames.nextElement(); 96 array[ count++ ] = key; 97 } 98 99 java.util.Arrays.sort(array, 0, count); 101 } 102 103 buffer = new StringBuffer (); 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 141 public static void copyProperties(Properties src_prop, Properties dest_prop) 142 { 143 for (Enumeration propertyNames = src_prop.propertyNames(); 144 propertyNames.hasMoreElements(); ) 145 { 146 Object key = propertyNames.nextElement(); 147 dest_prop.put(key, src_prop.get(key)); 148 } 149 } 150 151 177 public static void loadWithTrimmedValues(InputStream iStr, 178 Properties prop) throws IOException { 179 180 if ((iStr == null) || (prop == null)) { 181 return; 183 } 184 185 Properties p = new Properties (); 187 p.load(iStr); 188 189 for (Enumeration propKeys = p.propertyNames(); 193 propKeys.hasMoreElements();) { 194 String tmpKey = (String )propKeys.nextElement(); 197 String tmpValue = p.getProperty(tmpKey); 198 tmpValue = tmpValue.trim(); 199 prop.put(tmpKey, tmpValue); 200 } 201 202 return; 203 204 } 205 } 206 207 | Popular Tags |