home *** CD-ROM | disk | FTP | other *** search
- //
- // MSIEtoHTML.java -- Converts Microsoft Internet Explorer
- // bookmarks to HTML format.
- //
- // David Moisan
- // May 8th, 1996
- // Version 1.0
- //
- // Usage:
- //
- // MSIEtoHTML <favdir> <bookmarks> ["Title"]
- //
- // <favdir> Location of the Internet Explorer shortcuts
- // (usually C:\windows\favorites)
- //
- // <bookmarks> The HTML file that holds the converted
- // shortcuts
- //
- // ["Title"] Optional title for the bookmarks file
- // If omitted, the title is "Bookmarks of <bookmarks>"
- //
- //
-
- 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 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();
- linkURL = Parse.MSIEGetURL(Spec.getAbsolutePath());
- Bookmarks.ListItemTag();
- Bookmarks.AnchorLink(linkURL,linkname,true);
- Bookmarks.WriteNewline(true);
- }
- catch (BadIEShortcut e) {
- System.out.println(e);
- }
- finally {
- tot_files++;
- }
- }
- 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;
- FileExtension Ext = new FileExtension(".url");
- // If arguments not provided, then end
-
- if (argv.length <2) {
- System.out.println("Usage: MSIEtoHTML <favorites> <HTMLfile> [\"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.EmphTag();
- Bookmarks.WriteContent(today.toString());
- Bookmarks.EmphEndTag();
- 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.LineBreakTag();
- Bookmarks.WriteContent("** Total Shortcuts: "+ tot_files);
- 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);
- }
- }
- }
- }
-
- //
- // FileExtension -- Defines a FilenameFilter
- //
-
- class FileExtension implements FilenameFilter {
- private String ext;
- FileExtension(String ext) {
- this.ext = ext;
- }
-
- public boolean accept(File dir, String name) {
- if (name.endsWith(ext))
- return true;
- else
- return false;
- }
- }