Author: thomas.heute(a)jboss.com
Date: 2010-09-23 06:28:45 -0400 (Thu, 23 Sep 2010)
New Revision: 4336
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/text/TemplateService.java
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java
epp/portal/branches/EPP_5_1_Branch/webui/framework/src/main/java/org/exoplatform/webui/core/lifecycle/Lifecycle.java
Log:
JBEPP-493: Locale encoding in Groovy templates
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyPrinter.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -18,12 +18,16 @@
*/
package org.exoplatform.groovyscript;
+import groovy.lang.GString;
import groovy.lang.GroovyInterceptable;
import groovy.lang.GroovyObjectSupport;
import org.exoplatform.commons.utils.Text;
import java.io.IOException;
import java.io.Writer;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Locale;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
@@ -32,6 +36,19 @@
abstract class GroovyPrinter extends GroovyObjectSupport implements GroovyInterceptable
{
+ /** An optional locale. */
+ private Locale locale;
+
+ public Locale getLocale()
+ {
+ return locale;
+ }
+
+ public void setLocale(Locale locale)
+ {
+ this.locale = locale;
+ }
+
/**
* Optimize the call to the various print methods.
*
@@ -82,21 +99,65 @@
}
}
+ /**
+ * We handle in this method a conversion of an object to another one for formatting
purposes.
+ *
+ * @param o the object to format
+ * @return the formatted object
+ */
+ private Object format(Object o)
+ {
+ if (o instanceof Date)
+ {
+ if (locale != null)
+ {
+ DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,
locale);
+ o = dateFormat.format((Date)o);
+ }
+ }
+
+ //
+ return o;
+ }
+
+ private String toString(Object o)
+ {
+ Object f = format(o);
+ if (f == null)
+ {
+ return "null";
+ }
+ else if (f instanceof String)
+ {
+ return (String)f;
+ }
+ else
+ {
+ return o.toString();
+ }
+ }
+
public final void print(Object o)
{
try
{
- if (o == null)
+ if (o instanceof Text)
{
- write("null");
+ write((Text)o);
}
- else if (o instanceof Text)
+ else if (o instanceof GString)
{
- write((Text)o);
+ GString gs = (GString)o;
+ Object[] values = gs.getValues();
+ for (int i = 0;i < values.length;i++)
+ {
+ values[i] = format(values[i]);
+ }
+ write(o.toString());
}
else
{
- write(o.toString());
+ write(toString(o));
}
}
catch (IOException ignore)
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyScript.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.Writer;
+import java.util.Locale;
import java.util.Map;
/**
@@ -70,7 +71,19 @@
return scriptClass;
}
- public void render(Map context, Writer writer) throws IOException,
TemplateRuntimeException
+ /**
+ * Renders the script with the provided context and locale to the specified writer.
+ *
+ * @param context the context
+ * @param writer the writer
+ * @param locale the locale
+ * @throws IOException
+ * @throws TemplateRuntimeException
+ */
+ public void render(
+ Map context,
+ Writer writer,
+ Locale locale) throws IOException, TemplateRuntimeException
{
Binding binding = context != null ? new Binding(context) : new Binding();
@@ -86,6 +99,9 @@
}
//
+ printer.setLocale(locale);
+
+ //
BaseScript script = (BaseScript)InvokerHelper.createScript(scriptClass, binding);
script.printer = printer;
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/GroovyTemplate.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -22,6 +22,7 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.util.Locale;
import java.util.Map;
/**
@@ -104,12 +105,22 @@
public void render(Writer writer) throws IOException, TemplateRuntimeException
{
- render(writer, null);
+ render(writer, (Map)null);
}
+ public void render(Writer writer, Locale locale) throws IOException,
TemplateRuntimeException
+ {
+ render(writer, null, locale);
+ }
+
+ public void render(Writer writer, Map binding, Locale locale) throws IOException,
TemplateRuntimeException
+ {
+ script.render(binding, writer, locale);
+ }
+
public void render(Writer writer, Map binding) throws IOException,
TemplateRuntimeException
{
- script.render(binding, writer);
+ script.render(binding, writer, null);
}
public String render() throws IOException, TemplateRuntimeException
@@ -117,10 +128,20 @@
return render((Map)null);
}
+ public String render(Locale locale) throws IOException, TemplateRuntimeException
+ {
+ return render((Map)null, locale);
+ }
+
public String render(Map binding) throws IOException, TemplateRuntimeException
{
+ return render(binding, null);
+ }
+
+ public String render(Map binding, Locale locale) throws IOException,
TemplateRuntimeException
+ {
StringWriter buffer = new StringWriter();
- render(buffer, binding);
+ render(buffer, binding, locale);
buffer.close();
return buffer.toString();
}
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/text/TemplateService.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/text/TemplateService.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/main/java/org/exoplatform/groovyscript/text/TemplateService.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -44,6 +44,7 @@
import java.io.InputStream;
import java.util.ArrayList;
+import java.util.Locale;
/**
* Created by The eXo Platform SAS Dec 26, 2005
@@ -113,8 +114,7 @@
GroovyTemplate template = getTemplate(name, context.getResourceResolver());
context.put("_ctx", context);
context.setGroovyTemplateService(this);
- template.render(context.getWriter(), context);
-
+ template.render(context.getWriter(), context,
(Locale)context.get("locale"));
long endTime = System.currentTimeMillis();
TemplateStatistic templateStatistic = statisticService.getTemplateStatistic(name);
@@ -137,7 +137,7 @@
throw new Exception("Binding cannot be null");
context.put("_ctx", context);
GroovyTemplate template = getTemplate(name, context.getResourceResolver());
- template.render(context.getWriter(), context);
+ template.render(context.getWriter(), context,
(Locale)context.get("locale"));
}
final public GroovyTemplate getTemplate(String name, ResourceResolver resolver) throws
Exception
Modified:
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/component/scripting/src/test/java/org/exoplatform/groovyscript/TestTemplateRendering.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -26,6 +26,7 @@
import java.io.*;
import java.util.EmptyStackException;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
/**
@@ -45,6 +46,32 @@
assertEquals("abcde", baos.toString("UTF-8"));
}
+ public void testDate1() throws Exception
+ {
+ GroovyTemplate template = new GroovyTemplate("<% print(new Date(0));
%>");
+ assertEquals("1 janv. 1970", template.render(Locale.FRENCH));
+ assertEquals("Jan 1, 1970", template.render(Locale.ENGLISH));
+ assertEquals("Thu Jan 01 07:00:00 ICT 1970", template.render());
+ }
+
+ public void testDate2() throws Exception
+ {
+ GroovyTemplate template = new GroovyTemplate("<% def date = new Date(0)
%>$date");
+ System.out.println("template.getGroovy() = " + template.getGroovy());
+ assertEquals("1 janv. 1970", template.render(Locale.FRENCH));
+ assertEquals("Jan 1, 1970", template.render(Locale.ENGLISH));
+ assertEquals("Thu Jan 01 07:00:00 ICT 1970", template.render());
+ }
+
+ public void testDate3() throws Exception
+ {
+ GroovyTemplate template = new GroovyTemplate("<%= new Date(0)
%>");
+ System.out.println("template.getGroovy() = " + template.getGroovy());
+ assertEquals("1 janv. 1970", template.render(Locale.FRENCH));
+ assertEquals("Jan 1, 1970", template.render(Locale.ENGLISH));
+ assertEquals("Thu Jan 01 07:00:00 ICT 1970", template.render());
+ }
+
public void testFoo() throws Exception
{
GroovyTemplate template = new GroovyTemplate("a");
Modified:
epp/portal/branches/EPP_5_1_Branch/webui/framework/src/main/java/org/exoplatform/webui/core/lifecycle/Lifecycle.java
===================================================================
---
epp/portal/branches/EPP_5_1_Branch/webui/framework/src/main/java/org/exoplatform/webui/core/lifecycle/Lifecycle.java 2010-09-23
10:16:29 UTC (rev 4335)
+++
epp/portal/branches/EPP_5_1_Branch/webui/framework/src/main/java/org/exoplatform/webui/core/lifecycle/Lifecycle.java 2010-09-23
10:28:45 UTC (rev 4336)
@@ -101,9 +101,10 @@
*
*/
protected void renderTemplate(String template, WebuiBindingContext bcontext) throws
Exception
- {
+ {
+ WebuiRequestContext context = bcontext.getRequestContext();
bcontext.put("decorator", decorator_);
- WebuiRequestContext context = bcontext.getRequestContext();
+ bcontext.put("locale", context.getLocale());
ExoContainer pcontainer =
context.getApplication().getApplicationServiceContainer();
TemplateService service =
(TemplateService)pcontainer.getComponentInstanceOfType(TemplateService.class);
ResourceResolver resolver = bcontext.getResourceResolver();