无法将自定义参数添加到VTODO组件的STATUS属性(ical4j)

时间:2015-04-11 22:49:25

标签: java ical4j

我正在尝试使用此代码

为标准Status属性添加新的XParameter
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.parameter.XParameter;
import org.apache.commons.io.IOUtils;
import com.example.common.util.ical.ICalUtil;

import java.io.FileInputStream;
import java.io.IOException;

public class TestICal {
    public static void main(String[] args) throws IOException {
        String content = IOUtils.toString(new FileInputStream("/tmp/taskA.ics"));
        Calendar task = ICalUtil.parse(content);
        Component vtodo = task.getComponent(Component.VTODO);
        Property prop = vtodo.getProperty(Property.STATUS);
        prop.getParameters().add(new XParameter("X-TEST-PARAM", "TEST-VALUE")); // java.lang.UnsupportedOperationException
    }
}

但在执行期间抛出异常

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.add(Collections.java:1016)
        at net.fortuna.ical4j.model.ParameterList.add(ParameterList.java:157)
        at TestICal.main(TestICal.java:18)

在调试器中,我可以看到在add()上调用了内部的ical4j包java.util.Collections$UnmodifiableRandomAccessList方法,实际上由于某些原因我在API文档中找不到,并且实现了java.util.List

无法删除或替换该属性,我看不到允许替换或添加其他参数列表的方法。

所以现在我认为该字段不能有参数,至少如果使用ical4j。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

回答自己:可以通过搜索必需的属性索引并调用set()扩展的ArrayList方法来完成

PropertyList

结果

import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.parameter.XParameter;
import org.apache.commons.io.IOUtils;
import com.example.common.util.ical.ICalUtil;

import java.io.FileInputStream;
import java.util.Iterator;

public class TestICal {
    public static void main(String[] args) throws Exception {
        // reading and parsing ICS
        String content = IOUtils.toString(new FileInputStream("/tmp/taskA.ics"));
        Calendar task = ICalUtil.parse(content);
        Component vtodo = task.getComponent(Component.VTODO);
        Property prop = vtodo.getProperty(Property.STATUS);

        // checking the prop before
        System.out.println(prop);

        // preparing new param list and adding it to new created prop
        ParameterList paramList = new ParameterList();
        paramList.add(new XParameter("X-TEST-PARAM", "TEST-VALUE"));
        PropertyFactoryImpl propFactory = PropertyFactoryImpl.getInstance();
        Property myprop = propFactory.createProperty(Property.STATUS, paramList, "COMPLETED");

        // and finally
        PropertyList propList = vtodo.getProperties();
        int index = propList.indexOf(prop);
        propList.set(index, myprop);

        // checking 
        System.out.println(vtodo.getProperties().getProperty(Property.STATUS));
    }
}