Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / iapi / util / PropertyUtil.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/util/PropertyUtil.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/util/PropertyUtil.java
new file mode 100644 (file)
index 0000000..22378b4
--- /dev/null
@@ -0,0 +1,206 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.iapi.util.PropertyUtil\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.util;\r
+\r
+import java.util.Properties;\r
+import java.util.Enumeration;\r
+import java.io.InputStream;\r
+import java.io.IOException;\r
+\r
+public class PropertyUtil {\r
+       \r
+\r
+       //////////////////////////////////////////////////////////////////////////////\r
+       //\r
+       //      SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES\r
+       //\r
+       /////////////////////////////////////////////////////////////////////////////\r
+\r
+       /**\r
+         *     Sorts a property list and turns the sorted list into a string.\r
+         *\r
+         *     @param  list    property list to sort\r
+         *\r
+         *     @return a string version of the sorted list\r
+         */\r
+       public  static  String  sortProperties( Properties list )\r
+       {\r
+               // stringify them with no indentation\r
+               return sortProperties(list, null);\r
+       }\r
+\r
+       /**\r
+        * Sorts property list and print out each key=value pair prepended with \r
+        * specific indentation.  If indent is null, do not prepend with\r
+        * indentation. \r
+        *\r
+        * The output string shows up in two styles, style 1 looks like\r
+        * { key1=value1, key2=value2, key3=value3 }\r
+        *\r
+        * style 2 looks like\r
+        *              key1=value1\r
+        *              key2=value2\r
+        *              key3=value3\r
+        * where indent goes between the new line and the keys\r
+        *\r
+        * To get style 1, pass in a null indent\r
+        * To get sytle 2, pass in non-null indent (whatever you want to go before\r
+        * the key value)\r
+        */\r
+       public  static  String  sortProperties( Properties list, String indent )\r
+       {\r
+               int                             size = list == null ? 0 : list.size();\r
+               int                             count = 0;\r
+               String[]                array = new String[size];\r
+               String                  key;\r
+               String                  value;\r
+               StringBuffer    buffer;\r
+\r
+               // Calculate the number of properties in the property list and\r
+               // build an array of all the property names.\r
+               // We need to go thru the enumeration because Properties has a\r
+               // recursive list of defaults.\r
+               if (list != null)\r
+               {\r
+                       for (Enumeration propertyNames = list.propertyNames();\r
+                                propertyNames.hasMoreElements(); )\r
+                       {\r
+                               if (count == size)\r
+                               {\r
+                                       // need to expand the array\r
+                                       size = size*2;\r
+                                       String[] expandedArray = new String[size];\r
+                                       System.arraycopy(array, 0, expandedArray, 0, count);\r
+                                       array = expandedArray;\r
+                               }\r
+                               key = (String) propertyNames.nextElement();\r
+                               array[ count++ ] = key;\r
+                       }\r
+\r
+                       // now sort the array\r
+                       java.util.Arrays.sort(array, 0, count);\r
+               }\r
+\r
+               // now stringify the array\r
+               buffer = new StringBuffer();\r
+               if (indent == null)\r
+                       buffer.append( "{ " );\r
+\r
+               for ( int ictr = 0; ictr < count; ictr++ )\r
+               {\r
+                       if ( ictr > 0 && indent == null)\r
+                               buffer.append( ", " );\r
+\r
+                       key = array[ ictr ];\r
+\r
+                       if (indent != null)\r
+                               buffer.append( indent );\r
+\r
+                       buffer.append( key ); buffer.append( "=" );\r
+\r
+                       value = list.getProperty( key, "MISSING_VALUE" );\r
+                       buffer.append( value );\r
+\r
+                       if (indent != null)\r
+                               buffer.append( "\n" );\r
+\r
+               }\r
+               if (indent == null)\r
+                       buffer.append( " }" );\r
+\r
+               return  buffer.toString();\r
+       }\r
+\r
+    /**\r
+     * Copy a set of properties from one Property to another.\r
+     * <p>\r
+     *\r
+     * @param src_prop  Source set of properties to copy from.\r
+     * @param dest_prop Dest Properties to copy into.\r
+     *\r
+     **/\r
+    public static void copyProperties(Properties src_prop, Properties dest_prop)\r
+    {\r
+        for (Enumeration propertyNames = src_prop.propertyNames();\r
+             propertyNames.hasMoreElements(); )\r
+        {\r
+            Object key = propertyNames.nextElement();\r
+            dest_prop.put(key, src_prop.get(key));\r
+        }\r
+    }\r
+\r
+       /** \r
+        * Read a set of properties from the received input stream, strip\r
+        * off any excess white space that exists in those property values,\r
+        * and then add those newly-read properties to the received\r
+        * Properties object; not explicitly removing the whitespace here can\r
+        * lead to problems.\r
+        *\r
+        * This method exists because of the manner in which the jvm reads\r
+        * properties from file--extra spaces are ignored after a _key_, but\r
+        * if they exist at the _end_ of a property decl line (i.e. as part\r
+        * of a _value_), they are preserved, as outlined in the Java API:\r
+        *\r
+        * "Any whitespace after the key is skipped; if the first non-\r
+        * whitespace character after the key is = or :, then it is ignored\r
+        * and any whitespace characters after it are also skipped. All\r
+        * remaining characters on the line become part of the associated\r
+        * element string."\r
+        *\r
+        * @param       iStr An input stream from which the new properties are to be\r
+        *  loaded (should already be initialized).\r
+        * @param prop A set of properties to which the properties from\r
+        *  iStr will be added (should already be initialized).\r
+        * properties loaded from 'iStr' (with the extra whitespace (if any)\r
+        *  removed from all values), will be returned via the parameter.\r
+        *\r
+        **/\r
+       public static void loadWithTrimmedValues(InputStream iStr,\r
+               Properties prop) throws IOException {\r
+\r
+               if ((iStr == null) || (prop == null)) {\r
+               // shouldn't happen; just ignore this call and return.\r
+                       return;\r
+               }\r
+\r
+               // Else, load the properties from the received input stream.\r
+               Properties p = new Properties();\r
+               p.load(iStr);\r
+\r
+               // Now, trim off any excess whitespace, if any, and then\r
+               // add the properties from file to the received Properties\r
+               // set.\r
+               for (Enumeration propKeys = p.propertyNames();\r
+                 propKeys.hasMoreElements();) {\r
+               // get the value, trim off the whitespace, then store it\r
+               // in the received properties object.\r
+                       String tmpKey = (String)propKeys.nextElement();\r
+                       String tmpValue = p.getProperty(tmpKey);\r
+                       tmpValue = tmpValue.trim();\r
+                       prop.put(tmpKey, tmpValue);\r
+               }\r
+\r
+               return;\r
+\r
+       }\r
+}\r
+\r