Skip to content

Commit

Permalink
More cleaning in preparation of 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver.zeigermann committed Jan 25, 2011
1 parent 156f38a commit e098c80
Show file tree
Hide file tree
Showing 20 changed files with 350 additions and 416 deletions.
4 changes: 2 additions & 2 deletions Tool.launch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/MinimalTemplateEngine/src/com/floreysoft/jmte/Tool.java"/>
<listEntry value="/MinimalTemplateEngine/src/com/floreysoft/jmte/util/Tool.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.floreysoft.jmte.Tool"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.floreysoft.jmte.util.Tool"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="example/buffer.mte example/buffer.properties UTF-8"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="MinimalTemplateEngine"/>
</launchConfiguration>
2 changes: 1 addition & 1 deletion common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ version=2.0
name=jmte
full.name=Java Minimal Template Engine
test.class=com.floreysoft.jmte.AllTests
main-class=com.floreysoft.jmte.Tool
main-class=com.floreysoft.jmte.util.Tool
9 changes: 4 additions & 5 deletions src/com/floreysoft/jmte/DefaultModelAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ protected Object nextStep(Object o, String attributeName,
ErrorHandler errorHandler, Token token) {
Object result;
if (o instanceof String) {
errorHandler.error("no-call-on-string", token, Engine.toModel(
"receiver", o.toString()));
errorHandler.error("no-call-on-string", token, new ModelBuilder(
"receiver", o.toString()).build());
return o;
} else if (o instanceof Map) {
Map map = (Map) o;
Expand All @@ -89,9 +89,8 @@ protected Object nextStep(Object o, String attributeName,
try {
result = Util.getPropertyValue(o, attributeName);
} catch (Exception e) {
errorHandler.error("property-access-error", token, Engine
.toModel("property", attributeName, "object", o,
"exception", e));
errorHandler.error("property-access-error", token, new ModelBuilder("property", attributeName, "object", o,
"exception", e).build());
result = "";
}
}
Expand Down
244 changes: 38 additions & 206 deletions src/com/floreysoft/jmte/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.floreysoft.jmte.template.InterpretedTemplate;
import com.floreysoft.jmte.template.Template;
import com.floreysoft.jmte.token.Token;
import com.floreysoft.jmte.util.Tool;
import com.floreysoft.jmte.util.Util;

/**
Expand Down Expand Up @@ -64,180 +65,16 @@
* @see ProcessListener
*/
public final class Engine {
/**
* Replacement for {@link java.lang.String.format}. All arguments will be
* put into the model having their index starting from 1 as their name.
*
* @param pattern
* the template
* @param args
* any number of arguments
* @return the expanded template
*/
public static String format(String pattern, Object... args) {
Map<String, Object> model = arrayToModel(null, args);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

public static String formatNamed(String pattern, String name1, Object value1) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

public static String formatNamed(String pattern, String name1,
Object value1, String name2, Object value2) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
model.put(name2, value2);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

public static String formatNamed(String pattern, String name1,
Object value1, String name2, Object value2, String name3,
Object value3) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
model.put(name2, value2);
model.put(name3, value3);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

public static String formatNamed(String pattern, String name1,
Object value1, String name2, Object value2, String name3,
Object value3, String name4, Object value4) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
model.put(name2, value2);
model.put(name3, value3);
model.put(name4, value4);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

public static String formatNamed(String pattern, String name1,
Object value1, String name2, Object value2, String name3,
Object value3, String name4, Object value4, String name5,
Object value5) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
model.put(name2, value2);
model.put(name3, value3);
model.put(name4, value4);
model.put(name5, value4);
Engine engine = new Engine();
String output = engine.transform(pattern, model);
return output;
}

/**
* Transforms an array to a model using the index of the elements (starting
* from 1) in the array and a prefix to form their names.
*
* @param prefix
* the prefix to add to the index or <code>null</code> if none
* shall be applied
* @param args
* the array to be transformed into the model
* @return the model containing the arguments
*/
public static Map<String, Object> arrayToModel(String prefix,
Object... args) {
Map<String, Object> model = new HashMap<String, Object>();
if (prefix == null) {
prefix = "";
}
for (int i = 0; i < args.length; i++) {
Object value = args[i];
String name = prefix + (i + 1);
model.put(name, value);
}
return model;
}

public static Map<String, Object> toModel(String name1, Object value1) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(name1, value1);
return model;
}

public static Map<String, Object> toModel(String name1, Object value1,
String name2, Object value2) {
Map<String, Object> model = toModel(name1, value1);
model.put(name2, value2);
return model;
}

public static Map<String, Object> toModel(String name1, Object value1,
String name2, Object value2, String name3, Object value3) {
Map<String, Object> model = toModel(name1, value1, name2, value2);
model.put(name3, value3);
return model;
}

/**
* Merges any number of named lists into a single one containing their
* combined values. Can be very handy in case of a servlet request which
* might contain several lists of parameters that you want to iterate over
* in a combined way.
*
* @param names
* the names of the variables in the following lists
* @param lists
* the lists containing the values for the named variables
* @return a merge list containing the combined values of the lists
*/
public static List<Map<String, Object>> mergeLists(String[] names,
List<Object>... lists) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
if (lists.length != 0) {

// first check if all looks good
int expectedSize = lists[0].size();
for (int i = 1; i < lists.length; i++) {
List<Object> list = lists[i];
if (list.size() != expectedSize) {
throw new IllegalArgumentException(
"All lists and array of names must have the same size!");
}
}

// yes, things are ok
List<Object> masterList = lists[0];
for (int i = 0; i < masterList.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
for (int j = 0; j < lists.length; j++) {
String name = names[j];
List<Object> list = lists[j];
Object value = list.get(i);
map.put(name, value);
}
resultList.add(map);
}
}
return resultList;
}

private String exprStartToken = "${";
private String exprEndToken = "}";
private double expansionSizeFactor = 2;
private ErrorHandler errorHandler = new DefaultErrorHandler();
private Locale locale = new Locale("en");
private boolean useCompilation = false;
private ModelAdaptor modelAdaptor = new DefaultModelAdaptor();
// will be kept as long as the engine lives
// TODO: As classes will never be unloaded it might be a good idea to have the templates in a static, shared location?
private final Map<String, Template> compiledTemplates = new HashMap<String, Template>();

// As classes will never be unloaded and are thus global, it might be a good idea to have
// the templates in a static, shared location as well
private final static Map<String, Template> compiledTemplates = new HashMap<String, Template>();

private final Map<Class<?>, Renderer<?>> renderers = new HashMap<Class<?>, Renderer<?>>();
private final Map<Class<?>, Renderer<?>> resolvedRendererCache = new HashMap<Class<?>, Renderer<?>>();
Expand Down Expand Up @@ -267,29 +104,39 @@ private void init() {
*
* @param template
* the template to expand
* @param sourceName
* the name of the current template (if there is anything like
* that)
* @param model
* the model used to evaluate expressions inside the template
* @return the expanded output
*/
public String transform(String template, String sourceName, Map<String, Object> model) {
public String transform(String template, String sourceName,
Map<String, Object> model) {
Template templateImpl = getTemplate(template, sourceName);
String output = templateImpl.transform(model);
return output;
}

/**
* Transforms a template into an expanded output using the given model.
*
* @param template
* the template to expand
* @param model
* the model used to evaluate expressions inside the template
* @return the expanded output
*/
public String transform(String template, Map<String, Object> model) {
return transform(template, null, model);
}

/**
* Sets the error handler to be used in this engine
*
* @param errorHandler
* the new error handler
* Gets all variables used in the given template.
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
this.errorHandler.setLocale(locale);
public Set<String> getUsedVariables(String template) {
Template templateImpl = getTemplate(template, null);
return templateImpl.getUsedVariables();
}

public Engine registerNamedRenderer(NamedRenderer renderer) {
Expand Down Expand Up @@ -400,11 +247,10 @@ protected void notifyProcessListeners(Token token, Action action) {
}
}

/**
* Gets the currently used error handler
*
* @return the error handler
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}

public ErrorHandler getErrorHandler() {
return errorHandler;
}
Expand Down Expand Up @@ -433,22 +279,6 @@ public double getExpansionSizeFactor() {
return expansionSizeFactor;
}

public void setLocale(Locale locale) {
this.locale = locale;
if (this.errorHandler != null) {
this.errorHandler.setLocale(locale);
}
}

public Locale getLocale() {
return locale;
}

public Set<String> getUsedVariables(String template) {
Template templateImpl = getTemplate(template, null);
return templateImpl.getUsedVariables();
}

public boolean isUseCompilation() {
return useCompilation;
}
Expand All @@ -457,11 +287,20 @@ public void setUseCompilation(boolean useCompilation) {
this.useCompilation = useCompilation;
}

public void setModelAdaptor(ModelAdaptor modelAdaptor) {
this.modelAdaptor = modelAdaptor;
}

public ModelAdaptor getModelAdaptor() {
return modelAdaptor;
}

private Template getTemplate(String template, String sourceName) {
if (useCompilation) {
Template templateImpl = compiledTemplates.get(template);
if (templateImpl == null) {
templateImpl = new Compiler(template, sourceName, this).compile();
templateImpl = new Compiler(template, sourceName, this)
.compile();
compiledTemplates.put(template, templateImpl);
}
return templateImpl;
Expand All @@ -471,11 +310,4 @@ private Template getTemplate(String template, String sourceName) {
}
}

public void setModelAdaptor(ModelAdaptor modelAdaptor) {
this.modelAdaptor = modelAdaptor;
}

public ModelAdaptor getModelAdaptor() {
return modelAdaptor;
}
}
Loading

0 comments on commit e098c80

Please sign in to comment.