Bugfix: propyerty key is value of enum constant.

REL-BRANCH-PropertyToJavaGenerator-0.0.1
Markus Kreth 7 years ago
parent 02f54b22c6
commit ef5cdbfe4e
  1. 62
      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.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import de.kreth.property2java.cli.ArgumentConfiguration; import de.kreth.property2java.cli.ArgumentConfiguration;
@ -17,9 +18,11 @@ import freemarker.template.TemplateException;
public class Generator { 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) { public Generator(Configuration config) {
this.config = config; this.config = config;
@ -27,14 +30,13 @@ public class Generator {
template = FreemarkerConfig.INSTANCE.getTemplate(); template = FreemarkerConfig.INSTANCE.getTemplate();
} }
catch (IOException e) { catch (IOException e) {
throw new IllegalStateException("Unable to load freemarker template", e); throw new IllegalStateException("Unable to load freemarker template", e);
} }
} }
public void start() throws IOException, TemplateException { 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(); String fileName = entry.getKey();
Writer out = config.outWriter(fileName); Writer out = config.outWriter(fileName);
Properties properties = new Properties(); Properties properties = new Properties();
@ -47,11 +49,13 @@ public class Generator {
throws IOException, TemplateException { throws IOException, TemplateException {
Map<String, Object> root = new HashMap<>(); 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("package", config.getPackage());
root.put("fileName", fileName); root.put("fileName", fileName);
root.put("classname", config.mapFilenameToClassName(fileName)); root.put("classname", config.mapFilenameToClassName(fileName));
List<Entry<String, String>> entries = new ArrayList<>(); List<Entry> entries = new ArrayList<>();
root.put("entries", entries); root.put("entries", entries);
@ -62,31 +66,43 @@ public class Generator {
final String propertyKeyString = propertyNames.nextElement(); final String propertyKeyString = propertyNames.nextElement();
final String propertyValue = properties.getProperty(propertyKeyString); final String propertyValue = properties.getProperty(propertyKeyString);
Entry<String, String> entry = new Entry<String, String>() { entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString,
propertyValue));
@Override }
public String getKey() { template.process(root, out);
return propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_");
} }
@Override public static void main(String[] args) throws IOException, TemplateException {
public String getValue() { Generator generator = new Generator(ArgumentConfiguration.parse(args));
return propertyValue; generator.start();
} }
@Override public class Entry {
public String setValue(String value) {
throw new UnsupportedOperationException("Set Value not supported!"); 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;
} }
};
entries.add(entry); public String getConstant() {
return constant;
} }
template.process(root, out);
public String getKey() {
return key;
} }
public static void main(String[] args) throws IOException, TemplateException { public String getValue() {
Generator generator = new Generator(ArgumentConfiguration.parse(args)); return value;
generator.start();
} }
}
} }

@ -4,16 +4,19 @@
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.function.UnaryOperator; 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} { 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> </#sep>
</#list>; </#list>;

Loading…
Cancel
Save