Skip to content

Commit

Permalink
Make string helpers available in all templates
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroyle committed Sep 9, 2011
1 parent 662a32e commit 05afb9f
Show file tree
Hide file tree
Showing 3 changed files with 1,339 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.jboss.seam.render.util.StringUtils;

import org.mvel2.CompileException;
import org.mvel2.ParserContext;
Expand Down Expand Up @@ -317,6 +318,17 @@ template, captureOrbInternal(), start, parserContext)
return root;
}

/**
* Imports the StringUtils class as the alias "render" so that all of the
* static String utility methods in that class are available to the template
* like @{render.uncapitalize(entityBeanName)}.
*
* @param context
*/
private void addBundledHelpers(ParserContext context) {
context.addImport("render", StringUtils.class);
}

// Parse Utilities Start Here

private boolean isNext(char c) {
Expand Down Expand Up @@ -367,6 +379,10 @@ private Node markTextNode(Node n) {
public ParserContext getParserContext() {
return parserContext;
}
private void setParserContext(ParserContext context) {
addBundledHelpers(context);
this.parserContext = context;
}

public static CompiledTemplate compileTemplate(String template) {
return new CustomTemplateCompiler(template, true, ParserContext.create()).compile();
Expand Down Expand Up @@ -477,7 +493,7 @@ public CustomTemplateCompiler(char[] template, boolean codeCache) {
public CustomTemplateCompiler(char[] template, boolean codeCache, ParserContext context) {
this.length = (this.template = template).length;
this.codeCache = codeCache;
this.parserContext = context;
setParserContext(context);
}

public CustomTemplateCompiler(CharSequence sequence) {
Expand All @@ -492,7 +508,7 @@ public CustomTemplateCompiler(CharSequence sequence, boolean codeCache) {
public CustomTemplateCompiler(CharSequence sequence, boolean codeCache, ParserContext context) {
this.length = (this.template = sequence.toString().toCharArray()).length;
this.codeCache = codeCache;
this.parserContext = context;
setParserContext(context);
}

public CustomTemplateCompiler(String template, Map<String, Class<? extends Node>> customNodes) {
Expand Down Expand Up @@ -534,15 +550,15 @@ public CustomTemplateCompiler(String template, Map<String, Class<? extends Node>
this.length = (this.template = template.toCharArray()).length;
this.customNodes = customNodes;
this.codeCache = codeCache;
this.parserContext = context;
setParserContext(context);
}

public CustomTemplateCompiler(char[] template, Map<String, Class<? extends Node>> customNodes, boolean codeCache,
ParserContext context) {
this.length = (this.template = template).length;
this.customNodes = customNodes;
this.codeCache = codeCache;
this.parserContext = context;
setParserContext(context);
}

public CustomTemplateCompiler(CharSequence sequence, Map<String, Class<? extends Node>> customNodes,
Expand All @@ -551,6 +567,6 @@ public CustomTemplateCompiler(CharSequence sequence, Map<String, Class<? extends
this.length = (this.template = sequence.toString().toCharArray()).length;
this.customNodes = customNodes;
this.codeCache = codeCache;
this.parserContext = context;
setParserContext(context);
}
}
Loading

0 comments on commit 05afb9f

Please sign in to comment.