Skip to content

Instantly share code, notes, and snippets.

@liutf
Created November 13, 2017 07:38
Show Gist options
  • Save liutf/c2202368b90eac2228379b898dadcfd7 to your computer and use it in GitHub Desktop.
Save liutf/c2202368b90eac2228379b898dadcfd7 to your computer and use it in GitHub Desktop.
package com.net263.util;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class YamlConvertTool {
public static void main(String[] args) throws Exception {
convertToProps("application.yml", "application.properties");
}
private static void convertToProps(String ymlName, String propsName) throws Exception {
YamlPropertiesFactoryBean processor = new YamlPropertiesFactoryBean();
Resource resource = new ClassPathResource(ymlName);
processor.setResources(resource);
String content = convertToString(processor.getObject());
System.out.println(content);
File outputFile = new File(resource.getFile().getParent(), propsName);
Files.write(content, outputFile, Charsets.UTF_8);
}
/**
* 中划线形式转成驼峰形式,比如说:spring.datasource.delivery.driver-class-name转成spring.datasource.delivery.driverClassName
*
* @return
*/
private static String convertToString(Properties properties) {
if (properties == null) return null;
Pattern pattern = Pattern.compile(".*\\$\\{.+\\}.*");
List<String> lines = properties.entrySet().stream().map(e -> {
String k = e.getKey().toString();
if (k.contains("-")) {
k = formatKey(k, "-");
}
String v = e.getValue().toString();
// 替换掉占位符,比如说${server}
if (pattern.matcher(v).matches()) {
for (Object h : properties.keySet()) {
String holder = "\\$\\{" + h.toString() + "\\}";
v = v.replaceAll(holder, properties.getProperty(h.toString()));
}
}
return k + "=" + v;
}).collect(Collectors.toList());
// 字典序升序排序
Collections.sort(lines);
return lines.stream().collect(Collectors.joining("\n"));
}
/**
* 去掉特殊字符,如果含有特殊字符,把key的特殊字符所在位置处理成驼峰形式
*
* @param key
* @param sep
* @return
*/
private static String formatKey(String key, String sep) {
if (key == null) return null;
if (!key.contains(sep)) return key;
String[] items = key.split(sep);
String str = Arrays.stream(items).map(YamlConvertTool::capitalize).collect(Collectors.joining());
return uncapitalize(str);
}
/**
* 摘自commons-lang3的StringUtils
*
* @param str
* @return
*/
private static String capitalize(final String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
final int firstCodepoint = str.codePointAt(0);
final int newCodePoint = Character.toTitleCase(firstCodepoint);
if (firstCodepoint == newCodePoint) {
// already capitalized
return str;
}
final int newCodePoints[] = new int[strLen]; // cannot be longer than the char array
int outOffset = 0;
newCodePoints[outOffset++] = newCodePoint; // copy the first codepoint
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
final int codepoint = str.codePointAt(inOffset);
newCodePoints[outOffset++] = codepoint; // copy the remaining ones
inOffset += Character.charCount(codepoint);
}
return new String(newCodePoints, 0, outOffset);
}
/**
* 摘自commons-lang3的StringUtils
*
* @param str
* @return
*/
private static String uncapitalize(final String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
final int firstCodepoint = str.codePointAt(0);
final int newCodePoint = Character.toLowerCase(firstCodepoint);
if (firstCodepoint == newCodePoint) {
// already capitalized
return str;
}
final int newCodePoints[] = new int[strLen]; // cannot be longer than the char array
int outOffset = 0;
newCodePoints[outOffset++] = newCodePoint; // copy the first codepoint
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
final int codepoint = str.codePointAt(inOffset);
newCodePoints[outOffset++] = codepoint; // copy the remaining ones
inOffset += Character.charCount(codepoint);
}
return new String(newCodePoints, 0, outOffset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment