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 com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  
25  import java.io.BufferedReader;
26  import java.io.IOException;
27  import java.io.StringReader;
28  
29  import java.util.HashMap;
30  
31  /**
32   * <a href="UnicodeProperties.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * This is a rewrite of java.util.Properties that is not synchronized and
36   * natively supports non-ASCII encodings. It can also be configured to be
37   * "safe", allowing the values to have new line characters. When stored to a
38   * given BufferedWriter, "safe" properties will replace all new line characters
39   * with a _SAFE_NEWLINE_CHARACTER_.
40   * </p>
41   *
42   * <p>
43   * In its current form, this is not intended to replace java.util.Properties for
44   * reading properties flat files. This class is not thread-safe.
45   * </p>
46   *
47   * @author Alexander Chow
48   *
49   */
50  public class UnicodeProperties extends HashMap<String, String> {
51  
52      public UnicodeProperties() {
53          super();
54      }
55  
56      public UnicodeProperties(boolean safe) {
57          super();
58  
59          _safe = safe;
60      }
61  
62      public String getProperty(String key) {
63          return get(key);
64      }
65  
66      public String getProperty(String key, String defaultValue) {
67          if (containsKey(key)) {
68              return getProperty(key);
69          }
70          else {
71              return defaultValue;
72          }
73      }
74  
75      public boolean isSafe() {
76          return _safe;
77      }
78  
79      public void load(String props) throws IOException {
80          if (Validator.isNull(props)) {
81              return;
82          }
83  
84          BufferedReader br = null;
85  
86          try {
87              br = new BufferedReader(new StringReader(props));
88  
89              String line = br.readLine();
90  
91              while (line != null) {
92                  line = line.trim();
93  
94                  if (_isComment(line)) {
95                      line = br.readLine();
96  
97                      continue;
98                  }
99  
100                 int pos = line.indexOf(StringPool.EQUAL);
101 
102                 if (pos != -1) {
103                     String key = line.substring(0, pos).trim();
104                     String value = line.substring(pos + 1).trim();
105 
106                     if (_safe) {
107                         value = _decode(value);
108                     }
109 
110                     setProperty(key, value);
111                 }
112                 else {
113                     _log.error("Invalid property on line " + line);
114                 }
115 
116                 line = br.readLine();
117             }
118         }
119         finally {
120             if (br != null) {
121                 try {
122                     br.close();
123                 }
124                 catch (Exception e) {
125                 }
126             }
127         }
128     }
129 
130     public String put(String key, String value) {
131         if (key == null) {
132             return null;
133         }
134         else {
135             if (value == null) {
136                 return remove(key);
137             }
138             else {
139                 _length += key.length() + value.length() + 2;
140 
141                 return super.put(key, value);
142             }
143         }
144     }
145 
146     public String remove(Object key) {
147         if ((key == null) || !containsKey(key)) {
148             return null;
149         }
150         else {
151             String keyString = (String)key;
152 
153             String value = super.remove(key);
154 
155             _length -= keyString.length() + value.length() + 2;
156 
157             return value;
158         }
159     }
160 
161     public String setProperty(String key, String value) {
162         return put(key, value);
163     }
164 
165     public String toString() {
166         StringBuilder sb = new StringBuilder(_length);
167 
168         for (String key : keySet()) {
169             String value = get(key);
170 
171             if (Validator.isNotNull(value)) {
172                 if (_safe) {
173                     value = _encode(value);
174                 }
175 
176                 sb.append(key);
177                 sb.append(StringPool.EQUAL);
178                 sb.append(value);
179                 sb.append(StringPool.NEW_LINE);
180             }
181         }
182 
183         return sb.toString();
184     }
185 
186     protected int getToStringLength() {
187         return _length;
188     }
189 
190     private static String _decode(String value) {
191         return StringUtil.replace(
192             value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
193     }
194 
195     private static String _encode(String value) {
196         return StringUtil.replace(
197             value,
198             new String[] {
199                 StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
200                 StringPool.RETURN
201             },
202             new String[] {
203                 _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
204                 _SAFE_NEWLINE_CHARACTER
205             });
206     }
207 
208     private boolean _isComment(String line) {
209         return line.length() == 0 || line.startsWith(StringPool.POUND);
210     }
211 
212     private static final String _SAFE_NEWLINE_CHARACTER =
213         "_SAFE_NEWLINE_CHARACTER_";
214 
215     private static Log _log = LogFactoryUtil.getLog(UnicodeProperties.class);
216 
217     private boolean _safe = false;
218     private int _length;
219 
220 }