Android Open Source - Jupiter-Broadcasting-Holo Rss Handler






From Project

Back to project page Jupiter-Broadcasting-Holo.

License

The source code is released under:

Copyright (c) 2011 Shane Quigley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project Jupiter-Broadcasting-Holo 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 jupiter.broadcasting.live.holo.parser;
//from ww  w .j  a  v  a  2  s  .  c  o m
import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.Hashtable;
import java.util.Vector;

/*
 * Copyright (c) 2012 Shane Quigley
 *
 * This software is MIT licensed see link for details
 * http://www.opensource.org/licenses/MIT
 *
 * @author Shane Quigley
 * @hacked Adam Szabo
 */

/**
 * Constructor
 */
public class RssHandler extends DefaultHandler {
    private Vector<String> rssTitles;
    private Vector<String> rssLinks;
    private Vector<String> rssEnclosures;
    private Vector<String> thumbnails;
    private Vector<String> duration;
    private Vector<String> times;
    private Vector<String> sum;
    private String linkString;
    private String titleString;
    private int counter = 0;
    private int lineC = 0;
    private int titleC = 0;
    private int page = 0;
    private boolean isLink = false;
    private boolean isTitle = false;
    private boolean isTime = false;
    private boolean isDur = false;
    private boolean isSum = false;
    private boolean ifInsideItem = false;
    private boolean donethis = false;
    private StringBuffer toAdd;

    /**
     * Constructor
     */
    public RssHandler() {
        linkString = "link";
        titleString = "title";
        rssTitles = new Vector<String>();
        rssLinks = new Vector<String>();
        rssEnclosures = new Vector<String>();
        thumbnails = new Vector<String>();
        duration = new Vector<String>();
        times = new Vector<String>();
        sum = new Vector<String>();
        toAdd = new StringBuffer();

    }

    /*
* Constructer that allows a little more control over parsing the feed
* @param title
* @param link
* @param numberOfRecords The max number of item to be parsed.
*/
    public RssHandler(String title, String link, int targetpage) {
        titleString = title;
        linkString = link;
        rssTitles = new Vector<String>();
        rssLinks = new Vector<String>();
        page = targetpage;
        rssEnclosures = new Vector<String>();
        thumbnails = new Vector<String>();
        duration = new Vector<String>();
        times = new Vector<String>();
        sum = new Vector<String>();
        toAdd = new StringBuffer();

    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (ifInsideItem) {
            isLink = qName.equalsIgnoreCase(linkString);
            isTitle = qName.equalsIgnoreCase(titleString);
            isTime = qName.equalsIgnoreCase("pubDate");
            isDur = qName.equalsIgnoreCase("itunes:duration");
            isSum = qName.equalsIgnoreCase("itunes:summary");
            if (!donethis) {
                if (qName.equalsIgnoreCase("enclosure")) {
                    rssEnclosures.addElement(attributes.getValue("url"));
                }
                if (qName.equalsIgnoreCase("media:thumbnail")) {
                    thumbnails.addElement(attributes.getValue("url"));
                }
            }
        } else {
            ifInsideItem = qName.equalsIgnoreCase("item");

        }
        if (isTitle) {
            int maxRecords = 20;
            if (counter < (maxRecords * page + 1) && page > 0) {
                donethis = true;
            } else {
                donethis = false;
            }
            if (counter > maxRecords * (page + 1)) {
                throw new SAXException("Parsing limit reached");
            }
            //Log.e("Counting", "checked this much:" + counter + "on page " + page);
            counter++;
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (!donethis) {
            if (isLink) {
                rssLinks.addElement(toAdd.toString());
            } else if (isTitle) {
                rssTitles.addElement(toAdd.toString());
                titleC++;
            } else if (isDur) {
                duration.addElement(toAdd.toString());
            } else if (isTime) {
                times.addElement(toAdd.toString());
            } else if (isSum) {
                sum.addElement(toAdd.toString());
            }
        }
        isLink = isDur = isTitle = isTime = isSum = false;
        //if we start to store the data of a new episode (titleCount = finished + 1), we check for missing fields
        //this way we will have consistent data format, no matter what is in the rss
        if (titleC - lineC == 2) {

            if (titleC - thumbnails.size() == 2) {
                thumbnails.addElement("X");
            }
            if (titleC - duration.size() == 2) {
                duration.addElement("11:11");
            }
            if (titleC - times.size() == 2) {
                times.addElement("X");
            }
            if (titleC - sum.size() == 2) {
                times.addElement("Content");
            }

            lineC++;
        }
        toAdd = new StringBuffer();
    }

    public void characters(char ch[], int start, int length) throws SAXException {
        if ((isLink || isTitle || isDur || isTime || isSum) && !donethis) {
            toAdd.append(new String(ch, start, length));
        }
    }

    public Hashtable<String, String[]> getTable() {
        Hashtable<String, String[]> output = new Hashtable<String, String[]>();
        for (int i = 0; i < rssTitles.size(); i++) {
            try {
                output.put(rssTitles.elementAt(i), new String[]{rssLinks.elementAt(i),
                        rssEnclosures.elementAt(i), times.elementAt(i), thumbnails.elementAt(i),
                        duration.elementAt(i), sum.elementAt(i)});
            } catch (Exception e) {
                Log.e("Woops", e.getMessage());
                //if something is wrong, pop the title too
                rssTitles.remove(i);
            }
        }
        return output;
    }

    public Vector<String> getTitles() {
        return rssTitles;
    }
}




Java Source Code List

com.google.sample.castcompanionlibrary.cast.BaseCastManager.java
com.google.sample.castcompanionlibrary.cast.CastMediaRouterCallback.java
com.google.sample.castcompanionlibrary.cast.DataCastManager.java
com.google.sample.castcompanionlibrary.cast.DeviceSelectionListener.java
com.google.sample.castcompanionlibrary.cast.VideoCastManager.java
com.google.sample.castcompanionlibrary.cast.callbacks.BaseCastConsumerImpl.java
com.google.sample.castcompanionlibrary.cast.callbacks.DataCastConsumerImpl.java
com.google.sample.castcompanionlibrary.cast.callbacks.IBaseCastConsumer.java
com.google.sample.castcompanionlibrary.cast.callbacks.IDataCastConsumer.java
com.google.sample.castcompanionlibrary.cast.callbacks.IVideoCastConsumer.java
com.google.sample.castcompanionlibrary.cast.callbacks.VideoCastConsumerImpl.java
com.google.sample.castcompanionlibrary.cast.dialog.video.VideoMediaRouteControllerDialogFragment.java
com.google.sample.castcompanionlibrary.cast.dialog.video.VideoMediaRouteControllerDialog.java
com.google.sample.castcompanionlibrary.cast.dialog.video.VideoMediaRouteDialogFactory.java
com.google.sample.castcompanionlibrary.cast.exceptions.CastException.java
com.google.sample.castcompanionlibrary.cast.exceptions.NoConnectionException.java
com.google.sample.castcompanionlibrary.cast.exceptions.OnFailedListener.java
com.google.sample.castcompanionlibrary.cast.exceptions.TransientNetworkDisconnectionException.java
com.google.sample.castcompanionlibrary.cast.player.IMediaAuthListener.java
com.google.sample.castcompanionlibrary.cast.player.IMediaAuthService.java
com.google.sample.castcompanionlibrary.cast.player.IVideoCastController.java
com.google.sample.castcompanionlibrary.cast.player.MediaAuthStatus.java
com.google.sample.castcompanionlibrary.cast.player.OnVideoCastControllerListener.java
com.google.sample.castcompanionlibrary.cast.player.VideoCastControllerActivity.java
com.google.sample.castcompanionlibrary.cast.player.VideoCastControllerFragment.java
com.google.sample.castcompanionlibrary.notification.VideoCastNotificationService.java
com.google.sample.castcompanionlibrary.remotecontrol.RemoteControlClientCompat.java
com.google.sample.castcompanionlibrary.remotecontrol.RemoteControlHelper.java
com.google.sample.castcompanionlibrary.remotecontrol.VideoIntentReceiver.java
com.google.sample.castcompanionlibrary.utils.LogUtils.java
com.google.sample.castcompanionlibrary.utils.Utils.java
com.google.sample.castcompanionlibrary.widgets.IMiniController.java
com.google.sample.castcompanionlibrary.widgets.MiniController.java
jupiter.broadcasting.live.holo.CatalogueAdapter.java
jupiter.broadcasting.live.holo.Catalogue.java
jupiter.broadcasting.live.holo.EpisodeAdapter.java
jupiter.broadcasting.live.holo.EpisodeListFragment.java
jupiter.broadcasting.live.holo.Home.java
jupiter.broadcasting.live.holo.JBApplication.java
jupiter.broadcasting.live.holo.JBPlayer.java
jupiter.broadcasting.live.holo.MySpinnerAdapter.java
jupiter.broadcasting.live.holo.SettingsActivity.java
jupiter.broadcasting.live.holo.ShowActivity.java
jupiter.broadcasting.live.holo.ShowNotesView.java
jupiter.broadcasting.live.holo.list.BitmapLruCache.java
jupiter.broadcasting.live.holo.list.FadeImageView.java
jupiter.broadcasting.live.holo.parser.RssHandler.java
jupiter.broadcasting.live.holo.parser.SaxRssParser.java