|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2007 the original author or authors. |
| 2 | + * Copyright 2006-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.batch.support;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
20 |
| -import java.io.StringReader; |
21 |
| -import java.io.StringWriter; |
22 |
| -import java.util.Arrays; |
| 19 | +import java.util.ArrayList; |
23 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
24 | 22 | import java.util.Properties;
|
25 | 23 |
|
26 |
| -import org.springframework.util.DefaultPropertiesPersister; |
27 |
| -import org.springframework.util.PropertiesPersister; |
| 24 | +import org.springframework.lang.NonNull; |
| 25 | +import org.springframework.util.Assert; |
28 | 26 | import org.springframework.util.StringUtils;
|
29 | 27 |
|
30 | 28 | /**
|
31 |
| - * Utility to convert a Properties object to a String and back. Ideally this utility |
32 |
| - * should have been used to convert to string in order to convert that string back to a |
33 |
| - * Properties Object. Attempting to convert a string obtained by calling |
34 |
| - * Properties.toString() will return an invalid Properties object. The format of |
35 |
| - * Properties is that used by {@link PropertiesPersister} from the Spring Core, so a |
36 |
| - * String in the correct format for a Spring property editor is fine (key=value pairs |
37 |
| - * separated by new lines). |
| 29 | + * Utility to convert a Properties object to a String and back. The format of properties |
| 30 | + * is new line separated key=value pairs. |
38 | 31 | *
|
39 | 32 | * @author Lucas Ward
|
40 | 33 | * @author Dave Syer
|
41 |
| - * @see PropertiesPersister |
| 34 | + * @author Mahmoud Ben Hassine |
42 | 35 | */
|
43 | 36 | public final class PropertiesConverter {
|
44 | 37 |
|
45 |
| - private static final PropertiesPersister propertiesPersister = new DefaultPropertiesPersister(); |
46 |
| - |
47 |
| - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
| 38 | + private static final String LINE_SEPARATOR = "\n"; |
48 | 39 |
|
49 | 40 | // prevents the class from being instantiated
|
50 | 41 | private PropertiesConverter() {
|
51 | 42 | }
|
52 | 43 |
|
53 | 44 | /**
|
54 |
| - * Parse a String to a Properties object. If string is null, an empty Properties |
55 |
| - * object will be returned. The input String is a set of name=value pairs, delimited |
56 |
| - * by either newline or comma (for brevity). If the input String contains a newline it |
57 |
| - * is assumed that the separator is newline, otherwise comma. |
58 |
| - * @param stringToParse String to parse. |
59 |
| - * @return Properties parsed from each string. |
60 |
| - * @see PropertiesPersister |
| 45 | + * Parse a String to a Properties object. If string is empty, an empty Properties |
| 46 | + * object will be returned. The input String should be a set of key=value pairs, |
| 47 | + * separated by a new line. |
| 48 | + * @param stringToParse String to parse. Must not be {@code null}. |
| 49 | + * @return Properties parsed from each key=value pair. |
61 | 50 | */
|
62 |
| - public static Properties stringToProperties(String stringToParse) { |
63 |
| - |
64 |
| - if (stringToParse == null) { |
| 51 | + public static Properties stringToProperties(@NonNull String stringToParse) { |
| 52 | + Assert.notNull(stringToParse, "stringToParse must not be null"); |
| 53 | + if (!StringUtils.hasText(stringToParse)) { |
65 | 54 | return new Properties();
|
66 | 55 | }
|
67 |
| - |
68 |
| - if (!contains(stringToParse, "\n")) { |
69 |
| - stringToParse = StringUtils |
70 |
| - .arrayToDelimitedString(StringUtils.commaDelimitedListToStringArray(stringToParse), "\n"); |
71 |
| - } |
72 |
| - |
73 |
| - StringReader stringReader = new StringReader(stringToParse); |
74 |
| - |
75 | 56 | Properties properties = new Properties();
|
76 |
| - |
77 |
| - try { |
78 |
| - propertiesPersister.load(properties, stringReader); |
79 |
| - // Exception is only thrown by StringReader after it is closed, |
80 |
| - // so never in this case. |
81 |
| - } |
82 |
| - catch (IOException ex) { |
83 |
| - throw new IllegalStateException( |
84 |
| - "Error while trying to parse String to java.util.Properties," + " given String: " + properties); |
| 57 | + String[] keyValuePairs = stringToParse.split(LINE_SEPARATOR); |
| 58 | + for (String string : keyValuePairs) { |
| 59 | + if (!string.contains("=")) { |
| 60 | + throw new IllegalArgumentException(string + "is not a valid key=value pair"); |
| 61 | + } |
| 62 | + String[] keyValuePair = string.split("="); |
| 63 | + properties.setProperty(keyValuePair[0], keyValuePair[1]); |
85 | 64 | }
|
86 |
| - |
87 | 65 | return properties;
|
88 | 66 | }
|
89 | 67 |
|
90 | 68 | /**
|
91 |
| - * Convert Properties object to String. This is only necessary for compatibility with |
92 |
| - * converting the String back to a properties object. If an empty properties object is |
93 |
| - * passed in, a blank string is returned, otherwise it's string representation is |
94 |
| - * returned. |
95 |
| - * @param propertiesToParse contains the properties be converted. |
96 |
| - * @return String representation of properties object |
| 69 | + * Convert a Properties object to a String. This is only necessary for compatibility |
| 70 | + * with converting the String back to a properties object. If an empty properties |
| 71 | + * object is passed in, a blank string is returned, otherwise it's string |
| 72 | + * representation is returned. |
| 73 | + * @param propertiesToParse contains the properties to be converted. Must not be |
| 74 | + * {@code null}. |
| 75 | + * @return String representation of the properties object |
97 | 76 | */
|
98 |
| - public static String propertiesToString(Properties propertiesToParse) { |
99 |
| - |
100 |
| - // If properties is empty, return a blank string. |
101 |
| - if (propertiesToParse == null || propertiesToParse.size() == 0) { |
| 77 | + public static String propertiesToString(@NonNull Properties propertiesToParse) { |
| 78 | + Assert.notNull(propertiesToParse, "propertiesToParse must not be null"); |
| 79 | + if (propertiesToParse.isEmpty()) { |
102 | 80 | return "";
|
103 | 81 | }
|
104 |
| - |
105 |
| - StringWriter stringWriter = new StringWriter(); |
106 |
| - |
107 |
| - try { |
108 |
| - propertiesPersister.store(propertiesToParse, stringWriter, null); |
109 |
| - } |
110 |
| - catch (IOException ex) { |
111 |
| - // Exception is never thrown by StringWriter |
112 |
| - throw new IllegalStateException("Error while trying to convert properties to string"); |
113 |
| - } |
114 |
| - |
115 |
| - // If the value is short enough (and doesn't contain commas), convert to |
116 |
| - // comma-separated... |
117 |
| - String value = stringWriter.toString(); |
118 |
| - if (value.length() < 160) { |
119 |
| - List<String> list = Arrays |
120 |
| - .asList(StringUtils.delimitedListToStringArray(value, LINE_SEPARATOR, LINE_SEPARATOR)); |
121 |
| - String shortValue = StringUtils.collectionToCommaDelimitedString(list.subList(1, list.size())); |
122 |
| - int count = StringUtils.countOccurrencesOf(shortValue, ","); |
123 |
| - if (count == list.size() - 2) { |
124 |
| - value = shortValue; |
125 |
| - } |
126 |
| - if (value.endsWith(",")) { |
127 |
| - value = value.substring(0, value.length() - 1); |
128 |
| - } |
| 82 | + List<String> keyValuePairs = new ArrayList<>(); |
| 83 | + for (Map.Entry<Object, Object> entry : propertiesToParse.entrySet()) { |
| 84 | + keyValuePairs.add(entry.getKey() + "=" + entry.getValue()); |
129 | 85 | }
|
130 |
| - return value; |
131 |
| - } |
132 |
| - |
133 |
| - private static boolean contains(String str, String searchStr) { |
134 |
| - return str.indexOf(searchStr) != -1; |
| 86 | + return String.join(LINE_SEPARATOR, keyValuePairs); |
135 | 87 | }
|
136 | 88 |
|
137 | 89 | }
|
0 commit comments