Search in sources :

Example 26 with ValueFormatException

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;
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) URI(java.net.URI)

Example 27 with ValueFormatException

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");
    }
}
Also used : Calendar(java.util.Calendar) ValueFormatException(javax.jcr.ValueFormatException) Date(java.util.Date)

Example 28 with ValueFormatException

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");
    }
}
Also used : Calendar(java.util.Calendar) ValueFormatException(javax.jcr.ValueFormatException) Date(java.util.Date)

Example 29 with ValueFormatException

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
}
Also used : Calendar(java.util.Calendar) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) BigDecimal(java.math.BigDecimal)

Example 30 with ValueFormatException

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
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) BigDecimal(java.math.BigDecimal)

Aggregations

ValueFormatException (javax.jcr.ValueFormatException)106 Value (javax.jcr.Value)53 Property (javax.jcr.Property)34 RepositoryException (javax.jcr.RepositoryException)16 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)12 Calendar (java.util.Calendar)10 ItemNotFoundException (javax.jcr.ItemNotFoundException)10 Node (javax.jcr.Node)10 QValue (org.apache.jackrabbit.spi.QValue)10 PathNotFoundException (javax.jcr.PathNotFoundException)6 Name (org.apache.jackrabbit.spi.Name)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)5 InternalValue (org.apache.jackrabbit.core.value.InternalValue)5 InputStream (java.io.InputStream)4 BigDecimal (java.math.BigDecimal)4 Date (java.util.Date)4 Path (org.apache.jackrabbit.spi.Path)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3