Author: fmeschbe
Date: Fri Sep 19 08:17:31 2008
New Revision: 697128
URL: http://svn.apache.org/viewvc?rev=697128&view=rev
Log:
SLING-589 Support access to the raw JCR property
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrPropertyMapTest.java
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java?rev=697128&r1=697127&r2=697128&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
Fri Sep 19 08:17:31 2008
@@ -80,14 +80,7 @@
// special handling in case the default value implements one
// of the interface types supported by the convertToType method
- Class<T> type = (Class<T>) defaultValue.getClass();
- if (Calendar.class.isAssignableFrom(type)) {
- type = (Class<T>) Calendar.class;
- } else if (Date.class.isAssignableFrom(type)) {
- type = (Class<T>) Date.class;
- } else if (Value.class.isAssignableFrom(type)) {
- type = (Class<T>) Value.class;
- }
+ Class<T> type = (Class<T>) normalizeClass(defaultValue.getClass());
T value = get(name, type);
if (value == null) {
@@ -164,6 +157,7 @@
/**
* Return the path of the current node.
+ *
* @throws IllegalStateException If a repository exception occurs
*/
public String getPath() {
@@ -174,7 +168,6 @@
}
}
-
// ---------- Helpers to access the node's property
------------------------
protected Object read(String key) {
@@ -241,7 +234,7 @@
@SuppressWarnings("unchecked")
private <T> T convertToType(String name, Class<T> type) {
T result = null;
-
+
try {
if (node.hasProperty(name)) {
Property prop = node.getProperty(name);
@@ -250,38 +243,38 @@
boolean array = type.isArray();
if (multiValue) {
-
+
Value[] values = prop.getValues();
if (array) {
-
+
result = (T) convertToArray(prop, values,
type.getComponentType());
-
+
} else if (values.length > 0) {
-
+
result = convertToType(prop, -1, values[0], type);
-
+
}
-
+
} else {
-
+
Value value = prop.getValue();
if (array) {
-
+
result = (T) convertToArray(prop,
new Value[] { value }, type.getComponentType());
-
+
} else {
-
+
result = convertToType(prop, -1, value, type);
-
+
}
}
}
} catch (ValueFormatException vfe) {
- logger.info("converToType: Cannot convert value of " + name + " to
"
- + type, vfe);
+ logger.info("converToType: Cannot convert value of " + name
+ + " to " + type, vfe);
} catch (RepositoryException re) {
logger.info("converToType: Cannot get value of " + name, re);
}
@@ -307,50 +300,66 @@
}
@SuppressWarnings("unchecked")
- private <T> T convertToType(Property p, int index, Value jcrValue,
Class<T> type)
- throws ValueFormatException, RepositoryException {
+ private <T> T convertToType(Property p, int index, Value jcrValue,
+ Class<T> type) throws ValueFormatException, RepositoryException {
if (String.class == type) {
return (T) jcrValue.getString();
-
+
} else if (Byte.class == type) {
return (T) new Byte((byte) jcrValue.getLong());
} else if (Short.class == type) {
return (T) new Short((short) jcrValue.getLong());
-
+
} else if (Integer.class == type) {
return (T) new Integer((int) jcrValue.getLong());
} else if (Long.class == type) {
- if ( jcrValue.getType() == PropertyType.BINARY ) {
- if ( index == -1 ) {
- return (T)new Long(p.getLength());
+ if (jcrValue.getType() == PropertyType.BINARY) {
+ if (index == -1) {
+ return (T) new Long(p.getLength());
}
- return (T)new Long(p.getLengths()[index]);
+ return (T) new Long(p.getLengths()[index]);
}
return (T) new Long(jcrValue.getLong());
-
+
} else if (Float.class == type) {
return (T) new Float(jcrValue.getDouble());
-
+
} else if (Double.class == type) {
return (T) new Double(jcrValue.getDouble());
-
+
} else if (Boolean.class == type) {
return (T) Boolean.valueOf(jcrValue.getBoolean());
-
+
} else if (Date.class == type) {
return (T) jcrValue.getDate().getTime();
-
+
} else if (Calendar.class == type) {
return (T) jcrValue.getDate();
-
+
} else if (Value.class == type) {
return (T) jcrValue;
+
+ } else if (Property.class == type) {
+ return (T) p;
}
// fallback in case of unsupported type
return null;
}
+
+ private Class<?> normalizeClass(Class<?> type) {
+ if (Calendar.class.isAssignableFrom(type)) {
+ type = Calendar.class;
+ } else if (Date.class.isAssignableFrom(type)) {
+ type = Date.class;
+ } else if (Value.class.isAssignableFrom(type)) {
+ type = Value.class;
+ } else if (Property.class.isAssignableFrom(type)) {
+ type = Property.class;
+ }
+ return type;
+ }
}
Modified:
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrPropertyMapTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrPropertyMapTest.java?rev=697128&r1=697127&r2=697128&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrPropertyMapTest.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/JcrPropertyMapTest.java
Fri Sep 19 08:17:31 2008
@@ -21,12 +21,12 @@
import java.util.Date;
import javax.jcr.Node;
+import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
-import org.apache.sling.jcr.resource.JcrPropertyMap;
public class JcrPropertyMapTest extends RepositoryTestBase {
@@ -126,6 +126,24 @@
testDefaultValue(rootNode, refTime);
}
+ public void testProperty() throws Exception {
+ JcrPropertyMap map = createProperty(rootNode, "Sample Value For Prop");
+ Property prop = rootNode.getProperty(PROP_NAME);
+
+ // explicite type
+ Property result = map.get(PROP_NAME, Property.class);
+ assertEquals(prop, result);
+
+ // type by default value
+ Property defaultValue = rootNode.getProperty("jcr:primaryType");
+ result = map.get(PROP_NAME, defaultValue);
+ assertEquals(prop, result);
+
+ // default value
+ result = map.get(PROP_NAME_NIL, defaultValue);
+ assertSame(defaultValue, result);
+ }
+
// ---------- internal
private void testValue(Node node, Object value, Object defaultValue)
throws RepositoryException {