November 28, 2014
Hot Topics:

Creating a Portable Bookmark Library using Java, Part 2

  • December 28, 2005
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

It is important to note that one of the incoming parameter to the getIEBookmarks method is a reference to the ArrayList object that is to be populated and returned by the method.  The other incoming parameter is a String that identifies the Favorites directory.

A recursive method

Recall that the getFireFoxBookmarks method does not receive a reference to the ArrayList object as an incoming parameter.  Rather, that method instantiates, populates, and returns such an object completely internal to the method.

However, the method named getIEBookmarks is a recursive method.  This dictates that the method receive a reference to the ArrayList object as an incoming parameter.

Conceptually more difficult

Depending on your prior experience with recursion, you may or may not find this method to be conceptually more difficult than the getFireFoxBookmarks method.

If you are unfamiliar with recursion in programming, you would do well to search the web for some tutorial material on that topic before continuing with this lesson.  One reference that you will probably find on the web is a tutorial that I published several years ago involving recursion in JavaScript.  You will undoubtedly find many other, possibly better references as well.

Traversal of the directory tree

The getIEBookmarks method uses recursion to traverse the directory tree containing Internet Shortcut files and other directories.  Each bookmark is represented by an Internet Shortcut file.  An Internet Shortcut file has an extension of url.  The name of the file is the name of the bookmark.  The URL for the bookmark is contained as a line of text in the Internet Shortcut file.

Get a directory listing

The first task is to get a directory listing for the Favorites directory.  The code in Listing 11:

  • Gets an object of type File that represents the directory specified by the incoming parameter.
  • Confirms that the directory actually exists (a File object representing a directory can exist in the absence of the directory that it represents).
  • Confirms that it really represents a directory and not a file.
  • Gets and saves a directory listing for the directory.
  • Sorts the contents of the directory listing.

    //Get a File object that represents the directory.
    File fileObj = new File(bkMrkPath);
    //Make certain that the directory exists.
    if(fileObj.exists()){
      //Confirm that the File object represents a directory
      // and not a file.
      if(fileObj.isDirectory()){
        //Get a list of the directory contents in an array
        // object.
        File[] dirContents = fileObj.listFiles();
        //Sort the directory contents according to the
        // natural order.
        Arrays.sort(dirContents);
        //Process the contents of the directory that were
        // saved in the list of contents.

Listing 11

At this point, the program is ready to process the elements in the directory listing.  Each of those elements can be a file, or can be another directory (sub-directory).

Iterate on the directory listing

Listing 12 contains the beginning of a for loop that iterates on the directory listing, processing each element in the listing.

        for(int cnt = 0;cnt < dirContents.length;cnt++){
          if(dirContents[cnt].isDirectory()){
            //Make a recursive call to process this
            // directory before processing the remaining
            // contents in the list of contents.
            theBookmarks = getIEBookmarks(
                  dirContents[cnt].getPath(),theBookmarks);

Listing 12

Is a recursive call necessary?

The code in the for loop begins by testing the element to determine if it is another directory (sub directory).  If so, it makes a recursive call to itself, passing the path to the sub directory and a reference to the partially populated ArrayList object as parameters.

Another instance of the getIEBookmarks method is executed to process the directory just discovered in the directory listing.  (The word instance might not be a good word to use here, but I don't know of a better word to use.)

When this instance of the method returns, it returns a reference to the ArrayList object, which may have had additional bookmarks added to it.

Process a file

Listing 13 shows the beginning of the code that is executed if the directory element is found to be a file instead of a directory.

          }else if(dirContents[cnt].isFile()){
            pathAndFile = dirContents[cnt].getPath();
            fileName = dirContents[cnt].getName();

Listing 13

The code in Listing 13 gets and saves:

  • The path to the file.
  • The name of the file.

Is this an Internet Shortcut file?

All Internet Shortcut files that represent bookmarks must have an extension of url (but apparently the case is not consistent).  The code in Listing 14 tests to confirm that the directory element meets this criterion.

            if(fileName.toUpperCase().endsWith(".URL")){
              theName = fileName.substring(
                 0,fileName.toUpperCase().indexOf(".URL"));
              theUrl = getTheUrl(pathAndFile);
              theBookmarks.add(
                             new Bookmark(theName,theUrl));
            }//end if

Listing 14

If the file is not an Internet Shortcut file, the file is simply ignored.

Process an Internet Shortcut file

If the file is an Internet Shortcut file, the code in Listing 14 extracts and saves the name of the file (without the extension) as the name of the bookmark.

Then the code in Listing 14 invokes the method named getTheUrl to get and save the bookmark URL from the contents of the file.

Update the ArrayList object

Having gotten the name and the URL for the bookmark, the code in Listing 14 encapsulates those items in a new object of type Bookmark and adds that object to the ArrayList object that is being populated with bookmark information.

Cleanup and return the ArrayList object

Listing 15 does some necessary cleanup work and then returns a reference to the now populated ArrayList object

          }//end else
        }//end for loop
      }else{
        System.out.println(
                  bkMrkPath + ": not a directory.");
      }//end else
    }else{
      System.out.println("Directory " + bkMrkPath
                                     + " does not exist.");
    }//end else
    return theBookmarks;
  }//end getIEBookmarks

Listing 15

The code in Listing 15 returns control to the main method shown in Listing 1.  This is the end of the code that is specific to a particular browser.

Process the bookmark data 

As before, the main method goes on to invoke the processBkMrks method to process the now-populated ArrayList object as explained in Part 1 of this lesson.  The code to accomplish this is shown in Listing 16.

    //Process the ArrayList object, either extracting and
    // sending bookmarks as messages to destAdr, or simply
    // extracting and displaying the statistics.  The
    // TreeSet object will be updated to contain an
    // identification of all bookmarks sent to destAdr.
    thisObj.processBkMrks(destAdr,
                          smtpServer,
                          bkMrkCode,
                          lowBkMrkLimit,
                          numToProc,
                          sendMsgs,
                          bkMrkHistList,
                          theBookmarks);
    //Write a new bookmark history file containing the
    // final contents of the TreeSet object.
    thisObj.writeBkMrkHistFile(
                              bkMrkHistFile,bkMrkHistList);
  }// end main

Listing 16

I explained the processBkMrks method in Part 1, and won't repeat that explanation here.

Run the Program

I encourage you to copy the code from Listing 17 into your text editor, compile it, and execute it.  Experiment with it, making changes, and observing the results of your changes.

Make the program interactive, providing the capability for the user to edit the bookmark name and/or to enter keywords and a description of the bookmark into the body of the Email message that will be sent to your web mail account for each bookmark.  See Part 1 for an indication of where to insert the code to accomplish this upgrade.

If you use a browser other than Firefox, Netscape, or IE, modify the code in Listing 1 to accommodate this new browser.  Then write a method similar to the getFireFoxBookmarks method or the getIEBookmarks method to support your browser.

Remember that you can disable the actual sending of Email messages to avoid cluttering you inbox while performing these experiments, as explained in Listing 14 of Part 1.

Summary

In Part 1 and Part 2 of this lesson, I have taught you how to use Java to create and maintain a portable bookmark library that will follow you from browser to browser, machine to machine, and operating system to operating system.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel