是否可以将列表转换为 JCR 值?

Is it possible to convert a List to a JCR 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");