import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;


class MonUrl {
  
protected String Url;
protected DataOutputStream os;
protected DataInputStream is;
protected boolean Debug = false;
protected URLConnection Connection;
public int NbTriesOpenConnection = 10;

public String erreurText() {
  return "erreur:UrlDir";
}

private boolean Connect(String Location)
  {
    URL temp;
    Connection = null;
    for(int  i=0;i< NbTriesOpenConnection;i++)
      {
	try {
	  temp = new URL((Location));
	  Connection = temp.openConnection(); 
	  break;
	} catch (Exception e) {
	  if (Debug)
	    System.err.println("MonUrl class: OpenConnection : exception catched"+e );
	}
      }
    return Connection != null;
  }


MonUrl(String Url)
  {
    // The URL have to be finished by '/'
    if (!Url.endsWith(".") && !Url.endsWith("/"))
      this.Url = Url + "/";
    else
      this.Url = Url;


  }

MonUrl(String Url, boolean debug)
  {
    // The URL have to be finished by '/'
    if (!Url.endsWith(".") && !Url.endsWith("/"))
      this.Url = Url + "/";
    else
      this.Url = Url;

    Debug = debug;
  }

public boolean GetFile(String file)
  {
    if (Connect(Url+ "/" + file))
      return GetData(file,null);
    else
      {
	if (Debug)
	  System.err.println("MonUrl Class : Unable to open the connection (getfile)");
	return false;
      }
  }
  
public boolean GetFile(String file, String dest)
  {
    if (Connect(Url+ "/" + file))
      return GetData(dest,null);
    else
      {
	if (Debug)
	  System.err.println("MonUrl Class : Unable to open the connection (getfile)");
	return false;
      }
  }
  
private int myMin(int i,int j)
  {
    if (i == -1) return j;
    if (j == -1 || j > i) return i;
    return j;
  }

private String ParseHtml(String Page)
  {
    if (Page == null)
      return null;
    StringBuffer Resultat = new StringBuffer(1024);
    int i,j;
    String inputLine;
    StringTokenizer st = new StringTokenizer(Page,"<");   // looking for tags
    while (st.hasMoreTokens())
      {
	inputLine = st.nextToken();
	if ((i = inputLine.toLowerCase().indexOf("a href=")) != -1)
	  {
	    i+=7; //  == sizeof("a href="); 
	    if (inputLine.charAt(i) == '"') {
	      i++;       // cut the first " (if any)
	      j = myMin(inputLine.indexOf(">",i), inputLine.indexOf("\"",i));         //  look for the closing '"' or >
	    }
	    else
	      j= myMin(inputLine.indexOf(">",i), inputLine.indexOf(" ",i));               // look for the closing '"' or space
	    
	    
	    if (j == -1) continue;      // error in the html code ....
	    if (inputLine.charAt(j-1) == '"') j--;     
	    Resultat.append(inputLine.substring(i,j)+"\n");
	  }
	else
	  if ((i = inputLine.toLowerCase().indexOf("img")) != -1)
	    {
	      i+=3; //  == sizeof("img"); 
	      if ((i = inputLine.toLowerCase().indexOf("src=",i)) != -1) {
		i += 4;
		if (inputLine.charAt(i) == '"') {
		  i++;       // cut the first " (if any)
		  j = myMin(inputLine.indexOf(">",i), inputLine.indexOf("\"",i));         //  look for the closing '"' or >
		}
		else
		  j= myMin(inputLine.indexOf(">",i), inputLine.indexOf(" ",i));               // look for the closing '"' or space
		
		
		if (j == -1) continue;      // error in the html code ....
		if (inputLine.charAt(j-1) == '"') j--;     
		Resultat.append(inputLine.substring(i,j)+"\n");
	      }
	    }
      }
   
    return Resultat.toString();
  }
  
public String GetList(String Rep)
  {
    if (Connect(Url+Rep)) {

      if (!(Connection.getContentType().toLowerCase().startsWith("text"))) // to get the list we have to be sure it's something like text
	return null;

      StringBuffer Res = new StringBuffer(1024);
      if (Debug)
	System.err.println("MonUrl Class : getting list ");
      if (!(GetData("",Res)))
	  return null;
      return ParseHtml(Res.toString());
    }
    if (Debug)
      System.err.println("MonUrl Class : List, Not Opened");
    return null;
  }

public String GetList()
  {
    if (Connect(Url)) {

      if (!(Connection.getContentType().toLowerCase().startsWith("text"))) // to get the list we have to be sure it's something like text
	return null;

      StringBuffer Res = new StringBuffer(1024);
      if (Debug)
	System.err.println("MonUrl Class : getting list ");
      if (!(GetData("",Res)))
	  return null;

      return ParseHtml(Res.toString());
    }
    if (Debug)
      System.err.println("MonUrl Class : List, Not Opened");
    return null;
  }



private boolean GetData(String Dest, StringBuffer Res)
    {  
      DataInputStream dis ;

      FileOutputStream fou;
      DataOutputStream fic;

      fic = null;
      fou = null;

      try { 
	if (Res == null) // we have to put the result in a file
	  {
	    fou =  new FileOutputStream(Dest);
	    fic = new DataOutputStream(fou);
	  }
	dis = new DataInputStream(Connection.getInputStream());
	for(;;) {
	  try{
	    if (Res != null) {
	      String s = dis.readLine();
	      if (s == null) {
		dis.close();
		return true;
	      }
	      Res.append(s+"\n");
	    }
	    else
	      fic.writeByte(dis.readByte());
	      
	    } catch (EOFException e) {
	      if (Res == null) fic.close();
	      dis.close();
	      return true;
	    } catch (Exception e) {
	      if (Res == null) fic.close();
	      dis.close();
	      if (Debug)
		System.err.println("MonUrl Class: exception catched (GetFile) :"+e);
	      return false;
	    }
	  } // fin for
	
	
      } catch (Exception e) {
	if (Debug)
	  System.err.println("MonUrl Class: exception catched (GetFile):" +e);
      }
      return false;
    }
}
  
