Java tutorial
/* Copyright 2013 Mael Le Guvel This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details. */ package fr.mael.microrss.xml; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import fr.mael.microrss.domain.Article; import fr.mael.microrss.domain.Feed; import fr.mael.microrss.util.DateUtil; import fr.mael.microrss.util.RssHttpClient; import fr.mael.microrss.util.Tools; import fr.mael.microrss.util.XMLUtil; import fr.mael.microrss.xml.rss.Rss; import fr.mael.microrss.xml.rss.RssItem; @Component public class RSSFeedParser implements FeedParser { private static Logger LOG = LoggerFactory.getLogger(RSSFeedParser.class); @Autowired private RssHttpClient client; @Override public List<Article> readArticles(Feed feed) throws Exception { List<Article> articles = new ArrayList<Article>(); JAXBContext jc = JAXBContext.newInstance("fr.mael.microrss.xml.rss"); Unmarshaller unmarshaller = jc.createUnmarshaller(); HttpGet get = new HttpGet(feed.getUrl()); HttpResponse response = client.execute(get); try { Rss rss = (Rss) unmarshaller.unmarshal(response.getEntity().getContent()); for (RssItem item : rss.getChannel().getItem()) { Article article = readArticle(item); article.getFeeds().add(feed); articles.add(article); } return articles; } catch (Exception e) { EntityUtils.consume(response.getEntity()); throw new Exception(e); } } @Override public Feed parseFeedInfo(Feed feed) throws Exception { JAXBContext jc = JAXBContext.newInstance("fr.mael.microrss.xml.rss"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Rss rss = (Rss) unmarshaller.unmarshal(new URL(feed.getUrl()).openStream()); String link = XMLUtil.readProperty("link", rss.getChannel().getTitleOrLinkOrDescription()); feed.setIcon(Tools.getImage(Tools.getBaseUrl(link) + "/favicon.ico")); feed.setTitle(XMLUtil.readProperty("title", rss.getChannel().getTitleOrLinkOrDescription())); return feed; } private Article readArticle(RssItem item) { Article article = new Article(); article.setTitle(XMLUtil.readProperty("title", item.getTitleOrDescriptionOrLink())); article.setGuid(XMLUtil.readComplexProperty("guid", item.getTitleOrDescriptionOrLink(), "getValue")); article.setUrl(XMLUtil.readProperty("link", item.getTitleOrDescriptionOrLink())); article.setAuthor(XMLUtil.readProperty("dc:creator", item.getTitleOrDescriptionOrLink())); String content = XMLUtil.readProperty("content:encoded", item.getTitleOrDescriptionOrLink()); String description = XMLUtil.readProperty("description", item.getTitleOrDescriptionOrLink()); if (StringUtils.isEmpty(content)) { content = description; } String pubdate = XMLUtil.readProperty("pubDate", item.getTitleOrDescriptionOrLink()); if (pubdate == null || "".equals(pubdate)) { pubdate = XMLUtil.readProperty("dc:date", item.getTitleOrDescriptionOrLink()); } article.setCreated(DateUtil.parseRSSDate(pubdate)); article.setContent(content); article.setOverview(description); return article; } }