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