package jupiter.broadcasting.live.tv.parser;

import java.util.Hashtable;
import java.util.Vector;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
/*
 * Copyright (c) 2012 Shane Quigley
 *
 * This software is MIT licensed see link for details
 * http://www.opensource.org/licenses/MIT
 * 
 * @author Shane Quigley
 */
public class RssHandler extends DefaultHandler{
    private Vector<String> rssTitles;
    private Vector<String> rssLinks;
    private Vector<String> rssEnclosures;
    private String linkString;
    private String titleString;
    private int counter = 0;
    private int maxRecords = 15;
    private boolean isLink = false;
    private boolean isTitle = false;
    private boolean ifInsideItem = false;
    private boolean enclosure = true;
    /**
     * Constructor
     */
    public RssHandler() {
        linkString = "link";
        titleString = "title";
        rssTitles = new Vector<String>();
        rssLinks = new Vector<String>();
        rssEnclosures = new Vector<String>();
    }
    
    /**
     * 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 numberOfRecords) {
        titleString = title;
        linkString = link;
        rssTitles = new Vector<String>();
        rssLinks = new Vector<String>();
        maxRecords = numberOfRecords;
        rssEnclosures = new Vector<String>();
    }
           
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(ifInsideItem){
            isLink = qName.equalsIgnoreCase(linkString);
            isTitle = qName.equalsIgnoreCase(titleString);
            if(enclosure && qName.equalsIgnoreCase("enclosure")){
                rssEnclosures.addElement(attributes.getValue("url"));
            }
        }else{
            ifInsideItem = qName.equalsIgnoreCase("item");
        }
        if(isTitle){
            if(counter > maxRecords){
                throw new SAXException("Parsing limit of "+maxRecords+" items reached");
            }
            counter++;
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
    }
    
    public void characters(char ch[], int start, int length) throws SAXException {
        if (isLink) {
            rssLinks.addElement(new String(ch, start, length));
            isLink = false;
        }else if(isTitle){
            rssTitles.addElement(new String(ch, start, length));
            isTitle = false;
        }
    }
    
    public Hashtable<String,String[]> getTable(){
        Hashtable<String,String[]> output = new Hashtable<String,String[]>();
        for(int i =0; i< rssTitles.size(); i++){
            output.put(rssTitles.elementAt(i), new String[]{rssLinks.elementAt(i),rssEnclosures.elementAt(i)});
        }
        return output;
    }
    
    public Vector<String> getTitles(){
        return rssTitles;
    }
}
@@ -156,16 +156,42 @@
 Handler;
+%0A%0Aimport android.util.Log;
 %0A/*%0A * C
@@ -797,16 +797,57 @@
 = true;%0A
+    private boolean badLinkNext = false;%0A
     /**%0A
@@ -2547,26 +2547,136 @@
     
-    if (isLink
+%09String toAdd = new String(ch, start, length);%0A    %09if(!toAdd.contains(%22del.icio.us%22))%7B%0A%09        if (isLink && !badLinkNext
 ) %7B%0A
+%09
     
@@ -2727,32 +2727,33 @@
 tart, length));%0A
+%09
             isLi
@@ -2756,32 +2756,33 @@
 isLink = false;%0A
+%09
         %7Delse if
@@ -2784,32 +2784,33 @@
 se if(isTitle)%7B%0A
+%09
             rssT
@@ -2854,24 +2854,25 @@
 , length));%0A
+%09
             
@@ -2888,24 +2888,25 @@
  false;%0A
+%09
         
 %7D%0A    %7D%0A
@@ -2889,32 +2889,111 @@
 false;%0A%09        
+    badLinkNext = false;%0A%09        %7D%0A    %09%7Delse%7B%0A    %09%09badLinkNext = true;%0A    %09
 %7D%0A    %7D%0A    %0A   
@@ -3125,164 +3125,529 @@
     
-    for(int i =0; i%3C rssTitles.size(); i++)%7B%0A            output.put(rssTitles.elementAt(i), new String%5B%5D%7BrssLinks.elementAt(i),rssEnclosures.elementAt(i)%7D);
+%09Log.println(30, %22FUCK%22, rssTitles.size()+%22 %22+rssLinks.size() +%22 %22+rssEnclosures.size());%0A        for(int i =0; i%3C rssTitles.size(); i++)%7B%0A        %09try%7B%0A        %09%09Log.println(30, %22FUCK%22, rssTitles.elementAt(i)+%22 : %22+rssLinks.elementAt(i)+%22 %22+rssEnclosures.elementAt(i));%0A        %09%09output.put(rssTitles.elementAt(i), new String%5B%5D%7BrssLinks.elementAt(i),rssEnclosures.elementAt(i)%7D);%0A        %09%7Dcatch (Exception e)%7B%0A        %09%09Log.println(30, %22FUCK%22, rssTitles.size()+%22 %22+rssLinks.size() +%22 %22+rssEnclosures.size());%0A        %09%7D
 %0A   

Fix for issue with LAS fix. Only quick patch to get out to market will fix properly soon