/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xd.rest.domain.support;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.util.StringUtils;
/**
* Provides utility methods for formatting and parsing deployment properties.
*
* @author Eric Bottard
*/
public class DeploymentPropertiesFormat {
private DeploymentPropertiesFormat() {
// Prevent instantiation
}
/**
* Pattern used for parsing a String of comma-delimited key=value pairs.
*/
private static final Pattern DEPLOYMENT_PROPERTIES_PATTERN = Pattern.compile(",\\s*module\\.[^\\.]+\\.[^=]+=");
/**
* Parses a String comprised of 0 or more comma-delimited key=value pairs where
* each key has the format:
* {@code module.[modulename].[key]}.
* Values may themselves contain commas, since the split points will be based upon the key pattern.
*
* @param s the string to parse
* @return the Map of parsed key value pairs
*/
public static Map<String, String> parseDeploymentProperties(String s) {
Map<String, String> deploymentProperties = new HashMap<String, String>();
if (!StringUtils.isEmpty(s)) {
Matcher matcher = DEPLOYMENT_PROPERTIES_PATTERN.matcher(s);
int start = 0;
while (matcher.find()) {
addKeyValuePairAsProperty(s.substring(start, matcher.start()), deploymentProperties);
start = matcher.start() + 1;
}
addKeyValuePairAsProperty(s.substring(start), deploymentProperties);
}
return deploymentProperties;
}
/**
* Returns a String representation of deployment properties as a comma separated list of key=value pairs.
* @param properties the properties to format
* @return the properties formatted as a String
*/
public static String formatDeploymentProperties(Map<String, String> properties) {
StringBuilder sb = new StringBuilder(15 * properties.size());
for (Map.Entry<String, String> pair : properties.entrySet()) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(pair.getKey()).append("=").append(pair.getValue());
}
return sb.toString();
}
/**
* Adds a String of format key=value to the provided Map as a key/value pair.
*
* @param pair the String representation
* @param properties the Map to which the key/value pair should be added
*/
private static void addKeyValuePairAsProperty(String pair, Map<String, String> properties) {
int firstEquals = pair.indexOf('=');
if (firstEquals != -1) {
// todo: should key only be a "flag" as in: put(key, true)?
properties.put(pair.substring(0, firstEquals).trim(), pair.substring(firstEquals + 1).trim());
}
}
/**
* Convert a Properties to a Map with String keys and values. Entries whose key or value were not Strings are
* omitted.
*/
public static Map<String, String> convert(Properties properties) {
Map<String, String> result = new HashMap<>(properties.size());
for (String key : properties.stringPropertyNames()) {
result.put(key, properties.getProperty(key));
}
return result;
}
}