简体   繁体   中英

Trying to parse ical file with ical4j problems with newlines after description property

im trying to parse the ical here: http://www.dsek.se/kalender/ical.php?person=&dsek&tlth

with this code:

URL url=new URL("http://www.dsek.se/kalender/ical.php?person=&dsek&tlth");
calendar=Calendars.load(url);

well, that is basicly the gist of the calendar code.

But im getting problems, I think somehow the "DEDSCRIPTION: text" gets transformed into "DESCRIPTION: newLine text" before getting parsed and thus the parser wont work I think.

The problem only appears on the rows where after DESCRIPTION: there is a whitespace, the rows that look like "DESCRIPTION:text" work fine. I've also tested another file that don't have these newlines and that file works fine.

So im guessing that maybe it is some kind of character encoding problem? that the URL object changes the encoding of the file? the character encoding on the file is ISO-8859-15

Or is it just that they have written the file with newlines after "DESCRIPTION:"? And if that is the case how do I solve this? :S

if it matters somehow the the app is running on android :)

The issue is that the DESCRIPTION field does not follow proper line folding. See http://tools.ietf.org/html/rfc5545#section-3.1

So wherever you have something like

DESCRIPTION:
some text

you should have instead

DESCRIPTION:
 some text

(please note the space after the linefeed and before the text) or simply

DESCRIPTION:some text

You might be able to get away with a simple Regex to fix that.

Then the file is also missing line folding for those DESCRIPTION that have a length greater than 75 characters. iCal4j should be fine with that.

Finally, regarding the character encoding, UTF-8 is the default (other encoding are actually deprecated. see http://tools.ietf.org/html/rfc5545#section-6 ) so the Calendars.load() method just assumes UTF-8.

So, you will have to

Reader r = new InputStreamReader(url.openStream(), "ISO-8859-15");
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(r);

Of course, the best solution would be for the authors of those ics files to fix those issues (line folding AND content encoding) on their side.

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