|
|
|
@ -22,143 +22,137 @@ import freemarker.template.TemplateException; |
|
|
|
|
|
|
|
|
|
|
|
public class Generator { |
|
|
|
public class Generator { |
|
|
|
|
|
|
|
|
|
|
|
private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); |
|
|
|
private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); |
|
|
|
|
|
|
|
|
|
|
|
private final Configuration config; |
|
|
|
private final Configuration config; |
|
|
|
|
|
|
|
|
|
|
|
private final Template template; |
|
|
|
private final Template template; |
|
|
|
|
|
|
|
|
|
|
|
public Generator(Configuration config) { |
|
|
|
public Generator(Configuration config) { |
|
|
|
this.config = config; |
|
|
|
this.config = config; |
|
|
|
try { |
|
|
|
try { |
|
|
|
template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); |
|
|
|
template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); |
|
|
|
} 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, GeneratorException { |
|
|
|
public void start() throws IOException, GeneratorException { |
|
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) { |
|
|
|
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) { |
|
|
|
String fileName = entry.getKey(); |
|
|
|
String fileName = entry.getKey(); |
|
|
|
try (Writer out = config.outWriter(fileName)) { |
|
|
|
try (Writer out = config.outWriter(fileName)) { |
|
|
|
|
|
|
|
|
|
|
|
Properties properties = new Properties(); |
|
|
|
Properties properties = new Properties(); |
|
|
|
properties.load(entry.getValue()); |
|
|
|
properties.load(entry.getValue()); |
|
|
|
try { |
|
|
|
try { |
|
|
|
generate(properties, out, fileName, config); |
|
|
|
generate(properties, out, fileName, config); |
|
|
|
} catch (TemplateException e) { |
|
|
|
} catch (TemplateException e) { |
|
|
|
throw new GeneratorException("Error configuring Engine", e); |
|
|
|
throw new GeneratorException("Error configuring Engine", e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void generate(Properties properties, Writer out, String fileName, Configuration config) |
|
|
|
void generate(Properties properties, Writer out, String fileName, Configuration config) |
|
|
|
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("generator_name", getClass().getName()); |
|
|
|
root.put("generation_date", dateTimeInstance.format(new Date())); |
|
|
|
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("bundle_base_name", fileName.substring(0, min(fileName.length(), fileName.lastIndexOf('.')))); |
|
|
|
root.put("bundle_base_name", fileName.substring(0, min(fileName.length(), fileName.lastIndexOf('.')))); |
|
|
|
root.put("classname", config.mapFilenameToClassName(fileName)); |
|
|
|
root.put("classname", config.mapFilenameToClassName(fileName)); |
|
|
|
|
|
|
|
|
|
|
|
List<Entry> entries = new ArrayList<>(); |
|
|
|
List<Entry> entries = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
root.put("entries", entries); |
|
|
|
root.put("entries", entries); |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
List<String> propertyNames = Collections.list((Enumeration<String>) properties.propertyNames()); |
|
|
|
List<String> propertyNames = Collections.list((Enumeration<String>) properties.propertyNames()); |
|
|
|
Collections.sort(propertyNames); |
|
|
|
Collections.sort(propertyNames); |
|
|
|
|
|
|
|
|
|
|
|
for (String propertyKeyString : propertyNames) { |
|
|
|
for (String propertyKeyString : propertyNames) { |
|
|
|
final String propertyValue = properties.getProperty(propertyKeyString); |
|
|
|
final String propertyValue = properties.getProperty(propertyKeyString); |
|
|
|
|
|
|
|
|
|
|
|
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, |
|
|
|
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, |
|
|
|
propertyValue)); |
|
|
|
propertyValue)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
template.process(root, out); |
|
|
|
} |
|
|
|
} |
|
|
|
template.process(root, out); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int min(int a, int b) { |
|
|
|
int min(int a, int b) { |
|
|
|
int result = Math.min(a, b); |
|
|
|
int result = Math.min(a, b); |
|
|
|
if (result < 0) { |
|
|
|
if (result < 0) { |
|
|
|
result = Math.max(a, b); |
|
|
|
result = Math.max(a, b); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, GeneratorException { |
|
|
|
|
|
|
|
Generator generator = new Generator(ArgumentConfiguration.parse(args)); |
|
|
|
|
|
|
|
generator.start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void generateFor(Class<?> locationClass, List<URL> rescources, String relativeTargetDir) |
|
|
|
public static void main(String[] args) throws IOException, GeneratorException { |
|
|
|
throws IOException, GeneratorException { |
|
|
|
Generator generator = new Generator(ArgumentConfiguration.parse(args)); |
|
|
|
|
|
|
|
generator.start(); |
|
|
|
ArgumentConfiguration.Builder config = new ArgumentConfiguration.Builder(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rescources |
|
|
|
|
|
|
|
.stream() |
|
|
|
|
|
|
|
.map(URL::getFile) |
|
|
|
|
|
|
|
.map(File::new) |
|
|
|
|
|
|
|
.map(File::getAbsolutePath) |
|
|
|
|
|
|
|
.forEach(config::addPropFile); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.setPackageName(locationClass.getPackageName()) |
|
|
|
|
|
|
|
.setTarget(relativeTargetDir); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Generator g = new Generator(config.build()); |
|
|
|
public static void generateFor(Class<?> locationClass, List<URL> rescources, String relativeTargetDir) |
|
|
|
g.start(); |
|
|
|
throws IOException, GeneratorException { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
ArgumentConfiguration.Builder config = new ArgumentConfiguration.Builder(); |
|
|
|
* Represents an Property Entry for the generated java class. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @author markus |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public class Entry { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final String constant; |
|
|
|
rescources.stream().map(URL::getFile).map(File::new).map(File::getAbsolutePath).forEach(config::addPropFile); |
|
|
|
|
|
|
|
|
|
|
|
public final String key; |
|
|
|
config.setPackageName(locationClass.getPackageName()).setTarget(relativeTargetDir); |
|
|
|
|
|
|
|
|
|
|
|
public final String value; |
|
|
|
Generator g = new Generator(config.build()); |
|
|
|
|
|
|
|
g.start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates Property Entry data for the generated java class. |
|
|
|
* Represents an Property Entry for the generated java class. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @author markus |
|
|
|
* |
|
|
|
* |
|
|
|
* @param constant name for the created constant. |
|
|
|
|
|
|
|
* @param key property key |
|
|
|
|
|
|
|
* @param value property value |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public Entry(String constant, String key, String value) { |
|
|
|
public class Entry { |
|
|
|
super(); |
|
|
|
|
|
|
|
this.constant = constant; |
|
|
|
public final String constant; |
|
|
|
this.key = key; |
|
|
|
|
|
|
|
this.value = value; |
|
|
|
public final String key; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public final String value; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates Property Entry data for the generated java class. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param constant name for the created constant. |
|
|
|
|
|
|
|
* @param key property key |
|
|
|
|
|
|
|
* @param value property value |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Entry(String constant, String key, String value) { |
|
|
|
|
|
|
|
super(); |
|
|
|
|
|
|
|
this.constant = constant; |
|
|
|
|
|
|
|
this.key = key; |
|
|
|
|
|
|
|
this.value = value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getConstant() { |
|
|
|
public String getConstant() { |
|
|
|
return constant; |
|
|
|
return constant; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getKey() { |
|
|
|
public String getKey() { |
|
|
|
return key; |
|
|
|
return key; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getValue() { |
|
|
|
public String getValue() { |
|
|
|
return value; |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; |
|
|
|
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|