简体   繁体   中英

Can't add custom parameter to STATUS property of VTODO component (ical4j)

I'm trying to add new XParameter for standard Status property with this code

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
    }
}

but following exception is thrown during its execution

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)

In a debugger I can see that inside ical4j package add() method is called on java.util.Collections$UnmodifiableRandomAccessList which, actually I can't find in API doc for some reason, and which implements java.util.List

The property can't be deleted or replaced and I can't see a method which would allow to replace or add another parameter list.

So now I think the field can't have parameters, at least if using ical4j.

Any idea?

Answering myself: it can be done by searching required property index and calling set() method of ArrayList which PropertyList extends

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));
    }
}

result

STATUS:IN-PROCESS

STATUS;X-TEST-PARAM=TEST-VALUE:COMPLETED

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM