/** @param args */
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    /* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)
     * for the correct SWT library path in order to run with the SWT dlls.
     * The dlls are located in the SWT plugin jar.
     * For example, on Windows the Eclipse SWT 3.1 plugin jar is:
     *       installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
     */
    Display display = Display.getDefault();
    ActiveHostsFileWindow thisClass = new ActiveHostsFileWindow();
    thisClass.createSShell();
    thisClass.sShell.open();

    /*
     * Loads active hosts file
     */
    File activeHostsFile = getActiveHostsFile();
    thisClass.highlightActiveHostsFile(activeHostsFile);
    thisClass.label.setText("Current hosts file (located at " + activeHostsFile.getPath() + ")");

    HostsFile hf = thisClass.loadHostsFile(activeHostsFile);

    System.out.println("Lines: " + hf.getHostsFileLines().size());

    HostsFileDAO hfDAO = new HostsFileSerializationDAO();
    hfDAO.save(hf);

    while (!thisClass.sShell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
  /**
   * Parses a hosts file and loads it into its corresponding Javabean representation.
   *
   * @param hostsFile
   * @return
   * @throws IOException
   */
  private HostsFile loadHostsFile(File hostsFile) throws IOException, ParseException {
    /* Object to be returned */
    HostsFile hostsFileObject = new HostsFile();

    BufferedReader br = null;

    int lineCounter = 1;
    try {
      br = new BufferedReader(new FileReader(hostsFile));

      String line = null;
      while ((line = br.readLine()) != null) {
        line += "\n";

        /* Remark */
        if (line.startsWith(REM_LINE_CHAR)) {
          CommentLine commentLine = new CommentLine();
          commentLine.setLine(line);
          hostsFileObject.addLine(commentLine);
        } else if (StringUtils.isBlank(line)) // Blank line
        {
          BlankLine blankLine = new BlankLine();
          blankLine.setLine(line);
          hostsFileObject.addLine(blankLine);
        } else {
          Scanner scanner = new Scanner(line);
          HostLine hostLine = new HostLine();

          if (scanner.hasNext()) {
            /* Expects an IP address */
            String ipAddress = scanner.next();

            if (NetworkUtils.isIpAddress(ipAddress)) {
              hostLine.setIpAddress(ipAddress);
            } else
              throw new ParseException(
                  "Expected an IP address but found '" + ipAddress + "'", lineCounter);

            /* Expects a list of hostnames */
            List<String> hostNames = new LinkedList<String>();
            String hostName = null;
            while (scanner.hasNext()) {
              hostName = scanner.next();

              if (NetworkUtils.isHostName(hostName)) {
                hostNames.add(hostName);
              } else
                throw new ParseException(
                    "Expected a hostname but found '" + hostName + "'", lineCounter);
            }

            hostLine.setHostNames(hostNames);
            hostLine.setLine(line);
            hostsFileObject.addLine(hostLine);
          }
        }
      }
    } finally {
      if (br != null) br.close();
    }

    return hostsFileObject;
  }