This is actually a regression. Relaxed parsing should have covered this case. Just fixed it in both branch and tip (http://ical4j.hg.sourceforge.net/hgweb/ical4j/ical4j/rev/8c23205afac9).
how to make iCal4j not fail on parsing errors (like 00001231T000000Z)
-
13-12-2021 - |
Question
I am trying to get iCal4j to parse an exported Google .ics calendar file. Unfortunately the parser chokes on a particular value (00001231T000000Z) and throws an exception. My question is how can I get the parser to continue despite the bad data? I am about to edit the source code but is there not a way to configure it to not fail on a date parse error.
I am using ical4j-1.0.5-SNAPSHOT - which I believe is based on the latest code.
My data contains the following event (which appears to be a recurring one):
BEGIN:VEVENT
DTSTART;TZID=America/Vancouver:20101206T060000
DTEND;TZID=America/Vancouver:20101206T150000
RRULE:FREQ=DAILY;UNTIL=20101210T140000Z
DTSTAMP:20121231T143813Z
UID:[deleted]@google.com
CREATED:00001231T000000Z
DESCRIPTION:
LAST-MODIFIED:20110102T020817Z
LOCATION:
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:iCal4j chokes on this
TRANSP:OPAQUE
END:VEVENT
and dies when it tries to parse this date value (which seems to be a nonsense/bad value)
CREATED:00001231T000000Z
I don't particularly care if the created field is set or is set to a dummy value. What I care about is the parser continuing and not choking on bad data.
I have tried setting the parsing to relaxed, but somehow this case seems to slip through.
CompatibilityHints.setHintEnabled(
CompatibilityHints.KEY_RELAXED_PARSING, true);
So, how can I make this code more robust to parse errors? Can I supply my own parser? Can I get it to skip records that have errors?
Solution
OTHER TIPS
I copied the CalendarBuilder class and wrapped the property set method with a try/catch as below. I'm still interested to hear other solutions to this. I really dislike having to copy an entire class to fix something like this.
public void propertyValue(final String value)
throws URISyntaxException, ParseException, IOException {
assertProperty(property);
try {
if (property instanceof Escapable) {
property.setValue(Strings.unescape(value));
} else {
property.setValue(value);
}
} catch (Exception e) {
Logger.warn("Could not set propety: " + property.getName()
+ " to " + value);
}
}
Have you tried setting all these together ?
ical4j.unfolding.relaxed=true
ical4j.parsing.relaxed=true
ical4j.validation.relaxed=true
ical4j.compatibility.outlook=true