1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.io.PrintWriter;
26  
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Properties;
33  
34  /**
35   * <a href="PropertiesUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class PropertiesUtil {
41  
42      public static void copyProperties(Properties from, Properties to) {
43          Iterator itr = from.entrySet().iterator();
44  
45          while (itr.hasNext()) {
46              Map.Entry entry = (Map.Entry)itr.next();
47  
48              to.setProperty((String)entry.getKey(), (String)entry.getValue());
49          }
50      }
51  
52      public static Properties fromMap(Map map) {
53          if (map instanceof Properties) {
54              return (Properties)map;
55          }
56  
57          Properties p = new Properties();
58  
59          Iterator itr = map.entrySet().iterator();
60  
61          while (itr.hasNext()) {
62              Map.Entry entry = (Map.Entry)itr.next();
63  
64              String key = (String)entry.getKey();
65              String value = (String)entry.getValue();
66  
67              if (value != null) {
68                  p.setProperty(key, value);
69              }
70          }
71  
72          return p;
73      }
74  
75      public static void fromProperties(Properties p, Map map) {
76          map.clear();
77  
78          Iterator itr = p.entrySet().iterator();
79  
80          while (itr.hasNext()) {
81              Map.Entry entry = (Map.Entry)itr.next();
82  
83              map.put(entry.getKey(), entry.getValue());
84          }
85      }
86  
87      public static Properties load(String s) throws IOException {
88          Properties p = new Properties();
89  
90          load(p, s);
91  
92          return p;
93      }
94  
95      public static void load(Properties p, String s) throws IOException {
96          if (Validator.isNotNull(s)) {
97              s = UnicodeFormatter.toString(s);
98  
99              s = StringUtil.replace(s, "\\u003d", "=");
100             s = StringUtil.replace(s, "\\u000a", "\n");
101             s = StringUtil.replace(s, "\\u0021", "!");
102             s = StringUtil.replace(s, "\\u0023", "#");
103             s = StringUtil.replace(s, "\\u0020", " ");
104             s = StringUtil.replace(s, "\\u005c", "\\");
105 
106             p.load(new ByteArrayInputStream(s.getBytes()));
107 
108             List propertyNames = Collections.list(p.propertyNames());
109 
110             for (int i = 0; i < propertyNames.size(); i++) {
111                 String key = (String)propertyNames.get(i);
112 
113                 String value = p.getProperty(key);
114 
115                 // Trim values because it may leave a trailing \r in certain
116                 // Windows environments. This is a known case for loading SQL
117                 // scripts in SQL Server.
118 
119                 if (value != null) {
120                     value = value.trim();
121 
122                     p.setProperty(key, value);
123                 }
124             }
125         }
126     }
127 
128     public static void merge(Properties p1, Properties p2) {
129         Enumeration enu = p2.propertyNames();
130 
131         while (enu.hasMoreElements()) {
132             String key = (String)enu.nextElement();
133             String value = p2.getProperty(key);
134 
135             p1.setProperty(key, value);
136         }
137     }
138 
139     public static String list(Map map) {
140         Properties props = fromMap(map);
141 
142         ByteArrayMaker bam = new ByteArrayMaker();
143         PrintStream ps = new PrintStream(bam);
144 
145         props.list(ps);
146 
147         return bam.toString();
148     }
149 
150     public static void list(Map map, PrintStream out) {
151         Properties props = fromMap(map);
152 
153         props.list(out);
154     }
155 
156     public static void list(Map map, PrintWriter out) {
157         Properties props = fromMap(map);
158 
159         props.list(out);
160     }
161 
162     public static String toString(Properties p) {
163         SafeProperties safeProperties = null;
164 
165         if (p instanceof SafeProperties) {
166             safeProperties = (SafeProperties)p;
167         }
168 
169         StringBuilder sb = new StringBuilder();
170 
171         Enumeration enu = p.propertyNames();
172 
173         while (enu.hasMoreElements()) {
174             String key = (String)enu.nextElement();
175 
176             sb.append(key);
177             sb.append(StringPool.EQUAL);
178 
179             if (safeProperties != null) {
180                 sb.append(safeProperties.getEncodedProperty(key));
181             }
182             else {
183                 sb.append(p.getProperty(key));
184             }
185 
186             sb.append(StringPool.NEW_LINE);
187         }
188 
189         return sb.toString();
190     }
191 
192     public static void trimKeys(Properties p) {
193         Enumeration enu = p.propertyNames();
194 
195         while (enu.hasMoreElements()) {
196             String key = (String)enu.nextElement();
197             String value = p.getProperty(key);
198 
199             String trimmedKey = key.trim();
200 
201             if (!key.equals(trimmedKey)) {
202                 p.remove(key);
203                 p.setProperty(trimmedKey, value);
204             }
205         }
206     }
207 
208 }