/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 William Seemann
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sourceforge.servestream.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ASXPlaylistParser extends PlaylistParser {
	public final static String TAG = ASXPlaylistParser.class.getName();

	public final static String EXTENSION = "asx";
    
	private final String ABSTRACT_ELEMENT = "ABSTRACT";
	private final String ASX_ELEMENT = "ASX";
	private final String AUTHOR_ELEMENT = "AUTHOR";
	private final String BASE_ELEMENT = "BASE";
	private final String COPYRIGHT_ELEMENT = "COPYRIGHT";
	private final String DURATION_ELEMENT = "DURATION";
	private final String ENDMARKER_ELEMENT = "ENDMARKER";
	private final String ENTRY_ELEMENT = "ENTRY";
	private final String ENTRYREF_ELEMENT = "ENTRYREF";
	private final String EVENT_ELEMENT = "EVENT";
	private final String MOREINFO_ELEMENT = "MOREINFO";
	private final String PARAM_ELEMENT = "PARAM";
	private final String REF_ELEMENT = "REF";
	private final String REPEAT_ELEMENT = "REPEAT";
	private final String STARTMARKER_ELEMENT = "STARTMARKER";
	private final String STARTTIME_ELEMENT = "STARTTIME";
	private final String TITLE_ELEMENT = "TITLE";
	
	/**
	 * Default constructor
	 */
	public ASXPlaylistParser(URL playlistUrl) {
		super(playlistUrl);
	}
	
	/**
	 * Retrieves the files listed in a .asx file
	 */
    public void retrieveAndParsePlaylist() {
    	
    	if (mPlaylistUrl == null)
    		return;
    	
		HttpURLConnection conn = null;
		String xml = "";
        String line = null;
        BufferedReader reader = null;
        
        try { 	
        	conn = URLUtils.getConnection(mPlaylistUrl);
		    
		    // Start the query
		    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    conn.connect();
		    
		    while ((line = reader.readLine()) != null) {
		    	xml = xml + line; 
            }
		    
		    parseXML(xml);
        } catch (Exception ex) {
        	ex.printStackTrace();
        } finally {
        	Utils.closeBufferedReader(reader);
        	Utils.closeHttpConnection(conn);
        }
    }

	private void parseXML(String xml) {
		
		SAXBuilder builder = new SAXBuilder();
	    Reader in = new StringReader(xml);
	    Document doc = null;
	    Element root = null;
	    
	    try {
	    	doc = builder.build(in);
	    	root = doc.getRootElement();
	    	List<Element> children = root.getChildren();
	    	
	    	for (int i = 0; i < children.size(); i++) {
	    		String tag = children.get(i).getName();
	    		
	    		if (tag != null && tag.equalsIgnoreCase("entry")) {
	    			buildPlaylistEntry(children.get(i).getChildren());
	    		}
	    	}
	    } catch (JDOMException e) {
	    } catch (IOException e) {
	    } catch (Exception e) {
	    
	    }
	}
    
    private void buildPlaylistEntry(List<Element> children) {
    	
    	MediaFile mediaFile = new MediaFile();
    	
    	for(int i = 0; i < children.size(); i++) {
    	    String attributeName = children.get(i).getName();
    		
    	    if (attributeName.equalsIgnoreCase(ABSTRACT_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(ASX_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(AUTHOR_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(BASE_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(COPYRIGHT_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(DURATION_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(ENDMARKER_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(ENTRY_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(ENTRYREF_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(EVENT_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(MOREINFO_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(PARAM_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(REF_ELEMENT)) {
    	    	String href = children.get(i).getAttributeValue("href");
    	    	
    	    	if (href == null) {
    	    	    href = children.get(i).getValue();
    	    	}
    	    	
    	    	System.out.println(href);
    	    	mediaFile.setURL(href);
    	    } else if (attributeName.equalsIgnoreCase(REPEAT_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(STARTMARKER_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(STARTTIME_ELEMENT)) {
    	    } else if (attributeName.equalsIgnoreCase(TITLE_ELEMENT)) {
    	    	String title = children.get(i).getValue();
    	    	
    	    	if (title != null) {
    	    		System.out.println(title);
    	    		mediaFile.setPlaylistMetadata(title);
    	    	}
    	    }
    	}
    	
    	mNumberOfFiles = mNumberOfFiles + 1;
    	mediaFile.setTrackNumber(mNumberOfFiles);
    	mPlaylistFiles.add(mediaFile);
    }
}


@@ -896,16 +896,44 @@
 et.URL;%0A
+import java.util.ArrayList;%0A
 import j
@@ -3298,27 +3298,162 @@
 n = 
-root.getChildren();
+new ArrayList%3CElement%3E(root.getChildren().size());%0A%09    %09%0A%09        for (Object o: root.getChildren()) %7B%0A%09            children.add((Element) o);%0A%09        %7D
 %0A%09  
@@ -3632,56 +3632,301 @@
  %09%09%09
-buildPlaylistEntry(children.get(i).getChildren()
+Element element = children.get(i);%0A%09    %09%09%09List%3CElement%3E children2 = new ArrayList%3CElement%3E(element.getChildren().size());%0A%09    %09%09%09%0A%09    %09        for (Object o: element.getChildren()) %7B%0A%09    %09            children2.add((Element) o);%0A%09    %09        %7D%0A%09    %09%09%09%0A%09    %09%09%09buildPlaylistEntry(children2
 );%0A%09

Cleaned up code