MakiEatsWeb.java :  » Web-Crawler » WebEater » WebEater » Java Open Source

Java Open Source » Web Crawler » WebEater 
WebEater » WebEater » MakiEatsWeb.java
package WebEater;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import WebEater.TQueue.*;
import WebEater.ThreeStrings.*;
import WebEater.TwoStrings.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/*
A filter class for FileChooser that shows only directories in file chooser.
 */
class DirectoryFilter extends javax.swing.filechooser.FileFilter {

  public boolean accept(File f) {
    return f.isDirectory();
  }

  public String getDescription() {
    return "Directory";
  }
}

/*
Main class.
 */
public class MakiEatsWeb extends JDialog {

    /*******************************************************************************************
     CLASS FIELDS WHICH ARE (MOSTLY) SET AT THE BEGINNING AND ARE USEFUL DURING WHOLE
     PROCESS OF RETRIEVING A WEB SITE.
     *******************************************************************************************/
    private  String DOMAIN = "";
  /* Always contains current domain (if an URL was http://www.yahoo.com/mail, this field will be http://www.yahoo.com */
    private  String STARTING_URL = "";
  /* An URL user entered at the beginning */
    private  int DEPTH = 0;
  /* How much links to put on queue */
  private  boolean INFINITE = false;
  /* If set to true, program will put every link on queue (but not if on foreign domain) */
    private  boolean FOREIGN_DOMAIN_ALLOWED = false;
  /* If set to true, program will put on queue links that point to domains other than STARTING_URL */
    private  String ROOT_DIR_PATH = "";
  /* Absolute path to root directory, where we store web site */
  private  String USER_DIR = "";
  /* Absolute path to directory user chose from GUI. */
  private  File ROOT_DIR = null;
  /* Instance of File class, where program records data. Instantiated with ROOT_DIR = new File(ROOT_DIR_PATH) */
  private  boolean IN_SCRIPT = false;
  /* Set to true if program parses a HTML code between <SCRIPT> and </SCRIPT> tags.*/
  /* Search for links is much heavier when IN_SCRIPT == true. */
  private  HashSet VALID_CHARS = null;
  /* Hash set of Character objects containing characters that can be used to build a valid URL address. */
  private  HashSet TRIED_FILES = null;
  /* Hash set containing absolute URL addresses of files that are downloaded or will be downloaded. */
  private  boolean VERBOSE = true;
  /* If set to true, prints some debugging information to output.log. */
  private  boolean SILENT = true;
  /* If set to false, program will ask user what to do with every link (Download, Abort, Skip). */
  private  boolean LOGGING = false;
  /* If set to true, program will log output to output.log that is shown on screen. */

    private final static String APP_NAME = "WebEater";
    private final static String APP_VERSION = "v0.2";

  /***********************************************************************************************************/
  /* Fields used in GUI. */
    private JSpinner depthSpinner = null;
    private JCheckBox foreignCheckBox = null;
    private JCheckBox infCheckBox = null;
  private JCheckBox silentCheckBox = null;
    private JLabel numLabel = null;
    private JLabel dirLabel = null;
  private JTextArea output = null;
  private JLabel outputLabel = null;
  private JCheckBox logCheckBox = null;
    private JTextField dirText = null;
    private JLabel urlLabel = null;
    private JTextField urlText = null;
  private JButton directory_button = null;
    private JButton main_button = null;
  /***********************************************************************************************************/

  private FileWriter err_output;
  private BufferedWriter err_out;
  /* err_out will be instantiated to write to output.log */

    private boolean EOF = false;
  /* EOF will be set to true if method getTag reached */

    private TQueue queue = new TQueue();
  /* A main queue on which we put URLs and some other useful information about URLs.*/

    /*******************************************************************************************/

    /*
    Reads input file and makes a first string made of all chars until "open" char,
  and a second string made of all char between "open" and "close" char.    
     */
    private TwoStrings getTag(char open, char close, InputStreamReader in) throws IOException {
    String temp_str = "";
        String pre_tag = "";
        int temp_char = 0;
        TwoStrings ts = null;
        boolean newline = false;
        boolean open_tag = false, close_tag = false;
    
        do {
      temp_char = in.read();

      if ( ((char) temp_char == '\r') | ((char) temp_char == '\n') ) {
        newline = true; 
        /*
        we stop making string when we find a newline character, 
        because there are web pages with a lot of plain text and no tags,
        and processing pages like that causes java buffers to overflow.
         */
        pre_tag = pre_tag + (char) temp_char;
      }
      else if (temp_char == -1)
        EOF = true;
      else if ( (char) temp_char == open )
        open_tag = true;
      else
        pre_tag = pre_tag + (char) temp_char;

        } while (!EOF & !newline & !open_tag);

        if (!EOF & !newline & open_tag) {
            
            temp_str = temp_str + (char) temp_char; // open char goes to temp_str
           
      do {
        temp_char = in.read();

        if (temp_char == -1)
          EOF = true;
        else if ((char) temp_char == close) {
          close_tag = true;
          temp_str = temp_str + (char) temp_char;
        }
        else {
          temp_str = temp_str + (char) temp_char;
        }               
      }  while (!EOF & !close_tag);
        }

        ts = new TwoStrings(pre_tag, temp_str);

    return ts;
    }

  /*
  Returns true if string s contains string p, false otherwise.
   */
  private boolean StringContains(String s, String p) throws IOException {
    int i;

    i = QuickSearch(s, p);
    
    if (i != -1)
      return true;
    else
      return false;
  }

  /*
  Returns a string found between quotes (double or single) starting from string offset.
   */
    private String whatsInsideQuotes(String s, int offset) throws IOException {
        String temp_str = "";
        int i = offset;
        char temp_char = 'z';
    boolean single_quotes, ok = false;

    // There can be few characters between "href" and quotes, for example: href = "wapgoin" we have " = " between href and quotes...
    while ((i <= s.length() - 1) & ((s.charAt(i) == ' ') | (s.charAt(i) == '=') | (s.charAt(i) == '\n') | (s.charAt(i) == '\r')))
      i++;

    if (i <= s.length()-1) {
      temp_char = s.charAt(i);
      if ((temp_char != '\"') & (temp_char != '\''))
        // if first character found (other than those upthere) is not a quote, return empty string.
        return "";

      if (i <= s.length() - 1) {
        if (temp_char == '\'')
          single_quotes = true;
        else
          single_quotes = false;

        i++; // We are not interested in first quote anymore, so move on to next char...

        temp_char = s.charAt(i);

        if (single_quotes) {
          // make a new string made of all characters found until other single quote
          while ((i <= s.length() - 1) & (temp_char != '\'') ) {            

            temp_char = s.charAt(i);
            if ( (temp_char != '\r') & (temp_char != '\n') ) {
              temp_str = temp_str + temp_char;
            }
            
            i++;
          }
        }
        else {
          // make a new string made of all characters found until other double quote
          while ((i <= s.length() - 1) & (temp_char != '\"') ) {            

            temp_char = s.charAt(i);
            if ( (temp_char != '\r') & (temp_char != '\n') ) {
              temp_str = temp_str + temp_char;
            }

            i++;
          }
        }

        if (temp_str.length() > 0)
          temp_str = temp_str.substring(0, temp_str.length()-1);
      }
    }

        return temp_str;
    }

    /*
    Returns position of first instance of string p in string s. Returns -1 if p not found.
     */
    private int QuickSearch(String s, String p) throws IOException {
        int[] shift = new int[65536];
        char ch;
        int i, offset, test, position, end;

    // We want this method to search ignoring case so...
    s = s.toLowerCase();
    p = p.toLowerCase();

        if (s.length() < p.length()) {
            return -1;
        }
        else if (p.length() == 1) {
            position = 0;
            ch = p.charAt(0);
            while ( (position <= s.length()-1) && (s.charAt(position) != ch) ) {
                position++;
            }
            if (position > s.length()-1)
                return -1;
            else
                return position;

        }
        else if (s.length() == p.length()) {
            if (s.equalsIgnoreCase(p))
                return 0;
            else
                return -1;
        }
        else {
            offset = p.length();
            for (int j=0; j<= 65535; j++) {
                shift[j] = offset;
            }
            for (int k = 0; k <= p.length()-1; k++) {
                shift[(int) p.charAt(k)] = offset - k;
            }
            
            position = 0;
            test = 0;
            end = s.length() - p.length();
            do {
                if (p.charAt(test) == s.charAt(position + test)) {
                    test++;
                }
                else {
          if (position+offset == s.length()) // temporary fix for bug "if p.length() == s.length()/2 then crash"
                        return -1;
                    position = position + shift[(int) s.charAt(position + offset)];
                    test = 0;
                }
            } while ((test <= p.length()-1) & (position <= end));
            if (test > p.length()-1) {
                return position;
            }
            else {
                return -1;
            }
        }
    }

    /*
    Returns domain URL in complete form (http://www.yahoo.com instead of www.yahoo.com)
     */
    private String getDomain(String s) throws IOException {
    int i = QuickSearch(s, "://");

        if (i != -1) { // URL begins with ftp:// or http:// or file:// or similar
      i += 3;
      do {
        i++;
      } while ((i <= s.length() - 1) && (s.charAt(i) != '/'));

      if (i <= s.length() - 1)
        s = s.substring(0, i);
        }
        else {
      i = 0;
      do {
        i++;
      } while ((i <= s.length() - 1) && (s.charAt(i) != '/'));

      if (i <= s.length() - 1)
        s = s.substring(0, i);

            s = "http://" + s;
        }

    return s;
    }

  /* 
  Counts slashes in string :)
   */
  private int SlashCount(String s) throws IOException {
    int i = 0, count = 0;

    while (i <= s.length() - 1) {
      if (s.charAt(i) == '/')
        count++;

      i++;
    }

    return count;
  }

  /*
  Tries to determine whether given URL points to directory or not.
  TAKES FULL URL AS AN ARGUMENT!!! URL'S WITHOUT HTTP:// OR SIMILAR WILL NOT WORK!!!
   */
  private boolean IsDirectory(String s) throws IOException {
    int i;
    int dots = 0;

    i = SlashCount(s);

    if (i < 3)
      return true;

    else {
      if (s.charAt(s.length() - 1) == '/')
        return true;

      else {
        i = s.length() - 1;
        while (s.charAt(i) != '/') {
          if (s.charAt(i) == '.')
            dots++;

          i--;
        }
        if (dots >= 2)
          return true;
        else if (dots == 0)
          return true;
        else
          return false;
      }
    }
  }

  /*
  Makes root directory name from absolute URL.
  Relative URL's will NOT work!
   */
    private String mkRootDirName(String s) throws IOException  {
    int i = QuickSearch(s, "://");

        if (i != -1) {
            s = s.substring(i + 3);
      s = s.replace('/', File.separatorChar);
            return s;
        }
        else
            return "";
    }

  /*
  Searches for regular anchor within HTML tag...
   */
    private String getAnchor(String tag) throws IOException {
    int i = 1;
    boolean ok = true;
    String temp = "";

        tag = tag.substring(1, tag.length()-1);

        if ((i = QuickSearch(tag, "href")) != -1)
            temp = whatsInsideQuotes(tag, i+4); // i+4 because "href".length() == 4

        else if ((i = QuickSearch(tag, "src")) != -1)
            temp = whatsInsideQuotes(tag, i+3);

    else if ((i = QuickSearch(tag, "background")) != -1)
            temp = whatsInsideQuotes(tag, i+10);

    return temp;
    }

  /*
  Determines type of URL (absolute, relative, mailto)
   */
    private String typeOfURL(String URL) throws IOException {

    if (URL.startsWith(".") | URL.startsWith("..") | URL.startsWith("/"))
      return "relative";

    else if (URL.startsWith("http") | URL.startsWith("ftp"))
      return "absolute";

    else if (URL.startsWith("mailto:"))
      return "mail";

    else
      return "relative";

    }

  /*
  Returns file name from given URL, empty string if URL contains no file name
   */
    private String getFileName(String URLAddress) throws IOException {
        String temp = "";
        int i = URLAddress.length();

        if (i != 0) {
            i--;
            while ( (i > 0) && (URLAddress.charAt(i) != '/') ) {
                i--;
            }

            if (i != 0) {
                i++;
                while (i <= URLAddress.length() - 1) {
                    temp = temp + URLAddress.charAt(i);
                    i++;
                }

            }
        }

    return temp;
    }

  /*
  Removes file name from URL (useful when creating directories)
   */
    private String remFileName(String URLAddress) throws IOException {
        String temp = "";
        int i = URLAddress.length() - 1;
        int j = 0;
    boolean ok = true;

    if (VERBOSE) {
      err_out.write("remFileName: URLAddress: " + URLAddress);
      err_out.newLine();
      err_out.flush();
    }

        if (i > 0) {
            while ( (i >= 0) & ok ) {
        if (URLAddress.charAt(i) == '/')
          ok = false;

                i--;
      }

            if (!ok) {
        i++;
        if (i == URLAddress.length() - 1) {
          return URLAddress;
        }
      
        else if (i == 0) {
          return "/";
        }

        else {
          while (j <= i) {
            temp = temp + URLAddress.charAt(j);
            j++;
          }
          if (VERBOSE) {
            err_out.write("remFileName: This is returned: " + temp);
            err_out.newLine();
            err_out.flush();
          }
          return temp;
        }
      }
      else {
        if (VERBOSE) {
          err_out.write("remFileName: This is returned: " + URLAddress);
          err_out.newLine();
          err_out.flush();
        }
        return "";
      }
        }

    if (VERBOSE) {
      err_out.write("remFileName: This is returned: " + URLAddress);
      err_out.newLine();
      err_out.flush();
    }
    return URLAddress;
    }

  /*
  Converts absolute URL to relative.
   */
    private String AbsToRel(String s, String curr_dir, String domain) throws IOException {
        String temp = "";

        temp = s.replaceFirst(domain + curr_dir, "");

        return temp;
    }

  /*
  Returns true if URL is on the same domain, false if not.
   */
    private boolean sameDomain(String s) throws IOException {

        if (DOMAIN != "")
            return s.startsWith(DOMAIN);
        else
            return false;

    }

  /*
  Takes an URL as argument, and returns same URL pointing to parent directory.
  (We use this when we have an "../blah/blah.jpg" type of URL)
   */
  private String OneDirUp(String s) throws IOException {
    int i = s.length() - 1;
    boolean ok = false;
    char temp_char = 'z';
    String temp = "";

    while ((i >= 0) & !ok) {
      temp_char = s.charAt(i);
      if ((temp_char == '/') & (i < s.length() - 1)) {
        temp = s.substring(0, i);
        ok = true;
      }
      i--;
    }

    return temp;
  }

  /*
  Removes domain from an absolute URL.
   */
  private String remDomain(String url) throws IOException {
    String temp = "";
    int i;
    boolean ok = false;

    i = QuickSearch(url, "://");

    if (i != -1) {
      i += 3;
      while ( (i <= url.length() - 1) & !ok ) {
        if (url.charAt(i) == '/')
          ok = true;

        i++;
      }
      if (ok)
        temp = url.substring(i);

    }
    else
      temp = url;

    return temp;
  }

    /*
    Takes an URL, makes all necessary directories and returns 
    absolute file path on disk.
     */
    private String makeFilePath(String url, String curr_dir) throws IOException {
        String type = typeOfURL(url);
        String path = "", temp_path = "";
    char temp_char;
    int i = -1;
        File temp_dir = null;
        boolean ok;

        if (type != "") {

      if (type.equals("absolute")) {
        i = QuickSearch(url, "://");
        if (i != -1) {
          i += 3;
          path = url.substring(i);
        }
      }
      else if (type.equals("relative")) {
        if (url.startsWith(".")) {
          path = curr_dir + url.substring(2); // we don't need './' from url
        }
        else if (url.startsWith("/")) {
          path = curr_dir + url.substring(1); // we don't need '/' from url
        }
        else if (url.startsWith("..")) {
          do {
            url = url.substring(3);
            curr_dir = OneDirUp(curr_dir);
          } while (url.startsWith(".."));
          path = curr_dir + url;
        }
        else {
          path = curr_dir + url;
        }
      }

      if (!ROOT_DIR.getAbsolutePath().endsWith(File.separator) & !path.startsWith("/"))
        path = ROOT_DIR.getAbsolutePath() + File.separator + path;

      for (i = 0; i <= path.length()-1; i++) {
        temp_char = path.charAt(i);
        if (temp_char == '/')
          temp_char = File.separatorChar;
        temp_path = temp_path + temp_char;
      }

      path = temp_path;

            temp_dir = new File(path);

            ok = temp_dir.mkdirs();
      path = temp_dir.getAbsolutePath();
            if (ok)
                return path;

            else
                return path;

        }
        else
            return "";

    }

  /*
  Makes current directory (directory on the site where URL is) from URL and current directory
  of the file in which we've found this URL.
   */
  private String makeCurrDir(String url, String curr_dir) throws IOException {
    String type = typeOfURL(url);
    String dir = "", temp = "";
    int i;

    if (VERBOSE) {
      err_out.write("makeCurrDir: url: " + url + " curr_dir: " + curr_dir);
      err_out.newLine();
      err_out.flush();
    }

    if (type.equals("absolute")) {
      if (VERBOSE) {
        err_out.write("makeCurrDir: url is absolute");
        err_out.newLine();
        err_out.flush();
      }
      i = QuickSearch(url, "://");
      if (i != -1) {
        i += 3;
        dir = remFileName(url.substring(i));
      }
      else {
        dir = remFileName(remDomain(url));
      }
    }
    else if (type.equals("relative")) {
      if (VERBOSE) {
        err_out.write("makeCurrDir: url is relative");
        err_out.newLine();
        err_out.flush();
      }
      if (url.startsWith("./")) {
        temp = remFileName(url);
        if (temp.length() > 1) {
          if (curr_dir.endsWith("/"))
            dir = curr_dir + temp.substring(2);
          else
            dir = curr_dir + "/" + temp.substring(2);
        }
      }
      else if (url.startsWith("/")) {
        temp = remFileName(url);
        if (temp.length() > 0) {
          if (curr_dir.endsWith("/"))
            dir = curr_dir + temp.substring(1);
          else
            dir = curr_dir + "/" + temp.substring(1);
        }

      }
      else if (url.startsWith("..")) {
        do {
          url = url.substring(3);
          curr_dir = OneDirUp(curr_dir);
        } while (url.startsWith(".."));
        dir = curr_dir + remFileName(url);
      }
      else {
        temp = remFileName(url);
        if (!curr_dir.endsWith("/") & !temp.startsWith("/"))
          dir = curr_dir + "/" + temp;
        else
          dir = curr_dir + temp;
      }
    }

    if (VERBOSE) {
      err_out.write("makeCurrDir: This is returned: " + dir);
      err_out.newLine();
      err_out.flush();
    }
    return dir;
  }

  /*
  Simple method which replaces first occurence of target with replacement in string s.
   */
  private String Replace(String s, String target, String replacement) throws IOException {
    int i;
    String ret = "", temp1 = "", temp2 = "";

    i = QuickSearch(s, target);
    if (i != -1) {
      temp1 = s.substring(0, i);
      temp2 = s.substring(i + target.length(), s.length());
      ret = temp1 + replacement + temp2;
    }

    return ret;
  }

  private boolean NoFileExtension(String s) {
    int i = s.length();
    boolean no_extension = false, dot_exists = false, slash_exists = false;

    do {
      i--;
      if (i < 0)
        no_extension = true;
      else if (s.charAt(i) == '.')
        dot_exists = true;
      else if (s.charAt(i) == '/')
        slash_exists = true;
    } while (!no_extension & !dot_exists & !slash_exists);

    if (no_extension) {
      return true;
    }
    else if (slash_exists) {
      return true;
    }
    else if (dot_exists) {
      return false;
    }
    else return true;
  }

  /*
  Makes an absolute address from relative URL using relative URL and current directory
  of a file in which we've found this URL.
  We use this when we want to retrieve this relative URL from server.
   */
  private String makeAbsAddr(String addr, String curr_dir) throws IOException {
    String temp = "";
    int i;
    boolean from_root = false;

    if (VERBOSE) {
      err_out.write("makeAbsAddr: addr: " + addr + " curr_dir: " + curr_dir);
      err_out.newLine();
      err_out.flush();
    }

    if (addr.startsWith("..") & (addr.length() >= 3)) {
      do {
        curr_dir = OneDirUp(curr_dir);
        addr = addr.substring(3);
      } while (addr.startsWith("..") & (addr.length() >= 3));
    }
    else if (addr.startsWith("./")) {
      addr = addr.substring(2);
    }    
    else if (addr.startsWith("/")) {
      from_root = true;
      addr = addr.substring(1);
    }

    if (!curr_dir.endsWith("/") & !addr.startsWith("/"))
      temp = "http://" + curr_dir + "/" + addr;
    else    
      temp = "http://" + curr_dir + addr;

    if (VERBOSE) {
      err_out.write("makeAbsAddr: This is returned: " + temp);
      err_out.newLine();
      err_out.flush();
    }
    return temp;
  }

  /*
  In fact, simply searches for hash sign (#) in an URL, and returns true if # found.
   */
  private boolean ContainsLocalAnchor(String addr) throws IOException {
    int i;

    i = QuickSearch(addr, "#");

    if (i != -1)
      return true;
    else
      return false;
  }

  private boolean ContainsVariables(String addr) throws IOException {
    int i;

    i = QuickSearch(addr, "?");

    if (i != -1)
      return true;
    else
      return false;
  }

  private String RemVariables(String addr) throws IOException {
    int i;

    i = QuickSearch(addr, "?");
    if (i != -1)
      return addr.substring(0, i);
    else
      return addr;
  }

  /*
  Removes anything after hash sign (#), including that sign.
   */
  private String RemLocalAnchor(String addr) throws IOException {
    int i;

    i = QuickSearch(addr, "#");
    if (i != -1)
      return addr.substring(0, i);
    else
      return addr;
  }

  /*
  Searches for single or double quotes in a string.
   */
  private boolean HasMoreQuotes(String s) throws IOException {
    int i;

    i = QuickSearch(s, "\"");
    if (i != -1)
      return true;
    else {
      i = QuickSearch(s, "\'");
      if (i != -1)
        return true;
      else
        return false;
    }
  }

  /*
  This method searches for all strings in quotes, contained in string s. 
  Quotes can be single or double. This method does not recognize quotes within
  quotes (which is often used in javascript). All found strings are pushed
  to the vector, which is returned at the end.
   */
  private Vector EverythingInQuotes(String s) throws IOException {
    int i = 0;
    boolean end_of_string = false, single_quotes, added, ok = true;
    Vector strings = new Vector(50);
    String temp_str = "";
    char temp_char = 'z';

    while (!end_of_string) {
      temp_str = "";
      ok = true;

      if (i == s.length() - 1) {
        end_of_string = true;
      }
      else {
        while ((i <= s.length() - 1) & ok) {
          if ((s.charAt(i) == '\'') | (s.charAt(i) == '\"'))
            ok = false;

          i++;
        }

        i--;

        if (ok) {
          end_of_string = true;
        }
        else {
          temp_char = s.charAt(i);
          if (temp_char == '\'')
            single_quotes = true;

          else if (temp_char == '\"')
            single_quotes = false;

          else
            single_quotes = false;
            
          temp_char = 'z';
          i++;

          if (single_quotes) {
            while ((i <= s.length() - 1) & (temp_char != '\'') ) {
              temp_char = s.charAt(i);
              temp_str = temp_str + temp_char;
              i++;
            }
          }
          else if (!single_quotes) {            
            while ((i <= s.length() - 1) & (temp_char != '\"') ) {
              temp_char = s.charAt(i);
              temp_str = temp_str + temp_char;
              i++;
            }
          }

          temp_char = 'z';          

          if (i == s.length())
            end_of_string = true;
          else {
            temp_str = temp_str.substring(0, temp_str.length()-1);
            if ((temp_str.length() > 0) & (temp_str != " "))
              added = strings.add(temp_str);
            i++;
          }
        }
      }
    }// while (!end_of_string)

    return strings;
  }

  /*
  This method determines whether string s is a filename or not.
  String s is considered a filename if:
      a) Contains only valid characters.
    b) Has at least one dot.

  If valid_url_check is set to true, this method only checks if s is a valid url.
   */
  private boolean IsFile(String s, boolean valid_url_check) throws IOException {
    boolean ok = true;
    int i = 0;
    char temp_char;
    Character temp_Character = null;

    if ((s.length() == 0) | (s.length() == 1))
      return false;

    while (ok & (i <= s.length() - 1)) {
      temp_char = s.charAt(i);
      temp_Character = new Character(temp_char);
      ok = VALID_CHARS.contains(temp_Character);      
      i++;
    }
    if (!valid_url_check & ok) {
      i = s.length() - 1;
      while ((s.charAt(i) != '.') & (i > 0) & ok) {
        i--;
        if (i < s.length() - 10) {
          ok = false;
        }
      }
      if (s.charAt(i) == '.')
        return true;

      else
        return false;

    }
    else if (valid_url_check & ok) {
      return true;
    }
    else
      return false;
  }

  /*
  Takes an vector of strings found in quotes, and current directory as an argument.
  Iterates through vector, for every string taken out from vector checks if it's a file name,
  if it is a file name, makes an absolute address of that file and all other information
  needed, and pushes it to main queue.
   */
  private void FilesInScripts(Vector strings, String curr_dir) throws IOException {
    int i = 0;
    String temp_str = "", type = "", tempwola = "";
    Object temp = null;
    boolean ok = true, added;
    ThreeStrings threes = null;

    ok = !strings.isEmpty();

    while (ok) {
      if (!strings.isEmpty()) {
        temp = strings.remove(0);      
        if (temp != null) {
          temp_str = (String) temp;

          /*
          This one ensures that if we have more quotes encapsulated in this string, we go through all
          of them and search for filenames.
           */
          if (HasMoreQuotes(temp_str))
            FilesInScripts(EverythingInQuotes(temp_str), curr_dir);
          else {

            if (IsFile(temp_str, false)) {
              if (INFINITE)
                DEPTH = 1;

              tempwola = temp_str;
              if (ContainsLocalAnchor(tempwola))
                tempwola = RemLocalAnchor(tempwola);

              type = typeOfURL(temp_str);
              if (type.equals("absolute")) {
                if (!TRIED_FILES.contains(tempwola) & (DEPTH > 0)) {
                  added = TRIED_FILES.add(tempwola);                  
                  if (sameDomain(temp_str)) {
                    DEPTH--;
                    threes = new ThreeStrings(tempwola , makeCurrDir(tempwola, curr_dir), DOMAIN);
                    queue.Push(threes);
                  }
                  else {
                    if (FOREIGN_DOMAIN_ALLOWED) {
                      DEPTH--;
                      threes = new ThreeStrings(tempwola , makeCurrDir(tempwola, "#"), getDomain(temp_str));
                      queue.Push(threes);
                    }
                  }                  
                }
              }
              else if (type.equals("relative")) {
                if (!TRIED_FILES.contains(makeAbsAddr(tempwola, curr_dir)) & (DEPTH > 0)) {
                  DEPTH--;
                  added = TRIED_FILES.add(makeAbsAddr(tempwola, curr_dir));
                  threes = new ThreeStrings(makeAbsAddr(tempwola, curr_dir), makeCurrDir(tempwola, curr_dir), DOMAIN);
                  queue.Push(threes);
                }
              }              
            }
          }
        }
        else
          ok = false;
      }
      else
        ok = false;
    }
  }

  private void outputPrintLn(String s) {
    output.setText(output.getText() + "\r\n" + s);
  }

  private void outputClearScreen() {
    output.setText("");
  }

    /*
    This class will read files from given URL and store them to given location.
    If file is a HTML file, this class will parse tags when found, change href=""
    if needed, and then save HTML file.
     */
    public void RWFactory(MakiEatsWeb app) throws IOException, MalformedURLException {
        ThreeStrings ts = null, temp_ts = null;
    TwoStrings get_tag_output = null;
        URL URLconn = null;
        String URLAddress = "";
        String curr_dir = "";
        String file_name = "";
    String rel_addr = "";
        String temp_tag = "", type = "", temp = "", tempwola = ""; // tempwola = tempWithOutLocalAnchor
    String first_string = "";
    String file_path = "";
    String temp_abs_addr = "";
    String beginning_of_tag = "";
    String ContentType = "", ResponseMessage = "";
    String charset = "";
    String s = null;
    int ContentLength = -1, ResponseCode = -1;
    BufferedInputStream ins = null;
    BufferedReader in = null;
    InputStreamReader insr = null;
    OutputStreamWriter outw = null;
    FileOutputStream filew = null;
    File file = null;
    FileOutputStream fileos = null;
    BufferedOutputStream outos = null;
    HttpURLConnection HTTPconn = null;
    ImageIcon icon = new ImageIcon("images/question.png");
    int r, k;
    boolean urlOK, fileOK, binary = false, added, ok = false;

        while ((ts = (ThreeStrings) queue.Pop()) != null) {
      URLAddress = ts.getFirstString();
      curr_dir = ts.getSecString();
      DOMAIN = ts.getThirdString();

      outputClearScreen();
      outputPrintLn("----------------------------------------------------------------------");
      outputPrintLn("Currently downloading: " + URLAddress);

      if (LOGGING) {
        err_out.write("----------------------------------------------------------------------");
        err_out.newLine();
        err_out.write("Currently downloading: " + URLAddress);
        err_out.newLine();
        err_out.flush();
      }

      urlOK = true;
      fileOK = true;

      try {
        URLconn = new URL(URLAddress);
      }
      catch (MalformedURLException e) {
        urlOK = false;
      }

      if (urlOK) {

        try {
          HTTPconn = (HttpURLConnection) URLconn.openConnection();          
        }
        catch (MalformedURLException e) {
          outputPrintLn(e.toString());
          if (LOGGING) {
            err_out.write(e.toString());
            err_out.newLine();
          }
          continue;
        }
        catch (IOException e) {
          outputPrintLn(e.toString());
          if (LOGGING) {
            err_out.write(e.toString());
            err_out.newLine();
          }
          continue;
        }
        
          try {
          ResponseCode = HTTPconn.getResponseCode();
        }
        catch(UnknownHostException g) {
          outputPrintLn(g.toString());
          if (LOGGING) {
            err_out.write(g.toString());
            err_out.newLine();
          }
          continue;
        }
        catch (ConnectException f) {
          outputPrintLn(f.toString());
          if (LOGGING) {
            err_out.write(f.toString());
            err_out.newLine();
          }
          continue;
        }
        catch (MalformedURLException m) {
          outputPrintLn(m.toString());
          if (LOGGING) {
            err_out.write(m.toString());
            err_out.newLine();
          }
          continue;
        }

        if (ResponseCode/100 == 4)
          fileOK = false;
        else {
          ResponseMessage = HTTPconn.getResponseMessage();        
          ContentLength = HTTPconn.getContentLength();
          ContentType = HTTPconn.getContentType();
        }
        
      }

      if (urlOK & fileOK) {        

        outputPrintLn("Content type: " + ContentType + ".");
        if (LOGGING) {
          err_out.write("Content type: " + ContentType + ".");
          err_out.newLine();
        }

        if (ContentLength != -1) {
          outputPrintLn("Content length: " + ContentLength + " bytes.");
          if (LOGGING) {
            err_out.write("Content length: " + ContentLength + " bytes.");
            err_out.newLine();
          }
        }
        else {
          outputPrintLn("Content length: unknown.");
          if (LOGGING) {
            err_out.write("Content length: unknown.");
            err_out.newLine();
          }
        }
        err_out.flush();

        if (StringContains(ContentType, "text"))
          binary = false;
        else
          binary = true;

        if (!binary) {

          k = QuickSearch(ContentType, "charset=");
          if (k != -1)
            charset = ContentType.substring(k+8);
          else
            charset = "";

          try {
            if (charset != "")
              insr = new InputStreamReader(HTTPconn.getInputStream(), charset);
            else
              insr = new InputStreamReader(HTTPconn.getInputStream());

            in = new BufferedReader(insr);
          }
          catch (FileNotFoundException e) {
            outputPrintLn(e.toString());
            continue;
          }

          if (ContainsVariables(URLAddress))
            URLAddress = RemVariables(URLAddress);

          if (ContainsLocalAnchor(URLAddress))
            URLAddress = RemLocalAnchor(URLAddress);

          file_name = getFileName(URLAddress);        

          if (IsDirectory(URLAddress)) {
            if (URLAddress.endsWith("/"))
              file_name = "index.html";
            else
              file_name = "/index.html";
          }

          if (NoFileExtension(URLAddress) & StringContains(ContentType, "html")) {
            if (URLAddress.endsWith("/"))
              file_name = "index.html";
            else
              file_name = "/index.html";
          }
          else
            URLAddress = remFileName(URLAddress);
      
          if (!makeFilePath(URLAddress, curr_dir).endsWith("/"))
            file_path = makeFilePath(URLAddress, curr_dir) + File.separatorChar + file_name;
          else
            file_path = makeFilePath(URLAddress, curr_dir) + file_name;
          
          file = new File(file_path);          

          outputPrintLn("Saving to: " + file.getAbsolutePath());
          if (LOGGING) {
            err_out.write("Saving to: " + file.getAbsolutePath());
            err_out.newLine();
            err_out.flush();
          }

          if (!SILENT) {
            do {
              Object[] possibilities = {"Download", "Skip", "Abort"};
              s = (String) JOptionPane.showInputDialog(
                                                    app,
                                                    "What do you want\n"
                                                    + "to do with this file?",
                                                    "Customized Dialog",
                                                    JOptionPane.PLAIN_MESSAGE,
                                                    icon,
                                                    possibilities,
                                                    "Download");
              if (s != null) {
                s = s.trim();
                if (s.equals("Skip")) {
                  insr.close();
                  continue;
                }
                else if (s.equals("Abort")) {
                  insr.close();
                  return;
                }
              }
            } while (s == null);

          }

          try {
            ok = file.createNewFile();
          }
          catch (IOException e) {
            outputPrintLn(e.toString());
            continue;
          }

          if (ok & file.canWrite()) {

            filew = new FileOutputStream(file);
              

            if (charset != "")
              outw = new OutputStreamWriter(filew, charset);
            else
              outw = new OutputStreamWriter(filew);

            while (!EOF) {
              get_tag_output = getTag('<', '>', insr); //EOF is set/unset in getTag method
              first_string = get_tag_output.getFirstString();

              if ( (first_string != "") ) {
                if (IN_SCRIPT)
                  FilesInScripts(EverythingInQuotes(first_string), curr_dir);

                outw.write(first_string); // we will never change non-tag strings, so we can write 'em down
              }

              temp_tag = get_tag_output.getSecString();
              if (temp_tag != "") { // if there is a tag...
                
                if (temp_tag.length() >= 7) {
                  beginning_of_tag = temp_tag.substring(0, 7);
                  beginning_of_tag = beginning_of_tag.toLowerCase();
                
                  if (temp_tag.startsWith(beginning_of_tag))
                    IN_SCRIPT = true;
                  else if (temp_tag.equalsIgnoreCase("</script>"))
                    IN_SCRIPT = false;
                }                

                FilesInScripts(EverythingInQuotes(temp_tag), curr_dir);
                //I've enabled this because there can be an inline script or some unknown tag fields (other than href, src)

                temp = getAnchor(temp_tag); // see if there's some "href" or "src" in tag
                if ((temp != "") && IsFile(temp, true)) { // if there is...

                  type = typeOfURL(temp);

                  tempwola = temp;
                  if (ContainsLocalAnchor(temp))
                    tempwola = RemLocalAnchor(temp);
                  
                  if (type.equals("absolute")) {
                    if (sameDomain(temp)) {
                      if (INFINITE)
                        DEPTH = 1;

                      if (!TRIED_FILES.contains(tempwola)) {
                        if (DEPTH > 0) {
                          DEPTH--;                        
                          added = TRIED_FILES.add(tempwola);
                          ts = new ThreeStrings(tempwola, makeCurrDir(tempwola, curr_dir), DOMAIN);
                          queue.Push(ts);
                          rel_addr = AbsToRel(temp, curr_dir, DOMAIN);
                          temp_tag = Replace(temp_tag, temp, rel_addr);
                        }
                      }
                    }
                    else {
                      if (INFINITE)
                        DEPTH = 1;

                      if (!TRIED_FILES.contains(tempwola)) {
                        if (FOREIGN_DOMAIN_ALLOWED & (DEPTH > 0)) {
                          DEPTH--;
                          added = TRIED_FILES.add(tempwola);
                          ts = new ThreeStrings(tempwola , makeCurrDir(tempwola, "#"), getDomain(temp));
                          queue.Push(ts);
                          rel_addr = AbsToRel(temp, curr_dir, getDomain(temp));
                          temp_tag = Replace(temp_tag, temp, rel_addr);
                        }
                      }
                    }
                  }
                  else if (type.equals("relative")) {
                    if (INFINITE)
                      DEPTH = 1;

                    temp_abs_addr = makeAbsAddr(tempwola, curr_dir);

                    if (!TRIED_FILES.contains(temp_abs_addr)) {
                      if (DEPTH > 0) {
                        DEPTH--;
                        added = TRIED_FILES.add(temp_abs_addr);
                        ts = new ThreeStrings(temp_abs_addr, makeCurrDir(tempwola, curr_dir), DOMAIN);
                        queue.Push(ts);                        
                      }
                    }
                    if (temp.startsWith("/"))
                      temp_tag = Replace(temp_tag, temp, "." + temp);

                  }
                  outw.write(temp_tag);
                }
                else {
                  outw.write(temp_tag);
                }
              }
            } // while (!EOF)
            EOF = false;
            outw.flush();
            outw.close();
            insr.close();
            in.close();
          }
          else { // if (file.createNewFile() & file.canWrite())
            if (file.exists()) {
              outputPrintLn("File specified already exists!");
              if (LOGGING) {
                err_out.write("File specified already exists!");
                err_out.newLine();
              }
            }
            else if (!file.canWrite()) {
              outputPrintLn("I can't write to the specified file!");
              if (LOGGING) {
                err_out.write("I can't write to the specified file!");
                err_out.newLine();
              }
            }
            err_out.flush();
          }
        } // if (!binary)
        else {
          ins = new BufferedInputStream( HTTPconn.getInputStream() );

          file_name = getFileName(URLAddress);
          URLAddress = remFileName(URLAddress);

          if (!makeFilePath(URLAddress, curr_dir).endsWith("/"))
            file = new File(makeFilePath(URLAddress, curr_dir) + File.separatorChar + file_name);
          else
            file = new File(makeFilePath(URLAddress, curr_dir) + file_name);

          try {
            fileos = new FileOutputStream(file);
          }
          catch (FileNotFoundException e) {
            outputPrintLn(e.toString());
            continue;
          }
          outos = new BufferedOutputStream(fileos);

          outputPrintLn("Saving to: " + file.getAbsolutePath());
          if (LOGGING) {
            err_out.write("Saving to: " + file.getAbsolutePath());
            err_out.newLine();
            err_out.flush();
          }

          while ( (r = ins.read()) != -1) {
            outos.write(r);
          }

          outos.flush();
          outos.close();
          ins.close();
        }

        err_out.flush();
      } // if (urlOK & fileOK)
      else {
        if (!urlOK) {
          outputPrintLn("URL IS NOT VALID: " + URLAddress);
          if (LOGGING) {
            err_out.write("URL IS NOT VALID: " + URLAddress);
            err_out.newLine();
          }
        }

        if (!fileOK) {
          outputPrintLn("FILE NOT FOUND: " + URLAddress);
          if (LOGGING) {
            err_out.write("FILE NOT FOUND: " + URLAddress);
            err_out.newLine();
          }
        }

        err_out.flush();
      }
    }
  }

  public void setValidChars() {
    Character temp_Character = null;
    char temp_char = 'a';
    boolean ok = false;

    VALID_CHARS = new HashSet(74);
    
    while (temp_char <= 'z') {
      temp_Character = new Character(temp_char);
      ok = VALID_CHARS.add(temp_Character);
      temp_char++;
    }
    temp_char = 'A';
    while (temp_char <= 'Z') {
      temp_Character = new Character(temp_char);
      ok = VALID_CHARS.add(temp_Character);
      temp_char++;
    }
    temp_char = '0';
    while (temp_char <= '9') {
      temp_Character = new Character(temp_char);
      ok = VALID_CHARS.add(temp_Character);
      temp_char++;
    }

    temp_Character = new Character(':');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('/');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('@');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('#');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('$');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('%');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('^');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('&');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('-');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('_');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('.');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character(',');
    ok = VALID_CHARS.add(temp_Character);
    temp_Character = new Character('?');
    ok = VALID_CHARS.add(temp_Character);
  }

  public void setDirTextField(String text) {
    dirText.setText(text);
  }

    public void InitAndStart(MakiEatsWeb app) throws IOException {
    boolean ok;
    ThreeStrings threes = null;
    Integer temp = null;

        FOREIGN_DOMAIN_ALLOWED = foreignCheckBox.isSelected();
    LOGGING = logCheckBox.isSelected();
        INFINITE = infCheckBox.isSelected();
    SILENT = silentCheckBox.isSelected();
        STARTING_URL = urlText.getText();
        USER_DIR = dirText.getText();

    temp = (Integer) depthSpinner.getValue();
    DEPTH = temp.intValue();

    if (USER_DIR.length() == 0) {
      JOptionPane.showMessageDialog(app,
                      "You must enter absolute path of directory!",
                      "Warning!",
                      JOptionPane.WARNING_MESSAGE);

      return;
    }

    if (STARTING_URL.length() == 0) {
      JOptionPane.showMessageDialog(app,
                      "You must enter address of the site you want to retrieve!",
                      "Warning!",
                      JOptionPane.WARNING_MESSAGE);

      return;
    }

    setValidChars();

        DOMAIN = getDomain(STARTING_URL);

        if (!StringContains(STARTING_URL, "://"))
        STARTING_URL = "http://" + STARTING_URL;

    if (IsDirectory(STARTING_URL)) {
      if (!STARTING_URL.endsWith("/"))
        STARTING_URL += "/";
    }

    err_output = new FileWriter("output.log");
    err_out = new BufferedWriter(err_output);

    ROOT_DIR_PATH = mkRootDirName(remFileName(STARTING_URL));

    if (!USER_DIR.endsWith(File.separator))
      USER_DIR = USER_DIR + File.separator;

    ROOT_DIR = new File(USER_DIR);

    ok = ROOT_DIR.mkdirs();
    
    if (ROOT_DIR.canWrite()) {

      TRIED_FILES = new HashSet(1000);
      ok = TRIED_FILES.add(STARTING_URL);

      threes = new ThreeStrings(STARTING_URL, ROOT_DIR_PATH, DOMAIN);

      queue = new TQueue();
      queue.Push(threes);

      RWFactory(app);

      outputPrintLn("--------------------------------------------------------------------------------");
      JOptionPane.showMessageDialog(app,
                      "Site retrieval finished!",
                      "Information!",
                      JOptionPane.INFORMATION_MESSAGE);

      err_out.close();
      return;

    }
    else {
      JOptionPane.showMessageDialog(app,
                      "Can't write to specified directory!",
                      "Warning!",
                      JOptionPane.WARNING_MESSAGE);

      return;
    }
    }

    public Component createComponents(MakiEatsWeb app) {                

    numLabel = new JLabel("Number of links to follow:");
        numLabel.setPreferredSize(new Dimension(180, 15));
        numLabel.setMinimumSize(new Dimension(170, 15));
        numLabel.setHorizontalAlignment(SwingConstants.LEFT);

        depthSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
        depthSpinner.setPreferredSize(new Dimension(60, 20));
        depthSpinner.setMinimumSize(new Dimension(60, 20));

    JPanel numPanel = new JPanel();
        numPanel.setLayout(new BoxLayout(numPanel, BoxLayout.X_AXIS));
        numPanel.add(Box.createHorizontalGlue());
        numPanel.add(numLabel, BorderLayout.WEST);
        numPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        numPanel.add(depthSpinner, BorderLayout.WEST);
    numPanel.add(Box.createRigidArea(new Dimension(153, 0)));
        numPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    logCheckBox = new JCheckBox("Log to output.log?", false);
    logCheckBox.setPreferredSize(new Dimension(180, 15));
        logCheckBox.setMinimumSize(new Dimension(170, 15));

    JPanel logPanel = new JPanel();
    logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.X_AXIS));
    logPanel.add(Box.createHorizontalGlue());
    logPanel.add(logCheckBox);
    logPanel.add(Box.createRigidArea(new Dimension(215, 0)));

    output = new JTextArea();
    output.setPreferredSize(new Dimension(500, 150));
    output.setMinimumSize(new Dimension(500, 140));
    output.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    output.setEditable(false);

    outputLabel = new JLabel("Output:");
    outputLabel.setPreferredSize(new Dimension(70, 15));
    outputLabel.setMinimumSize(new Dimension(60, 15));

    JPanel temp_outPanel = new JPanel();
    temp_outPanel.setLayout(new BoxLayout(temp_outPanel, BoxLayout.X_AXIS));
    temp_outPanel.add(outputLabel);
    temp_outPanel.add(Box.createRigidArea(new Dimension(435, 0)));

    JPanel outputPanel = new JPanel();
    outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
    outputPanel.add(temp_outPanel);
    outputPanel.add(Box.createRigidArea(new Dimension(0, 5)));
    outputPanel.add(output);

    urlLabel = new JLabel("Enter address of site/page you want to retrieve:");
        urlLabel.setPreferredSize(new Dimension(200, 15));
        urlLabel.setMinimumSize(new Dimension(200, 15));
        urlLabel.setHorizontalAlignment(SwingConstants.LEFT);

        urlText = new JTextField();
      urlText.setPreferredSize(new Dimension(200, 20));
      urlText.setMinimumSize(new Dimension(200, 20));
      urlText.setBorder(BorderFactory.createLineBorder(Color.black, 1));

    JPanel temp_urlPanel = new JPanel();
    temp_urlPanel.setLayout(new BoxLayout(temp_urlPanel, BoxLayout.X_AXIS));
    temp_urlPanel.add(urlLabel);
    temp_urlPanel.add(Box.createRigidArea(new Dimension(180, 0)));  

    ImageIcon att_icon = new ImageIcon("images/button.png");
        main_button = new JButton(att_icon);
        main_button.setHorizontalAlignment(SwingConstants.CENTER);
        main_button.setPreferredSize(new Dimension(70, 70));
        main_button.setMaximumSize(new Dimension(70, 70));
        main_button.setMinimumSize(new Dimension(70, 70));
        main_button.addActionListener(new MainListener(app));
    main_button.setToolTipText("Start download!");

    dirLabel = new JLabel("Directory to store files in:");
        dirLabel.setPreferredSize(new Dimension(180, 15));
        dirLabel.setMinimumSize(new Dimension(170, 15));
        dirLabel.setHorizontalAlignment(SwingConstants.LEFT);

    dirText = new JTextField();
      dirText.setPreferredSize(new Dimension(150, 20));
      dirText.setMinimumSize(new Dimension(150, 20));
      dirText.setBorder(BorderFactory.createLineBorder(Color.black, 1));

    directory_button = new JButton("Open");
    directory_button.setHorizontalAlignment(SwingConstants.CENTER);
    directory_button.setPreferredSize(new Dimension(70, 20));
        directory_button.setMaximumSize(new Dimension(70, 20));
        directory_button.setMinimumSize(new Dimension(70, 20));
    directory_button.addActionListener(new DirListener(app));

    JPanel temp_dirPanel = new JPanel();
    temp_dirPanel.setLayout(new BoxLayout(temp_dirPanel, BoxLayout.X_AXIS));
    temp_dirPanel.add(dirLabel);
    temp_dirPanel.add(Box.createRigidArea(new Dimension(320, 0)));

    JPanel temp_dirPanel2 = new JPanel();
    temp_dirPanel2.setLayout(new BoxLayout(temp_dirPanel2, BoxLayout.X_AXIS));
    temp_dirPanel2.add(dirText);
    temp_dirPanel2.add(Box.createRigidArea(new Dimension(10, 0)));
    temp_dirPanel2.add(directory_button);

        JPanel dirPanel = new JPanel();
        dirPanel.setLayout(new BoxLayout(dirPanel, BoxLayout.Y_AXIS));
        dirPanel.add(Box.createVerticalGlue());
        dirPanel.add(temp_dirPanel);
        dirPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        dirPanel.add(temp_dirPanel2);
        dirPanel.add(Box.createRigidArea(new Dimension(0, 15)));
        dirPanel.add(temp_urlPanel);
        dirPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        dirPanel.add(urlText, BorderLayout.WEST);
        dirPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    foreignCheckBox = new JCheckBox("Allow following links to other sites?", false);

    JPanel foreignPanel = new JPanel();
    foreignPanel.setLayout(new BoxLayout(foreignPanel, BoxLayout.X_AXIS));
    foreignPanel.add(Box.createHorizontalGlue());
    foreignPanel.add(foreignCheckBox);
    foreignPanel.add(Box.createRigidArea(new Dimension(140, 0)));

    infCheckBox = new JCheckBox("Retrieve infinite number of links?", false);

    JPanel infPanel = new JPanel();
    infPanel.setLayout(new BoxLayout(infPanel, BoxLayout.X_AXIS));
    infPanel.add(Box.createHorizontalGlue());
    infPanel.add(infCheckBox);
    infPanel.add(Box.createRigidArea(new Dimension(160, 0)));

    silentCheckBox = new JCheckBox("Silent mode on?", true);

    JPanel silentPanel = new JPanel();
    silentPanel.setLayout(new BoxLayout(silentPanel, BoxLayout.X_AXIS));
    silentPanel.add(Box.createHorizontalGlue());
    silentPanel.add(silentCheckBox);
    silentPanel.add(Box.createRigidArea(new Dimension(269, 0)));

        JPanel optionsPanel = new JPanel();
        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS));
        optionsPanel.add(Box.createHorizontalGlue());
        optionsPanel.add(numPanel);
        optionsPanel.add(foreignPanel);
        optionsPanel.add(infPanel);
        optionsPanel.add(silentPanel);
    optionsPanel.add(logPanel);
        optionsPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        JPanel tempPanel = new JPanel();
        tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS));
        tempPanel.add(Box.createHorizontalGlue());
        tempPanel.add(main_button);
        tempPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        tempPanel.add(optionsPanel);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.add(tempPanel, BorderLayout.CENTER);
        mainPanel.add(dirPanel, BorderLayout.WEST);
    mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
    mainPanel.add(outputPanel);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        return mainPanel;
    }

    public static void main(String[] args) throws Exception {

        String first_string = "";
        String temp3 = "";
        String inputLine = "", temp = "", type = "", temp2 = "";
        TwoStrings ts = null;
        ThreeStrings threes = null;
        boolean ok = false, urlOK = true, fileOK = true;
        URL URLconn = null;
        Integer temp_depth;
        
        try {
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) { }

        //Create the top-level container and add contents to it.
        JFrame frame = new JFrame(APP_NAME + " " + APP_VERSION);
        MakiEatsWeb app = new MakiEatsWeb();
        Component contents = app.createComponents(app);
        frame.getContentPane().add(contents, BorderLayout.CENTER);

        //Finish setting up the frame, and show it.
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }

}

class DirListener implements ActionListener {
  MakiEatsWeb app_ref = null;
  JFileChooser fc = null;
  DirectoryFilter df = null;

  /**
    @param app Reference to MainWindow so we can call MainWindow methods.
  */
  public DirListener(MakiEatsWeb app) {
    app_ref = app;
    fc = new JFileChooser();
    df = new DirectoryFilter();
    fc.addChoosableFileFilter(df);
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  }
  
  /**
    Implemented method from ActionListener interface.
  */
  public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showOpenDialog(app_ref);
    
    if (returnVal == JFileChooser.APPROVE_OPTION) { // Creates new dialog, file chooser
      File file = fc.getSelectedFile();
      app_ref.setDirTextField(file.getAbsolutePath());
    }
  }
}

class MainListener implements ActionListener, Runnable {
  MakiEatsWeb app_ref = null;

  public MainListener(MakiEatsWeb app) {
    app_ref = app;
  }

  private void start_download() {
    try {
      app_ref.InitAndStart(app_ref);
    }
    catch (IOException e) {
      JOptionPane.showMessageDialog(app_ref,
                      e.toString(),
                      "Warning!",
                      JOptionPane.WARNING_MESSAGE);
    }
  }

  public void run() {
    this.start_download();
  }

  public void actionPerformed(ActionEvent e) {
    // Create new thread.
    Runnable instance = new MainListener(app_ref);
    // Start it.
    new Thread(instance).start();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.