001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021
022 import java.io.IOException;
023
024 import java.util.HashMap;
025 import java.util.Map;
026
027
043 public class UnicodeProperties extends HashMap<String, String> {
044
045 public UnicodeProperties() {
046 super();
047 }
048
049 public UnicodeProperties(boolean safe) {
050 super();
051
052 _safe = safe;
053 }
054
055 public void fastLoad(String props) {
056 if (Validator.isNull(props)) {
057 return;
058 }
059
060 int x = props.indexOf(CharPool.NEW_LINE);
061 int y = 0;
062
063 while (x != -1) {
064 put(props.substring(y, x));
065
066 y = x;
067
068 x = props.indexOf(CharPool.NEW_LINE, y + 1);
069 }
070
071 put(props.substring(y));
072 }
073
074 public String getProperty(String key) {
075 return get(key);
076 }
077
078 public String getProperty(String key, String defaultValue) {
079 if (containsKey(key)) {
080 return getProperty(key);
081 }
082 else {
083 return defaultValue;
084 }
085 }
086
087 public boolean isSafe() {
088 return _safe;
089 }
090
091 public void load(String props) throws IOException {
092 if (Validator.isNull(props)) {
093 return;
094 }
095
096 UnsyncBufferedReader unsyncBufferedReader = null;
097
098 try {
099 unsyncBufferedReader = new UnsyncBufferedReader(
100 new UnsyncStringReader(props));
101
102 String line = unsyncBufferedReader.readLine();
103
104 while (line != null) {
105 put(line);
106 line = unsyncBufferedReader.readLine();
107 }
108 }
109 finally {
110 if (unsyncBufferedReader != null) {
111 try {
112 unsyncBufferedReader.close();
113 }
114 catch (Exception e) {
115 }
116 }
117 }
118 }
119
120 private void put(String line) {
121 line = line.trim();
122
123 if (!_isComment(line)) {
124 int pos = line.indexOf(StringPool.EQUAL);
125
126 if (pos != -1) {
127 String key = line.substring(0, pos).trim();
128 String value = line.substring(pos + 1).trim();
129
130 if (_safe) {
131 value = _decode(value);
132 }
133
134 setProperty(key, value);
135 }
136 else {
137 _log.error("Invalid property on line " + line);
138 }
139 }
140 }
141
142 public String put(String key, String value) {
143 if (key == null) {
144 return null;
145 }
146 else {
147 if (value == null) {
148 return remove(key);
149 }
150 else {
151 _length += key.length() + value.length() + 2;
152
153 return super.put(key, value);
154 }
155 }
156 }
157
158 public String remove(Object key) {
159 if ((key == null) || !containsKey(key)) {
160 return null;
161 }
162 else {
163 String keyString = (String)key;
164
165 String value = super.remove(key);
166
167 _length -= keyString.length() + value.length() + 2;
168
169 return value;
170 }
171 }
172
173 public String setProperty(String key, String value) {
174 return put(key, value);
175 }
176
177 public String toString() {
178 StringBuilder sb = new StringBuilder(_length);
179
180 for (Map.Entry<String, String> entry : entrySet()) {
181 String value = entry.getValue();
182
183 if (Validator.isNotNull(value)) {
184 if (_safe) {
185 value = _encode(value);
186 }
187
188 sb.append(entry.getKey());
189 sb.append(StringPool.EQUAL);
190 sb.append(value);
191 sb.append(StringPool.NEW_LINE);
192 }
193 }
194
195 return sb.toString();
196 }
197
198 protected int getToStringLength() {
199 return _length;
200 }
201
202 private static String _decode(String value) {
203 return StringUtil.replace(
204 value, _SAFE_NEWLINE_CHARACTER, StringPool.NEW_LINE);
205 }
206
207 private static String _encode(String value) {
208 return StringUtil.replace(
209 value,
210 new String[] {
211 StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE,
212 StringPool.RETURN
213 },
214 new String[] {
215 _SAFE_NEWLINE_CHARACTER, _SAFE_NEWLINE_CHARACTER,
216 _SAFE_NEWLINE_CHARACTER
217 });
218 }
219
220 private boolean _isComment(String line) {
221 return line.length() == 0 || line.startsWith(StringPool.POUND);
222 }
223
224 private static final String _SAFE_NEWLINE_CHARACTER =
225 "_SAFE_NEWLINE_CHARACTER_";
226
227 private static Log _log = LogFactoryUtil.getLog(UnicodeProperties.class);
228
229 private boolean _safe = false;
230 private int _length;
231
232 }