/portal-kernel/src/com/liferay/portal/kernel/util/PropertiesUtil.java

https://github.com/kiyoshilee/liferay-portal · Java · 273 lines · 176 code · 77 blank · 20 comment · 24 complexity · 53b0c05a5dcab956d6c265f230b4d18a MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present 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. package com.liferay.portal.kernel.util;
  15. import com.liferay.petra.string.StringPool;
  16. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
  17. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
  18. import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.PrintStream;
  23. import java.io.PrintWriter;
  24. import java.io.Reader;
  25. import java.util.Collections;
  26. import java.util.Enumeration;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Properties;
  31. /**
  32. * @author Brian Wing Shun Chan
  33. * @author Shuyang Zhou
  34. */
  35. public class PropertiesUtil {
  36. public static void copyProperties(
  37. Properties sourceProperties, Properties targetProperties) {
  38. for (Map.Entry<Object, Object> entry : sourceProperties.entrySet()) {
  39. String key = (String)entry.getKey();
  40. String value = (String)entry.getValue();
  41. targetProperties.setProperty(key, value);
  42. }
  43. }
  44. public static Properties fromMap(Map<String, ?> map) {
  45. Properties properties = new Properties();
  46. for (Map.Entry<String, ?> entry : map.entrySet()) {
  47. Object value = entry.getValue();
  48. if ((value != null) && (value instanceof String)) {
  49. properties.setProperty(entry.getKey(), (String)value);
  50. }
  51. }
  52. return properties;
  53. }
  54. public static void fromProperties(
  55. Properties properties, Map<String, String> map) {
  56. map.clear();
  57. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  58. map.put((String)entry.getKey(), (String)entry.getValue());
  59. }
  60. }
  61. public static Properties getProperties(
  62. Properties properties, String prefix, boolean removePrefix) {
  63. Properties newProperties = new Properties();
  64. Enumeration<String> enu =
  65. (Enumeration<String>)properties.propertyNames();
  66. while (enu.hasMoreElements()) {
  67. String key = enu.nextElement();
  68. if (key.startsWith(prefix)) {
  69. String value = properties.getProperty(key);
  70. if (removePrefix) {
  71. key = key.substring(prefix.length());
  72. }
  73. newProperties.setProperty(key, value);
  74. }
  75. }
  76. return newProperties;
  77. }
  78. public static String list(Map<String, String> map) {
  79. Properties properties = fromMap(map);
  80. return list(properties);
  81. }
  82. public static void list(Map<String, String> map, PrintStream printWriter) {
  83. Properties properties = fromMap(map);
  84. properties.list(printWriter);
  85. }
  86. public static void list(Map<String, String> map, PrintWriter printWriter) {
  87. Properties properties = fromMap(map);
  88. properties.list(printWriter);
  89. }
  90. public static String list(Properties properties) {
  91. UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
  92. new UnsyncByteArrayOutputStream();
  93. PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
  94. properties.list(printStream);
  95. return unsyncByteArrayOutputStream.toString();
  96. }
  97. public static Properties load(InputStream is, String charsetName)
  98. throws IOException {
  99. return load(new InputStreamReader(is, charsetName));
  100. }
  101. public static void load(Properties properties, String s)
  102. throws IOException {
  103. if (Validator.isNull(s)) {
  104. return;
  105. }
  106. s = UnicodeFormatter.toString(s);
  107. s = StringUtil.replace(s, "\\u003d", "=");
  108. s = StringUtil.replace(s, "\\u000a", "\n");
  109. s = StringUtil.replace(s, "\\u0021", "!");
  110. s = StringUtil.replace(s, "\\u0023", "#");
  111. s = StringUtil.replace(s, "\\u0020", " ");
  112. s = StringUtil.replace(s, "\\u005c", "\\");
  113. properties.load(new UnsyncByteArrayInputStream(s.getBytes()));
  114. List<String> propertyNames = Collections.list(
  115. (Enumeration<String>)properties.propertyNames());
  116. for (String key : propertyNames) {
  117. String value = properties.getProperty(key);
  118. // Trim values because it may leave a trailing \r in certain Windows
  119. // environments. This is a known case for loading SQL scripts in SQL
  120. // Server.
  121. if (value != null) {
  122. value = value.trim();
  123. properties.setProperty(key, value);
  124. }
  125. }
  126. }
  127. public static Properties load(Reader reader) throws IOException {
  128. Properties properties = new Properties();
  129. properties.load(reader);
  130. return properties;
  131. }
  132. public static Properties load(String s) throws IOException {
  133. return load(new UnsyncStringReader(s));
  134. }
  135. public static void merge(Properties properties1, Properties properties2) {
  136. Enumeration<String> enu =
  137. (Enumeration<String>)properties2.propertyNames();
  138. while (enu.hasMoreElements()) {
  139. String key = enu.nextElement();
  140. String value = properties2.getProperty(key);
  141. properties1.setProperty(key, value);
  142. }
  143. }
  144. @SuppressWarnings("rawtypes")
  145. public static Map toMap(Properties properties) {
  146. Map<String, String> propertiesMap = new HashMap<>();
  147. Enumeration<?> enumeration = properties.propertyNames();
  148. while (enumeration.hasMoreElements()) {
  149. String key = (String)enumeration.nextElement();
  150. String value = properties.getProperty(key);
  151. propertiesMap.put(key, value);
  152. }
  153. return propertiesMap;
  154. }
  155. public static String toString(Properties properties) {
  156. SafeProperties safeProperties = null;
  157. if (properties instanceof SafeProperties) {
  158. safeProperties = (SafeProperties)properties;
  159. }
  160. StringBundler sb = null;
  161. if (properties.isEmpty()) {
  162. sb = new StringBundler();
  163. }
  164. else {
  165. sb = new StringBundler(properties.size() * 4);
  166. }
  167. Enumeration<String> enu =
  168. (Enumeration<String>)properties.propertyNames();
  169. while (enu.hasMoreElements()) {
  170. String key = enu.nextElement();
  171. sb.append(key);
  172. sb.append(StringPool.EQUAL);
  173. if (safeProperties != null) {
  174. sb.append(safeProperties.getEncodedProperty(key));
  175. }
  176. else {
  177. sb.append(properties.getProperty(key));
  178. }
  179. sb.append(StringPool.NEW_LINE);
  180. }
  181. return sb.toString();
  182. }
  183. public static void trimKeys(Properties properties) {
  184. Enumeration<String> enu =
  185. (Enumeration<String>)properties.propertyNames();
  186. while (enu.hasMoreElements()) {
  187. String key = enu.nextElement();
  188. String trimmedKey = key.trim();
  189. if (!key.equals(trimmedKey)) {
  190. properties.remove(key);
  191. String value = properties.getProperty(key);
  192. properties.setProperty(trimmedKey, value);
  193. }
  194. }
  195. }
  196. }