incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r656687 - in /incubator/sling/trunk: commons/json/src/main/java/org/apache/sling/commons/json/jcr/ scripting/javascript/ scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/ scripting/javascript/src/main/java/org/apache...
Date Thu, 15 May 2008 14:46:26 GMT
Author: bdelacretaz
Date: Thu May 15 07:46:26 2008
New Revision: 656687

URL: http://svn.apache.org/viewvc?rev=656687&view=rev
Log:
SLING-454 - javascript wrappers now output an ECMA-formatted String for the default value
of Properties of type Date

Added:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableCalendar.java
Modified:
    incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
    incubator/sling/trunk/scripting/javascript/pom.xml
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
    incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java

Modified: incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java?rev=656687&r1=656686&r2=656687&view=diff
==============================================================================
--- incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
(original)
+++ incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
Thu May 15 07:46:26 2008
@@ -43,6 +43,13 @@
     private static DateFormat calendarFormat;
 
     private final Set<String> propertyNamesToIgnore;
+    
+    /** Used to format date values */
+    public static final String ECMA_DATE_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
+    
+    /** Used to format date values */
+    public static final Locale DATE_FORMAT_LOCALE = Locale.US;
+
 
     /**
      * Create a JsonItemWriter
@@ -240,8 +247,7 @@
 
     public static synchronized String format(Calendar date) {
         if (calendarFormat == null) {
-            calendarFormat = new SimpleDateFormat(
-                "EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US);
+            calendarFormat = new SimpleDateFormat(ECMA_DATE_FORMAT, DATE_FORMAT_LOCALE);
         }
         return calendarFormat.format(date.getTime());
     }

Modified: incubator/sling/trunk/scripting/javascript/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/pom.xml?rev=656687&r1=656686&r2=656687&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/pom.xml (original)
+++ incubator/sling/trunk/scripting/javascript/pom.xml Thu May 15 07:46:26 2008
@@ -101,6 +101,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java?rev=656687&r1=656686&r2=656687&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/RhinoJavaScriptEngineFactory.java
Thu May 15 07:46:26 2008
@@ -24,6 +24,7 @@
 import org.apache.sling.scripting.javascript.helper.SlingContextFactory;
 import org.apache.sling.scripting.javascript.helper.SlingWrapFactory;
 import org.apache.sling.scripting.javascript.helper.SlingWrapper;
+import org.apache.sling.scripting.javascript.wrapper.ScriptableCalendar;
 import org.apache.sling.scripting.javascript.wrapper.ScriptableItemMap;
 import org.apache.sling.scripting.javascript.wrapper.ScriptableNode;
 import org.apache.sling.scripting.javascript.wrapper.ScriptablePrintWriter;
@@ -55,7 +56,8 @@
         ScriptableItemMap.class,
         ScriptablePrintWriter.class,
         ScriptableVersionHistory.class,
-        ScriptableVersion.class
+        ScriptableVersion.class,
+        ScriptableCalendar.class
     };
 
     /** default log */

Added: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableCalendar.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableCalendar.java?rev=656687&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableCalendar.java
(added)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableCalendar.java
Thu May 15 07:46:26 2008
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.javascript.wrapper;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+import org.apache.sling.scripting.javascript.helper.SlingWrapper;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.Undefined;
+import org.apache.sling.commons.json.jcr.JsonItemWriter;
+
+@SuppressWarnings("serial")
+public class ScriptableCalendar extends ScriptableBase implements SlingWrapper {
+
+	public static final String CLASSNAME = "Calendar";
+	private SimpleDateFormat calendarFormat;
+	
+	/** Calendar is a class, not an interface - so we need to enumerate possible implementations
here */
+    public static final Class<?> [] WRAPPED_CLASSES = { Calendar.class, GregorianCalendar.class
};
+
+	private Calendar calendar;
+	
+    public Class<?>[] getWrappedClasses() {
+		return WRAPPED_CLASSES;
+	}
+
+    public void jsConstructor(Object o) {
+        this.calendar = (Calendar) o;
+    }
+	
+    @Override
+    public Object get(String name, Scriptable start) {
+
+        // builtin javascript properties (jsFunction_ etc.) have priority
+        final Object fromSuperclass = super.get(name, start);
+        if(fromSuperclass != Scriptable.NOT_FOUND) {
+            return fromSuperclass;
+        }
+
+        if(calendar == null) {
+            return Undefined.instance;
+        }
+        
+        if("date".equals(name)) {
+        	return ScriptRuntime.toObject(this, calendar.getTime());
+        }
+        
+        return getNative(name, start);
+    }
+    
+	@Override
+	protected Class<?> getStaticType() {
+		return Calendar.class;
+	}
+
+	@Override
+	protected Object getWrappedObject() {
+		return calendar;
+	}
+
+	@Override
+	public Class<?> jsGet_javascriptWrapperClass() {
+		return getClass();
+	}
+
+	@Override
+	public String getClassName() {
+		return CLASSNAME;
+	}
+
+	@Override
+	public String toString() {
+        if (calendarFormat == null) {
+            calendarFormat = new SimpleDateFormat(JsonItemWriter.ECMA_DATE_FORMAT, JsonItemWriter.DATE_FORMAT_LOCALE);
+        }
+        return calendarFormat.format(calendar.getTime());
+	}
+	
+    public Object unwrap() {
+        return calendar;
+    }
+
+    @SuppressWarnings("unchecked")
+	@Override
+    public Object getDefaultValue(Class typeHint) {
+    	return toString();
+    }
+}

Modified: incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java?rev=656687&r1=656686&r2=656687&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java
(original)
+++ incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableNodeTest.java
Thu May 15 07:46:26 2008
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.scripting.wrapper;
 
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
 import javax.jcr.Node;
@@ -25,6 +26,7 @@
 
 import org.apache.sling.scripting.RepositoryScriptingTestBase;
 import org.apache.sling.scripting.ScriptEngineHelper;
+import org.apache.sling.commons.json.jcr.JsonItemWriter;
 
 /** Test the ScriptableNode class "live", by retrieving
  *  Nodes from a Repository and executing javascript code
@@ -142,10 +144,26 @@
         );
     }
     
+    public void testDateWrapperClass() throws Exception {
+        assertEquals(
+                "org.apache.sling.scripting.javascript.wrapper.ScriptableCalendar", 
+                script.eval("node.cal.javascriptWrapperClass.getName()", data)
+        );
+    }
+    
     public void testViaNodeDirectPropertyAccessCal() throws Exception {
+    	final SimpleDateFormat f = new SimpleDateFormat(JsonItemWriter.ECMA_DATE_FORMAT, JsonItemWriter.DATE_FORMAT_LOCALE);
+    	final String expected = f.format(testCal.getTime());
         assertEquals(
-                testCal,
-                script.eval("node.cal", data)
+                expected,
+                script.evalToString("out.print(node.cal)", data)
+        );
+    }
+    
+    public void testCalDateClass() throws Exception {
+        assertEquals(
+                "number",
+                script.evalToString("out.print(typeof node.cal.date.time)", data)
         );
     }
     



Mime
View raw message