Author: julien_viet
Date: 2009-11-03 07:58:47 -0500 (Tue, 03 Nov 2009)
New Revision: 479
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/BaseScript.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/WriterGroovyPrinter.java
portal/trunk/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java
Log:
GTNPORTAL-160 : New Groovy template engine / optmize call made to print method of the
groovy printer by implementing GroovyInterceptable to avoid a long unnecessary reflective
call
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/BaseScript.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/BaseScript.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/BaseScript.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -42,6 +42,19 @@
}
@Override
+ public Object getProperty(String property)
+ {
+ if ("out".equals(property))
+ {
+ return printer;
+ }
+ else
+ {
+ return super.getProperty(property);
+ }
+ }
+
+ @Override
public void println(Object o)
{
printer.println(o);
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -16,6 +16,8 @@
*/
package org.exoplatform.groovyscript;
+import groovy.lang.GroovyInterceptable;
+import groovy.lang.GroovyObjectSupport;
import org.exoplatform.commons.utils.Text;
import java.io.IOException;
@@ -25,9 +27,42 @@
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public abstract class GroovyPrinter extends Writer
+abstract class GroovyPrinter extends GroovyObjectSupport implements GroovyInterceptable
{
+ /**
+ * Optimize the call to the various print methods.
+ *
+ * @param name the method name
+ * @param args the method arguments
+ * @return the return value
+ */
+ @Override
+ public Object invokeMethod(String name, Object args)
+ {
+ // Optimize access to print methods
+ if (args instanceof Object[])
+ {
+ Object[] array = (Object[])args;
+ if (array.length == 1)
+ {
+ if ("print".equals(name))
+ {
+ print(array[0]);
+ return null;
+ }
+ else if ("println".equals(name))
+ {
+ println(array[0]);
+ return null;
+ }
+ }
+ }
+
+ // Back to Groovy method call
+ return super.invokeMethod(name, args);
+ }
+
public final void println(Object o)
{
print(o);
@@ -55,7 +90,7 @@
}
else if (o instanceof Text)
{
- ((Text)o).writeTo(this);
+ write((Text)o);
}
else
{
@@ -66,4 +101,15 @@
{
}
}
+
+ protected abstract void write(char c) throws IOException;
+
+ protected abstract void write(String s) throws IOException;
+
+ protected abstract void write(Text text) throws IOException;
+
+ protected abstract void flush() throws IOException;
+
+ protected abstract void close() throws IOException;
+
}
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -18,8 +18,10 @@
import groovy.lang.Binding;
import org.codehaus.groovy.runtime.InvokerHelper;
+import org.exoplatform.commons.utils.OutputStreamPrinter;
import java.io.IOException;
+import java.io.Writer;
import java.util.Map;
/**
@@ -57,19 +59,24 @@
return scriptClass;
}
- public Map<Integer, TextItem> getLineTable()
+ public void render(Map context, Writer writer) throws IOException,
TemplateRuntimeException
{
- return lineTable;
- }
-
- public void render(Map context, GroovyPrinter writer) throws IOException,
TemplateRuntimeException
- {
Binding binding = context != null ? new Binding(context) : new Binding();
//
+ GroovyPrinter printer;
+ if (writer instanceof OutputStreamPrinter)
+ {
+ printer = new OutputStreamWriterGroovyPrinter((OutputStreamPrinter)writer);
+ }
+ else
+ {
+ printer = new WriterGroovyPrinter(writer);
+ }
+
+ //
BaseScript script = (BaseScript)InvokerHelper.createScript(scriptClass, binding);
- script.printer = writer;
- script.setProperty("out", script.printer);
+ script.printer = printer;
//
try
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -16,8 +16,6 @@
*/
package org.exoplatform.groovyscript;
-import org.exoplatform.commons.utils.OutputStreamPrinter;
-
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
@@ -111,16 +109,7 @@
public void render(Writer writer, Map binding) throws IOException,
TemplateRuntimeException
{
- GroovyPrinter printer;
- if (writer instanceof OutputStreamPrinter)
- {
- printer = new OutputStreamWriterGroovyPrinter((OutputStreamPrinter)writer);
- }
- else
- {
- printer = new WriterGroovyPrinter(writer);
- }
- script.render(binding, printer);
+ script.render(binding, writer);
}
public String render() throws IOException, TemplateRuntimeException
@@ -131,9 +120,8 @@
public String render(Map binding) throws IOException, TemplateRuntimeException
{
StringWriter buffer = new StringWriter();
- WriterGroovyPrinter printer = new WriterGroovyPrinter(buffer);
- render(printer, binding);
- printer.close();
+ render(buffer, binding);
+ buffer.close();
return buffer.toString();
}
}
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/OutputStreamWriterGroovyPrinter.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -18,6 +18,7 @@
import org.exoplatform.commons.utils.BinaryOutput;
import org.exoplatform.commons.utils.OutputStreamPrinter;
+import org.exoplatform.commons.utils.Text;
import java.io.IOException;
import java.nio.charset.Charset;
@@ -41,30 +42,22 @@
this.out = out;
}
- public Charset getCharset()
+ @Override
+ protected void write(char c) throws IOException
{
- return out.getCharset();
+ out.write(c);
}
- public void write(byte[] bytes) throws IOException
+ @Override
+ protected void write(String s) throws IOException
{
- out.write(bytes);
+ out.write(s);
}
- public void write(byte[] b, int off, int len) throws IOException
- {
- out.write(b, off, len);
- }
-
- public void write(byte b) throws IOException
- {
- out.write(b);
- }
-
@Override
- public void write(char[] cbuf, int off, int len) throws IOException
+ protected void write(Text text) throws IOException
{
- out.write(cbuf, off, len);
+ text.writeTo(out);
}
@Override
@@ -78,4 +71,24 @@
{
out.close();
}
+
+ public Charset getCharset()
+ {
+ return out.getCharset();
+ }
+
+ public void write(byte[] bytes) throws IOException
+ {
+ out.write(bytes);
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ out.write(b, off, len);
+ }
+
+ public void write(byte b) throws IOException
+ {
+ out.write(b);
+ }
}
Modified:
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/WriterGroovyPrinter.java
===================================================================
---
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/WriterGroovyPrinter.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/main/java/org/exoplatform/groovyscript/WriterGroovyPrinter.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -16,6 +16,8 @@
*/
package org.exoplatform.groovyscript;
+import org.exoplatform.commons.utils.Text;
+
import java.io.IOException;
import java.io.Writer;
@@ -39,20 +41,32 @@
}
@Override
- public void write(char[] cbuf, int off, int len) throws IOException
+ protected void write(char c) throws IOException
{
- writer.write(cbuf, off, len);
+ writer.write(c);
}
@Override
- public void close() throws IOException
+ protected void write(String s) throws IOException
{
- writer.close();
+ writer.write(s);
}
@Override
+ protected void write(Text text) throws IOException
+ {
+ text.writeTo(writer);
+ }
+
+ @Override
public void flush() throws IOException
{
writer.flush();
}
+
+ @Override
+ public void close() throws IOException
+ {
+ writer.close();
+ }
}
Modified:
portal/trunk/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java
===================================================================
---
portal/trunk/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java 2009-11-03
11:39:06 UTC (rev 478)
+++
portal/trunk/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java 2009-11-03
12:58:47 UTC (rev 479)
@@ -40,9 +40,8 @@
GroovyTemplate template = new
GroovyTemplate("a<%='b'%>c<%out.print('d');%>e");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamPrinter writer = new OutputStreamPrinter(CharsetTextEncoder.getUTF8(),
baos);
- OutputStreamWriterGroovyPrinter printer = new
OutputStreamWriterGroovyPrinter(writer);
- template.render(printer);
- printer.close();
+ template.render(writer);
+ writer.close();
assertEquals("abcde", baos.toString("UTF-8"));
}