home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / APPLETS / MSIETOHT.JAV < prev    next >
Encoding:
Text File  |  1997-02-27  |  6.2 KB  |  251 lines

  1. //
  2. // MSIEtoHTML.java -- Converts Microsoft Internet Explorer
  3. // bookmarks to HTML format.
  4. //
  5. // David Moisan
  6. // May 8th, 1996
  7. // Version 1.0
  8. //
  9. // Usage:
  10. //
  11. // MSIEtoHTML <favdir> <bookmarks> ["Title"]
  12. //
  13. // <favdir>    Location of the Internet Explorer shortcuts
  14. //             (usually C:\windows\favorites)
  15. //
  16. // <bookmarks> The HTML file that holds the converted
  17. //             shortcuts
  18. //
  19. // ["Title"]   Optional title for the bookmarks file
  20. //             If omitted, the title is "Bookmarks of <bookmarks>"
  21. //
  22. //
  23.  
  24. import java.io.*;
  25. import java.util.*;
  26.  
  27. public class MSIEtoHTML {
  28.  
  29.     static long tot_files = 0; // Total number of shortcuts 
  30.  
  31.    static void TraverseTree(WriteHTMLFile Bookmarks, String dirspec) throws IOException {
  32.  
  33.    // Walks through specified directory tree
  34.     
  35.        
  36.         File Spec = new File(dirspec);
  37.         String linkname;
  38.         String linkURL;
  39.         String newdir;
  40.       String dirlist[];
  41.         MSIEParser Parse;        
  42.       
  43.       
  44.       // If file doesn't exist, report that and return
  45.       if (!Spec.exists()) {
  46.             System.out.println ("File not found:" +  Spec.getAbsolutePath());
  47.          }
  48.       else if (Spec.isFile()) {
  49.             int i = (Spec.getName()).lastIndexOf(".");
  50.             
  51.          // Get the link text from the filename
  52.             // minus the ".url" extension
  53.          //            
  54.          if (i==-1) 
  55.                 linkname = Spec.getName();
  56.             else
  57.                  linkname = Spec.getName().substring(0,i);
  58.                 
  59.         try {
  60.             
  61.                 // Parse the shortcut file for the 
  62.             // URL
  63.              
  64.             Parse = new MSIEParser();
  65.                 linkURL = Parse.MSIEGetURL(Spec.getAbsolutePath());
  66.                 Bookmarks.ListItemTag();
  67.                 Bookmarks.AnchorLink(linkURL,linkname,true);
  68.                 Bookmarks.WriteNewline(true);
  69.                 } 
  70.             catch (BadIEShortcut e) {
  71.                 System.out.println(e);
  72.                 }
  73.             finally {
  74.                 tot_files++;
  75.                 }
  76.             }
  77.       else if (Spec.isDirectory())  {
  78.          //
  79.          // For directories, create a heading and
  80.          // start a new unordered list
  81.          //
  82.         
  83.             Bookmarks.HeadingTag(2,Spec.getName());
  84.             Bookmarks.UnordListTag();
  85.             
  86.          // Get the directory and sort it putting 
  87.          // files first, subdirectories last
  88.          // to make the list more readable
  89.          //            
  90.             dirlist = SortDir(Spec.getPath(),Spec.list());
  91.             for (int i=0;i < dirlist.length; i++) {
  92.               newdir = dirspec+File.separator+dirlist[i];
  93.                 try {
  94.                     TraverseTree(Bookmarks, newdir);
  95.                  }
  96.                 catch (IOException e) {
  97.                     System.out.println(e);                
  98.                     }
  99.               
  100.                }
  101.  
  102.          Bookmarks.UnordListEndTag();    
  103.          // Once done, end the list
  104.          }
  105.  
  106.       else {
  107.          System.err.println("Internal error: unknown file"+Spec.getName());
  108.             System.exit(0);
  109.            }
  110.           
  111.         }
  112.             
  113.         
  114.  
  115.     // SortDir -- Given a directory list and its path
  116.    // sort it, putting files at the beginning, directories
  117.    // towards the end
  118.     //
  119.  
  120.     static String[] SortDir(String path, String[] dirlist) {
  121.       String temp;        
  122.       if (dirlist.length > 1) {
  123.             for (int i=0; i < dirlist.length-1; i++) {
  124.                 for (int j=i+1; j < dirlist.length; j++) {
  125.                     File Fi = new File(path+File.separator+dirlist[i]);
  126.                     File Fj = new File(path+File.separator+dirlist[j]);
  127.                                         
  128.                     if (Fi.isDirectory() & Fj.isFile()) 
  129.                   {
  130.                      temp = dirlist[i];
  131.                      dirlist[i] = dirlist[j];
  132.                      dirlist[j] = temp;
  133.                   }            
  134.                                            
  135.                else if ((Fi.isDirectory() & Fj.isDirectory())
  136.                        & (Fi.getName().compareTo(Fj.getName()) > 0))
  137.                   {
  138.                      temp = dirlist[i];
  139.                      dirlist[i] = dirlist[j];
  140.                      dirlist[j] = temp;
  141.                   }
  142.                else if ((Fi.isFile() & Fj.isFile()) 
  143.                        & (Fi.getName().compareTo(Fj.getName()) > 0))
  144.                   {
  145.                      temp = dirlist[i];
  146.                      dirlist[i] = dirlist[j];
  147.                      dirlist[j] = temp;
  148.                   }    
  149.                 }
  150.             }
  151.         }
  152.         return dirlist;
  153.    }
  154.  
  155.  
  156.  
  157. //
  158. // Main procedure
  159. //
  160.           
  161.     public static void main (String argv[]) {
  162.         String Dirspec;
  163.         String Bookspec;
  164.         String BookmarkTitle;
  165.         Date today = new Date();
  166.         WriteHTMLFile Bookmarks;
  167.         FileExtension Ext = new FileExtension(".url");
  168.         // If arguments not provided, then end
  169.       
  170.         if (argv.length <2) {
  171.          System.out.println("Usage: MSIEtoHTML <favorites> <HTMLfile> [\"Title\"]");
  172.          System.exit(0);
  173.       }             
  174.       else {
  175.          Dirspec = argv[0];
  176.             Bookspec = argv[1];    
  177.          if (argv.length == 3) {
  178.                 BookmarkTitle = argv[2];            
  179.              }
  180.             else {
  181.                 BookmarkTitle = "Bookmarks of "+argv[0];
  182.             }
  183.             try {
  184.                 Bookmarks = new WriteHTMLFile(Bookspec);
  185.  
  186.                 //
  187.                 // Write title and heading 
  188.                 //
  189.  
  190.                 Bookmarks.HeadTag();
  191.                 Bookmarks.TitleTag(BookmarkTitle);
  192.                 Bookmarks.HeadEndTag();
  193.                 Bookmarks.BodyTag();
  194.                 Bookmarks.HeadingTag(1,BookmarkTitle);                
  195.  
  196.                 // 
  197.                 // Write today's date on the HTML file
  198.                 //
  199.                 
  200.                 Bookmarks.LineBreakTag();
  201.                 Bookmarks.EmphTag();
  202.                 Bookmarks.WriteContent(today.toString());
  203.                 Bookmarks.EmphEndTag();
  204.                 Bookmarks.WriteNewline(true);
  205.  
  206.                 //
  207.                 //    Start traversing the Favorites tree
  208.                 //
  209.  
  210.                 TraverseTree(Bookmarks, Dirspec);  //Walk through the directory 
  211.  
  212.                 //
  213.                 // Put horizontal rule, statistics and close the HTML file
  214.                 //
  215.         
  216.                 Bookmarks.HorizRuleTag();
  217.                 Bookmarks.LineBreakTag();
  218.                 Bookmarks.WriteContent("** Total Shortcuts: "+ tot_files);
  219.                 Bookmarks.BodyEndTag();
  220.                 Bookmarks.CloseHTML();
  221.         
  222.              System.out.println("** Total Shortcuts: " + tot_files);
  223.                   
  224.             }
  225.             catch    (IOException e) {
  226.                 System.out.println("I/O Exception: "+e);
  227.             }
  228.             finally {
  229.                 System.exit(0);
  230.             }
  231.         }
  232.     }
  233. }          
  234.  
  235. //
  236. // FileExtension -- Defines a FilenameFilter 
  237. //
  238.  
  239. class FileExtension implements FilenameFilter {
  240.     private String ext;
  241.     FileExtension(String ext) {
  242.        this.ext = ext;
  243.       }
  244.  
  245.     public boolean accept(File dir, String name) {
  246.         if (name.endsWith(ext)) 
  247.             return true;
  248.         else
  249.             return false;
  250.         }
  251. }