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