1   /**
2    * Copyright (c) 2000-2007 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         StringMaker sm = new StringMaker();
167 
168         Enumeration enu = p.propertyNames();
169 
170         while (enu.hasMoreElements()) {
171             String key = (String)enu.nextElement();
172 
173             sm.append(key);
174             sm.append(StringPool.EQUAL);
175             sm.append(p.getProperty(key));
176             sm.append("\n");
177         }
178 
179         return sm.toString();
180     }
181 
182     public static void trimKeys(Properties p) {
183         Enumeration enu = p.propertyNames();
184 
185         while (enu.hasMoreElements()) {
186             String key = (String)enu.nextElement();
187             String value = p.getProperty(key);
188 
189             String trimmedKey = key.trim();
190 
191             if (!key.equals(trimmedKey)) {
192                 p.remove(key);
193                 p.setProperty(trimmedKey, value);
194             }
195         }
196     }
197 
198 }