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.JAXBElement; import javax.xml.bind.Unmarshaller; import javax.xml.datatype.XMLGregorianCalendar; 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.RssHttpClient; import fr.mael.microrss.util.Tools; import fr.mael.microrss.util.XMLUtil; import fr.mael.microrss.xml.atom.EntryType; import fr.mael.microrss.xml.atom.FeedType; import fr.mael.microrss.xml.atom.PersonType; @SuppressWarnings("rawtypes") @Component public class AtomFeedParser implements FeedParser { private static Logger LOG = LoggerFactory.getLogger(AtomFeedParser.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.atom"); Unmarshaller unmarshaller = jc.createUnmarshaller(); HttpGet get = new HttpGet(feed.getUrl()); HttpResponse response = client.execute(get); try { Object o = unmarshaller.unmarshal(response.getEntity().getContent()); if (o instanceof JAXBElement) { JAXBElement element = (JAXBElement) o; FeedType feedType = (FeedType) element.getValue(); articles = readEntries(feedType.getAuthorOrCategoryOrContributor(), feed, feedType); } 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.atom"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Object o = unmarshaller.unmarshal(new URL(feed.getUrl()).openStream()); if (o instanceof JAXBElement) { JAXBElement element = (JAXBElement) o; FeedType feedType = (FeedType) element.getValue(); String link = XMLUtil.readLinkType("link", feedType.getAuthorOrCategoryOrContributor()); feed.setIcon(Tools.getImage(Tools.getBaseUrl(link) + "/favicon.ico")); feed.setTitle(XMLUtil.readTextType("title", feedType.getAuthorOrCategoryOrContributor())); } return feed; } private List<Article> readEntries(List<Object> objects, Feed feed, FeedType feedType) { List<Article> articles = new ArrayList<Article>(); for (Object o : objects) { if (o instanceof JAXBElement) { JAXBElement element = (JAXBElement) o; if (element.getValue() instanceof EntryType) { EntryType entry = (EntryType) element.getValue(); Article article = readEntry(entry, feedType); article.getFeeds().add(feed); articles.add(article); } } } return articles; } private Article readEntry(EntryType entry, FeedType feedType) { Article article = new Article(); // String author = XMLUtil.readComplexProperty("author", entry.getAuthorOrCategoryOrContent(), "getValue"); // if (StringUtils.isEmpty(author)) { String author = readFeedAuthor(feedType); // } article.setAuthor(author); article.setGuid(XMLUtil.readComplexProperty("id", entry.getAuthorOrCategoryOrContent(), "getValue")); article.setContent(XMLUtil.readContentType("content", entry.getAuthorOrCategoryOrContent())); article.setOverview(XMLUtil.readTextType("summary", entry.getAuthorOrCategoryOrContent())); if (article.getContent() == null && article.getOverview() != null) { article.setContent(article.getOverview()); } else if (article.getContent() == null && article.getOverview() == null) { LOG.error("article with guid " + article.getGuid() + " has empty content and summary"); article.setContent(""); } article.setTitle(XMLUtil.readTextType("title", entry.getAuthorOrCategoryOrContent())); article.setUrl(XMLUtil.readLinkType("link", entry.getAuthorOrCategoryOrContent())); XMLGregorianCalendar published = XMLUtil.readDateTimeType("published", entry.getAuthorOrCategoryOrContent()); if (published == null) { published = XMLUtil.readDateTimeType("updated", entry.getAuthorOrCategoryOrContent()); } article.setCreated(published.toGregorianCalendar().getTime()); return article; } private String readFeedAuthor(FeedType feedType) { for (Object o : feedType.getAuthorOrCategoryOrContributor()) { if (o instanceof JAXBElement) { JAXBElement element = (JAXBElement) o; if (element.getValue() instanceof PersonType) { PersonType person = (PersonType) element.getValue(); return XMLUtil.readProperty("name", person.getNameOrUriOrEmail()); } } } return null; } }