From do-not-reply at jboss.org Sun Nov 1 06:24:59 2009
Content-Type: multipart/mixed; boundary="===============3851220618313644502=="
MIME-Version: 1.0
From: do-not-reply at jboss.org
To: gatein-commits at lists.jboss.org
Subject: [gatein-commits] gatein SVN: r467 - in
portal/branches/performance/component/scripting/src:
main/java/org/exoplatform/groovyscript/text and 1 other directories.
Date: Sun, 01 Nov 2009 06:24:59 -0500
Message-ID: <200911011124.nA1BOxer004701@svn01.web.mwc.hst.phx2.redhat.com>
--===============3851220618313644502==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: julien_viet
Date: 2009-11-01 06:24:58 -0500 (Sun, 01 Nov 2009)
New Revision: 467
Added:
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/GroovyCompilationException.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/GroovyScript.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/Position.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TemplateCompilationException.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TemplateRuntimeException.java
Removed:
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TemplateCompiler.java
Modified:
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/BaseScript.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/GroovyScriptBuilder.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/GroovyTemplate.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/GroovyTemplateEngine.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/LineBreakItem.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/SectionItem.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TemplateParser.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TemplateSection.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/TextItem.java
portal/branches/performance/component/scripting/src/main/java/org/exopla=
tform/groovyscript/text/TemplateService.java
portal/branches/performance/component/scripting/src/test/java/org/exopla=
tform/groovyscript/TestTemplateCompiler.java
portal/branches/performance/component/scripting/src/test/java/org/exopla=
tform/groovyscript/TestTemplateParser.java
portal/branches/performance/component/scripting/src/test/java/org/exopla=
tform/groovyscript/TestTemplateRendering.java
Log:
GTNPORTAL-146 : Contextual error reporting of Groovy templates runtime exce=
ption
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/BaseScript.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/BaseScript.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/BaseScript.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -22,6 +22,8 @@
import java.io.IOException;
=
/**
+ * The internal base script of a Groovy script as seen by the Groovy world.
+ *
* @author Julien Viet
* @version $Revision$
*/
Added: portal/branches/performance/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/GroovyCompilationException.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyCompilationException.java =
(rev 0)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyCompilationException.java 2009-11-01 11:24:58 UTC=
(rev 467)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see.
+ */
+package org.exoplatform.groovyscript;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class GroovyCompilationException extends TemplateCompilationExcepti=
on
+{
+
+ /** . */
+ private final String groovyText;
+
+ public GroovyCompilationException(Throwable cause, String templateText,=
String groovyText)
+ {
+ super(cause, templateText);
+
+ //
+ this.groovyText =3D groovyText;
+ }
+
+ public String getGroovyText()
+ {
+ return groovyText;
+ }
+
+ @Override
+ public String getMessage()
+ {
+ return "Groovy compilation exception\n" +
+ "template: " +
+ getTemplateText() + "\n" +
+ "compiled to Groovy: " +
+ getGroovyText() + "\n";
+ }
+}
Added: portal/branches/performance/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/GroovyScript.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyScript.java (rev 0)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyScript.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see.
+ */
+package org.exoplatform.groovyscript;
+
+import groovy.lang.Binding;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * A wrapper for a Groovy script and its meta data.
+ *
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class GroovyScript
+{
+
+ /** . */
+ private final String groovyText;
+
+ /** . */
+ private final Class> scriptClass;
+
+ /** . */
+ private final Map lineTable;
+
+ public GroovyScript(String groovyText, Class> scriptClass, Map lineTable)
+ {
+ this.groovyText =3D groovyText;
+ this.scriptClass =3D scriptClass;
+ this.lineTable =3D lineTable;
+ }
+
+ public String getGroovyText()
+ {
+ return groovyText;
+ }
+
+ public Class> getScriptClass()
+ {
+ return scriptClass;
+ }
+
+ public Map getLineTable()
+ {
+ return lineTable;
+ }
+
+ public void render(Map context, GroovyPrinter writer) throws IOExceptio=
n, TemplateRuntimeException
+ {
+ Binding binding =3D context !=3D null ? new Binding(context) : new B=
inding();
+
+ //
+ BaseScript script =3D (BaseScript)InvokerHelper.createScript(scriptC=
lass, binding);
+ script.printer =3D writer;
+ script.setProperty("out", script.printer);
+
+ //
+ try
+ {
+ script.run();
+ }
+ catch (Exception e)
+ {
+ if (e instanceof IOException)
+ {
+ throw (IOException)e;
+ }
+ else
+ {
+ TextItem textItem =3D findOut(e);
+ throw new TemplateRuntimeException(textItem, e);
+ }
+ }
+ catch (Throwable e)
+ {
+ if (e instanceof Error)
+ {
+ throw ((Error)e);
+ }
+ TextItem textItem =3D findOut(e);
+ throw new TemplateRuntimeException(textItem, e);
+ }
+
+ //
+ script.flush();
+ }
+
+ private TextItem findOut(Throwable t)
+ {
+ StackTraceElement[] elements =3D t.getStackTrace();
+
+ // The index of the script based on the current stack trace
+ int index =3D elements.length - Thread.currentThread().getStackTrace=
().length + 1;
+
+ // Try to find the groovy script line
+ if (index >=3D 0 && index < elements.length)
+ {
+ int groovyLineNumber =3D elements[index].getLineNumber();
+ return lineTable.get(groovyLineNumber);
+ }
+ else
+ {
+ return null;
+ }
+ }
+}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/GroovyScriptBuilder.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyScriptBuilder.java 2009-10-30 21:04:13 UTC (rev 4=
66)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyScriptBuilder.java 2009-11-01 11:24:58 UTC (rev 4=
67)
@@ -16,8 +16,19 @@
*/
package org.exoplatform.groovyscript;
=
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyCodeSource;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
=
/**
* @author Julien Viet
@@ -27,6 +38,12 @@
{
=
/** . */
+ private final String name;
+
+ /** . */
+ private final String templateText;
+
+ /** . */
private SectionType currentType =3D null;
=
/** . */
@@ -35,12 +52,22 @@
/** . */
private Script script =3D new Script();
=
- public void begin(SectionType sectionType)
+ public GroovyScriptBuilder(String name, String templateText)
{
+ this.name =3D name;
+ this.templateText =3D templateText;
+ }
+
+ private void begin(SectionType sectionType, Position pos)
+ {
if (sectionType =3D=3D null)
{
throw new NullPointerException();
}
+ if (pos =3D=3D null)
+ {
+ throw new NullPointerException();
+ }
if (currentType !=3D null)
{
throw new IllegalStateException();
@@ -60,39 +87,48 @@
}
}
=
- public void append(String text)
+ private void append(SectionItem item)
{
- switch (currentType)
+ if (item instanceof TextItem)
{
- case STRING:
- accumulatedText.append(text);
- break;
- case SCRIPTLET:
- script.appendGroovy(text);
- break;
- case EXPR:
- script.appendGroovy(text);
- break;
+ TextItem textItem =3D (TextItem)item;
+ String text =3D textItem.getData();
+ switch (currentType)
+ {
+ case STRING:
+ accumulatedText.append(text);
+ break;
+ case SCRIPTLET:
+ script.appendGroovy(text);
+ script.positionTable.put(script.lineNumber, textItem);
+ break;
+ case EXPR:
+ script.appendGroovy(text);
+ script.positionTable.put(script.lineNumber, textItem);
+ break;
+ }
}
- }
-
- public void lineBreak()
- {
- switch (currentType)
+ else if (item instanceof LineBreakItem)
{
- case STRING:
- accumulatedText.append("\n");
- break;
- case SCRIPTLET:
- script.appendGroovy("\n");
- break;
- case EXPR:
- script.appendGroovy("\n");
- break;
+ switch (currentType)
+ {
+ case STRING:
+ accumulatedText.append("\n");
+ break;
+ case SCRIPTLET:
+ case EXPR:
+ script.appendGroovy("\n");
+ script.lineNumber++;
+ break;
+ }
}
+ else
+ {
+ throw new AssertionError();
+ }
}
=
- public void end()
+ private void end()
{
if (currentType =3D=3D null)
{
@@ -110,10 +146,13 @@
}
break;
case SCRIPTLET:
+ // We append a line break because we want that any line commen=
t does not affect the template
script.appendGroovy("\n");
+ script.lineNumber++;
break;
case EXPR:
- script.appendGroovy("}\");");
+ script.appendGroovy("}\");\n");
+ script.lineNumber++;
break;
}
=
@@ -121,13 +160,65 @@
this.currentType =3D null;
}
=
- public String getScript()
+ public GroovyScript build() throws TemplateCompilationException
{
- return script.toString();
- }
+ List sections =3D new TemplateParser().parse(templa=
teText);
=
+ //
+ for (TemplateSection section : sections)
+ {
+ begin(section.getType(), section.getItems().get(0).getPosition());
+ for (SectionItem item : section.getItems())
+ {
+ append(item);
+ }
+ end();
+ }
=
+ //
+ String groovyText =3D script.toString();
=
+ //
+ CompilerConfiguration config =3D new CompilerConfiguration();
+
+ //
+ byte[] bytes;
+ try
+ {
+ config.setScriptBaseClass(BaseScript.class.getName());
+ bytes =3D groovyText.getBytes(config.getSourceEncoding());
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new TemplateCompilationException(e, groovyText);
+ }
+
+ //
+ InputStream in =3D new ByteArrayInputStream(bytes);
+ GroovyCodeSource gcs =3D new GroovyCodeSource(in, name, "/groovy/she=
ll");
+ GroovyClassLoader loader =3D new GroovyClassLoader(Thread.currentThr=
ead().getContextClassLoader(), config);
+ Class> scriptClass;
+ try
+ {
+ scriptClass =3D loader.parseClass(gcs, false);
+ }
+ catch (CompilationFailedException e)
+ {
+ throw new GroovyCompilationException(e, templateText, groovyText);
+ }
+ catch (ClassFormatError e)
+ {
+ throw new GroovyCompilationException(e, templateText, groovyText);
+ }
+
+ return new GroovyScript(
+ script.toString(), scriptClass, Collections.unmodifiableMap(new H=
ashMap(script.positionTable))
+ );
+ }
+
+ /**
+ * Internal representation of a script
+ */
private static class Script
{
=
@@ -140,6 +231,12 @@
/** . */
private int methodCount =3D 0;
=
+ /** The line number table. */
+ private Map positionTable =3D new HashMap();
+
+ /** The current line number. */
+ private int lineNumber =3D 1;
+
@Override
public String toString()
{
@@ -159,8 +256,9 @@
public void appendText(String text)
{
TextContant m =3D new TextContant("s" + methodCount++, text);
- out.append(";out.print(Constants.").append(m.name).append(");\n");
+ out.append("out.print(Constants.").append(m.name).append(");\n");
textMethods.add(m);
+ lineNumber++;
}
=
public void appendGroovy(String s)
@@ -203,26 +301,6 @@
}
else
{
-/*
- String code =3D Integer.toHexString(c);
- switch (code.length())
- {
- default:
- throw new AssertionError();
- case 1:
- code =3D "000" + code;
- break;
- case 2:
- code =3D "00" + code;
- break;
- case 3:
- code =3D "0" + code;
- break;
- case 4:
- break;
- }
- builder.append("\\u").append(code);
-*/
builder.append(c);
}
}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/GroovyTemplate.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyTemplate.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyTemplate.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -16,29 +16,21 @@
*/
package org.exoplatform.groovyscript;
=
-import groovy.lang.Binding;
-import groovy.lang.GroovyClassLoader;
-import groovy.lang.GroovyCodeSource;
-import groovy.lang.Writable;
-import groovy.text.Template;
-import org.codehaus.groovy.control.CompilationFailedException;
-import org.codehaus.groovy.control.CompilerConfiguration;
-import org.codehaus.groovy.runtime.InvokerHelper;
import org.exoplatform.commons.utils.OutputStreamPrinter;
=
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
=
/**
+ * A Groovy template.
+ *
* @author Julien Viet
* @version $Revision$
*/
-public class GroovyTemplate implements Template
+public class GroovyTemplate
{
=
private static String read(Reader reader) throws IOException
@@ -52,34 +44,31 @@
return builder.toString();
}
=
- /** . */
+ /** The name of the template. */
private final String name;
=
- /** . */
- private final String scriptText;
+ /** The text of the template. */
+ private final String templateText;
=
- /** . */
- private final String groovyText;
+ /** The groovy script. */
+ private final GroovyScript script;
=
- /** . */
- private final Class> scriptClass;
-
- public GroovyTemplate(String name, Reader scriptReader) throws IOExcept=
ion
+ public GroovyTemplate(String name, Reader scriptReader) throws IOExcept=
ion, TemplateCompilationException
{
this(name, read(scriptReader));
}
=
- public GroovyTemplate(Reader scriptReader) throws IOException
+ public GroovyTemplate(Reader scriptReader) throws IOException, Template=
CompilationException
{
this(read(scriptReader));
}
=
- public GroovyTemplate(String scriptText) throws IOException
+ public GroovyTemplate(String templateText) throws TemplateCompilationEx=
ception
{
- this(null, scriptText);
+ this(null, templateText);
}
=
- public GroovyTemplate(String name, String scriptText) throws IOException
+ public GroovyTemplate(String name, String templateText) throws Template=
CompilationException
{
if (name =3D=3D null)
{
@@ -87,38 +76,12 @@
}
=
//
- TemplateCompiler compiler =3D new TemplateCompiler();
- String groovyText =3D compiler.compile(scriptText);
+ GroovyScriptBuilder compiler =3D new GroovyScriptBuilder(name, templ=
ateText);
=
//
- CompilerConfiguration config =3D new CompilerConfiguration();
- config.setScriptBaseClass(BaseScript.class.getName());
- byte[] bytes =3D groovyText.getBytes(config.getSourceEncoding());
- InputStream in =3D new ByteArrayInputStream(bytes);
- GroovyCodeSource gcs =3D new GroovyCodeSource(in, name, "/groovy/she=
ll");
- GroovyClassLoader loader =3D new GroovyClassLoader(Thread.currentThr=
ead().getContextClassLoader(), config);
-
- Class> scriptClass;
- try
- {
- scriptClass =3D loader.parseClass(gcs, false);
- }
- catch (CompilationFailedException e)
- {
- System.out.println("Could not compile script <<<" + groovyText + =
">>>");
- throw e;
- }
- catch (ClassFormatError e)
- {
- System.out.println("Could not compile script <<<" + groovyText + =
">>>");
- throw e;
- }
-
- //
this.name =3D name;
- this.scriptText =3D scriptText;
- this.groovyText =3D groovyText;
- this.scriptClass =3D scriptClass;
+ this.script =3D compiler.build();
+ this.templateText =3D templateText;
}
=
public String getName()
@@ -126,33 +89,41 @@
return name;
}
=
- public String getScriptText()
+ public String getTemplateText()
{
- return scriptText;
+ return templateText;
}
=
public String getGroovyText()
{
- return groovyText;
+ return script.getGroovyText();
}
=
- public void render(GroovyPrinter writer) throws IOException
+ public void render(Writer writer) throws IOException, TemplateRuntimeEx=
ception
{
render(writer, null);
}
=
- public void render(GroovyPrinter writer, Map binding) throws IOException
+ public void render(Writer writer, Map binding) throws IOException, Temp=
lateRuntimeException
{
- Writable writable =3D make(binding);
- writable.writeTo(writer);
+ GroovyPrinter printer;
+ if (writer instanceof OutputStreamPrinter)
+ {
+ printer =3D new OutputStreamWriterGroovyPrinter((OutputStreamPrin=
ter)writer);
+ }
+ else
+ {
+ printer =3D new WriterGroovyPrinter(writer);
+ }
+ script.render(binding, printer);
}
=
- public String render() throws IOException
+ public String render() throws IOException, TemplateRuntimeException
{
return render((Map)null);
}
=
- public String render(Map binding) throws IOException
+ public String render(Map binding) throws IOException, TemplateRuntimeEx=
ception
{
StringWriter buffer =3D new StringWriter();
WriterGroovyPrinter printer =3D new WriterGroovyPrinter(buffer);
@@ -160,47 +131,4 @@
printer.close();
return buffer.toString();
}
-
- public Writable make()
- {
- return make(null);
- }
-
- public Writable make(final Map binding)
- {
- Writable writable =3D new Writable()
- {
- /**
- * Write the template document with the set binding applied to th=
e writer.
- *
- * @see groovy.lang.Writable#writeTo(java.io.Writer)
- */
- public Writer writeTo(Writer writer)
- {
- Binding context;
- if (binding =3D=3D null)
- context =3D new Binding();
- else
- context =3D new Binding(binding);
-
- //
- BaseScript script =3D (BaseScript)InvokerHelper.createScript(s=
criptClass, context);
- if (writer instanceof OutputStreamPrinter)
- {
- script.printer =3D new OutputStreamWriterGroovyPrinter((Out=
putStreamPrinter)writer);
- }
- else
- {
- script.printer =3D new WriterGroovyPrinter(writer);
- }
- script.setProperty("out", script.printer);
- script.run();
- script.flush();
- return writer;
- }
- };
-
- //
- return writable;
- }
}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/GroovyTemplateEngine.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyTemplateEngine.java 2009-10-30 21:04:13 UTC (rev =
466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/GroovyTemplateEngine.java 2009-11-01 11:24:58 UTC (rev =
467)
@@ -16,23 +16,14 @@
*/
package org.exoplatform.groovyscript;
=
-import groovy.text.Template;
-import groovy.text.TemplateEngine;
-import org.codehaus.groovy.control.CompilationFailedException;
-
-import java.io.IOException;
-import java.io.Reader;
-
/**
* @author Julien Viet
* @version $Revision$
*/
-public class GroovyTemplateEngine extends TemplateEngine
+public class GroovyTemplateEngine
{
-
- @Override
- public Template createTemplate(Reader reader) throws CompilationFailedE=
xception, ClassNotFoundException, IOException
+ public GroovyTemplate createTemplate(String text) throws TemplateCompil=
ationException
{
- return new GroovyTemplate(reader);
+ return new GroovyTemplate(text);
}
}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/LineBreakItem.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/LineBreakItem.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/LineBreakItem.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -23,14 +23,15 @@
public class LineBreakItem extends SectionItem
{
=
- public LineBreakItem()
+ public LineBreakItem(Position pos)
{
+ super(pos);
}
=
@Override
public String toString()
{
- return "LineBreak[]";
+ return "LineBreak[position=3D" + getPosition() + "]";
}
=
@Override
Added: portal/branches/performance/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/Position.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/Position.java (rev 0)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/Position.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see.
+ */
+package org.exoplatform.groovyscript;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class Position
+{
+
+ /** . */
+ private final int col;
+
+ /** . */
+ private final int line;
+
+ public Position(int col, int line)
+ {
+ if (col < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (line < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ =
+ //
+ this.col =3D col;
+ this.line =3D line;
+ }
+
+ public int getCol()
+ {
+ return col;
+ }
+
+ public int getLine()
+ {
+ return line;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj =3D=3D this)
+ {
+ return true;
+ }
+ if (obj instanceof Position)
+ {
+ Position that =3D (Position)obj;
+ return col =3D=3D that.col && line =3D=3D that.line;
+ }
+ return false;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Position[col=3D" + col + ",line=3D" + line + "]";
+ }
+}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/SectionItem.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/SectionItem.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/SectionItem.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -22,4 +22,21 @@
*/
public abstract class SectionItem
{
+
+ /** . */
+ private final Position pos;
+
+ protected SectionItem(Position pos)
+ {
+ if (pos =3D=3D null)
+ {
+ throw new NullPointerException("No null position accepted");
+ }
+ this.pos =3D pos;
+ }
+
+ public Position getPosition()
+ {
+ return pos;
+ }
}
Added: portal/branches/performance/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/TemplateCompilationException.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateCompilationException.java =
(rev 0)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateCompilationException.java 2009-11-01 11:24:58 U=
TC (rev 467)
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see.
+ */
+package org.exoplatform.groovyscript;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class TemplateCompilationException extends Exception
+{
+
+ /** . */
+ private final String templateText;
+
+ public TemplateCompilationException(Throwable cause, String templateTex=
t)
+ {
+ super(cause);
+
+ //
+ this.templateText =3D templateText;
+ }
+
+ public String getTemplateText()
+ {
+ return templateText;
+ }
+}
Deleted: portal/branches/performance/component/scripting/src/main/java/org/=
exoplatform/groovyscript/TemplateCompiler.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateCompiler.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateCompiler.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2003-2007 eXo Platform SAS.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Affero General Public License
- * as published by the Free Software Foundation; either version 3
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see.
- */
-package org.exoplatform.groovyscript;
-
-import java.io.IOException;
-import java.util.List;
-
-/**
- * @author Julien Viet
- * @version $Revision$
- */
-public class TemplateCompiler
-{
-
- public String compile(String script) throws IOException
- {
- List sections =3D new TemplateParser().parse(script=
);
- return compile(sections);
- }
-
- public String compile(List sections)
- {
- GroovyScriptBuilder scriptBuilder =3D new GroovyScriptBuilder();
- for (TemplateSection section : sections)
- {
- scriptBuilder.begin(section.getType());
- for (SectionItem item : section.getItems())
- {
- if (item instanceof TextItem)
- {
- scriptBuilder.append(((TextItem)item).getData());
- }
- else
- {
- scriptBuilder.lineBreak();
- }
- }
- scriptBuilder.end();
- }
- return scriptBuilder.getScript();
- }
-}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/TemplateParser.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateParser.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateParser.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -16,8 +16,10 @@
*/
package org.exoplatform.groovyscript;
=
-import java.io.BufferedReader;
+import org.exoplatform.commons.utils.UndeclaredIOException;
+
import java.io.IOException;
+import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
@@ -54,27 +56,68 @@
GSTRING_EXPR
}
=
- public List parse(String s) throws IOException
+ public List parse(String s)
{
- return parse(new StringReader(s));
+ try
+ {
+ return parse(new StringReader(s));
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
}
=
- public List parse(Reader reader) throws IOException
+ public List parse(Reader tmp) throws IOException
{
- if (!reader.markSupported())
- {
- reader =3D new BufferedReader(reader);
- }
+ PushbackReader reader =3D new PushbackReader(tmp);
=
+
//
ArrayList sections =3D new ArrayList();
StringBuilder accumulator =3D new StringBuilder();
=
//
+ int lineNumber =3D 1;
+ int colNumber =3D 1;
+ Position pos =3D new Position(1, 1);
Status status =3D Status.TEXT;
- int c;
- while ((c =3D reader.read()) !=3D -1)
+ int i;
+ while ((i =3D reader.read()) !=3D -1)
{
+ char c =3D (char)i;
+ =
+ //
+ if (c =3D=3D '\r')
+ {
+ // On Windows, "\r\n" is a new line
+ int j =3D reader.read();
+ if (j !=3D -1)
+ {
+ char c2 =3D (char)j;
+ if (c2 =3D=3D '\n')
+ {
+ c =3D '\n';
+ }
+ else
+ {
+ reader.unread(j);
+ }
+ }
+ }
+
+ // Update current position
+ if (c =3D=3D '\n')
+ {
+ colNumber =3D 1;
+ lineNumber++;
+ }
+ else
+ {
+ colNumber++;
+ }
+
+ //
switch (status)
{
case TEXT:
@@ -88,7 +131,7 @@
}
else
{
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
case EXPR:
@@ -98,7 +141,7 @@
}
else
{
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
case SCRIPTLET:
@@ -108,7 +151,7 @@
}
else
{
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
case START_ANGLE:
@@ -119,14 +162,15 @@
else
{
status =3D Status.TEXT;
- accumulator.append('<').append((char)c);
+ accumulator.append('<').append(c);
}
break;
case SCRIPLET_OR_EXPR:
if (accumulator.length() > 0)
{
- sections.add(new TemplateSection(SectionType.STRING, acc=
umulator.toString()));
+ sections.add(new TemplateSection(SectionType.STRING, acc=
umulator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
}
if (c =3D=3D '=3D')
{
@@ -139,15 +183,18 @@
else
{
status =3D Status.SCRIPTLET;
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
case MAYBE_SCRIPLET_END:
if (c =3D=3D '>')
{
+ sections.add(new TemplateSection(SectionType.SCRIPTLET, =
accumulator.toString(), pos));
+ accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
+
+ //
status =3D Status.TEXT;
- sections.add(new TemplateSection(SectionType.SCRIPTLET, =
accumulator.toString()));
- accumulator.setLength(0);
}
else if (c =3D=3D '%')
{
@@ -156,15 +203,18 @@
else
{
status =3D Status.SCRIPTLET;
- accumulator.append('%').append((char)c);
+ accumulator.append('%').append(c);
}
break;
case MAYBE_EXPR_END:
if (c =3D=3D '>')
{
+ sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString(), pos));
+ accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
+
+ //
status =3D Status.TEXT;
- sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString()));
- accumulator.setLength(0);
}
else if (c =3D=3D '%')
{
@@ -173,7 +223,7 @@
else
{
status =3D Status.EXPR;
- accumulator.append('%').append((char)c);
+ accumulator.append('%').append(c);
}
break;
case MAYBE_GSTRING_EXPR:
@@ -181,49 +231,57 @@
{
if (accumulator.length() > 0)
{
- sections.add(new TemplateSection(SectionType.STRING, =
accumulator.toString()));
+ sections.add(new TemplateSection(SectionType.STRING, =
accumulator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
}
status =3D Status.GSTRING_CURLY_EXPR;
}
- else if (Character.isJavaIdentifierStart((char)c))
+ else if (Character.isJavaIdentifierStart(c))
{
if (accumulator.length() > 0)
{
- sections.add(new TemplateSection(SectionType.STRING, =
accumulator.toString()));
+ sections.add(new TemplateSection(SectionType.STRING, =
accumulator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
}
status =3D Status.GSTRING_EXPR;
- accumulator.append((char)c);
+ accumulator.append(c);
}
else
{
- accumulator.append('$').append((char)c);
+ accumulator.append('$').append(c);
}
break;
case GSTRING_CURLY_EXPR:
if (c =3D=3D '}')
{
- sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString()));
+ sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
+
+ //
status =3D Status.TEXT;
}
else
{
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
case GSTRING_EXPR:
- if (c =3D=3D '.' || Character.isJavaIdentifierPart((char)c))
+ if (c =3D=3D '.' || Character.isJavaIdentifierPart(c))
{
- accumulator.append((char)c);
+ accumulator.append(c);
}
else
{
- sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString()));
+ sections.add(new TemplateSection(SectionType.EXPR, accum=
ulator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
+
+ //
status =3D Status.TEXT;
- accumulator.append((char)c);
+ accumulator.append(c);
}
break;
default:
@@ -237,12 +295,14 @@
switch (status)
{
case TEXT:
- sections.add(new TemplateSection(SectionType.STRING, accumu=
lator.toString()));
+ sections.add(new TemplateSection(SectionType.STRING, accumu=
lator.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
break;
case GSTRING_EXPR:
- sections.add(new TemplateSection(SectionType.EXPR, accumula=
tor.toString()));
+ sections.add(new TemplateSection(SectionType.EXPR, accumula=
tor.toString(), pos));
accumulator.setLength(0);
+ pos =3D new Position(colNumber, lineNumber);
break;
default:
throw new AssertionError();
Added: portal/branches/performance/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/TemplateRuntimeException.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateRuntimeException.java (=
rev 0)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateRuntimeException.java 2009-11-01 11:24:58 UTC (=
rev 467)
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see.
+ */
+package org.exoplatform.groovyscript;
+
+/**
+ * A *checked* exception that denotes a Groovy runtime exception.
+ *
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class TemplateRuntimeException extends Exception
+{
+
+ /** . */
+ private final TextItem textItem;
+
+ public TemplateRuntimeException(TextItem textItem, String message, Thro=
wable cause)
+ {
+ super(message, cause);
+
+ //
+ this.textItem =3D textItem;
+ }
+
+ public TemplateRuntimeException(TextItem textItem, Throwable cause)
+ {
+ super(cause);
+
+ //
+ this.textItem =3D textItem;
+ }
+
+ public TextItem getTextItem()
+ {
+ return textItem;
+ }
+
+ public Integer getLineNumber()
+ {
+ return textItem !=3D null ? textItem.getPosition().getLine() : null;
+ }
+
+ public String getText()
+ {
+ return textItem !=3D null ? textItem.getData() : null;
+ }
+
+ @Override
+ public String getMessage()
+ {
+ return textItem !=3D null ? ("Groovy template exception at " + textI=
tem) : "Groovy template exception";
+ }
+}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/TemplateSection.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateSection.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TemplateSection.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -35,6 +35,16 @@
=
public TemplateSection(SectionType type, String text)
{
+ this(type, text, 0, 0);
+ }
+
+ public TemplateSection(SectionType type, String text, Position pos)
+ {
+ this(type, text, pos.getCol(), pos.getLine());
+ }
+
+ public TemplateSection(SectionType type, String text, int colNumber, in=
t lineNumber)
+ {
if (type =3D=3D null)
{
throw new NullPointerException();
@@ -44,13 +54,9 @@
throw new NullPointerException();
}
=
- //
// Now we process the line breaks
ArrayList sections =3D new ArrayList();
=
- // on Windows, "\r\n" is a new line
- text =3D text.replaceAll("\r\n", "\n");
-
//
int from =3D 0;
while (true)
@@ -61,14 +67,20 @@
if (to !=3D -1)
{
String chunk =3D text.substring(from, to);
- sections.add(new TextItem(chunk));
- sections.add(new LineBreakItem());
+ sections.add(new TextItem(new Position(colNumber, lineNumber),=
chunk));
+
+ //
+ sections.add(new LineBreakItem(new Position(colNumber + (to - =
from), lineNumber)));
+
+ //
from =3D to + 1;
+ lineNumber++;
+ colNumber =3D 0;
}
else
{
String chunk =3D text.substring(from);
- sections.add(new TextItem(chunk));
+ sections.add(new TextItem(new Position(colNumber, lineNumber),=
chunk));
break;
}
}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/TextItem.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TextItem.java 2009-10-30 21:04:13 UTC (rev 466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/TextItem.java 2009-11-01 11:24:58 UTC (rev 467)
@@ -26,8 +26,11 @@
/** . */
private final String data;
=
- public TextItem(String data)
+ public TextItem(Position pos, String data)
{
+ super(pos);
+
+ //
if (data =3D=3D null)
{
throw new NullPointerException();
@@ -60,6 +63,6 @@
@Override
public String toString()
{
- return "DataText[data=3D" + data + "]";
+ return "DataText[pos=3D" + getPosition() + ",data=3D" + data + "]";
}
}
Modified: portal/branches/performance/component/scripting/src/main/java/org=
/exoplatform/groovyscript/text/TemplateService.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/text/TemplateService.java 2009-10-30 21:04:13 UTC (rev =
466)
+++ portal/branches/performance/component/scripting/src/main/java/org/exopl=
atform/groovyscript/text/TemplateService.java 2009-11-01 11:24:58 UTC (rev =
467)
@@ -22,9 +22,9 @@
import groovy.lang.Writable;
import groovy.text.Template;
=
-import groovy.text.TemplateEngine;
import org.exoplatform.commons.utils.IOUtil;
import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.groovyscript.GroovyTemplate;
import org.exoplatform.groovyscript.GroovyTemplateEngine;
import org.exoplatform.management.annotations.Managed;
import org.exoplatform.management.annotations.ManagedDescription;
@@ -47,9 +47,9 @@
public class TemplateService
{
=
- private TemplateEngine engine_;
+ private GroovyTemplateEngine engine_;
=
- private ExoCache templatesCache_;
+ private ExoCache templatesCache_;
=
private TemplateStatisticService statisticService;
=
@@ -68,11 +68,10 @@
{
long startTime =3D System.currentTimeMillis();
=
- Template template =3D getTemplate(name, context.getResourceResolver(=
));
+ GroovyTemplate template =3D getTemplate(name, context.getResourceRes=
olver());
context.put("_ctx", context);
context.setGroovyTemplateService(this);
- Writable writable =3D template.make(context);
- writable.writeTo(context.getWriter());
+ template.render(context.getWriter(), context);
=
long endTime =3D System.currentTimeMillis();
=
@@ -95,24 +94,22 @@
if (context =3D=3D null)
throw new Exception("Binding cannot be null");
context.put("_ctx", context);
- Template template =3D getTemplate(name, context.getResourceResolver(=
));
- Writable writable =3D template.make(context);
- writable.writeTo(context.getWriter());
-
+ GroovyTemplate template =3D getTemplate(name, context.getResourceRes=
olver());
+ template.render(context.getWriter(), context);
}
=
- final public Template getTemplate(String name, ResourceResolver resolve=
r) throws Exception
+ final public GroovyTemplate getTemplate(String name, ResourceResolver r=
esolver) throws Exception
{
return getTemplate(name, resolver, cacheTemplate_);
}
=
- final public Template getTemplate(String url, ResourceResolver resolver=
, boolean cacheable) throws Exception
+ final public GroovyTemplate getTemplate(String url, ResourceResolver re=
solver, boolean cacheable) throws Exception
{
- Template template =3D null;
+ GroovyTemplate template =3D null;
if (cacheable)
{
String resourceId =3D resolver.createResourceId(url);
- template =3D (Template)getTemplatesCache().get(resourceId);
+ template =3D getTemplatesCache().get(resourceId);
}
if (template !=3D null)
return template;
@@ -140,13 +137,8 @@
getTemplatesCache().remove(resourceId);
}
=
- public void setTemplatesCache(ExoCache templatesCache=
_)
+ public ExoCache getTemplatesCache()
{
- this.templatesCache_ =3D templatesCache_;
- }
-
- public ExoCache getTemplatesCache()
- {
return templatesCache_;
}
=
Modified: portal/branches/performance/component/scripting/src/test/java/org=
/exoplatform/groovyscript/TestTemplateCompiler.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateCompiler.java 2009-10-30 21:04:13 UTC (rev =
466)
+++ portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateCompiler.java 2009-11-01 11:24:58 UTC (rev =
467)
@@ -17,7 +17,6 @@
package org.exoplatform.groovyscript;
=
import junit.framework.TestCase;
-import org.exoplatform.groovyscript.TemplateCompiler;
=
import java.io.IOException;
=
@@ -29,7 +28,7 @@
{
=
/** . */
- private final TemplateCompiler compiler =3D new TemplateCompiler();
+// private final TemplateCompiler compiler =3D new TemplateCompiler();
=
public void testFoo() throws IOException
{
Modified: portal/branches/performance/component/scripting/src/test/java/org=
/exoplatform/groovyscript/TestTemplateParser.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateParser.java 2009-10-30 21:04:13 UTC (rev 46=
6)
+++ portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateParser.java 2009-11-01 11:24:58 UTC (rev 46=
7)
@@ -24,6 +24,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
=
/**
* @author Julien Viet
@@ -102,4 +103,20 @@
new TemplateSection(SectionType.STRING, "c")
), parser.parse("a<%=3Db%>c"));
}
+
+ public void testWindowsLineBreak() throws IOException
+ {
+
+ }
+
+ public void testPosition() throws IOException
+ {
+ List sections =3D parser.parse("a\nb<%=3D foo %>d");
+ assertEquals(new Position(0, 0), sections.get(0).getItems().get(0).g=
etPosition());
+ assertEquals(new Position(1, 0), sections.get(0).getItems().get(1).g=
etPosition());
+ assertEquals(new Position(0, 1), sections.get(0).getItems().get(2).g=
etPosition());
+ assertEquals(new Position(4, 1), sections.get(1).getItems().get(0).g=
etPosition());
+ assertEquals(new Position(11, 1), sections.get(2).getItems().get(0).=
getPosition());
+
+ }
}
Modified: portal/branches/performance/component/scripting/src/test/java/org=
/exoplatform/groovyscript/TestTemplateRendering.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateRendering.java 2009-10-30 21:04:13 UTC (rev=
466)
+++ portal/branches/performance/component/scripting/src/test/java/org/exopl=
atform/groovyscript/TestTemplateRendering.java 2009-11-01 11:24:58 UTC (rev=
467)
@@ -19,11 +19,12 @@
import junit.framework.TestCase;
import org.exoplatform.commons.utils.CharsetTextEncoder;
import org.exoplatform.commons.utils.OutputStreamPrinter;
-import org.exoplatform.groovyscript.GroovyTemplate;
-import org.exoplatform.groovyscript.OutputStreamWriterGroovyPrinter;
=
+import java.awt.*;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.InputStream;
+import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Map;
=
@@ -156,4 +157,105 @@
String gtmpl =3D baos.toString("UTF-8");
GroovyTemplate template =3D new GroovyTemplate(gtmpl);
}
+
+ public void testException() throws Exception
+ {
+ GroovyTemplate template =3D new GroovyTemplate("<% throw new java.aw=
t.AWTException(); %>");
+ try
+ {
+ template.render();
+ fail();
+ }
+ catch (TemplateRuntimeException e)
+ {
+ assertTrue(e.getCause() instanceof AWTException);
+ }
+ }
+
+ public void testRuntimeException() throws Exception
+ {
+ GroovyTemplate template =3D new GroovyTemplate("<% throw new java.ut=
il.EmptyStackException(); %>");
+ try
+ {
+ template.render();
+ fail();
+ }
+ catch (TemplateRuntimeException e)
+ {
+ assertTrue(e.getCause() instanceof EmptyStackException);
+ }
+ }
+
+ public void testIOException() throws Exception
+ {
+ GroovyTemplate template =3D new GroovyTemplate("<% throw new java.io=
.IOException(); %>");
+ try
+ {
+ template.render();
+ fail();
+ }
+ catch (IOException e)
+ {
+ }
+ }
+
+ public void testError() throws Exception
+ {
+ GroovyTemplate template =3D new GroovyTemplate("<% throw new java.aw=
t.AWTError(); %>");
+ try
+ {
+ template.render();
+ fail();
+ }
+ catch (AWTError e)
+ {
+ }
+ }
+
+ public void testThrowable() throws Exception
+ {
+ GroovyTemplate template =3D new GroovyTemplate("<% throw new Throwab=
le(); %>");
+ try
+ {
+ template.render();
+ fail();
+ }
+ catch (Throwable t)
+ {
+ }
+ }
+
+ public void testScriptLineNumber() throws Exception
+ {
+ testLineNumber("<%");
+ assertLineNumber(2, "throw new Exception('e')", "<%\nthrow new Excep=
tion('e')%>");
+ }
+
+ public void testExpressionLineNumber() throws Exception
+ {
+ testLineNumber("<%=3D");
+ }
+
+ private void testLineNumber(String prolog) throws Exception
+ {
+ assertLineNumber(1, "throw new Exception('a')", prolog + "throw new =
Exception('a')%>");
+ assertLineNumber(1, "throw new Exception('b')", "foo" + prolog + "th=
row new Exception('b')%>");
+ assertLineNumber(2, "throw new Exception('c')", "foo\n" + prolog + "=
throw new Exception('c')%>");
+ assertLineNumber(1, "throw new Exception('d')", "<%;%>foo" + prolog =
+ "throw new Exception('d')%>");
+ }
+
+ private void assertLineNumber(int expectedLineNumber, String expectedTe=
xt, String script) throws TemplateCompilationException, IOException
+ {
+ try
+ {
+ new GroovyTemplate(script).render();
+ fail();
+ }
+ catch (TemplateRuntimeException t)
+ {
+ assertEquals(expectedText, t.getText());
+ assertEquals(expectedLineNumber, (Object)t.getLineNumber());
+ }
+ }
+
}
--===============3851220618313644502==--