| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.springframework.batch.support; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.io.StringReader; |
| 21 | import java.io.StringWriter; |
| 22 | import java.util.Properties; |
| 23 | |
| 24 | import org.springframework.util.DefaultPropertiesPersister; |
| 25 | import org.springframework.util.PropertiesPersister; |
| 26 | import org.springframework.util.StringUtils; |
| 27 | |
| 28 | /** |
| 29 | * Utility to convert a Properties object to a String and back. Ideally this utility should have been used to convert to |
| 30 | * string in order to convert that string back to a Properties Object. Attempting to convert a string obtained by |
| 31 | * calling Properties.toString() will return an invalid Properties object. The format of Properties is that used by |
| 32 | * {@link PropertiesPersister} from the Spring Core, so a String in the correct format for a Spring property editor is |
| 33 | * fine (key=value pairs separated by new lines). |
| 34 | * |
| 35 | * @author Lucas Ward |
| 36 | * @author Dave Syer |
| 37 | * |
| 38 | * @see PropertiesPersister |
| 39 | */ |
| 40 | public final class PropertiesConverter { |
| 41 | |
| 42 | private static final PropertiesPersister propertiesPersister = new DefaultPropertiesPersister(); |
| 43 | |
| 44 | // prevents the class from being instantiated |
| 45 | private PropertiesConverter() { |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Parse a String to a Properties object. If string is null, an empty Properties object will be returned. The input |
| 50 | * String is a set of name=value pairs, delimited by either newline or comma (for brevity). If the input String |
| 51 | * contains a newline it is assumed that the separator is newline, otherwise comma. |
| 52 | * |
| 53 | * @param stringToParse String to parse. |
| 54 | * @return Properties parsed from each string. |
| 55 | * @see PropertiesPersister |
| 56 | */ |
| 57 | public static Properties stringToProperties(String stringToParse) { |
| 58 | |
| 59 | if (stringToParse == null) { |
| 60 | return new Properties(); |
| 61 | } |
| 62 | |
| 63 | if (!contains(stringToParse, "\n")) { |
| 64 | return StringUtils.splitArrayElementsIntoProperties(StringUtils |
| 65 | .commaDelimitedListToStringArray(stringToParse), "="); |
| 66 | } |
| 67 | |
| 68 | StringReader stringReader = new StringReader(stringToParse); |
| 69 | |
| 70 | Properties properties = new Properties(); |
| 71 | |
| 72 | try { |
| 73 | propertiesPersister.load(properties, stringReader); |
| 74 | // Exception is only thrown by StringReader after it is closed, |
| 75 | // so never in this case. |
| 76 | } catch (IOException ex) { |
| 77 | throw new IllegalStateException("Error while trying to parse String to java.util.Properties," |
| 78 | + " given String: " + properties); |
| 79 | } |
| 80 | |
| 81 | return properties; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Convert Properties object to String. This is only necessary for compatibility with converting the String back to |
| 86 | * a properties object. If an empty properties object is passed in, a blank string is returned, otherwise it's |
| 87 | * string representation is returned. |
| 88 | * |
| 89 | * @param propertiesToParse |
| 90 | * @return String representation of properties object |
| 91 | */ |
| 92 | public static String propertiesToString(Properties propertiesToParse) { |
| 93 | |
| 94 | // If properties is empty, return a blank string. |
| 95 | if (propertiesToParse == null || propertiesToParse.size() == 0) { |
| 96 | return ""; |
| 97 | } |
| 98 | |
| 99 | StringWriter stringWriter = new StringWriter(); |
| 100 | |
| 101 | try { |
| 102 | propertiesPersister.store(propertiesToParse, stringWriter, null); |
| 103 | } catch (IOException ex) { |
| 104 | // Exception is never thrown by StringWriter |
| 105 | throw new IllegalStateException("Error while trying to convert properties to string"); |
| 106 | } |
| 107 | |
| 108 | return stringWriter.toString(); |
| 109 | } |
| 110 | |
| 111 | private static boolean contains(String str, String searchStr) { |
| 112 | return str.indexOf(searchStr) != -1; |
| 113 | } |
| 114 | } |