//
// MSIEtoHTML.java -- Converts Microsoft Internet Explorer
// bookmarks to HTML format.
//
// David Moisan
// dmoisan@shore.net
// April 1st, 1999
// Version 1.1
//
// This code is in the public domain
//
// Usage:
//
// MSIEtoHTML ["Title"]
//
// Location of the Internet Explorer shortcuts
// (usually C:\windows\favorites)
//
// The HTML file that holds the converted
// shortcuts
//
// ["Title"] Optional title for the bookmarks file
// If omitted, the title is "Bookmarks of "
//
//
// Changes:
//
// MSIEtoHTML will no longer try to parse non-IE-shortcut files
// (files that don't end in ".URL" or ".url") The FilenameFilter
// class that was supposed to filter out non-IE shortcuts was
// removed--it's a known bug in Java 1.1.
//
// MSIEtoHTML now processes IE5 shortcuts correctly.
// The "Total Shortcuts" count is now accurate.
//
//
// Future changes (if I have time, which is unlikely...):
//
// 1) Bookmarks will be read into a tree structure in memory, thus
// eliminating empty lists in the HTML code caused by empty
// directories.
//
// 2) HTML Output will pass validation. The HTML in this version
// is much better than the last version since it escapes out
// spurious ampersands, but it does not pass validation presently,
// probably due to improper nesting of the list elements.
// Change #1 should make this easier to accomplish.
//
// 3) A GUI has been in the works, forever. Since the command
// line interface fit my needs already and the AWT (in Java 1.0.2)
// was so miserable to code, I've never given a thought to a GUI.
// If I find a good GUI builder, that could change.
//
// 4) Improve the simplistic exchange sort I use to sort shortcuts
// so that the directories end up near the head of the list, and
// files are alphabetized. Presently, it's brutally slow for
// anything over about 100 items.
//
import java.io.*;
import java.util.*;
public class MSIEtoHTML {
static long tot_files = 0; // Total number of shortcuts
static void TraverseTree(WriteHTMLFile Bookmarks, String dirspec) throws IOException {
// Walks through specified directory tree
File Spec = new File(dirspec);
String linkname;
String linkURL;
String URLFile;
String newdir;
String dirlist[];
MSIEParser Parse;
// If file doesn't exist, report that and return
if (!Spec.exists()) {
System.out.println ("File not found:" + Spec.getAbsolutePath());
}
else if (Spec.isFile()) {
int i = (Spec.getName()).lastIndexOf(".");
// Get the link text from the filename
// minus the ".url" extension
//
if (i==-1)
linkname = Spec.getName();
else
linkname = Spec.getName().substring(0,i);
try {
// Parse the shortcut file for the
// URL
Parse = new MSIEParser();
URLFile = Spec.getAbsolutePath();
//
// Filter out any files that aren't shortcuts
// (that do not end in .URL or .url
//
if (URLFile.endsWith(".URL")
|| URLFile.endsWith(".url")) {
linkURL = Parse.MSIEGetURL(URLFile);
Bookmarks.ListItemTag();
Bookmarks.AnchorLink(linkURL,linkname,false);
Bookmarks.ListItemEndTag();
Bookmarks.WriteNewline(true);
tot_files++;
}
}
catch (BadIEShortcut e) {
System.out.println(e);
}
}
else if (Spec.isDirectory()) {
//
// For directories, create a heading and
// start a new unordered list
//
Bookmarks.HeadingTag(2,Spec.getName());
Bookmarks.UnordListTag();
// Get the directory and sort it putting
// files first, subdirectories last
// to make the list more readable
//
dirlist = SortDir(Spec.getPath(),Spec.list());
for (int i=0;i < dirlist.length; i++) {
newdir = dirspec+File.separator+dirlist[i];
try {
TraverseTree(Bookmarks, newdir);
}
catch (IOException e) {
System.out.println(e);
}
}
Bookmarks.UnordListEndTag();
// Once done, end the list
}
else {
System.err.println("Internal error: unknown file"+Spec.getName());
System.exit(0);
}
}
// SortDir -- Given a directory list and its path
// sort it, putting files at the beginning, directories
// towards the end
//
static String[] SortDir(String path, String[] dirlist) {
String temp;
if (dirlist.length > 1) {
for (int i=0; i < dirlist.length-1; i++) {
for (int j=i+1; j < dirlist.length; j++) {
File Fi = new File(path+File.separator+dirlist[i]);
File Fj = new File(path+File.separator+dirlist[j]);
if (Fi.isDirectory() & Fj.isFile())
{
temp = dirlist[i];
dirlist[i] = dirlist[j];
dirlist[j] = temp;
}
else if ((Fi.isDirectory() & Fj.isDirectory())
& (Fi.getName().compareTo(Fj.getName()) > 0))
{
temp = dirlist[i];
dirlist[i] = dirlist[j];
dirlist[j] = temp;
}
else if ((Fi.isFile() & Fj.isFile())
& (Fi.getName().compareTo(Fj.getName()) > 0))
{
temp = dirlist[i];
dirlist[i] = dirlist[j];
dirlist[j] = temp;
}
}
}
}
return dirlist;
}
//
// Main procedure
//
public static void main (String argv[]) {
String Dirspec;
String Bookspec;
String BookmarkTitle;
Date today = new Date();
WriteHTMLFile Bookmarks;
//
// If arguments not provided, then end
//
if (argv.length <2) {
System.out.println("Usage: MSIEtoHTML [\"Title\"]");
System.exit(0);
}
else {
Dirspec = argv[0];
Bookspec = argv[1];
if (argv.length == 3) {
BookmarkTitle = argv[2];
}
else {
BookmarkTitle = "Bookmarks of "+argv[0];
}
try {
Bookmarks = new WriteHTMLFile(Bookspec);
//
// Write title and heading
//
Bookmarks.HeadTag();
Bookmarks.TitleTag(BookmarkTitle);
Bookmarks.HeadEndTag();
Bookmarks.BodyTag();
Bookmarks.HeadingTag(1,BookmarkTitle);
//
// Write today's date on the HTML file
//
Bookmarks.LineBreakTag();
Bookmarks.CiteTag("Generated on: "+today.toString());
Bookmarks.WriteNewline(true);
//
// And a citation for this program
//
Bookmarks.LineBreakTag();
Bookmarks.CiteTag("Generated by MSIEtoHTML, version 1.1 (April 1999)");
Bookmarks.LineBreakTag();
Bookmarks.WriteNewline(true);
//
// Start traversing the Favorites tree
//
TraverseTree(Bookmarks, Dirspec); //Walk through the directory
//
// Put horizontal rule, statistics and close the HTML file
//
Bookmarks.HorizRuleTag();
Bookmarks.WriteContent("** Total Shortcuts: "+ tot_files);
Bookmarks.LineBreakTag();
Bookmarks.CiteTag("Generated on: "+today.toString());
Bookmarks.WriteNewline(true);
//
// And a citation for this program
//
Bookmarks.LineBreakTag();
Bookmarks.CiteTag("Generated by MSIEtoHTML, version 1.1");
Bookmarks.WriteNewline(true);
Bookmarks.BodyEndTag();
Bookmarks.CloseHTML();
System.out.println("** Total Shortcuts: " + tot_files);
}
catch (IOException e) {
System.out.println("I/O Exception: "+e);
}
finally {
System.exit(0);
}
}
}
}