Android Open Source - openaccessbutton Rss Parser






From Project

Back to project page openaccessbutton.

License

The source code is released under:

MIT License

If you think the Android project openaccessbutton listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Open Access Button
 *// w  w w .j  a  va  2 s.  co m
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */
package org.openaccessbutton.openaccessbutton.blog;

import android.util.Xml;

import org.apache.commons.lang3.StringEscapeUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Parse the blog RSS (XML) feed into a List of Posts.
 * Based on the examples given in the Android documentation at
 * http://developer.android.com/training/basics/network-ops/xml.html.
 */
public class RssParser {
    // Namespaces aren't needed at this point
    private static final String ns = null;

    /**
     * Parse an InputStream containing the RSS feed.
     * @param in InputStream containing blog RSS feed
     * @return List of Posts
     * @throws XmlPullParserException
     * @throws IOException
     */
    public List<Post> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }

    /**
     * Parse the entire feed.
     */
    private List<Post> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        // Everything's contained in an <rss> tag
        parser.require(XmlPullParser.START_TAG, ns, "rss");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // All posts are contained in a parent <channel> tag
            if (name.equals("channel")) {
                // There's only one channel, so we can simply return Posts from the first channel
                return readChannel(parser);
            } else {
                skip(parser);
            }
        }
        return null;
    }

    /**
     * Parse a <channel> tag.
     */
    private List<Post> readChannel(XmlPullParser parser) throws XmlPullParserException, IOException {
        // To store parsed posts in
        List<Post> posts = new ArrayList<Post>();

        parser.require(XmlPullParser.START_TAG, ns, "channel");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Posts are <item> tags
            if (name.equals("item")) {
                posts.add(readPost(parser));
            } else {
                skip(parser);
            }
        }

        return posts;
    }

    /**
     * Parse an individual post from a <post> tag.
     */
    private Post readPost(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "item");

        // Post properties
        String title = null;
        String description = null;
        Date date = null;
        String creator = null;
        String content = null;
        String link = null;

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Read post properties
            if (name.equals("title")) {
                title = readTitle(parser);
            } else if (name.equals("description")) {
                description = readDescription(parser);
            } else if (name.equals("pubDate")) {
                date = readDate(parser);
            } else if (name.equals("dc:creator")) {
                creator = readCreator(parser);
            } else if (name.equals("content:encoded")) {
                content = readContent(parser);
            } else if (name.equals("link")) {
                link = readLink(parser);
            } else {
                skip(parser);
            }
        }
        return new Post(title, description, date, creator, content, link);
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }

    /**
     * Extract text from a <title> tag
     */
    private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "title");
        String title = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "title");
        return title;
    }

    /**
     * Extract text from a <pubDate> tag
     */
    private Date readDate(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "pubDate");

        String dateString = readText(parser);
        SimpleDateFormat dateParser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZZ", Locale.ENGLISH);
        Date date = null;

        try {
            date = dateParser.parse(dateString);
        } catch (Exception e) {
        }

        return date;
    }
    /**
     * Extract text from a <dc:creator> tag
     */
    private String readCreator(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "dc:creator");
        String title = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "dc:creator");
        return title;
    }

    /**
     * Extract and format text from a <description> tag
     */
    private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "description");
        // Descriptions end in " [...]" (but with unicode ellipses) and then a 1x1px image so we
        // remove those
        String description = readText(parser).replaceAll("(?m) \\[\u2026\\]<img.*/>$", "");
        parser.require(XmlPullParser.END_TAG, ns, "description");
        return description + "...";
    }

    /**
     * Extract and format text from a <content:encoded> tag
     */
    private String readContent(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "content:encoded");
        // Descriptions end in " [...]" (but with unicode ellipses) and then a 1x1px image so we
        // remove those
        String content = readText(parser).replaceAll("<img.*/>$", "");
        parser.require(XmlPullParser.END_TAG, ns, "content:encoded");
        return content;
    }

    /**
     * Extract text from a <link> tag
     */
    private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "link");
        String link = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "link");
        return link;
    }

    /**
     * Extract text from a tag
     */
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        // Unicode characters are escaped in the XML, so we unescape those
        return StringEscapeUtils.unescapeXml(result);
    }
}




Java Source Code List

com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.openaccessbutton.openaccessbutton.ApplicationTest.java
org.openaccessbutton.openaccessbutton.MainActivity.java
org.openaccessbutton.openaccessbutton.NavigationItem.java
org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
org.openaccessbutton.openaccessbutton.OnFragmentNeededListener.java
org.openaccessbutton.openaccessbutton.OnShareIntentInterface.java
org.openaccessbutton.openaccessbutton.about.AboutActivity.java
org.openaccessbutton.openaccessbutton.advocacy.AdvocacyFragment.java
org.openaccessbutton.openaccessbutton.advocacy.FaqView.java
org.openaccessbutton.openaccessbutton.advocacy.Question.java
org.openaccessbutton.openaccessbutton.advocacy.QuestionsActivity.java
org.openaccessbutton.openaccessbutton.advocacy.XmlParser.java
org.openaccessbutton.openaccessbutton.api.API.java
org.openaccessbutton.openaccessbutton.blog.BlogAdapter.java
org.openaccessbutton.openaccessbutton.blog.BlogDetailsFragment.java
org.openaccessbutton.openaccessbutton.blog.BlogFragment.java
org.openaccessbutton.openaccessbutton.blog.DownloadTask.java
org.openaccessbutton.openaccessbutton.blog.Post.java
org.openaccessbutton.openaccessbutton.blog.RssParser.java
org.openaccessbutton.openaccessbutton.browser.BrowserFragment.java
org.openaccessbutton.openaccessbutton.browser.ScrollingWebView.java
org.openaccessbutton.openaccessbutton.button.ButtonSubmitActivity.java
org.openaccessbutton.openaccessbutton.intro.ForgotPasswordActivity.java
org.openaccessbutton.openaccessbutton.intro.IntroActivity.java
org.openaccessbutton.openaccessbutton.intro.LaunchActivity.java
org.openaccessbutton.openaccessbutton.intro.ScreenSlidePageFragment.java
org.openaccessbutton.openaccessbutton.intro.SigninActivity.java
org.openaccessbutton.openaccessbutton.intro.SigninButtonClickListener.java
org.openaccessbutton.openaccessbutton.intro.SignupActivity.java
org.openaccessbutton.openaccessbutton.intro.SignupEmailButtonClickListener.java
org.openaccessbutton.openaccessbutton.map.Item.java
org.openaccessbutton.openaccessbutton.map.MapFragment.java
org.openaccessbutton.openaccessbutton.menu.MenuActivity.java
org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity.java
org.openaccessbutton.openaccessbutton.push.PushReceiver.java
org.openaccessbutton.openaccessbutton.push.Push.java