2012-11-23 20:59:12 +01:00
|
|
|
package com.Acrobot.Breeze.Configuration;
|
|
|
|
|
2013-04-23 20:09:23 +02:00
|
|
|
import com.Acrobot.Breeze.Configuration.Annotations.ConfigurationComment;
|
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
import java.lang.reflect.Field;
|
2023-03-01 18:27:04 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2012-11-23 21:00:35 +01:00
|
|
|
|
2012-11-23 20:59:12 +01:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class FieldParser {
|
2012-11-23 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Parses a field into a YAML-compatible string
|
|
|
|
*
|
|
|
|
* @param field Field to parse
|
|
|
|
* @return Parsed field
|
|
|
|
*/
|
|
|
|
public static String parse(Field field) {
|
|
|
|
StringBuilder builder = new StringBuilder(50);
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-04-02 19:20:52 +02:00
|
|
|
if (field.isAnnotationPresent(ConfigurationComment.class)) {
|
2021-03-18 14:41:34 +01:00
|
|
|
for (String commentLine : field.getAnnotation(ConfigurationComment.class).value().split("\n")) {
|
|
|
|
builder.append("# ").append(commentLine).append('\n');
|
|
|
|
}
|
2018-04-02 19:20:52 +02:00
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
ValueParser parser = Configuration.getParser(field);
|
|
|
|
|
2012-11-23 21:00:35 +01:00
|
|
|
try {
|
2018-10-23 20:04:18 +02:00
|
|
|
builder.append(field.getName()).append(": ").append(parser.parseToYAML(field.get(null)));
|
2012-11-23 21:00:35 +01:00
|
|
|
} catch (IllegalAccessException e) {
|
2023-03-01 18:27:04 +01:00
|
|
|
Logger.getLogger("FieldParser").log(Level.SEVERE, "Error while parsing field", e);
|
2012-11-23 21:00:35 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2012-11-23 20:59:12 +01:00
|
|
|
}
|