1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.Properties;
18  
19  /**
20   * <a href="SafeProperties.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Brian Wing Shun Chan
23   */
24  public class SafeProperties extends Properties {
25  
26      public SafeProperties() {
27          super();
28      }
29  
30      public synchronized Object get(Object key) {
31          Object value = super.get(key);
32  
33          value = _decode((String)value);
34  
35          return value;
36      }
37  
38      public String getEncodedProperty(String key) {
39          return super.getProperty(key);
40      }
41  
42      public String getProperty(String key) {
43          return (String)get(key);
44      }
45  
46      public synchronized Object put(Object key, Object value) {
47          if (key == null) {
48              return null;
49          }
50          else {
51              if (value == null) {
52                  return super.remove(key);
53              }
54              else {
55                  value = _encode((String)value);
56  
57                  return super.put(key, value);
58              }
59          }
60      }
61  
62      public synchronized Object remove(Object key) {
63          if (key == null) {
64              return null;
65          }
66          else {
67              return super.remove(key);
68          }
69      }
70  
71      private static String _decode(String value) {
72          return StringUtil.replace(
73              value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
74      }
75  
76      private static String _encode(String value) {
77          return StringUtil.replace(
78              value,
79              new String[] {
80                  StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
81                  StringPool.RETURN
82              },
83              new String[] {
84                  _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
85                  _SAFE_NEWLINE_CHARACTER
86              });
87      }
88  
89      private static final String _SAFE_NEWLINE_CHARACTER =
90          "_SAFE_NEWLINE_CHARACTER_";
91  
92  }