Feed
interface provides ‘getEntry’ method, it return the entry by id.
Entry getEntry(String
id)
Retrieves
the first entry in the feed with the given id value.
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.Optional; import org.apache.abdera.Abdera; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.parser.ParseException; import org.apache.abdera.writer.Writer; public class ParserDemo { private static String url = "https://self-learning-java-tutorial.blogspot.com/atom.xml"; private static String id = "tag:blogger.com,1999:blog-3062500619105519975.post-8736482404064859734"; 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(); Entry entry = feed.getEntry(id); /* Print the feed to console */ Writer writer = Abdera.getInstance().getWriterFactory().getWriter("prettyxml"); entry.writeTo(writer, System.out); util.closeStreamAndConnection(); } }
No comments:
Post a Comment