Android Open Source - AURHelperDroid Rss News Handler






From Project

Back to project page AURHelperDroid.

License

The source code is released under:

Apache License

If you think the Android project AURHelperDroid 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

package com.stomachion.aurhelperdroid.parser;
/*from w  w w  .  jav a  2  s  . c  om*/
import android.util.Log;
import com.stomachion.aurhelperdroid.logic.NewsItem;
import com.stomachion.aurhelperdroid.utils.CommonUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * User: Pedro Veloso
 */
public final class RssNewsHandler extends DefaultHandler {

    private StringBuilder mStrBuilder;

    // fill this list while parsing
    private ArrayList<NewsItem> newsItems;

    private NewsItem currentItem;

    // Parsing intermediary state flags
    private boolean parsingTitle;
    private boolean parsingURL;
    private boolean parsingPublishDate;
    private boolean parsingDescription;
    private boolean parsingAuthor;

    public RssNewsHandler() {
        newsItems = new ArrayList<NewsItem>();
    }

    public ArrayList<NewsItem> getItems() {
        return newsItems;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("item".equals(qName)) {
            currentItem = new NewsItem();
        } else if ("title".equals(qName)) {
            mStrBuilder = new StringBuilder("");
            parsingTitle = true;
        } else if ("link".equals(qName)) {
            parsingURL = true;
        } else if ("description".equals(qName)) {
            mStrBuilder = new StringBuilder("");
            parsingDescription = true;
        } else if ("author".equals(qName)) {
            mStrBuilder = new StringBuilder("");
            parsingAuthor = true;
        } else if ("pubDate".equals(qName)) {
            parsingPublishDate = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("item".equals(qName)) {
            newsItems.add(currentItem);
            currentItem = null;
        } else if ("title".equals(qName)) {
            //should check this because there is also the title outside the Item
            if (currentItem != null) {
                currentItem.setTitle(mStrBuilder.toString());
            }
            parsingTitle = false;

        } else if ("link".equals(qName)) {
            parsingURL = false;
        } else if ("description".equals(qName)) {
            if (currentItem != null) {
                currentItem.setDescription(mStrBuilder.toString());
            }
            parsingDescription = false;
        } else if ("author".equals(qName)) {
            parsingAuthor = false;
        } else if ("pubDate".equals(qName)) {
            parsingPublishDate = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        // if is not currentItem it is because we caught an element that validated outside of the <item>, so ignore it
        if (currentItem != null) {
            if (parsingTitle) {

                mStrBuilder.append(new String(ch, start, length));
            } else if (parsingURL) {
                currentItem.setUrl(new String(ch, start, length));
                parsingURL = false;
            } else if (parsingDescription) {
                mStrBuilder.append(new String(ch, start, length));
            } else if (parsingPublishDate) {
                parseFormattedDate(new String(ch, start, length));
                parsingPublishDate = false;
            } else if (parsingAuthor) {
                currentItem.setAuthor(new String(ch, start, length));
                parsingAuthor = false;
            }
        }
    }

    /**
     * Treats and adds the publish date on RSS item
     *
     * @param formattedDate date formatted like this : "Sun, 21 Apr 2013 16:05:44 +0100"
     */
    private void parseFormattedDate(String formattedDate) {
        formattedDate = formattedDate.substring(5, 22);
        Date dateAux;
        // use Locale.US because months are named in their format
        DateFormat formatterDate = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.US);
        try {
            dateAux = formatterDate.parse(formattedDate);
            GregorianCalendar tmpDate = new GregorianCalendar();
            tmpDate.setTimeInMillis(dateAux.getTime());
            currentItem.setPublishDate(tmpDate);
        } catch (ParseException e) {
            CommonUtils.debugFunc("Failed to parse date. E.: " + e.getMessage(), Log.ERROR);
        }
    }
}




Java Source Code List

com.stomachion.aurhelperdroid.activities.MainActivity.java
com.stomachion.aurhelperdroid.activities.PreferencesActivity.java
com.stomachion.aurhelperdroid.adapters.NewsAdapter.java
com.stomachion.aurhelperdroid.fallbacksupport.ActivityHostFragment.java
com.stomachion.aurhelperdroid.fallbacksupport.LocalActivityManagerFragment.java
com.stomachion.aurhelperdroid.fragments.AboutFrag.java
com.stomachion.aurhelperdroid.fragments.NewsRSSFrag.java
com.stomachion.aurhelperdroid.fragments.PreferencesFrag.java
com.stomachion.aurhelperdroid.fragments.SearchFrag.java
com.stomachion.aurhelperdroid.logic.NewsItem.java
com.stomachion.aurhelperdroid.network.InternetState.java
com.stomachion.aurhelperdroid.parser.MainRssParser.java
com.stomachion.aurhelperdroid.parser.RssNewsHandler.java
com.stomachion.aurhelperdroid.utils.CommonUtils.java
com.stomachion.aurhelperdroid.utils.Constants.java
com.stomachion.aurhelperdroid.utils.Prefs.java