use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class ValueHelper method convert.
/**
* Converts the given value to a value of the specified target type.
* The conversion is performed according to the rules described in
* "3.6.4 Property Type Conversion" in the JSR 283 specification.
*
* @param srcValue
* @param targetType
* @param factory
* @throws ValueFormatException
* @throws IllegalStateException
* @throws IllegalArgumentException
*/
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:
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case // a name is always also a relative path
PropertyType.NAME:
// 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:
// (redundant code, just here for the sake of clarity)
return srcValue;
case PropertyType.BINARY:
case PropertyType.STRING:
case // path might be a name (relative path of length 1)
PropertyType.PATH:
// 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:
// (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:
// (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:
// (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;
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class DoubleValue method getDate.
// ----------------------------------------------------------------< Value >
/**
* {@inheritDoc}
*/
public Calendar getDate() throws ValueFormatException, IllegalStateException, RepositoryException {
if (dblNumber != null) {
// loosing timezone information...
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(dblNumber.longValue()));
return cal;
} else {
throw new ValueFormatException("empty value");
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class LongValue method getDate.
// ----------------------------------------------------------------< Value >
/**
* {@inheritDoc}
*/
public Calendar getDate() throws ValueFormatException, IllegalStateException, RepositoryException {
if (lNumber != null) {
// loosing timezone information...
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(lNumber));
return cal;
} else {
throw new ValueFormatException("empty value");
}
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class SimpleValueFactoryTest method testDate.
public void testDate() throws RepositoryException {
Calendar a = Calendar.getInstance();
a.setTimeInMillis(1234567890);
a.setTimeZone(TimeZone.getTimeZone("GMT"));
Value value = factory.createValue(a);
assertEquals(PropertyType.DATE, value.getType());
assertEquals(value, factory.createValue(a));
try {
value.getBoolean();
fail();
} catch (ValueFormatException e) {
}
assertEquals(a, value.getDate());
assertEquals(new BigDecimal(a.getTimeInMillis()), value.getDecimal());
assertEquals((double) a.getTimeInMillis(), value.getDouble());
assertEquals(a.getTimeInMillis(), value.getLong());
assertEquals("1970-01-15T06:56:07.890Z", value.getString());
// TODO: binary representation
}
use of javax.jcr.ValueFormatException in project jackrabbit by apache.
the class SimpleValueFactoryTest method testDouble.
public void testDouble() throws RepositoryException {
double a = 123456789.0;
Value value = factory.createValue(a);
assertEquals(PropertyType.DOUBLE, value.getType());
assertEquals(value, factory.createValue(a));
try {
value.getBoolean();
fail();
} catch (ValueFormatException e) {
}
assertEquals((long) a, value.getDate().getTimeInMillis());
assertEquals(new BigDecimal(a), value.getDecimal());
assertEquals(a, value.getDouble());
assertEquals((long) a, value.getLong());
assertEquals(Double.toString(a), value.getString());
// TODO: binary representation
}
Aggregations