// Updates: 2007.08.16, 2007.07.17, 2004.08.11


import javax.swing.*;
import java.io.*;


/**
 * This program constructs an html bookmark from an input text file like:
 * <pre>
 *
 * $t Search
 * Google  http://www.google.com 
 * Metacrawler  http://www.metacrawler.com
 * Yahoo  http://www.yahoo.com
 *
 * $t Java
 * Freewarejava  http://www.freewarejava.com/
 * Sun  http://java.sun.com/
 * Tutorial  http://java.sun.com/docs/books/tutorial/
 *
 * </pre>
 *
 * Notes:<br/>
 * - "$t" means title.<br/>
 * - There are two spaces between the name and the link.
 *
 */
public class MakePBM {
  public static final String DESCRIPTION = "FoxyTag, a free, legal and collaborative system to signal speed cameras on mobile phones.";
  public static final String KEYWORDS = "Michel, Deriaz, pbm, foxytag, speed camera, radar, mobile, phone, gps, tag, j2me, java";
  public static final String IDENTIFIER_URL = "http://www.michelderiaz.com";
  public static final String TITLE_IMAGE = "foxycircletext.png";
  public static final String TITLE_LINK = "http://www.foxytag.com";
  public static final String HOME_IMAGE = "house.png";
  public static final String HOME_LINK = "http://cui.unige.ch/~deriazm/";
  public static final String INPUT_FILENAME = "CoolLinks.txt";
  public static final String OUTPUT_FILENAME = "pbm.html";


  public static void main(String[] args) {
    String menu = "";
    String output = "";
    String[] lines = readFile(INPUT_FILENAME).split(System.getProperty("line.separator"));
    for (int i = 0; i < lines.length; i++) {
      String s = lines[i];
      if (s.toLowerCase().startsWith("$t")) menu += "<a href=\"#" + removeAccents(s.substring(3)).replaceAll(" ", "_") + "\">" + s.substring(3) + "</a> ||\n";
    }
    for (int i = 0; i < lines.length; i++) {
      String s = lines[i];
      if (s.length() < 2) continue;
      else if (s.toLowerCase().startsWith("$t")) {
        if (output.equals("")) output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
            + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n"
            + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\n"
            + "<meta http-equiv=\"pragma\" content=\"no-cache\"/>\n"
            + "<meta name=\"description\" content=\"" + DESCRIPTION + "\"/>\n"
            + "<meta name=\"keywords\" content=\"" + KEYWORDS + "\"/>\n"
            + "<meta name=\"indentifier-url\" content=\"" + IDENTIFIER_URL + "\"/>\n"
            + "<meta name=\"robots\" content=\"all\"/>\n"
            + "<meta name=\"revisit-after\" content=\"7\"/>\n"
            + "<title>Public bookmarks</title>\n</head>\n"
            + "<body>\n<br/><br/><br/><br/><center><a href=\"" + TITLE_LINK + "\"><img src=\"" + TITLE_IMAGE + "\" border=\"0\" alt=\"\"/></a></center><br/><br/><br/><br/>\n\n";
        else output += sectionEnd();
        output += "<center>\n<a name=\"" + removeAccents(s.substring(3)).replaceAll(" ", "_") + "\"></a> ||\n" + menu + "<br/><br/>\n<h1>" + s.substring(3) + "</h1></center>\n";
      }
      else if (!s.matches(".+  .+")) {
        JOptionPane.showMessageDialog(null, "Error in line " + (i+1), "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
      }
      else output += "<a href=\"" + s.substring(s.indexOf("  ") + 2) + "\">" + s.substring(0, s.indexOf("  ")) + "</a><br/>\n";
    }
    writeFile(OUTPUT_FILENAME, accentsToHtml(output) + sectionEnd() + "</body>\n</html>");
  }


  private static String sectionEnd() {
    StringBuffer sb = new StringBuffer();
    sb.append("<br/><p align=\"right\">\n");
    sb.append("<a href=\"" + HOME_LINK + "\"><img src=\"" + HOME_IMAGE + "\" border=\"0\" alt=\"\"/></a>\n");
    sb.append("<a href=\"http://validator.w3.org/check?uri=referer\"><img src=\"http://www.w3.org/Icons/valid-xhtml10\" border=\"0\" alt=\"Valid XHTML 1.0!\" height=\"31\" width=\"88\" /></a>\n");
    sb.append("</p>\n");
    for (int i = 0; i < 60; i++) {
      sb.append("<br/>");
    }
    sb.append("\n\n");
    return sb.toString();
  }


  private static String removeAccents(String input) {
    return input
    .replaceAll("À", "A")
    .replaceAll("Á", "A")
    .replaceAll("Â", "A")
    .replaceAll("Ã", "A")
    .replaceAll("Ä", "A")
    .replaceAll("Å", "A")
    .replaceAll("Æ", "AE")
    .replaceAll("à", "a")
    .replaceAll("á", "a")
    .replaceAll("â", "a")
    .replaceAll("ã", "a")
    .replaceAll("ä", "a")
    .replaceAll("å", "a")
    .replaceAll("æ", "ae")
    .replaceAll("Ç", "C")
    .replaceAll("ç", "c")
    .replaceAll("Ð", "D")
    .replaceAll("ð", "d")
    .replaceAll("È", "E")
    .replaceAll("É", "E")
    .replaceAll("Ê", "E")
    .replaceAll("Ë", "E")
    .replaceAll("è", "e")
    .replaceAll("é", "e")
    .replaceAll("ê", "e")
    .replaceAll("ë", "e")
    .replaceAll("Ì", "I")
    .replaceAll("Í", "I")
    .replaceAll("Î", "I")
    .replaceAll("Ï", "I")
    .replaceAll("ì", "i")
    .replaceAll("í", "i")
    .replaceAll("î", "i")
    .replaceAll("ï", "i")
    .replaceAll("Ñ", "N")
    .replaceAll("ñ", "n")
    .replaceAll("Ò", "O")
    .replaceAll("Ó", "O")
    .replaceAll("Ô", "O")
    .replaceAll("Õ", "O")
    .replaceAll("Ö", "O")
    .replaceAll("Ø", "O")
    .replaceAll("Œ", "OE")
    .replaceAll("ò", "o")
    .replaceAll("ó", "o")
    .replaceAll("ô", "o")
    .replaceAll("õ", "o")
    .replaceAll("ö", "o")
    .replaceAll("ø", "o")
    .replaceAll("œ", "oe")
    .replaceAll("Š", "S")
    .replaceAll("š", "s")
    .replaceAll("Ù", "U")
    .replaceAll("Ú", "U")
    .replaceAll("Û", "U")
    .replaceAll("Ü", "U")
    .replaceAll("ù", "u")
    .replaceAll("ú", "u")
    .replaceAll("û", "u")
    .replaceAll("ü", "u")
    .replaceAll("Ý", "Y")
    .replaceAll("Ÿ", "Y")
    .replaceAll("ý", "y")
    .replaceAll("ÿ", "y")
    .replaceAll("Ž", "Z")
    .replaceAll("ž", "z");
  }


  private static String accentsToHtml(String input) {
    return input
    .replaceAll("À", "&Agrave;")
    .replaceAll("Á", "&Aacute;")
    .replaceAll("Â", "&Acirc;")
    .replaceAll("Ã", "&Atilde;")
    .replaceAll("Ä", "&Auml;")
    .replaceAll("Å", "&Aring;")
    .replaceAll("Æ", "&Aelig;")
    .replaceAll("à", "&agrave;")
    .replaceAll("á", "&aacute;")
    .replaceAll("â", "&acirc;")
    .replaceAll("ã", "&atilde;")
    .replaceAll("ä", "&auml;")
    .replaceAll("å", "&aring;")
    .replaceAll("æ", "&aelig;")
    .replaceAll("Ç", "&Ccedil;")
    .replaceAll("ç", "&ccedil;")
    .replaceAll("Ð", "&ETH;")
    .replaceAll("ð", "&eth;")
    .replaceAll("È", "&Egrave;")
    .replaceAll("É", "&Eacute;")
    .replaceAll("Ê", "&Ecirc;")
    .replaceAll("Ë", "&Euml;")
    .replaceAll("è", "&egrave;")
    .replaceAll("é", "&eacute;")
    .replaceAll("ê", "&ecirc;")
    .replaceAll("ë", "&euml;")
    .replaceAll("Ì", "&Igrave;")
    .replaceAll("Í", "&Iacute;")
    .replaceAll("Î", "&Icirc;")
    .replaceAll("Ï", "&Iuml;")
    .replaceAll("ì", "&igrave;")
    .replaceAll("í", "&iacute;")
    .replaceAll("î", "&icirc;")
    .replaceAll("ï", "&iuml;")
    .replaceAll("Ñ", "&Ntilde;")
    .replaceAll("ñ", "&ntilde;")
    .replaceAll("Ò", "&Ograve;")
    .replaceAll("Ó", "&Oacute;")
    .replaceAll("Ô", "&Ocirc;")
    .replaceAll("Õ", "&Otilde;")
    .replaceAll("Ö", "&Ouml;")
    .replaceAll("Ø", "&Oslash;")
    .replaceAll("Œ", "&OElig;")
    .replaceAll("ò", "&ograve;")
    .replaceAll("ó", "&oacute;")
    .replaceAll("ô", "&ocirc;")
    .replaceAll("õ", "&otilde;")
    .replaceAll("ö", "&ouml;")
    .replaceAll("ø", "&oslash;")
    .replaceAll("œ", "&oelig;")
    .replaceAll("Ù", "&Ugrave;")
    .replaceAll("Ú", "&Uacute;")
    .replaceAll("Û", "&Ucirc;")
    .replaceAll("Ü", "&Uuml;")
    .replaceAll("ù", "&ugrave;")
    .replaceAll("ú", "&uacute;")
    .replaceAll("û", "&ucirc;")
    .replaceAll("ü", "&uuml;")
    .replaceAll("Ý", "&Yacute;")
    .replaceAll("Ÿ", "&Yuml;")
    .replaceAll("ý", "&yacute;")
    .replaceAll("ÿ", "&yuml;");
  }


  private static String readFile(String filename) {
    File f = new File(filename);
    int size = (int)f.length();
    char cbuf[] = new char[size];
    try {
      FileReader fr = new FileReader(filename);
      fr.read(cbuf);
      fr.close();
    }
    catch(IOException ioe) {
      return null;
    }
     return new String(cbuf);
  }


  private static void writeFile(String filename, String text) {
    try {
      FileWriter fw = new FileWriter(filename);
      fw.write(text);
      fw.close();
    }
    catch(IOException ioe) {
      System.out.println(ioe);
    }
  }
}