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

Abdera: Sort the entries by edited date

Feed interface provides ‘sortEntriesByEdited’ method to sort the entries by edited date.

Feed sortEntriesByEdited(boolean new_first)
If new_first is set to true, then the latest edited entries comes first, if new_first is set to false, then the latest edited entries comes last.

Following is the complete working application.

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("\nInput Stream is closed");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }

    if (conn != null) {
    conn.disconnect();
    //System.out.println("Connection 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.Entry;
    import org.apache.abdera.model.Feed;
    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();

    feed.sortEntriesByEdited(true);

    ListEntry> entries = feed.getEntries();
    for (Entry entry : entries) {
    String title = entry.getTitle();
    Date editedDate = entry.getUpdated();

    System.out.println("title : " + title + " editedDate : " + editedDate);

    }

    System.out.println("\n\n\n");

    feed.sortEntriesByEdited(false);

    entries = feed.getEntries();
    for (Entry entry : entries) {
    String title = entry.getTitle();
    Date editedDate = entry.getUpdated();

    System.out.println("title : " + title + " editedDate : " + editedDate);

    }

    util.closeStreamAndConnection();

    }
    }


    Sample Output
    title : SWT: Slider editedDate : Sat Dec 17 08:39:42 IST 2016
    title : SWT: Scale widget editedDate : Sat Dec 17 08:38:55 IST 2016
    title : Apache Commons CLI Tutorial editedDate : Fri Dec 16 22:00:00 IST 2016
    title : Commons CLI: Parsing command line options editedDate : Fri Dec 16 21:24:04 IST 2016
    title : Commons CLI: OptionGroup: group mutually Exclusive options editedDate : Fri Dec 16 21:23:39 IST 2016
    title : Commons CLI: Print command line argument in the order you inserted editedDate : Fri Dec 16 21:21:35 IST 2016
    title : Commons-CLI: Setting the compulsory options editedDate : Fri Dec 16 21:13:59 IST 2016
    title : Commons CLI: Add multiple arguments to an option editedDate : Fri Dec 16 21:10:33 IST 2016
    title : Commons CLI: Boolean options editedDate : Fri Dec 16 21:07:02 IST 2016
    title : Commons CLI : Options class editedDate : Fri Dec 16 21:03:47 IST 2016
    title : Commons-CLI Define options using builder editedDate : Fri Dec 16 20:59:50 IST 2016
    title : Commons-CLI: Set the display name for the argument editedDate : Fri Dec 16 20:56:15 IST 2016
    title : Commons CLI: Option: Specify single command line option editedDate : Fri Dec 16 19:48:44 IST 2016
    title : SWT: TabFolder widget editedDate : Thu Dec 15 10:47:20 IST 2016
    title : zeroMQ C# Tutorial editedDate : Wed Dec 14 21:12:01 IST 2016
    title : C#: Get the version of zeroMQ used in the application editedDate : Wed Dec 14 21:08:34 IST 2016
    title : C#: zeroMQ: Publish-subscribe model sockets editedDate : Wed Dec 14 21:07:09 IST 2016
    title : zeroMQ: Hello World Application editedDate : Wed Dec 14 20:56:36 IST 2016
    title : SWT: Table widget editedDate : Tue Dec 13 17:49:43 IST 2016
    title : SWT: Focus listeners editedDate : Mon Dec 12 19:54:58 IST 2016
    title : Client in C# and Server in Java editedDate : Sat Dec 10 14:41:59 IST 2016
    title : Two way communication in C# client and socket editedDate : Sat Dec 10 13:06:51 IST 2016
    title : C#: Socket Programming editedDate : Sat Dec 10 13:02:13 IST 2016
    title : SWT: Text listeners editedDate : Fri Dec 09 20:21:59 IST 2016
    title : SWT: Mouse listeners editedDate : Fri Dec 09 08:36:39 IST 2016




    title : SWT: Mouse listeners editedDate : Fri Dec 09 08:36:39 IST 2016
    title : SWT: Text listeners editedDate : Fri Dec 09 20:21:59 IST 2016
    title : C#: Socket Programming editedDate : Sat Dec 10 13:02:13 IST 2016
    title : Two way communication in C# client and socket editedDate : Sat Dec 10 13:06:51 IST 2016
    title : Client in C# and Server in Java editedDate : Sat Dec 10 14:41:59 IST 2016
    title : SWT: Focus listeners editedDate : Mon Dec 12 19:54:58 IST 2016
    title : SWT: Table widget editedDate : Tue Dec 13 17:49:43 IST 2016
    title : zeroMQ: Hello World Application editedDate : Wed Dec 14 20:56:36 IST 2016
    title : C#: zeroMQ: Publish-subscribe model sockets editedDate : Wed Dec 14 21:07:09 IST 2016
    title : C#: Get the version of zeroMQ used in the application editedDate : Wed Dec 14 21:08:34 IST 2016
    title : zeroMQ C# Tutorial editedDate : Wed Dec 14 21:12:01 IST 2016
    title : SWT: TabFolder widget editedDate : Thu Dec 15 10:47:20 IST 2016
    title : Commons CLI: Option: Specify single command line option editedDate : Fri Dec 16 19:48:44 IST 2016
    title : Commons-CLI: Set the display name for the argument editedDate : Fri Dec 16 20:56:15 IST 2016
    title : Commons-CLI Define options using builder editedDate : Fri Dec 16 20:59:50 IST 2016
    title : Commons CLI : Options class editedDate : Fri Dec 16 21:03:47 IST 2016
    title : Commons CLI: Boolean options editedDate : Fri Dec 16 21:07:02 IST 2016
    title : Commons CLI: Add multiple arguments to an option editedDate : Fri Dec 16 21:10:33 IST 2016
    title : Commons-CLI: Setting the compulsory options editedDate : Fri Dec 16 21:13:59 IST 2016
    title : Commons CLI: Print command line argument in the order you inserted editedDate : Fri Dec 16 21:21:35 IST 2016
    title : Commons CLI: OptionGroup: group mutually Exclusive options editedDate : Fri Dec 16 21:23:39 IST 2016
    title : Commons CLI: Parsing command line options editedDate : Fri Dec 16 21:24:04 IST 2016
    title : Apache Commons CLI Tutorial editedDate : Fri Dec 16 22:00:00 IST 2016
    title : SWT: Scale widget editedDate : Sat Dec 17 08:38:55 IST 2016
    title : SWT: Slider editedDate : Sat Dec 17 08:39:42 IST 2016



    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: Sort the entries by edited date

    ×

    Subscribe to Java Tutorial : Blog To Learn Java Programming

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×