Skip to content

Commit

Permalink
separate ParamValue and ParamValueList
Browse files Browse the repository at this point in the history
new data template engine
templates remains old
  • Loading branch information
zen0wu committed May 7, 2014
1 parent c01d7d5 commit c528859
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 75 deletions.
18 changes: 16 additions & 2 deletions src/main/java/greed/Greed.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import greed.model.Param;
import greed.model.Problem;
import greed.template.TemplateEngine;
import greed.template.TemplateEngineFactory;
import greed.ui.ConfigurationDialog;
import greed.ui.GreedEditorPanel;
import greed.util.*;
Expand Down Expand Up @@ -199,7 +200,7 @@ private void setProblem(Contest contest, Problem problem, Language language, boo
sharedModel.put("CutEnd", langConfig.getCutEnd());

// Switch language
currentEngine = TemplateEngine.newLanguageEngine(language);
currentEngine = TemplateEngineFactory.newLanguageEngine(language);

// Validate template definitions and calculate order
ArrayList<String> templateOrder;
Expand Down Expand Up @@ -291,10 +292,23 @@ else if (!templateSet.contains(depTemplate)) {
indivModel.put("Options", template.getOptions());
dependencyModel.put(templateName, indivModel);

TemplateEngine engine = currentEngine;
if (template.getOptions() != null && template.getOptions().containsKey("engine")) {
try {
engine = TemplateEngineFactory.newSpecialEngine(template.getOptions().get("engine"));
}
catch (IllegalArgumentException e) {
talkingWindow.indent();
talkingWindow.error(e.getMessage() + ", skip");
talkingWindow.unindent();
continue;
}
}

// Generate code from templates
String output;
try {
output = currentEngine.render(
output = engine.render(
FileSystem.getResource(template.getTemplateFile()),
mergeModels(sharedModel, indivModel)
);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/greed/code/lang/AbstractLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ public ParamValue parseValue(String value, Param param) {
}
}

return new ParamValue(param, valueList.toArray(new String[0]));
return new ParamValueList(param, valueList.toArray(new String[valueList.size()]));
} else if (value.length() == 0) {
//Empty array
return new ParamValue( param, new String[]{} );
return new ParamValueList(param, new String[]{});
} else {
String[] valueList = value.split(",");
Param paramWithPrim = new Param(param.getName(), Type.primitiveType(param.getType().getPrimitive()), param.getIndex());
for (int i = 0; i < valueList.length; i++) {
valueList[i] = renderParamValue(new ParamValue(paramWithPrim, valueList[i].trim()));
valueList[i] = valueList[i].trim();
}
return new ParamValue(param, valueList);
return new ParamValueList(param, valueList);
}
}

Expand Down
32 changes: 10 additions & 22 deletions src/main/java/greed/model/ParamValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@
public class ParamValue {
private Param param;
private String value;
private String[] valueList;

public ParamValue(Param param, String value) {
this.param = param;
this.value = value;
this.valueList = new String[]{value};
}

public ParamValue(Param param, String[] valueList) {
this.param = param;
StringBuilder buf = new StringBuilder();
buf.append("{ ");
String sep = "";
for (String s : valueList) {
buf.append(sep);
sep = ", ";
buf.append(s);
}
buf.append(" }");
this.value = buf.toString();
this.valueList = valueList;
}

public Param getParam() {
return param;
}
Expand All @@ -37,15 +20,20 @@ public String getValue() {
return value;
}

public String[] getValueList() {
return valueList;
public String[] getStrings() {
return new String[] { value };
}

public ParamValue[] getValueList() {
return new ParamValue[] { this };
}

public int getValueListLength() {
return valueList.length;
return this.getValueList().length;
}

public boolean isMultiLine() {
return valueList.length > 1;
return this.getValueListLength() > 1;
}

}
45 changes: 45 additions & 0 deletions src/main/java/greed/model/ParamValueList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package greed.model;

/**
* Greed is good! Cheers!
*/
public class ParamValueList extends ParamValue {

private ParamValue[] values;
private String[] valueStrings;

public ParamValueList(Param param, String[] valueList) {
super(param, joinValues(valueList));
assert param.getType().isArray();

this.valueStrings = valueList;
this.values = new ParamValue[valueList.length];
Type elemType = new Type(param.getType().getPrimitive(), param.getType().getDim() - 1);
Param elemParam = new Param(param.getName(), elemType, param.getIndex());
for (int i = 0; i < values.length; ++i) {
this.values[i] = new ParamValue(elemParam, valueList[i]);
}
}

private static String joinValues(String[] valueList) {
StringBuilder buf = new StringBuilder();
buf.append("{ ");
String sep = "";
for (String s : valueList) {
buf.append(sep);
sep = ", ";
buf.append(s);
}
buf.append(" }");
return buf.toString();
}

public ParamValue[] getValueList() {
return values;
}

@Override
public String[] getStrings() {
return valueStrings;
}
}
7 changes: 2 additions & 5 deletions src/main/java/greed/template/HTMLRenderer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package greed.template;

import greed.code.LanguageManager;
import greed.model.Language;
import greed.model.Method;
import greed.model.ParamValue;
import greed.model.Type;
import greed.model.*;
import greed.util.StringUtil;

import java.util.Locale;
Expand Down Expand Up @@ -52,7 +49,7 @@ private String renderParamValue(ParamValue pv, String param) {

if (t.isString()) {
if (t.isArray()) {
String[] x = pv.getValueList();
String[] x = pv.getStrings();
boolean useGrid = isGridMode(param, x);
return doRenderStringArray(x, useGrid);
} else {
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/greed/template/TemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@
*/
public class TemplateEngine {

private static TemplateEngine bare = new TemplateEngine();

public static TemplateEngine getBareEngine() { return bare; }

public static TemplateEngine newLanguageEngine(Language lang) {
return new TemplateEngine(lang);
}

private Engine engine = null;
// Black magic for the renderer to access the current model
private Stack<Map<String, Object>> modelStack = new Stack<Map<String, Object>>();

private TemplateEngine() {
TemplateEngine() {
engine = new Engine();
engine.registerNamedRenderer(new StringUtilRenderer());
engine.registerNamedRenderer(new ContestCategoryRenderer());
Expand All @@ -37,9 +29,8 @@ private TemplateEngine() {
engine.registerNamedRenderer(new SeqRenderer());
}

private TemplateEngine(Language language) {
this();
LanguageManager.getInstance().registerRenderer(language, engine);
Engine getEngine() {
return engine;
}

public String render(InputStream templateStream, Map<String, Object> model) {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/greed/template/TemplateEngineFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package greed.template;

import com.floreysoft.jmte.Renderer;
import greed.code.LanguageManager;
import greed.model.Language;
import greed.model.ParamValue;

import java.util.Locale;

/**
* Greed is good! Cheers!
*/
public class TemplateEngineFactory {
private static TemplateEngine bare = new TemplateEngine();

public static TemplateEngine getBareEngine() {
return bare;
}

public static TemplateEngine newLanguageEngine(Language language) {
TemplateEngine engine = new TemplateEngine();
LanguageManager.getInstance().registerRenderer(language, engine.getEngine());
return engine;
}

public static TemplateEngine newDataEngine() {
TemplateEngine engine = new TemplateEngine();
engine.getEngine().registerRenderer(ParamValue.class, new Renderer<ParamValue>() {
@Override
public String render(ParamValue paramValue, Locale locale) {
return paramValue.getValue();
}
});
return engine;
}

public static TemplateEngine newSpecialEngine(String name) {
if ("data".equals(name))
return newDataEngine();
throw new IllegalArgumentException("No engine named " + name);
}
}
3 changes: 2 additions & 1 deletion src/main/java/greed/util/FileSystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package greed.util;

import greed.template.TemplateEngine;
import greed.template.TemplateEngineFactory;

import java.io.*;
import java.util.HashMap;
Expand Down Expand Up @@ -87,7 +88,7 @@ private static File getBackupFile(File file, int num)
model.put("GeneratedFileName", name);
model.put("BackupNumber", num);
greed.conf.schema.BackupConfig bc = Utils.getGreedConfig().getBackup();
String newname = TemplateEngine.getBareEngine().render( bc.getFileName(), model );
String newname = TemplateEngineFactory.getBareEngine().render( bc.getFileName(), model );
File newfile = new File( file.getParentFile(), newname);
if ( ! newfile.getParentFile().exists() ) {
newfile.getParentFile().mkdirs();
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/template-defs.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ testcase {
overwrite = skip
outputFile = "${Problem.Name}.sample"
templateFile = builtin(testcase/testcases.tmpl)
options = {
engine = data
}

This comment has been minimized.

Copy link
@wookayin

wookayin Jul 6, 2014

Collaborator

what are theses for? OK, I got it.

}

# Problem description
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/templates/testcase/testcases.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ ${<foreach e.Input in}
${<if in.Param.Type.Array}
${in.ValueListLength}
${<foreach in.ValueList v}
${v;string("unquote")}
${v}
${<end}
${<else}
${in.Value;string("unquote")}
${in.Value}
${<end}
${<end}

${<if e.Output.Param.Type.Array}
${e.Output.ValueListLength}
${<foreach e.Output.ValueList v}
${v;string("unquote")}
${v}
${<end}
${<else}
${e.Output.Value;string("unquote")}
${e.Output.Value}
${<end}
${<end}
6 changes: 3 additions & 3 deletions src/test/java/greed/code/CStyleLanguageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void parseStringArrayTest() {
sb.append("\"Abcde\"\n, \"12345\", \n\n\n");
sb.append("\"Hello\"\n");
sb.append(", \"world\" }");
String[] parsedValueList = trait.parseValue(sb.toString(), new Param("arg", Type.STRING_ARRAY_TYPE, 0)).getValueList();
String[] parsedValueList = trait.parseValue(sb.toString(), new Param("arg", Type.STRING_ARRAY_TYPE, 0)).getStrings();
for (String pv : parsedValueList)
System.out.println(pv);
Assert.assertArrayEquals("Parsed value is " + Arrays.toString(parsedValueList), parsedValueList,
Expand All @@ -34,10 +34,10 @@ public void parseOtherArrayTest() {
StringBuilder sb = new StringBuilder();
sb.append("{123, ");
sb.append("\n123\n,125,999,\n\n12\n,123\n } \n");
String[] parsedValueList = trait.parseValue(sb.toString(), new Param("arg", Type.LONG_ARRAY_TYPE, 0)).getValueList();
String[] parsedValueList = trait.parseValue(sb.toString(), new Param("arg", Type.LONG_ARRAY_TYPE, 0)).getStrings();
for (String pv : parsedValueList)
System.out.println(pv);
Assert.assertArrayEquals("Parsed value is " + Arrays.toString(parsedValueList), parsedValueList,
new String[]{"123LL", "123LL", "125LL", "999LL", "12LL", "123LL"});
new String[]{"123", "123", "125", "999", "12", "123"});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private Map<String, Object> createModel(String key, String name, int div) {

@Before
public void setup() {
engine = TemplateEngine.newLanguageEngine(Language.CPP);
engine = TemplateEngineFactory.newLanguageEngine(Language.CPP);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/greed/template/CppTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setupTemplates() throws IOException {
this.testTemplate = getClass().getResourceAsStream("/templates/test/cpp.tmpl");
assertThat(this.testTemplate, notNullValue());

engine = TemplateEngine.newLanguageEngine(Language.CPP);
engine = TemplateEngineFactory.newLanguageEngine(Language.CPP);
}

@Test
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/greed/template/HTMLRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import greed.model.Language;
import greed.model.Param;
import greed.model.ParamValue;
import greed.model.Type;

import greed.model.*;

import java.util.HashMap;

Expand All @@ -24,11 +22,11 @@ public class HTMLRendererTest {

@Before
public void setup() {
engine = TemplateEngine.newLanguageEngine(Language.CPP);
engine = TemplateEngineFactory.newLanguageEngine(Language.CPP);
}

static final ParamValue GRID_LIKE_STRING_ARRAY_PARAM =
new ParamValue(
new ParamValueList(
new Param("gridParam", Type.STRING_ARRAY_TYPE, 0),
new String[] { "####", "#..#", "#..#", "####" }
);
Expand Down Expand Up @@ -66,7 +64,7 @@ public void testRenderHtmlStringQuote() {
System.out.println(result);
assertThat(result, equalTo("&quot;quoted&quot;"));

model.put("gridParam", new ParamValue(
model.put("gridParam", new ParamValueList(
new Param("gridParam", Type.STRING_ARRAY_TYPE, 1),
new String[] {"\"\"", ".."}
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/greed/template/JavaTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void setupTemplates() throws IOException {
this.testJunitTemplate = getClass().getResourceAsStream("/templates/unittest/junit.java.tmpl");
assertThat(this.testTemplate, notNullValue());

engine = TemplateEngine.newLanguageEngine(Language.JAVA);
engine = TemplateEngineFactory.newLanguageEngine(Language.JAVA);
}

@Test
Expand Down
Loading

0 comments on commit c528859

Please sign in to comment.