1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }