Source Code Cross Referenced for PropertyUtil.java in  » Database-DBMS » db-derby-10.2 » org » apache » derby » iapi » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derby.iapi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derby.iapi.util.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.derby.iapi.util;
023:
024:        import java.util.Properties;
025:        import java.util.Enumeration;
026:        import java.io.InputStream;
027:        import java.io.IOException;
028:
029:        public class PropertyUtil {
030:
031:            //////////////////////////////////////////////////////////////////////////////
032:            //
033:            //	SORTS A PROPERTY LIST AND STRINGIFIES THE SORTED PROPERTIES
034:            //
035:            /////////////////////////////////////////////////////////////////////////////
036:
037:            /**
038:             *	Sorts a property list and turns the sorted list into a string.
039:             *
040:             *	@param	list	property list to sort
041:             *
042:             *	@return	a string version of the sorted list
043:             */
044:            public static String sortProperties(Properties list) {
045:                // stringify them with no indentation
046:                return sortProperties(list, null);
047:            }
048:
049:            /**
050:             * Sorts property list and print out each key=value pair prepended with 
051:             * specific indentation.  If indent is null, do not prepend with
052:             * indentation. 
053:             *
054:             * The output string shows up in two styles, style 1 looks like
055:             * { key1=value1, key2=value2, key3=value3 }
056:             *
057:             * style 2 looks like
058:             *		key1=value1
059:             *		key2=value2
060:             *		key3=value3
061:             * where indent goes between the new line and the keys
062:             *
063:             * To get style 1, pass in a null indent
064:             * To get sytle 2, pass in non-null indent (whatever you want to go before
065:             * the key value)
066:             */
067:            public static String sortProperties(Properties list, String indent) {
068:                int size = list == null ? 0 : list.size();
069:                int count = 0;
070:                String[] array = new String[size];
071:                String key;
072:                String value;
073:                StringBuffer buffer;
074:
075:                // Calculate the number of properties in the property list and
076:                // build an array of all the property names.
077:                // We need to go thru the enumeration because Properties has a
078:                // recursive list of defaults.
079:                if (list != null) {
080:                    for (Enumeration propertyNames = list.propertyNames(); propertyNames
081:                            .hasMoreElements();) {
082:                        if (count == size) {
083:                            // need to expand the array
084:                            size = size * 2;
085:                            String[] expandedArray = new String[size];
086:                            System.arraycopy(array, 0, expandedArray, 0, count);
087:                            array = expandedArray;
088:                        }
089:                        key = (String) propertyNames.nextElement();
090:                        array[count++] = key;
091:                    }
092:
093:                    // now sort the array
094:                    java.util.Arrays.sort(array, 0, count);
095:                }
096:
097:                // now stringify the array
098:                buffer = new StringBuffer();
099:                if (indent == null)
100:                    buffer.append("{ ");
101:
102:                for (int ictr = 0; ictr < count; ictr++) {
103:                    if (ictr > 0 && indent == null)
104:                        buffer.append(", ");
105:
106:                    key = array[ictr];
107:
108:                    if (indent != null)
109:                        buffer.append(indent);
110:
111:                    buffer.append(key);
112:                    buffer.append("=");
113:
114:                    value = list.getProperty(key, "MISSING_VALUE");
115:                    buffer.append(value);
116:
117:                    if (indent != null)
118:                        buffer.append("\n");
119:
120:                }
121:                if (indent == null)
122:                    buffer.append(" }");
123:
124:                return buffer.toString();
125:            }
126:
127:            /**
128:             * Copy a set of properties from one Property to another.
129:             * <p>
130:             *
131:             * @param src_prop  Source set of properties to copy from.
132:             * @param dest_prop Dest Properties to copy into.
133:             *
134:             **/
135:            public static void copyProperties(Properties src_prop,
136:                    Properties dest_prop) {
137:                for (Enumeration propertyNames = src_prop.propertyNames(); propertyNames
138:                        .hasMoreElements();) {
139:                    Object key = propertyNames.nextElement();
140:                    dest_prop.put(key, src_prop.get(key));
141:                }
142:            }
143:
144:            /** 
145:             * Read a set of properties from the received input stream, strip
146:             * off any excess white space that exists in those property values,
147:             * and then add those newly-read properties to the received
148:             * Properties object; not explicitly removing the whitespace here can
149:             * lead to problems.
150:             *
151:             * This method exists because of the manner in which the jvm reads
152:             * properties from file--extra spaces are ignored after a _key_, but
153:             * if they exist at the _end_ of a property decl line (i.e. as part
154:             * of a _value_), they are preserved, as outlined in the Java API:
155:             *
156:             * "Any whitespace after the key is skipped; if the first non-
157:             * whitespace character after the key is = or :, then it is ignored
158:             * and any whitespace characters after it are also skipped. All
159:             * remaining characters on the line become part of the associated
160:             * element string."
161:             *
162:             * @param	iStr An input stream from which the new properties are to be
163:             *  loaded (should already be initialized).
164:             * @param prop A set of properties to which the properties from
165:             *  iStr will be added (should already be initialized).
166:             * properties loaded from 'iStr' (with the extra whitespace (if any)
167:             *  removed from all values), will be returned via the parameter.
168:             *
169:             **/
170:            public static void loadWithTrimmedValues(InputStream iStr,
171:                    Properties prop) throws IOException {
172:
173:                if ((iStr == null) || (prop == null)) {
174:                    // shouldn't happen; just ignore this call and return.
175:                    return;
176:                }
177:
178:                // Else, load the properties from the received input stream.
179:                Properties p = new Properties();
180:                p.load(iStr);
181:
182:                // Now, trim off any excess whitespace, if any, and then
183:                // add the properties from file to the received Properties
184:                // set.
185:                for (Enumeration propKeys = p.propertyNames(); propKeys
186:                        .hasMoreElements();) {
187:                    // get the value, trim off the whitespace, then store it
188:                    // in the received properties object.
189:                    String tmpKey = (String) propKeys.nextElement();
190:                    String tmpValue = p.getProperty(tmpKey);
191:                    tmpValue = tmpValue.trim();
192:                    prop.put(tmpKey, tmpValue);
193:                }
194:
195:                return;
196:
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.