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

Abdera: Atom Source element

If an atom entry copied from one feed to another, then source element is used to preserve original entry metadata.

If you got the feed instance, then you can call ‘getAsSource’ method to get the Source instance from feed.

Example
xmlns="http://www.w3.org/2005/Atom" xml:base="http://self-learning-java-tutorial.blogspot.in/atom.xml">
tag:blogger.com,1999:blog-3062500619105519975
2016-12-17T03:22:05.420-08:00
<span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text"</span><span style="color: #007700;">></span>java tutorial : Blog to learn java programming<span style="color: #007700;"></span>
type="html"/>
rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://self-learning-java-tutorial.blogspot.com/feeds/posts/default"/>
rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3062500619105519975/posts/default?alt=atom"/>
rel="alternate" type="text/html" href="http://self-learning-java-tutorial.blogspot.com/"/>
rel="hub" href="http://pubsubhubbub.appspot.com/"/>
rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/3062500619105519975/posts/default?alt=atom&start-index=26&max-results=25"/>

hari krishna
[email protected]
xmlns:gd="http://schemas.google.com/g/2005" rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-6kwxUAiFEDA/AAAAAAAAAAI/AAAAAAAADTE/LmWjnEaeU5c/s512-c/photo.jpg"/>

version="7.00" uri="http://www.blogger.com">Blogger
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">3834
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25



Following application get the source element from the feed generated by atom document ‘http://self-learning-java-tutorial.blogspot.in/atom.xml’.

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();
    }
    }


    ParseDemo.java
    import java.io.IOException;
    import java.util.Optional;

    import org.apache.abdera.Abdera;
    import org.apache.abdera.model.Feed;
    import org.apache.abdera.model.Source;
    import org.apache.abdera.parser.ParseException;
    import org.apache.abdera.writer.Writer;

    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();

    Source source = feed.getAsSource();

    Writer writer = Abdera.getInstance().getWriterFactory().getWriter("prettyxml");
    source.writeTo(writer, System.out);

    util.closeStreamAndConnection();
    ;

    }
    }





    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: Atom Source element

    ×

    Subscribe to Java Tutorial : Blog To Learn Java Programming

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×