是否可以将List转换为JCR值?



所以我使用反射来创建一个List的新实例。然后,我想将该列表设置为一个Node。问题是节点只接受原语、string、Value和Value[]。当我尝试这样做时,我得到一个ClassCastException:

 Value[] valueArray = (Value[])Array.newInstance(elementType,size);

我想做:

node.setProperty(name,valueArray);

有没有人找到一种方法来正确地做到这一点?或者能指引我正确方向的地方?这可能吗?

那么为什么不使用自己的Value实现呢?例如:

class MyValue implements Value {
    private Object value;
    private int type;
    public MyValue(Object value) throws RepositoryException {
        if (value == null)
            throw new RepositoryException("Value can not be null");
        this.value = value;
        if (value instanceof Boolean) {
            type = PropertyType.BOOLEAN;
        } else if (value instanceof Calendar) {
            type = PropertyType.DATE;
        } else if (value instanceof Double) {
            type = PropertyType.DOUBLE;
        } else if (value instanceof Long) {
            type = PropertyType.LONG;
        } else if (value instanceof String) {
            type = PropertyType.STRING;
        } else {
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    }
    @Override
    public Binary getBinary() throws RepositoryException {
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public boolean getBoolean() throws ValueFormatException,
            RepositoryException {
        if (type == PropertyType.BOOLEAN)
            return (Boolean)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public Calendar getDate() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.DATE)
            return (Calendar)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public BigDecimal getDecimal() throws ValueFormatException,
            RepositoryException {
        if (type == PropertyType.DECIMAL)
            return (BigDecimal)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public double getDouble() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.DOUBLE)
            return (Double)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public long getLong() throws ValueFormatException, RepositoryException {
        if (type == PropertyType.LONG)
            return (Long)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public InputStream getStream() throws RepositoryException {
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public String getString() throws ValueFormatException,
            IllegalStateException, RepositoryException {
        if (type == PropertyType.STRING)
            return (String)value;
        throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
    }
    @Override
    public int getType() {
        return type;
    }
    @Override
    public String toString() {
        return value.getClass().getSimpleName() + "(" + value + ")";
    }
}

那么你的数组代码就像:

    Value[] valueArray = new Value[size];
    valueArray[0] = new MyValue(123L);
    valueArray[1] = new MyValue(123.45);
    valueArray[2] = new MyValue("12345");

相关内容

  • 没有找到相关文章

最新更新