Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added Date support for RhinoWrapper #10

Open
wants to merge 2 commits into
base: 2.3-gae
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
28 changes: 28 additions & 0 deletions src/main/java/freemarker/ext/rhino/RhinoDateModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package freemarker.ext.rhino;

import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateModelException;
import java.util.Date;

/**
*
* @author Vakhurin Sergey
*/
public class RhinoDateModel implements TemplateDateModel {

private final Date date;

RhinoDateModel(Date date) {
this.date = date;
}

@Override
public Date getAsDate() throws TemplateModelException {
return date;
}

@Override
public int getDateType() {
return DATETIME;
}
}
3 changes: 3 additions & 0 deletions src/main/java/freemarker/ext/rhino/RhinoScriptableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript._FreeMarkerNativeDateHelper;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.util.ModelFactory;
Expand Down Expand Up @@ -47,6 +48,8 @@ public TemplateModel get(String key) throws TemplateModelException {
if(retval instanceof Function) {
return new RhinoFunctionModel((Function)retval, scriptable, wrapper);
}
if (_FreeMarkerNativeDateHelper.instanceofNativeDate(retval))
return new RhinoDateModel(new java.util.Date(_FreeMarkerNativeDateHelper.nativeDateToMillisUTC(retval)));
else {
return wrapper.wrap(retval);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mozilla.javascript;

/**
* Don't use this; used internally by FreeMarker/ext/rhino, might changes without notice.
*
* @author Vakhurin Sergey
*/
public class _FreeMarkerNativeDateHelper {

public static boolean instanceofNativeDate(Object object) {
return object instanceof NativeDate;
}

public static long nativeDateToMillisUTC(Object object) {
return (long) ((NativeDate) object).getJSTimeValue();
}
}
40 changes: 40 additions & 0 deletions src/test/java/freemarker/ext/rhino/DateModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package freemarker.ext.rhino;

import java.io.StringWriter;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import org.mozilla.javascript.*;

import freemarker.template.Template;

@RunWith(JUnit4.class)
public class DateModelTest {

@Test
public void formatDate() throws Exception {
assertEquals("25.11.2013 0:00:00", process("new Date(2013,10,25)"));
}

@Test
public void formatDateTime() throws Exception {
assertEquals("02.04.2015 21:12:16", process("new Date(2015,03,02, 21,12,16)"));
}

private String process(String js) throws Exception {
Context cx = Context.enter();
try {
Scriptable go = cx.initStandardObjects();
Object o = cx.evaluateString(go, "({x:" + js + "})", null, 0, null);
Template t = new Template(null, "${x}", null);
StringWriter sw = new StringWriter();
t.process(o, sw, new RhinoWrapper());
return sw.toString();
} finally {
Context.exit();
}
}
}