2016-08-24 11:22:23 +00:00
|
|
|
|
package pw.yumc.YumCore.config;
|
|
|
|
|
|
2016-11-04 02:15:42 +00:00
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
|
import org.yaml.snakeyaml.DumperOptions;
|
|
|
|
|
|
2016-08-24 11:22:23 +00:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
public class CommentConfig extends AbstractConfig {
|
|
|
|
|
// 新增保留注释字段
|
2016-10-11 17:14:57 +00:00
|
|
|
|
protected static String commentPrefixSymbol = "'注释 ";
|
|
|
|
|
protected static String commentSuffixSymbol = "': 注释";
|
2016-08-24 11:22:23 +00:00
|
|
|
|
|
2016-11-04 02:15:42 +00:00
|
|
|
|
protected static String fromRegex = "( *)(#.*)";
|
2016-10-11 17:14:57 +00:00
|
|
|
|
protected static Pattern fromPattern = Pattern.compile(fromRegex);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
|
2016-11-04 02:15:42 +00:00
|
|
|
|
protected static String toRegex = "( *)(- )*" + "(" + commentPrefixSymbol + ")" + "(#.*)" + "(" + commentSuffixSymbol + ")";
|
2016-10-11 17:14:57 +00:00
|
|
|
|
protected static Pattern toPattern = Pattern.compile(toRegex);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
|
2016-11-04 02:15:42 +00:00
|
|
|
|
protected static Pattern countSpacePattern = Pattern.compile("( *)(- )*(.*)");
|
2016-08-24 11:22:23 +00:00
|
|
|
|
|
2016-10-11 17:14:57 +00:00
|
|
|
|
protected static int commentSplitWidth = 90;
|
2016-08-24 11:22:23 +00:00
|
|
|
|
|
2016-10-11 17:14:57 +00:00
|
|
|
|
private static String[] split(String string, int partLength) {
|
|
|
|
|
String[] array = new String[string.length() / partLength + 1];
|
2016-08-24 11:22:23 +00:00
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
2016-10-11 17:14:57 +00:00
|
|
|
|
int beginIndex = i * partLength;
|
2016-08-24 11:22:23 +00:00
|
|
|
|
int endIndex = beginIndex + partLength;
|
|
|
|
|
if (endIndex > string.length()) {
|
|
|
|
|
endIndex = string.length();
|
|
|
|
|
}
|
|
|
|
|
array[i] = string.substring(beginIndex, endIndex);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-10-11 17:14:57 +00:00
|
|
|
|
public void loadFromString(String contents) throws InvalidConfigurationException {
|
|
|
|
|
String[] parts = contents.split(newLine);
|
|
|
|
|
List<String> lastComments = new ArrayList<>();
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
for (String part : parts) {
|
2016-08-24 11:22:23 +00:00
|
|
|
|
Matcher matcher = fromPattern.matcher(part);
|
|
|
|
|
if (matcher.find()) {
|
2016-10-11 17:14:57 +00:00
|
|
|
|
String originComment = matcher.group(2);
|
|
|
|
|
String[] splitComments = split(originComment, commentSplitWidth);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
for (int i = 0; i < splitComments.length; i++) {
|
|
|
|
|
String comment = splitComments[i];
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
comment = comment.substring(1);
|
|
|
|
|
}
|
|
|
|
|
comment = COMMENT_PREFIX + comment;
|
|
|
|
|
lastComments.add(comment.replaceAll("\\.", ".").replaceAll("'", "'").replaceAll(":", ":"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
matcher = countSpacePattern.matcher(part);
|
2016-09-19 08:59:10 +00:00
|
|
|
|
if (matcher.find() && !lastComments.isEmpty()) {
|
2016-10-11 17:14:57 +00:00
|
|
|
|
for (String comment : lastComments) {
|
2016-09-19 08:59:10 +00:00
|
|
|
|
builder.append(matcher.group(1));
|
|
|
|
|
builder.append(this.checkNull(matcher.group(2)));
|
|
|
|
|
builder.append(commentPrefixSymbol);
|
|
|
|
|
builder.append(comment);
|
|
|
|
|
builder.append(commentSuffixSymbol);
|
|
|
|
|
builder.append(newLine);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
}
|
2016-09-19 08:59:10 +00:00
|
|
|
|
lastComments.clear();
|
2016-08-24 11:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
builder.append(part);
|
|
|
|
|
builder.append(newLine);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
super.loadFromString(builder.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String saveToString() {
|
|
|
|
|
yamlOptions.setIndent(options().indent());
|
|
|
|
|
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
|
|
|
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
2016-10-11 17:14:57 +00:00
|
|
|
|
String header = buildHeader();
|
2016-08-24 11:22:23 +00:00
|
|
|
|
String dump = yamlz.dump(getValues(false));
|
|
|
|
|
if (dump.equals(BLANK_CONFIG)) {
|
|
|
|
|
dump = "";
|
|
|
|
|
}
|
2016-10-11 17:14:57 +00:00
|
|
|
|
String contents = header + dump;
|
|
|
|
|
StringBuilder savcontent = new StringBuilder();
|
|
|
|
|
String[] parts = contents.split(newLine);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
for (String part : parts) {
|
2016-10-11 17:14:57 +00:00
|
|
|
|
Matcher matcher = toPattern.matcher(part);
|
2016-09-19 08:59:10 +00:00
|
|
|
|
if (matcher.find() && matcher.groupCount() == 5) {
|
|
|
|
|
part = this.checkNull(matcher.group(1)) + matcher.group(4);
|
2016-08-24 11:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
savcontent.append(part.replaceAll(".", ".").replaceAll("'", "'").replaceAll(":", ":"));
|
|
|
|
|
savcontent.append(newLine);
|
|
|
|
|
}
|
|
|
|
|
data = savcontent.toString();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查字符串
|
|
|
|
|
*
|
|
|
|
|
* @param string
|
|
|
|
|
* 检查字符串
|
|
|
|
|
* @return 返回非null字符串
|
|
|
|
|
*/
|
2016-10-11 17:14:57 +00:00
|
|
|
|
private String checkNull(String string) {
|
2016-08-24 11:22:23 +00:00
|
|
|
|
return string == null ? "" : string;
|
|
|
|
|
}
|
|
|
|
|
}
|