Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Abdera: Get all the entries in a feed

Feed interface provides getEntries method, it return all the entries of the Feed Object.

Following application get all the entries from the url ‘http://self-learning-java-tutorial.blogspot.in/atom.xml’ and print some meta information of all the entries associated with given feed.

Feedutil.java
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;
import java.util.zip.GZIPInputStream;

import org.apache.abdera.Abdera;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.Parser;

/**
* Utility class to parse Atom feed.
*
* Usage :
*

    *
  • Instantiate FeedUtil with atom document url. Call the getFeed method

  • *
  • to get the feed document associated with url.

  • *
    *
    * @author Hari Krishna Gurram
    *
    */
    public class FeedUtil {
    private String url;
    private InputStream in = null;
    private Feed feed = null;
    HttpURLConnection conn = null;

    public FeedUtil(String url) {
    this.url = url;
    }

    /**
    *
    * @param url
    * @return Optional.empty if url is null (or) unable to parse the url, else
    * return the Document object.
    */
    private OptionalDocumentFeed>> getDocument() {
    if (url == null) {
    return Optional.empty();
    }

    try {
    URL urlTemp = new URL(url);

    conn = (HttpURLConnection) urlTemp.openConnection();
    conn.setRequestProperty("Accept-Encoding", "gzip");
    conn.connect();

    String contentEncoding = conn.getContentEncoding();
    boolean isGzip = contentEncoding != null && contentEncoding.contains("gzip");

    in = !isGzip ? conn.getInputStream() : new GZIPInputStream(conn.getInputStream());
    Abdera abdera = Abdera.getInstance();
    Parser parser = abdera.getParser();
    DocumentFeed> document = parser.parse(in, url.toString());
    return Optional.of(document);

    } catch (Exception e) {
    e.printStackTrace();
    }

    return Optional.empty();
    }

    /**
    *
    * @return the Feed object associated with given {@link url}. In any case if
    * the feed creation is failed, then it return Optional.empty().
    */

    public synchronized OptionalFeed> getFeed() {
    if (feed != null) {
    return Optional.of(feed);
    }

    OptionalDocumentFeed>> documentOpt = getDocument();

    if (!documentOpt.isPresent()) {
    return Optional.empty();
    }

    DocumentFeed> document = documentOpt.get();

    feed = document.getRoot();
    return Optional.of(feed);
    }

    public void closeStreamAndConnection() {
    if (in != null) {
    try {
    in.close();
    System.out.println("Input Stream is closed");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }

    if (conn != null) {
    conn.disconnect();
    System.out.println("Input stream is disconnected");
    }
    }

    /**
    * This is just a safety check, if user forget to close the stram, it will
    * be closed once object is out of scope.
    */
    @Override
    public void finalize() {
    closeStreamAndConnection();
    }
    }

    ParserDemo.java
    import java.io.IOException;
    import java.util.Date;
    import java.util.List;
    import java.util.Optional;

    import org.apache.abdera.model.Content.Type;
    import org.apache.abdera.model.Entry;
    import org.apache.abdera.model.Feed;
    import org.apache.abdera.model.Person;
    import org.apache.abdera.parser.ParseException;

    public class ParserDemo {
    private static String url = "http://self-learning-java-tutorial.blogspot.in/atom.xml";

    public static void main(String args[]) throws ParseException, IOException {
    FeedUtil util = new FeedUtil(url);

    OptionalFeed> feedOpt = util.getFeed();

    if (!feedOpt.isPresent()) {
    System.out.println("No feed present");
    return;
    }

    Feed feed = feedOpt.get();

    ListEntry> entries = feed.getEntries();
    for (Entry entry : entries) {
    System.out.println("*************************************");
    String title = entry.getTitle();
    Date publishedDate = entry.getPublished();
    Type contentType = entry.getContentType();

    Person author = entry.getAuthor();
    String authorName = author.getName();
    String authorMail = author.getEmail();

    System.out.println("title : " + title);
    System.out.println("Published Date : " + publishedDate);
    System.out.println("Content Type : " + contentType);

    System.out.println("Author Name : " + authorName);
    System.out.println("Author Mail : " + authorMail);
    System.out.println("*************************************\n\n");
    }

    util.closeStreamAndConnection();;

    }
    }


    Sample Output
    *************************************
    title : SWT: Focus listeners
    Published Date : Fri Dec 09 20:16:00 IST 2016
    Content Type : HTML
    Author Name : hari krishna
    Author Mail : [email protected]
    *************************************


    *************************************
    title : SWT: Text listeners
    Published Date : Fri Dec 09 08:35:00 IST 2016
    Content Type : HTML
    Author Name : hari krishna
    Author Mail : [email protected]
    *************************************


    *************************************
    title : SWT: Mouse listeners
    Published Date : Wed Dec 07 22:07:00 IST 2016
    Content Type : HTML
    Author Name : hari krishna
    Author Mail : [email protected]
    *************************************


    Input Stream is closed
    Input stream is disconnected




    Previous                                                 Next                                                 Home


    This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

    Share the post

    Abdera: Get all the entries in a feed

    ×

    Subscribe to Java Tutorial : Blog To Learn Java Programming

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×