incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1494643 - in /sling/trunk: bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/ bundles/scripting/jsp-taglib/src/main/resources/META-INF/ bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/js...
Date Wed, 19 Jun 2013 14:49:55 GMT
Author: dklco
Date: Wed Jun 19 14:49:55 2013
New Revision: 1494643

URL: http://svn.apache.org/r1494643
Log:
Fixing SLING-1500 - Added a EL function to support getting a value with a default value or
casting the result

Modified:
    sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
    sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/sling.tld
    sling/trunk/bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/jsp/taglib/TestSlingFunctions.java
    sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/taglib-test.jsp

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java?rev=1494643&r1=1494642&r2=1494643&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
(original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
Wed Jun 19 14:49:55 2013
@@ -21,6 +21,7 @@ import java.util.Iterator;
 import org.apache.sling.api.adapter.Adaptable;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -90,21 +91,6 @@ public class SlingFunctions {
 	}
 
 	/**
-	 * Loads the Class for the name from the current thread's classload.
-	 * 
-	 * @param className
-	 *            The name of the class to load
-	 * @return the class
-	 * @throws ClassNotFoundException
-	 *             a class with the specified name could not be found
-	 */
-	private static Class<?> loadClass(String className)
-			throws ClassNotFoundException {
-		return Thread.currentThread().getContextClassLoader()
-				.loadClass(className);
-	}
-
-	/**
 	 * Gets the resource at the relative path to the provided resource.
 	 * 
 	 * @param base
@@ -142,13 +128,37 @@ public class SlingFunctions {
 		log.trace("getResource");
 
 		log.debug("Getting resource at path {}", path);
-		if(resolver == null){
+		if (resolver == null) {
 			throw new IllegalArgumentException("Null resource resolver");
 		}
 		return resolver.getResource(path);
 	}
 
 	/**
+	 * Gets the value of the specified key from the ValueMap and either coerses
+	 * the value into the specified type or uses the specified type as a default
+	 * depending on the parameter passed in.
+	 * 
+	 * @param properties
+	 *            the ValueMap from which to retrieve the value
+	 * @param key
+	 *            the key for the value to retrieve
+	 * @param defaultOrType
+	 *            either the default value or the class to which to coerce the
+	 *            value
+	 * @return
+	 */
+	@SuppressWarnings("unchecked")
+	public static final <E> E getValue(ValueMap properties, String key,
+			Object defaultOrType) {
+		if (defaultOrType instanceof Class<?>) {
+			return properties.get(key, (Class<E>) defaultOrType);
+		} else {
+			return properties.get(key, (E) defaultOrType);
+		}
+	}
+
+	/**
 	 * Method for allowing the invocation of the Sling Resource listChildren
 	 * method.
 	 * 
@@ -159,7 +169,7 @@ public class SlingFunctions {
 	 */
 	public static final Iterator<Resource> listChildren(Resource resource) {
 		log.trace("listChildren");
-		
+
 		Iterator<Resource> children = null;
 		if (resource != null) {
 			log.debug("Listing children at path {}", resource.getPath());
@@ -169,4 +179,19 @@ public class SlingFunctions {
 		}
 		return children;
 	}
+
+	/**
+	 * Loads the Class for the name from the current thread's classload.
+	 * 
+	 * @param className
+	 *            The name of the class to load
+	 * @return the class
+	 * @throws ClassNotFoundException
+	 *             a class with the specified name could not be found
+	 */
+	private static Class<?> loadClass(String className)
+			throws ClassNotFoundException {
+		return Thread.currentThread().getContextClassLoader()
+				.loadClass(className);
+	}
 }

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/sling.tld
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/sling.tld?rev=1494643&r1=1494642&r2=1494643&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/sling.tld (original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/main/resources/META-INF/sling.tld Wed Jun
19 14:49:55 2013
@@ -42,6 +42,13 @@
 		<function-signature>org.apache.sling.api.resource.Resource getResource(org.apache.sling.api.resource.ResourceResolver,java.lang.String)</function-signature>
 	</function>
 
+    <function>
+        <name>getValue</name>
+        <function-class>org.apache.sling.scripting.jsp.taglib.SlingFunctions</function-class>
+        <function-signature>java.lang.Object getValue(org.apache.sling.api.resource.ValueMap,java.lang.String,java.lang.Object)</function-signature>
+    </function>
+	
+
 	<function>
 		<name>listChildren</name>
 		<function-class>org.apache.sling.scripting.jsp.taglib.SlingFunctions</function-class>

Modified: sling/trunk/bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/jsp/taglib/TestSlingFunctions.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/jsp/taglib/TestSlingFunctions.java?rev=1494643&r1=1494642&r2=1494643&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/jsp/taglib/TestSlingFunctions.java
(original)
+++ sling/trunk/bundles/scripting/jsp-taglib/src/test/java/org/apache/sling/scripting/jsp/taglib/TestSlingFunctions.java
Wed Jun 19 14:49:55 2013
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
@@ -45,6 +46,7 @@ public class TestSlingFunctions {
 			.getLogger(TestGetResourceTag.class);
 	private MockResource resource;
 	private MockResourceResolver resolver;
+	private Date date;
 	private static final String TEST_PATH = "/content";
 
 	/**
@@ -68,6 +70,9 @@ public class TestSlingFunctions {
 			}
 		};
 		resource = new MockResource(resolver, TEST_PATH, "test");
+		this.date = new Date();
+		resource.addProperty("date", date);
+		resource.addProperty("long", new Long(0L));
 		resolver.addResource(resource);
 		MockResource child1 = new MockResource(resolver, TEST_PATH + "/child1",
 				"test");
@@ -127,6 +132,34 @@ public class TestSlingFunctions {
 	}
 
 	@Test
+	public void testGetValue(){
+		log.info("testGetValue");
+		Resource resource = SlingFunctions.getResource(resolver, TEST_PATH);
+		ValueMap properties = resource.adaptTo(ValueMap.class);
+		
+		log.info("Testing using class coersion");
+		Date retrievedDate = SlingFunctions.getValue(properties, "date", Date.class);
+		assertEquals(date,retrievedDate);
+		assertTrue(retrievedDate instanceof Date);
+		
+		log.info("Testing with default value on existing key");
+		Long retrievedLong = SlingFunctions.getValue(properties, "long", new Long(-123L));
+		assertEquals(new Long(0L),retrievedLong);
+		assertTrue(retrievedLong instanceof Long);
+		
+		log.info("Testing with no value and class coersion");
+		Date fakeDate = SlingFunctions.getValue(properties, "date1", Date.class);
+		assertTrue(fakeDate == null);
+		
+		log.info("Testing with no value and default specified");
+		Long fakeLong = SlingFunctions.getValue(properties, "long1", new Long(-123L));
+		assertEquals(new Long(-123L),fakeLong);
+		assertTrue(fakeLong instanceof Long);
+
+		log.info("Tests successful!");
+	}
+	
+	@Test
 	public void testListChildResources() {
 		log.info("testListChildResources");
 		Iterator<Resource> children = SlingFunctions.listChildren(resource);

Modified: sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/taglib-test.jsp
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/taglib-test.jsp?rev=1494643&r1=1494642&r2=1494643&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/taglib-test.jsp
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/taglib-test.jsp
Wed Jun 19 14:49:55 2013
@@ -1,4 +1,5 @@
 <%@page session="false" contentType="text/html; charset=utf-8"%>
+<%@page import="java.util.Date" %>
 <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <sling:defineObjects/><c:set var="success" value="true" />
@@ -106,6 +107,20 @@ Get Resource Function
     Test 3: Not Found Handling
     Result: <c:set var="nfresource7" value="${sling:getResource(resourceResolver, 'test123123')}"
/><c:choose><c:when test="${empty nfresource7.path}">SUCCESS</c:when><c:otherwise>ERROR<c:set
var="success" value="false" /></c:otherwise></c:choose>
     
+Get Value Function
+    Test 1: Get Property
+    <%
+    pageContext.setAttribute("dateClass", Date.class);
+    %>
+    Result: <c:set var="property5" value="${sling:getValue(componentProps,'jcr:created',dateClass)}"
/><c:choose><c:when test="${not empty property5}">SUCCESS</c:when><c:otherwise>ERROR<c:set
var="success" value="false" /></c:otherwise></c:choose>
+    
+    Test 2: Not Found Property
+    Result: <c:set var="property6" value="${sling:getValue(componentProps,'jcr:created2',dateClass)}"
/><c:choose><c:when test="${empty property6}">SUCCESS</c:when><c:otherwise>ERROR<c:set
var="success" value="false" /></c:otherwise></c:choose>
+
+    Test 3: Default Value
+    Result: <c:set var="property7" value="${sling:getValue(componentProps,'jcr:created2','fakeval')}"
/><c:choose><c:when test="${not empty property7}">SUCCESS</c:when><c:otherwise>ERROR<c:set
var="success" value="false" /></c:otherwise></c:choose>
+    
+    
 List Children Function
     Test 1: Get Children
     Result: <c:set var="childResources4" value="${sling:listChildren(contentResource)}"
/><c:choose><c:when test="${not empty childResources4}">SUCCESS</c:when><c:otherwise>ERROR<c:set
var="success" value="false" /></c:otherwise></c:choose>



Mime
View raw message