You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/07/03 13:43:35 UTC

svn commit: r673658 - in /incubator/sling/trunk: api/src/main/java/org/apache/sling/api/resource/ValueMap.java jcr/resource/pom.xml jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java

Author: fmeschbe
Date: Thu Jul  3 04:43:34 2008
New Revision: 673658

URL: http://svn.apache.org/viewvc?rev=673658&view=rev
Log:
SLING-557 Define ValueMap and implementation for Node based resources

Added:
    incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java
Modified:
    incubator/sling/trunk/jcr/resource/pom.xml
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java

Added: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java?rev=673658&view=auto
==============================================================================
--- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java (added)
+++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/resource/ValueMap.java Thu Jul  3 04:43:34 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.api.resource;
+
+import java.util.Map;
+
+public interface ValueMap extends Map<String, Object> {
+    
+    // return named value converted to type T or
+    // null if not existing
+    <T> T get(String name, Class<T> type);
+
+    // return named value converted to the type T of
+    // the default value or the default value if the
+    // named value does not exist
+    <T> T get(String name, T defaultValue);
+    
+}

Modified: incubator/sling/trunk/jcr/resource/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/pom.xml?rev=673658&r1=673657&r2=673658&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/pom.xml (original)
+++ incubator/sling/trunk/jcr/resource/pom.xml Thu Jul  3 04:43:34 2008
@@ -103,7 +103,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.0.2-incubator</version>
+            <version>2.0.3-incubator-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java?rev=673658&r1=673657&r2=673658&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyMap.java Thu Jul  3 04:43:34 2008
@@ -18,8 +18,12 @@
  */
 package org.apache.sling.jcr.resource.internal.helper.jcr;
 
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -27,11 +31,19 @@
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
 
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-class JcrPropertyMap implements Map<String, Object> {
+class JcrPropertyMap implements ValueMap {
 
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
     private final Node node;
 
     private final Map<String, Object> cache;
@@ -44,6 +56,33 @@
         this.fullyRead = false;
     }
 
+    // ---------- ValueMap
+
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, Class<T> type) {
+        if (type == null) {
+            return (T) get(name);
+        }
+
+        return convertToType(name, type);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, T defaultValue) {
+        if (defaultValue == null) {
+            return (T) get(name);
+        }
+
+        T value = get(name, (Class<T>) defaultValue.getClass());
+        if (value == null) {
+            value = defaultValue;
+        }
+
+        return value;
+    }
+
+    // ---------- Map
+
     public Object get(Object key) {
         Object value = cache.get(key);
         if (value == null) {
@@ -63,6 +102,7 @@
     }
 
     public boolean isEmpty() {
+        // only start reading if there is nothing in the cache yet
         if (cache.isEmpty()) {
             readFully();
         }
@@ -133,7 +173,7 @@
         }
     }
 
-    // ---------- Unsupported Modification methods -----------------------------
+    // ---------- Unsupported Modification methods
 
     public void clear() {
         throw new UnsupportedOperationException();
@@ -151,4 +191,71 @@
         throw new UnsupportedOperationException();
     }
 
+    // ---------- Implementation helper
+
+    @SuppressWarnings("unchecked")
+    private <T> T convertToType(String name, Class<T> type) {
+        try {
+            if (node.hasProperty(name)) {
+                Property prop = node.getProperty(name);
+
+                boolean multiValue = prop.getDefinition().isMultiple();
+                boolean array = type.isArray();
+
+                if (array && multiValue) {
+                    return (T) convertToArray(prop.getValues(),
+                        type.getComponentType());
+                } else if (!array && !multiValue) {
+                    return convertToType(prop.getValue(), type);
+                }
+            }
+
+        } catch (ValueFormatException vfe) {
+            log.info("converToType: Cannot convert value of " + name + " to "
+                + type, vfe);
+        } catch (RepositoryException re) {
+            log.info("converToType: Cannot get value of " + name, re);
+        }
+
+        // fall back to nothing
+        return null;
+    }
+
+    private <T> T[] convertToArray(Value[] jcrValues, Class<T> type)
+            throws ValueFormatException, RepositoryException {
+        List<T> values = new ArrayList<T>();
+        for (int i = 0; i < jcrValues.length; i++) {
+            T value = convertToType(jcrValues[i], type);
+            if (value != null) {
+                values.add(value);
+            }
+        }
+
+        @SuppressWarnings("unchecked")
+        T[] result = (T[]) Array.newInstance(type, values.size());
+
+        return values.toArray(result);
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T> T convertToType(Value jcrValue, Class<T> type)
+            throws ValueFormatException, RepositoryException {
+
+        if (String.class == type) {
+            return (T) jcrValue.getString();
+        } else if (Long.class == type) {
+            return (T) new Long(jcrValue.getLong());
+        } else if (Double.class == type) {
+            return (T) new Double(jcrValue.getDouble());
+        } else if (Boolean.class == type) {
+            return (T) Boolean.valueOf(jcrValue.getBoolean());
+        } else if (Calendar.class == type) {
+            return (T) jcrValue.getDate();
+        } else if (Value.class == type) {
+            return (T) jcrValue;
+        }
+
+        // fallback in case of unsupported type
+        return null;
+    }
 }