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

Methods should be protected so that they can be used/overidden from outs... #802

Merged
Merged
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
2 changes: 1 addition & 1 deletion framework/src/play/templates/BaseTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void throwException(Throwable e) {
throw new RuntimeException(e);
}

abstract Throwable cleanStackTrace(Throwable e);
protected abstract Throwable cleanStackTrace(Throwable e);
public static ThreadLocal<BaseTemplate> layout = new ThreadLocal<BaseTemplate>();
public static ThreadLocal<Map<Object, Object>> layoutData = new ThreadLocal<Map<Object, Object>>();
public static ThreadLocal<BaseTemplate> currentTemplate = new ThreadLocal<BaseTemplate>();
Expand Down
98 changes: 76 additions & 22 deletions framework/src/play/templates/GroovyTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;

import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.GroovyShell;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
Expand All @@ -23,13 +25,19 @@
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
import org.codehaus.groovy.control.messages.ExceptionMessage;
import org.codehaus.groovy.control.messages.Message;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.tools.GroovyClass;

import play.Logger;
import play.Play;
import play.Play.Mode;
import play.classloading.ApplicationClassloader;
import play.classloading.BytecodeCache;
import play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer;
import play.data.binding.Unbinder;
Expand Down Expand Up @@ -61,7 +69,7 @@
public class GroovyTemplate extends BaseTemplate {

static final Map<String, SafeFormatter> safeFormatters = new HashMap<String, SafeFormatter>();

static {
safeFormatters.put("csv", new SafeCSVFormatter());
safeFormatters.put("html", new SafeHTMLFormatter());
Expand All @@ -75,7 +83,7 @@ public static <T> void registerFormatter(String format, SafeFormatter formatter)
static {
new GroovyShell().evaluate("java.lang.String.metaClass.if = { condition -> if(condition) delegate; else '' }");
}

public GroovyTemplate(String name, String source) {
super(name, source);
}
Expand All @@ -89,7 +97,7 @@ public static class TClassLoader extends GroovyClassLoader {
public TClassLoader() {
super(Play.classloader);
}

public Class defineTemplate(String name, byte[] byteCode) {
return defineClass(name, byteCode, 0, byteCode.length, Play.classloader.protectionDomain);
}
Expand All @@ -110,21 +118,34 @@ void directLoad(byte[] code) throws Exception {
}
}
}

/**
* Define the Compiler configuration
* @return the compiler Configuration
*/
protected CompilerConfiguration setUpCompilerConfiguration(){
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setSourceEncoding("utf-8"); // ouf
return compilerConfiguration;
}

protected void onCompileEnd(){
}

public void compile() {
if (compiledTemplate == null) {
try {
long start = System.currentTimeMillis();

TClassLoader tClassLoader = new TClassLoader();

// Let's compile the groovy source
final List<GroovyClass> groovyClassesForThisTemplate = new ArrayList<GroovyClass>();
// ~~~ Please !
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setSourceEncoding("utf-8"); // ouf
CompilerConfiguration compilerConfiguration = this.setUpCompilerConfiguration();

CompilationUnit compilationUnit = new CompilationUnit(compilerConfiguration);
compilationUnit.addSource(new SourceUnit(name, compiledSource, compilerConfiguration, tClassLoader, compilationUnit.getErrorCollector()));
compilationUnit.addSource(new SourceUnit(name, compiledSource, compilerConfiguration, tClassLoader, compilationUnit.getErrorCollector()));

Field phasesF = compilationUnit.getClass().getDeclaredField("phaseOperations");
phasesF.setAccessible(true);
LinkedList[] phases = (LinkedList[]) phasesF.get(compilationUnit);
Expand Down Expand Up @@ -175,21 +196,32 @@ public void call(GroovyClass gclass) {

} catch (MultipleCompilationErrorsException e) {
if (e.getErrorCollector().getLastError() != null) {
SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) e.getErrorCollector().getLastError();
SyntaxException syntaxException = errorMessage.getCause();
Integer line = this.linesMatrix.get(syntaxException.getLine());
if (line == null) {
line = 0;
}
String message = syntaxException.getMessage();
if (message.indexOf("@") > 0) {
message = message.substring(0, message.lastIndexOf("@"));
Message errorMsg = e.getErrorCollector().getLastError();
if (errorMsg instanceof SyntaxErrorMessage) {
SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) e.getErrorCollector().getLastError();
SyntaxException syntaxException = errorMessage.getCause();
Integer line = this.linesMatrix.get(syntaxException.getLine());
if (line == null) {
line = 0;
}
String message = syntaxException.getMessage();
if (message.indexOf("@") > 0) {
message = message.substring(0, message.lastIndexOf("@"));
}
throw new TemplateCompilationException(this, line, message);
} else{
ExceptionMessage errorMessage = (ExceptionMessage ) e.getErrorCollector().getLastError();
Exception exception = errorMessage.getCause();
Integer line = 0;
String message = exception.getMessage();
throw new TemplateCompilationException(this, line, message);
}
throw new TemplateCompilationException(this, line, message);
}
throw new UnexpectedException(e);
} catch (Exception e) {
throw new UnexpectedException(e);
} finally{
this.onCompileEnd();
}
}
compiledTemplateName = compiledTemplate.getName();
Expand All @@ -204,13 +236,20 @@ public String render(Map<String, Object> args) {
}
}

@Override
protected String internalRender(Map<String, Object> args) {
compile();
protected Binding setUpBindingVariables(Map<String, Object> args){
Binding binding = new Binding(args);
binding.setVariable("play", new Play());
binding.setVariable("messages", new Messages());
binding.setVariable("lang", Lang.get());
return binding;
}

@Override
protected String internalRender(Map<String, Object> args) {
compile();

Binding binding = this.setUpBindingVariables(args);

// If current response-object is present, add _response_encoding'
Http.Response currentResponse = Http.Response.current();
if (currentResponse != null) {
Expand Down Expand Up @@ -299,7 +338,7 @@ protected String internalRender(Map<String, Object> args) {
return null;
}

Throwable cleanStackTrace(Throwable e) {
protected Throwable cleanStackTrace(Throwable e) {
List<StackTraceElement> cleanTrace = new ArrayList<StackTraceElement>();
for (StackTraceElement se : e.getStackTrace()) {
//Here we are parsing the classname to find the file on disk the template was generated from.
Expand Down Expand Up @@ -406,7 +445,22 @@ public void invokeTag(Integer fromLine, String tag, Map<String, Object> attrs, C
TagContext.exitTag();
}

/**
* @deprecated '_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on
* use {@link #__loadClass} instead
*/
@Deprecated
public Class _(String className) throws Exception {
return __loadClass(className);
}

/**
* Load the class from Pay Class loader
* @param className : the class name
* @return the given class
* @throws Exception
*/
public Class __loadClass(String className) throws Exception {
try {
return Play.classloader.loadClass(className);
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -557,7 +611,7 @@ public Object invokeMethod(String name, Object param) {
}
}

static boolean isSimpleParam(Class type) {
protected static boolean isSimpleParam(Class type) {
return Number.class.isAssignableFrom(type) || type.equals(String.class) || type.isPrimitive();
}
}
20 changes: 10 additions & 10 deletions framework/src/play/templates/GroovyTemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BaseTemplate compile(BaseTemplate template) {
}

@Override
String source() {
protected String source() {
String source = template.source;

// If a plugin has something to change in the template before the compilation
Expand Down Expand Up @@ -111,7 +111,7 @@ public int compare(String o1, String o2) {
}

@Override
void head() {
protected void head() {
print("class ");
//This generated classname is parsed when creating cleanStackTrace.
//The part after "Template_" is used as key when
Expand All @@ -131,7 +131,7 @@ void head() {

@Override
@SuppressWarnings("unused")
void end() {
protected void end() {
for (String n : extensionsClassnames) {
println(" } ");
}
Expand All @@ -148,7 +148,7 @@ void end() {
*/

@Override
void plain() {
protected void plain() {
String text = parser.getToken().replace("\\", "\\\\").replaceAll("\"", "\\\\\"").replace("$", "\\$");
if (skipLineBreak && text.startsWith("\n")) {
text = text.substring(1);
Expand Down Expand Up @@ -184,7 +184,7 @@ void plain() {
}

@Override
void script() {
protected void script() {
String text = parser.getToken();
if (text.indexOf("\n") > -1) {
String[] lines = parser.getToken().split("\n");
Expand All @@ -202,23 +202,23 @@ void script() {
}

@Override
void expr() {
protected void expr() {
String expr = parser.getToken().trim();
print(";out.print(__safeFaster("+expr+"))");
markLine(parser.getLine());
println();
}

@Override
void message() {
protected void message() {
String expr = parser.getToken().trim();
print(";out.print(__getMessage("+expr+"))");
markLine(parser.getLine());
println();
}

@Override
void action(boolean absolute) {
protected void action(boolean absolute) {
String action = parser.getToken().trim();
if (action.trim().matches("^'.*'$")) {
if (absolute) {
Expand All @@ -241,7 +241,7 @@ void action(boolean absolute) {
}

@Override
void startTag() {
protected void startTag() {
tagIndex++;
String tagText = parser.getToken().trim().replaceAll("\r", "").replaceAll("\n", " ");
String tagName = "";
Expand Down Expand Up @@ -302,7 +302,7 @@ void startTag() {
}

@Override
void endTag() {
protected void endTag() {
String tagName = parser.getToken().trim();
if (tagsStack.isEmpty()) {
throw new TemplateCompilationException(template, parser.getLine(), "#{/" + tagName + "} is not opened.");
Expand Down
Loading