Tuesday, 6 June 2017

Abdera: Sort the entries by updated date

Feed interface provides sortEntriesByUpdated method, by calling this function you can sort the entries in their updated date.

Feed sortEntriesByUpdated(boolean new_first);
If the flag new_first is set to true, then all the entries are sorted from newly updated date to old updated date. if the new_first flag is set to false, then the entries are sorted from old to new.

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 :
 * <ul>
 * <li>Instantiate FeedUtil with atom document url. Call the getFeed method</li>
 * <li>to get the feed document associated with url.</li>
 * </ul>
 * 
 * @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 Optional<Document<Feed>> 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();
   Document<Feed> 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 Optional<Feed> getFeed() {
  if (feed != null) {
   return Optional.of(feed);
  }

  Optional<Document<Feed>> documentOpt = getDocument();

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

  Document<Feed> 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 = "https://self-learning-java-tutorial.blogspot.com/atom.xml";

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

  Optional<Feed> feedOpt = util.getFeed();

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

  Feed feed = feedOpt.get();

  feed.sortEntriesByUpdated(true);

  List<Entry> entries = feed.getEntries();
  for (Entry entry : entries) {
   String title = entry.getTitle();
   Date updatedDate = entry.getUpdated();

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

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

  feed.sortEntriesByUpdated(false);

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

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

  }
  
  util.closeStreamAndConnection();

 }
}


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




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





Previous                                                 Next                                                 Home

No comments:

Post a Comment