in jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java [234:611]
public static Value convert(Value srcValue, int targetType, ValueFactory factory)
throws ValueFormatException, IllegalStateException,
IllegalArgumentException {
if (srcValue == null) {
return null;
}
Value val;
int srcType = srcValue.getType();
if (srcType == targetType) {
// no conversion needed, return original value
return srcValue;
}
switch (targetType) {
case PropertyType.STRING:
// convert to STRING
try {
val = factory.createValue(srcValue.getString());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.BINARY:
// convert to BINARY
try {
val = factory.createValue(srcValue.getBinary());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.BOOLEAN:
// convert to BOOLEAN
try {
val = factory.createValue(srcValue.getBoolean());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.DATE:
// convert to DATE
try {
val = factory.createValue(srcValue.getDate());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.DOUBLE:
// convert to DOUBLE
try {
val = factory.createValue(srcValue.getDouble());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.LONG:
// convert to LONG
try {
val = factory.createValue(srcValue.getLong());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.DECIMAL:
// convert to DECIMAL
try {
val = factory.createValue(srcValue.getDecimal());
} catch (RepositoryException re) {
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType), re);
}
break;
case PropertyType.PATH:
// convert to PATH
switch (srcType) {
case PropertyType.PATH:
// no conversion needed, return original value
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case PropertyType.NAME: // a name is always also a relative path
// try conversion via string
String path;
try {
// get string value
path = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to PATH value",
re);
}
// the following call will throw ValueFormatException
// if p is not a valid PATH
val = factory.createValue(path, targetType);
break;
case PropertyType.URI:
URI uri;
try {
uri = URI.create(srcValue.getString());
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to PATH value",
re);
}
if (uri.isAbsolute()) {
// uri contains scheme...
throw new ValueFormatException("failed to convert URI value to PATH value");
}
String p = uri.getPath();
if (p.startsWith("./")) {
p = p.substring(2);
}
// the following call will throw ValueFormatException
// if p is not a valid PATH
val = factory.createValue(p, targetType);
break;
case PropertyType.BOOLEAN:
case PropertyType.DATE:
case PropertyType.DOUBLE:
case PropertyType.DECIMAL:
case PropertyType.LONG:
case PropertyType.REFERENCE:
case PropertyType.WEAKREFERENCE:
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType));
default:
throw new IllegalArgumentException("not a valid type constant: " + srcType);
}
break;
case PropertyType.NAME:
// convert to NAME
switch (srcType) {
case PropertyType.NAME:
// no conversion needed, return original value
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case PropertyType.PATH: // path might be a name (relative path of length 1)
// try conversion via string
String name;
try {
// get string value
name = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to NAME value",
re);
}
// the following call will throw ValueFormatException
// if p is not a valid NAME
val = factory.createValue(name, targetType);
break;
case PropertyType.URI:
URI uri;
try {
uri = URI.create(srcValue.getString());
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to NAME value",
re);
}
if (uri.isAbsolute()) {
// uri contains scheme...
throw new ValueFormatException("failed to convert URI value to NAME value");
}
String p = uri.getPath();
if (p.startsWith("./")) {
p = p.substring(2);
}
// the following call will throw ValueFormatException
// if p is not a valid NAME
val = factory.createValue(p, targetType);
break;
case PropertyType.BOOLEAN:
case PropertyType.DATE:
case PropertyType.DOUBLE:
case PropertyType.DECIMAL:
case PropertyType.LONG:
case PropertyType.REFERENCE:
case PropertyType.WEAKREFERENCE:
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType));
default:
throw new IllegalArgumentException("not a valid type constant: " + srcType);
}
break;
case PropertyType.REFERENCE:
// convert to REFERENCE
switch (srcType) {
case PropertyType.REFERENCE:
// no conversion needed, return original value
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case PropertyType.WEAKREFERENCE:
// try conversion via string
String uuid;
try {
// get string value
uuid = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to REFERENCE value", re);
}
val = factory.createValue(uuid, targetType);
break;
case PropertyType.BOOLEAN:
case PropertyType.DATE:
case PropertyType.DOUBLE:
case PropertyType.LONG:
case PropertyType.DECIMAL:
case PropertyType.PATH:
case PropertyType.URI:
case PropertyType.NAME:
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType));
default:
throw new IllegalArgumentException("not a valid type constant: " + srcType);
}
break;
case PropertyType.WEAKREFERENCE:
// convert to WEAKREFERENCE
switch (srcType) {
case PropertyType.WEAKREFERENCE:
// no conversion needed, return original value
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case PropertyType.REFERENCE:
// try conversion via string
String uuid;
try {
// get string value
uuid = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to WEAKREFERENCE value", re);
}
val = factory.createValue(uuid, targetType);
break;
case PropertyType.BOOLEAN:
case PropertyType.DATE:
case PropertyType.DOUBLE:
case PropertyType.LONG:
case PropertyType.DECIMAL:
case PropertyType.URI:
case PropertyType.PATH:
case PropertyType.NAME:
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType));
default:
throw new IllegalArgumentException("not a valid type constant: " + srcType);
}
break;
case PropertyType.URI:
// convert to URI
switch (srcType) {
case PropertyType.URI:
// no conversion needed, return original value
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
// try conversion via string
String uuid;
try {
// get string value
uuid = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to URI value", re);
}
val = factory.createValue(uuid, targetType);
break;
case PropertyType.NAME:
String name;
try {
// get string value
name = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to URI value", re);
}
// prefix name with "./" (jsr 283 spec 3.6.4.8)
val = factory.createValue("./" + name, targetType);
break;
case PropertyType.PATH:
String path;
try {
// get string value
path = srcValue.getString();
} catch (RepositoryException re) {
// should never happen
throw new ValueFormatException("failed to convert source value to URI value", re);
}
if (!path.startsWith("/")) {
// prefix non-absolute path with "./" (jsr 283 spec 3.6.4.9)
path = "./" + path;
}
val = factory.createValue(path, targetType);
break;
case PropertyType.BOOLEAN:
case PropertyType.DATE:
case PropertyType.DOUBLE:
case PropertyType.LONG:
case PropertyType.DECIMAL:
case PropertyType.REFERENCE:
case PropertyType.WEAKREFERENCE:
throw new ValueFormatException("conversion failed: "
+ PropertyType.nameFromValue(srcType) + " to "
+ PropertyType.nameFromValue(targetType));
default:
throw new IllegalArgumentException("not a valid type constant: " + srcType);
}
break;
default:
throw new IllegalArgumentException("not a valid type constant: " + targetType);
}
return val;
}