package com.commafeed.backend.feeds;

import java.io.StringReader;
import java.util.Calendar;
import java.util.List;

import javax.ejb.Stateless;

import com.commafeed.model.Feed;
import com.commafeed.model.FeedEntry;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;

@Stateless
public class FeedParser {

	@SuppressWarnings("unchecked")
	public Feed parse(String feedUrl, String xml) throws FeedException {

		Feed feed = new Feed();
		feed.setUrl(feedUrl);
		feed.setLastUpdated(Calendar.getInstance().getTime());

		try {
			SyndFeed rss = new SyndFeedInput().build(new StringReader(xml));

			List<SyndEntry> items = rss.getEntries();
			for (SyndEntry item : items) {
				FeedEntry entry = new FeedEntry();
				entry.setGuid(item.getUri());
				entry.setTitle(item.getTitle());
				entry.setContent(item.getDescription() == null ? null : item
						.getDescription().getValue());
				entry.setUrl(item.getLink());
				entry.setUpdated(item.getUpdatedDate() != null ? item
						.getUpdatedDate() : item.getPublishedDate());

				feed.getEntries().add(entry);
			}
		} catch (Exception e) {
			throw new FeedException("Could not parse feed : " + e.getMessage(),
					e);
		}
		return feed;
	}

}

@@ -159,71 +159,303 @@
 ort 
-com.commafeed.model.Feed;%0D%0Aimport com.commafeed.model.FeedEntry
+org.apache.commons.lang.StringUtils;%0D%0Aimport org.apache.commons.lang.SystemUtils;%0D%0A%0D%0Aimport com.commafeed.model.Feed;%0D%0Aimport com.commafeed.model.FeedEntry;%0D%0Aimport com.google.common.base.Function;%0D%0Aimport com.google.common.collect.Collections2;%0D%0Aimport com.sun.syndication.feed.synd.SyndContent
 ;%0D%0Ai
@@ -1202,86 +1202,23 @@
 ent(
-item.getDescription() == null ? null : item%0D%0A%09%09%09%09%09%09.getDescription().getValue(
+getContent(item
 ));%0D
@@ -1544,11 +1544,541 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09@SuppressWarnings(%22unchecked%22)%0D%0A%09private String getContent(SyndEntry item) %7B%0D%0A%09%09String content = null;%0D%0A%09%09if (item.getContents().isEmpty()) %7B%0D%0A%09%09%09content = item.getDescription() == null ? null : item%0D%0A%09%09%09%09%09.getDescription().getValue();%0D%0A%09%09%7D else %7B%0D%0A%09%09%09content = StringUtils.join(Collections2.transform(%0D%0A%09%09%09%09%09item.getContents(), new Function%3CSyndContent, String%3E() %7B%0D%0A%09%09%09%09%09%09public String apply(SyndContent content) %7B%0D%0A%09%09%09%09%09%09%09return content.getValue();%0D%0A%09%09%09%09%09%09%7D%0D%0A%09%09%09%09%09%7D), SystemUtils.LINE_SEPARATOR);%0D%0A%09%09%7D%0D%0A%09%09return content;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

use content instead of description if available