001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.IOException;
025: import java.io.PrintStream;
026: import java.io.PrintWriter;
027:
028: import java.util.Collections;
029: import java.util.Enumeration;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: /**
036: * <a href="PropertiesUtil.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class PropertiesUtil {
042:
043: public static void copyProperties(Properties from, Properties to) {
044: Iterator itr = from.entrySet().iterator();
045:
046: while (itr.hasNext()) {
047: Map.Entry entry = (Map.Entry) itr.next();
048:
049: to.setProperty((String) entry.getKey(), (String) entry
050: .getValue());
051: }
052: }
053:
054: public static Properties fromMap(Map map) {
055: if (map instanceof Properties) {
056: return (Properties) map;
057: }
058:
059: Properties p = new Properties();
060:
061: Iterator itr = map.entrySet().iterator();
062:
063: while (itr.hasNext()) {
064: Map.Entry entry = (Map.Entry) itr.next();
065:
066: String key = (String) entry.getKey();
067: String value = (String) entry.getValue();
068:
069: if (value != null) {
070: p.setProperty(key, value);
071: }
072: }
073:
074: return p;
075: }
076:
077: public static void fromProperties(Properties p, Map map) {
078: map.clear();
079:
080: Iterator itr = p.entrySet().iterator();
081:
082: while (itr.hasNext()) {
083: Map.Entry entry = (Map.Entry) itr.next();
084:
085: map.put(entry.getKey(), entry.getValue());
086: }
087: }
088:
089: public static Properties load(String s) throws IOException {
090: Properties p = new Properties();
091:
092: load(p, s);
093:
094: return p;
095: }
096:
097: public static void load(Properties p, String s) throws IOException {
098: if (Validator.isNotNull(s)) {
099: s = UnicodeFormatter.toString(s);
100:
101: s = StringUtil.replace(s, "\\u003d", "=");
102: s = StringUtil.replace(s, "\\u000a", "\n");
103: s = StringUtil.replace(s, "\\u0021", "!");
104: s = StringUtil.replace(s, "\\u0023", "#");
105: s = StringUtil.replace(s, "\\u0020", " ");
106: s = StringUtil.replace(s, "\\u005c", "\\");
107:
108: p.load(new ByteArrayInputStream(s.getBytes()));
109:
110: List propertyNames = Collections.list(p.propertyNames());
111:
112: for (int i = 0; i < propertyNames.size(); i++) {
113: String key = (String) propertyNames.get(i);
114:
115: String value = p.getProperty(key);
116:
117: // Trim values because it may leave a trailing \r in certain
118: // Windows environments. This is a known case for loading SQL
119: // scripts in SQL Server.
120:
121: if (value != null) {
122: value = value.trim();
123:
124: p.setProperty(key, value);
125: }
126: }
127: }
128: }
129:
130: public static void merge(Properties p1, Properties p2) {
131: Enumeration enu = p2.propertyNames();
132:
133: while (enu.hasMoreElements()) {
134: String key = (String) enu.nextElement();
135: String value = p2.getProperty(key);
136:
137: p1.setProperty(key, value);
138: }
139: }
140:
141: public static String list(Map map) {
142: Properties props = fromMap(map);
143:
144: ByteArrayMaker bam = new ByteArrayMaker();
145: PrintStream ps = new PrintStream(bam);
146:
147: props.list(ps);
148:
149: return bam.toString();
150: }
151:
152: public static void list(Map map, PrintStream out) {
153: Properties props = fromMap(map);
154:
155: props.list(out);
156: }
157:
158: public static void list(Map map, PrintWriter out) {
159: Properties props = fromMap(map);
160:
161: props.list(out);
162: }
163:
164: public static String toString(Properties p) {
165: StringMaker sm = new StringMaker();
166:
167: Enumeration enu = p.propertyNames();
168:
169: while (enu.hasMoreElements()) {
170: String key = (String) enu.nextElement();
171:
172: sm.append(key);
173: sm.append(StringPool.EQUAL);
174: sm.append(p.getProperty(key));
175: sm.append("\n");
176: }
177:
178: return sm.toString();
179: }
180:
181: public static void trimKeys(Properties p) {
182: Enumeration enu = p.propertyNames();
183:
184: while (enu.hasMoreElements()) {
185: String key = (String) enu.nextElement();
186: String value = p.getProperty(key);
187:
188: String trimmedKey = key.trim();
189:
190: if (!key.equals(trimmedKey)) {
191: p.remove(key);
192: p.setProperty(trimmedKey, value);
193: }
194: }
195: }
196:
197: }
|