Bugfix: propyerty key is value of enum constant.

REL-BRANCH-PropertyToJavaGenerator-0.0.1
Markus Kreth 7 years ago
parent 02f54b22c6
commit ef5cdbfe4e
  1. 64
      src/main/java/de/kreth/property2java/Generator.java
  2. 11
      src/main/resources/template/enum_template.tpl

@ -3,12 +3,13 @@ package de.kreth.property2java;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import de.kreth.property2java.cli.ArgumentConfiguration;
@ -17,9 +18,11 @@ import freemarker.template.TemplateException;
public class Generator {
private Configuration config;
private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance();
private Template template;
private final Configuration config;
private final Template template;
public Generator(Configuration config) {
this.config = config;
@ -27,14 +30,13 @@ public class Generator {
template = FreemarkerConfig.INSTANCE.getTemplate();
}
catch (IOException e) {
throw new IllegalStateException("Unable to load freemarker template", e);
}
}
public void start() throws IOException, TemplateException {
for (Entry<String, Reader> entry : config.getInput().entrySet()) {
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) {
String fileName = entry.getKey();
Writer out = config.outWriter(fileName);
Properties properties = new Properties();
@ -47,11 +49,13 @@ public class Generator {
throws IOException, TemplateException {
Map<String, Object> root = new HashMap<>();
root.put("generator_name", getClass().getName());
root.put("generation_date", dateTimeInstance.format(new Date()));
root.put("package", config.getPackage());
root.put("fileName", fileName);
root.put("classname", config.mapFilenameToClassName(fileName));
List<Entry<String, String>> entries = new ArrayList<>();
List<Entry> entries = new ArrayList<>();
root.put("entries", entries);
@ -62,24 +66,8 @@ public class Generator {
final String propertyKeyString = propertyNames.nextElement();
final String propertyValue = properties.getProperty(propertyKeyString);
Entry<String, String> entry = new Entry<String, String>() {
@Override
public String getKey() {
return propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_");
}
@Override
public String getValue() {
return propertyValue;
}
@Override
public String setValue(String value) {
throw new UnsupportedOperationException("Set Value not supported!");
}
};
entries.add(entry);
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString,
propertyValue));
}
template.process(root, out);
}
@ -89,4 +77,32 @@ public class Generator {
generator.start();
}
public class Entry {
public final String constant;
public final String key;
public final String value;
public Entry(String constant, String key, String value) {
super();
this.constant = constant;
this.key = key;
this.value = value;
}
public String getConstant() {
return constant;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
}

@ -4,16 +4,19 @@
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;
import javax.annotation.processing.Generated;
/**
* Property key from ${fileName}
* Property keys from ${fileName}
*/
@Generated(date = "${generation_date}", value = "${generator_name}")
public enum ${classname} {
<#list entries as entry>
<#list entries as e>
/**
* "${entry.value}"
* ${e.key} = "${e.value}"
*/
${entry.key} ("${entry.value}")<#sep>,
${e.constant} ("${e.key}")<#sep>,
</#sep>
</#list>;

Loading…
Cancel
Save