X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=inline;f=org.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcr.java;h=b6ee85eef345f6cf8f9ce0e2f521cdbb346c0d44;hb=HEAD;hp=2ff49e7ce92c988902a54d613acd6e39ef39ea08;hpb=705b111ed306ae7daefb7d02247a7e677aea1cbd;p=gpl%2Fargeo-jcr.git
diff --git a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java
index 2ff49e7..b6ee85e 100644
--- a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java
+++ b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java
@@ -14,6 +14,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
+import java.util.Objects;
import javax.jcr.Binary;
import javax.jcr.ItemNotFoundException;
@@ -49,11 +50,15 @@ public class Jcr {
/**
* The name of a node which will be serialized as XML text, as per section 7.3.1
* of the JCR 2.0 specifications.
+ *
+ * @see Workspace#NAME_JCR_XMLTEXT
*/
public final static String JCR_XMLTEXT = "jcr:xmltext";
/**
* The name of a property which will be serialized as XML text, as per section
* 7.3.1 of the JCR 2.0 specifications.
+ *
+ * @see Workspace#NAME_JCR_XMLCHARACTERS
*/
public final static String JCR_XMLCHARACTERS = "jcr:xmlcharacters";
/**
@@ -427,6 +432,20 @@ public class Jcr {
}
}
+ /**
+ * Whether this node has this property.
+ *
+ * @see Node#hasProperty(String)
+ * @throws JcrException caused by {@link RepositoryException}
+ */
+ public static boolean hasProperty(Node node, String property) {
+ try {
+ return node.hasProperty(property);
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot check whether " + node + " has property " + property, e);
+ }
+ }
+
/**
* Set a property to the given value, or remove it if the value is
* null
.
@@ -577,6 +596,7 @@ public class Jcr {
*/
@SuppressWarnings("unchecked")
public static T getAs(Node node, String property, T defaultValue) {
+ Objects.requireNonNull(defaultValue);
try {
// TODO deal with multiple
if (node.hasProperty(property)) {
@@ -599,14 +619,32 @@ public class Jcr {
}
}
+ @SuppressWarnings("unchecked")
public static T getAs(Node node, String property, Class clss) {
- if (String.class.isAssignableFrom(clss)) {
- return (T) get(node, property);
- } else if (Long.class.isAssignableFrom(clss)) {
- return (T) get(node, property);
- } else {
- throw new IllegalArgumentException("Unsupported format " + clss);
+ try {
+ Property p = node.getProperty(property);
+ try {
+ if (p.isMultiple()) {
+ throw new UnsupportedOperationException("Multiple values properties are not supported");
+ }
+ Value value = p.getValue();
+ return (T) get(value);
+ } catch (ClassCastException e) {
+ throw new IllegalArgumentException(
+ "Cannot cast property of type " + PropertyType.nameFromValue(p.getType()), e);
+ }
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot retrieve property " + property + " from " + node, e);
}
+// if (String.class.isAssignableFrom(clss)) {
+// return (T) get(node, property);
+// } else if (Long.class.isAssignableFrom(clss)) {
+// return (T) get(node, property);
+// } else if (Boolean.class.isAssignableFrom(clss)) {
+// return (T) get(node, property);
+// } else {
+// throw new IllegalArgumentException("Unsupported format " + clss);
+// }
}
/**