package fishrungames.bashgid.core;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.client.ClientProtocolException;

import android.util.Log;


public class DownloadManager {

	public static String getXmlFromUrl(String urlString) {
        String xml = null;
 
        try {
            // defaultHttpClient
        	
        	URL url = new URL(urlString);         	
        	URLConnection ucon = url.openConnection();

        	BufferedReader in = new BufferedReader(new InputStreamReader(
        			ucon.getInputStream()));
			String inputLine;
			
			StringBuilder stringBuilder = new StringBuilder();
			
			while ((inputLine = in.readLine()) != null) 
			{
				stringBuilder.append(inputLine);
			}
            
            
			xml = stringBuilder.toString();
            
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
	
	
	public static boolean MimeTypeIsImage(String mimeType)
	{
		return mimeType.equals("image/bmp") || mimeType.equals("image/gif") || mimeType.equals("image/jpeg") || mimeType.equals("image/png");
	}
	
	public static String GetUrlFolder(String url)
	{
		int pos = url.lastIndexOf("/");
		
		if (pos == -1)
		{
			Log.e("Error", "GetUrlFolder got incorrect agrument");
			
			return "";
		}
		
		String substr = url.substring(0, pos);
		
		return substr;
	}
}
