2008年4月10日木曜日

[JAVA>Rome, Itunes RSS/XML] When using Rome ITunes RSS Reader, ParsingFeedException(Invalid XML) error occured.

When using Rome's itunes module, the error "com.sun.syndication.io.ParsingFeedException: Invalid XML" sometimes occures.The error stack is as follows. This error isn't related to the Rome core program, this error is depended on the itunes module.
The Podcast RSS feeds has itunes:duration attribute which means the duration of the podcast file. The value format of this field is always similar to "09:30", but some site are including the invalid value(Long type value).

References
  1. Same error, but the root cause is different from this problem.
    http://taapps-javalibs.blogspot.com/2008/04/javarome-rssxml-when-using-rome-rss.html
  2. Itunes module site
    http://wiki.java.net/bin/view/Javawsxml/ITunes
  3. Source Code repository
    https://rome.dev.java.net/source/browse/rome/
Error Stack
  • Caused by: java.lang.RuntimeException: Illegal time value: 9174935
    at com.sun.syndication.feed.module.itunes.types.Duration.(Duration.java:98)
    at com.sun.syndication.feed.module.itunes.io.ITunesParser.parse(ITunesParser.java:149)
    at com.sun.syndication.io.impl.ModuleParsers.parseModules(ModuleParsers.java:51)
    at com.sun.syndication.io.impl.BaseWireFeedParser.parseItemModules(BaseWireFeedParser.java:57)
    at com.sun.syndication.io.impl.RSS090Parser.parseItem(RSS090Parser.java:289)
    at com.sun.syndication.io.impl.RSS091UserlandParser.parseItem(RSS091UserlandParser.java:222)
    at com.sun.syndication.io.impl.RSS092Parser.parseItem(RSS092Parser.java:81)
    at com.sun.syndication.io.impl.RSS093Parser.parseItem(RSS093Parser.java:39)
    at com.sun.syndication.io.impl.RSS094Parser.parseItem(RSS094Parser.java:68)
    at com.sun.syndication.io.impl.RSS090Parser.parseItems(RSS090Parser.java:263)
    at com.sun.syndication.io.impl.RSS090Parser.parseChannel(RSS090Parser.java:178)
    at com.sun.syndication.io.impl.RSS091UserlandParser.parseChannel(RSS091UserlandParser.java:84)
    at com.sun.syndication.io.impl.RSS092Parser.parseChannel(RSS092Parser.java:49)
    at com.sun.syndication.io.impl.RSS094Parser.parseChannel(RSS094Parser.java:45)
    at com.sun.syndication.io.impl.RSS090Parser.parse(RSS090Parser.java:82)
    at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:252)
    at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:179)
The reason why this error occures.
When system reads the RSS, Rome and itunes module parses the rss feed. In Duration field, ITuneParser tries to check whether the duration value includes the ":" character or not. But some duration field doesn't include the ":" character, ITuneParser will raise the ParseException.

Solution
Please download the latest source code from the CVS repository, this problem has already solved. I downloaded the latest source code and compiled it. You can get the jar file I compiled.
URL is http://groups.google.com/group/taapps-sourcecode-libraries/web/itunes-0.4-tafix2.jar
(I recompiled itues-0.4.jar file again at 2008/04/14)

2008年4月4日金曜日

[JAVA>Rome, RSS/XML] When using Rome RSS Reader, ParsingFeedException(Invalid XML) error occured.

References
  1. Rome WebSite
    https://rome.dev.java.net/
Problems
When using Rome RSS Reader, "com.sun.syndication.io.ParsingFeedException: Invalid XML" error sometimes occures.This problem occure while parsing the RSS feed and System raises the ParsingFeedException and NumberFormatException.

Error Stack when occuring this problem
com.sun.syndication.io.ParsingFeedException: Invalid XML
com.sun.syndication.io.ParsingFeedException: Invalid XML
at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:185)
at com.sun.syndication.io.SyndFeedInput.build(SyndFeedInput.java:122)
at RomeTest.main(RomeTest.java:35)
Caused by: java.lang.NumberFormatException: For input string: "09:31"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Long.parseLong(Long.java:427)
at java.lang.Long.parseLong(Long.java:476)
at com.sun.syndication.io.impl.RSS092Parser.parseItem(RSS092Parser.java:107)
at com.sun.syndication.io.impl.RSS093Parser.parseItem(RSS093Parser.java:39)
at com.sun.syndication.io.impl.RSS094Parser.parseItem(RSS094Parser.java:68)
at com.sun.syndication.io.impl.RSS090Parser.parseItems(RSS090Parser.java:263)
at com.sun.syndication.io.impl.RSS090Parser.parseChannel(RSS090Parser.java:178)
at com.sun.syndication.io.impl.RSS091UserlandParser.parseChannel(RSS091UserlandParser.java:84)
at com.sun.syndication.io.impl.RSS092Parser.parseChannel(RSS092Parser.java:49)
at com.sun.syndication.io.impl.RSS094Parser.parseChannel(RSS094Parser.java:45)
at com.sun.syndication.io.impl.RSS090Parser.parse(RSS090Parser.java:82)
at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:252)
at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:179)
Reason why this errr occured
Some RSS data has a enclosure tag in channel/item element as follows and length parameter is defined in enclosure element.
 enclosure url="http://domain.com/file.mp3" length="123456789" type="audio/mpeg"
According to the wikipedia(http://en.wikipedia.org/wiki/RSS_Enclosures) or other web site(http://www.w3schools.com/rss/rss_tag_enclosure.asp), the length parameter means the length (in bytes) of the media file and this parameter is required field. The length parameter should be numerical value, but sometimes it isn't numerical in some RSS feeds.(In some feeds, the value of length parameter includes the comma separator like "123,456,789", or other RSS includes the "09:31" value similar to the timestamp)
In this case, while System tries to convert the length parameter from String object to Long object by using java.lang.Long.parseLong method, System throws the NumberFormatException because the value in the length field has invalid format value.

Solution
  1. Fixed file
    rome-0.9/src/java/com/sun/syndication/io/impl/RSS092Parser.java
  2. Fixed function
    Change as follows in parseItem() function.
  3. Description
    catches the exception while converting the String object to Long for the Length parameter.
    In case of my fix, catches the NumberFormatException and then, tries to remove the comma characters from the parameter and retry the conversion. retry is failed, ignore occuring this exception and doesn't set the value inf Length attribute.
  4. Fixed file
    Jar File: http://groups.google.com/group/taapps-sourcecode-libraries/web/rome-0.9-tafix.jar
    This jar file includes both this fix and http://taapps-javalibs.blogspot.com/2007/06/javarome-rss-when-using-rome-rss-reader.html problem.
  5. Fix Example
        att = e.getAttributeValue("length");//getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
    if (att!=null && att.trim().length()>0) {
    //Starting customization, added by tatsuya anno(taapps@gmail.com)
    //sometimes occures com.sun.syndication.io.ParsingFeedException
    //when converting the attribute value to the Long
    String trimmedAtt=att.trim();
    long enclosureLength=-1;
    try{
    enclosureLength=Long.parseLong(trimmedAtt);
    }catch(Exception exception){
    try{
    //remove commma character before converting
    //string to long.
    trimmedAtt=trimmedAtt.replaceAll(",","");

    //retry conversion from string to long
    //if exception occures, system will ignore it
    //in this case
    enclosureLength=Long.parseLong(trimmedAtt);
    }catch(Exception ignore){}
    }

    //if enclosureLength is greater than 0, set length
    if(enclosureLength>=0){
    enclosure.setLength(enclosureLength);
    }
    //end of customization
    }