Skip to content

Commit

Permalink
0.1.0 - Initial version with TemplateRenderBuilder fix (internally cr…
Browse files Browse the repository at this point in the history
…eated output stream wasn't being closed automatically after rendering template).
  • Loading branch information
TeamworkGuy2 committed Feb 8, 2016
1 parent ac7ae8f commit 6188104
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
JTwg2Templating
==============
version: 0.1.0

Templating tools built on top of StringTemplate (http://www.stringtemplate.org/), part of the ANTLR grammar builder/parser.
Templating tools built on top of [StringTemplate] (http://www.stringtemplate.org/), part of the ANTLR grammar builder/parser.
Emphasis on building templates for generating Java source code files. Particularly, classes that rely on a primitive type, for example a primitive 'int' version of ArrayList.
Also includes some example templates in 'commonTemplates' and 'templates' packages.
49 changes: 33 additions & 16 deletions src/twg2/template/codeTemplate/render/TemplateRenderBuilder.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package twg2.template.codeTemplate.render;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import lombok.Getter;

import org.stringtemplate.v4.ST;

import twg2.template.codeTemplate.ClassInfo;
Expand Down Expand Up @@ -69,8 +74,9 @@ public static <T extends PrimitiveTypeClassTemplate> Builder fromPrimitiveClassT


public static class Builder implements SetDstBeforeParamsStep, SetParamsStep<Builder>, SetDstStep, SetParamsOrDstStep<Builder> {
private Map<String, Object> params = new HashMap<>();
private Appendable dst;
private @Getter Map<String, Object> params = new HashMap<>();
private @Getter Appendable dstStream;
private @Getter ClassLocation dstLocation;


public Builder() {
Expand All @@ -80,7 +86,8 @@ public Builder() {
public Builder copy() {
Builder copy = new Builder();
copy.params = new HashMap<>(this.params);
copy.dst = this.dst;
copy.dstStream = this.dstStream;
copy.dstLocation = this.dstLocation;
return copy;
}

Expand All @@ -101,11 +108,6 @@ public Object getParam(String paramName) {
}


public Map<String, Object> getParams() {
return this.params;
}


@Override
public Builder addParam(String name, Object param) {
this.params.put(name, param);
Expand All @@ -129,29 +131,44 @@ public Builder setParams(Map<String, Object> params) {
}


public Appendable getDst() {
return dst;
}


@Override
public Builder writeDst(ClassLocation locationInfo) {
this.dst = TemplateFilesIo.getDefaultInst().getSrcRelativeStream(locationInfo);
this.dstLocation = locationInfo;
return this;
}


@Override
public Builder writeDst(Appendable out) {
this.dst = out;
this.dstStream = out;
return this;
}


public void render(ST template) {
params.forEach(template::add);

StringTemplatesUtil.render(template, dst);
if(this.dstStream != null) {
Appendable dst = this.dstStream;

StringTemplatesUtil.render(template, dst);
}
else if(this.dstLocation != null) {
Writer dst = TemplateFilesIo.getDefaultInst().getSrcRelativeStream(this.dstLocation);

try {
StringTemplatesUtil.render(template, dst);
} finally {
try {
dst.close();
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
}
else {
throw new IllegalStateException("cannot render template without destination");
}

params.keySet().forEach(template::remove);
}
Expand Down
8 changes: 8 additions & 0 deletions versions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--------
####0.1.0
date: 2016-2-8

commit: ?

* Initial versioning of existing code, includes utilities for easily generating Java classes, interfaces, and DTOs using the [StringTemplate] (http://www.stringtemplate.org/) library
* Fixed TemplateRenderBuilder not closing internal ClassLocation based output stream after rendering template

0 comments on commit 6188104

Please sign in to comment.