Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make string helpers available in all templates #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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