use of javax.jcr.ValueFormatException in project jackrabbit-oak by apache.
the class PropertyImpl method getNode.
@Override
@Nonnull
public Node getNode() throws RepositoryException {
return perform(new PropertyOperation<Node>(dlg, "getNode") {
@Nonnull
@Override
public Node perform() throws RepositoryException {
// TODO: avoid nested calls
Value value = getValue();
switch(value.getType()) {
case PropertyType.REFERENCE:
case PropertyType.WEAKREFERENCE:
return getSession().getNodeByIdentifier(value.getString());
case PropertyType.PATH:
case PropertyType.NAME:
String path = value.getString();
if (path.startsWith("[") && path.endsWith("]")) {
// identifier path
String identifier = path.substring(1, path.length() - 1);
return getSession().getNodeByIdentifier(identifier);
} else {
try {
return (path.charAt(0) == '/') ? getSession().getNode(path) : getParent().getNode(path);
} catch (PathNotFoundException e) {
throw new ItemNotFoundException(path);
}
}
case PropertyType.STRING:
try {
Value refValue = ValueHelper.convert(value, PropertyType.REFERENCE, getValueFactory());
return getSession().getNodeByIdentifier(refValue.getString());
} catch (ItemNotFoundException e) {
throw e;
} catch (RepositoryException e) {
// try if STRING value can be interpreted as PATH value
Value pathValue = ValueHelper.convert(value, PropertyType.PATH, getValueFactory());
path = pathValue.getString();
try {
return (path.charAt(0) == '/') ? getSession().getNode(path) : getParent().getNode(path);
} catch (PathNotFoundException e1) {
throw new ItemNotFoundException(pathValue.getString());
}
}
default:
throw new ValueFormatException("Property value cannot be converted to a PATH, REFERENCE or WEAKREFERENCE");
}
}
});
}
use of javax.jcr.ValueFormatException in project sling by apache.
the class JcrNodeResourceMetadata method get.
@Override
public Object get(final Object key) {
final Object result = super.get(key);
if (result != null) {
return result;
}
if (CREATION_TIME.equals(key)) {
promoteNode();
internalPut(CREATION_TIME, creationTime);
return creationTime;
} else if (CONTENT_TYPE.equals(key)) {
String contentType = null;
final Node targetNode = promoteNode();
try {
if (targetNode.hasProperty(JCR_MIMETYPE)) {
contentType = targetNode.getProperty(JCR_MIMETYPE).getString();
}
} catch (final RepositoryException re) {
report(re);
}
internalPut(CONTENT_TYPE, contentType);
return contentType;
} else if (CHARACTER_ENCODING.equals(key)) {
String characterEncoding = null;
final Node targetNode = promoteNode();
try {
if (targetNode.hasProperty(JCR_ENCODING)) {
characterEncoding = targetNode.getProperty(JCR_ENCODING).getString();
}
} catch (final RepositoryException re) {
report(re);
}
internalPut(CHARACTER_ENCODING, characterEncoding);
return characterEncoding;
} else if (MODIFICATION_TIME.equals(key)) {
long modificationTime = -1;
final Node targetNode = promoteNode();
try {
if (targetNode.hasProperty(JCR_LASTMODIFIED)) {
// We don't check node type, so JCR_LASTMODIFIED might not be a long
final Property prop = targetNode.getProperty(JCR_LASTMODIFIED);
try {
modificationTime = prop.getLong();
} catch (final ValueFormatException vfe) {
LOGGER.debug("Property {} cannot be converted to a long, ignored ({})", prop.getPath(), vfe);
}
}
} catch (final RepositoryException re) {
report(re);
}
internalPut(MODIFICATION_TIME, modificationTime);
return modificationTime;
} else if (CONTENT_LENGTH.equals(key)) {
long contentLength = -1;
final Node targetNode = promoteNode();
try {
// if the node has a jcr:data property, use that property
if (targetNode.hasProperty(JCR_DATA)) {
final Property prop = targetNode.getProperty(JCR_DATA);
contentLength = JcrItemResource.getContentLength(prop);
} else {
// otherwise try to follow default item trail
Item item = getPrimaryItem(targetNode);
while (item != null && item.isNode()) {
item = getPrimaryItem((Node) item);
}
if (item != null) {
final Property data = (Property) item;
// set the content length property as a side effect
// for resources which are not nt:file based and whose
// data is not in jcr:content/jcr:data this will lazily
// set the correct content length
contentLength = JcrItemResource.getContentLength(data);
}
}
} catch (final RepositoryException re) {
report(re);
}
internalPut(CONTENT_LENGTH, contentLength);
return contentLength;
}
return null;
}
use of javax.jcr.ValueFormatException in project jackrabbit-oak by apache.
the class NodeDelegate method setProperty.
/**
* Set a property
*
* @return the set property
*/
@Nonnull
public PropertyDelegate setProperty(PropertyState propertyState, boolean exactTypeMatch, boolean setProtected) throws RepositoryException {
Tree tree = getTree();
String name = propertyState.getName();
Type<?> type = propertyState.getType();
PropertyState old = tree.getProperty(name);
if (old != null && old.isArray() && !propertyState.isArray()) {
throw new ValueFormatException("Can not assign a single value to multi-valued property: " + propertyState);
}
if (old != null && !old.isArray() && propertyState.isArray()) {
throw new ValueFormatException("Can not assign multiple values to single valued property: " + propertyState);
}
Tree definition = findMatchingPropertyDefinition(getNodeTypes(tree), name, type, exactTypeMatch);
if (definition == null) {
throw new ConstraintViolationException("No matching property definition: " + propertyState);
} else if (!setProtected && TreeUtil.getBoolean(definition, JCR_PROTECTED)) {
throw new ConstraintViolationException("Property is protected: " + propertyState);
}
Type<?> requiredType = Type.fromString(TreeUtil.getString(definition, JCR_REQUIREDTYPE));
if (requiredType != Type.UNDEFINED) {
if (TreeUtil.getBoolean(definition, JCR_MULTIPLE)) {
requiredType = requiredType.getArrayType();
}
ValueHelper.checkSupportedConversion(propertyState.getType().tag(), requiredType.tag());
propertyState = PropertyStates.convert(propertyState, requiredType);
}
tree.setProperty(propertyState);
return new PropertyDelegate(sessionDelegate, tree, name);
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class DatePropertyTest method testGetBoolean.
/**
* Tests failure of conversion from Date type to Boolean type.
*/
public void testGetBoolean() throws RepositoryException {
try {
Value val = PropertyUtil.getValue(prop);
val.getBoolean();
fail("Conversion from a Date value to a Boolean value " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class PropertyImpl method setValue.
/**
* Sets the values of this property.
*
* @param values property values (possibly <code>null</code>)
* @param valueType default value type if not set in the node type,
* may be {@link PropertyType#UNDEFINED}
* @throws RepositoryException if the property values could not be set
*/
public void setValue(Value[] values, int valueType) throws RepositoryException {
// check state of this instance
sanityCheck();
// check pre-conditions for setting property value
checkSetValue(true);
if (values != null) {
// check type of values
int firstValueType = UNDEFINED;
for (Value value : values) {
if (value != null) {
if (firstValueType == UNDEFINED) {
firstValueType = value.getType();
} else if (firstValueType != value.getType()) {
throw new ValueFormatException("inhomogeneous type of values");
}
}
}
}
final PropertyDefinition definition = data.getPropertyDefinition();
int reqType = definition.getRequiredType();
if (reqType == UNDEFINED) {
// use the given type as property type
reqType = valueType;
}
InternalValue[] internalValues = null;
// convert to internal values of correct type
if (values != null) {
internalValues = new InternalValue[values.length];
// check type of values
for (int i = 0; i < values.length; i++) {
Value value = values[i];
if (value != null) {
if (reqType == UNDEFINED) {
// Use the type of the fist value as the type
reqType = value.getType();
}
if (reqType != value.getType()) {
value = ValueHelper.convert(value, reqType, getSession().getValueFactory());
}
internalValues[i] = InternalValue.create(value, sessionContext, sessionContext.getDataStore());
} else {
internalValues[i] = null;
}
}
}
internalSetValue(internalValues, reqType);
}
Aggregations