import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
+import java.util.Objects;
import javax.jcr.Binary;
import javax.jcr.ItemNotFoundException;
}
}
+ /**
+ * 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
* <code>null</code>.
*/
@SuppressWarnings("unchecked")
public static <T> T getAs(Node node, String property, T defaultValue) {
+ Objects.requireNonNull(defaultValue);
try {
// TODO deal with multiple
if (node.hasProperty(property)) {
}
}
+ @SuppressWarnings("unchecked")
public static <T> T getAs(Node node, String property, Class<T> 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);
+// }
}
/**