November 28, 2014
Hot Topics:

Creating a Portable Bookmark Library using Java

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

Discussion and Sample Code

This program, named Bookmarks02a, is the same as the program named Bookmarks02 (that I will provide and explain in Part 2), except that the two methods listed above have been replaced in this version by stubs.  You can view the stubs in Listing 22 near the end of the lesson.

Purpose of the program

The purpose of this program is to:

  • Extract bookmarks from one or more Firefox, Netscape, or Internet Explorer (IE) browsers within a specified range of bookmark indices.
  • Construct an Email message containing the name and the URL of each extracted bookmark.
  • Send the messages to a specified destination Email address.

Email message format

Each Email message is formatted with the name of the bookmark as the Subject and the URL for the bookmark in the body of the message.  To use this message as a bookmark later, simply open the message and double-click the URL.

(Note that the actual sending of the messages may be disabled for test purposes.  Search for the word disable to find the code that may be disabled.)

A bookmark code

The Subject may optionally be prepended with a text string provided by the user and identified as bkMrkCode below.  The string that is prepended to the subject can be used by the Email client to recognize the message as a bookmark message.

(If you don't want anything prepended to the subject, specify this parameter as an empty string.)

A bookmark history list

The program maintains a list of bookmarks that have previously been sent to the destination Email address.  The purpose of this list is to prevent the sending of duplicate bookmark messages to the destination Email address during successive runs of the program.

The history list is maintained in a text file that can easily be edited by the user if such editing becomes necessary.  Several backup copies of the file are automatically generated and maintained by the program. 

History file is automatically created, backed up, and maintained

The history file is named BkMrkHistory.txt.  If it doesn't already exist, it is automatically created (in a folder specified by the user and identified below as dataOutPath) the first time the program is run.  Once the file exists, it is backed up at the beginning of each successive run of the program.  Then it is updated during that run.

Prevention of duplicate Email messages

Bookmarks identified in the history list are not sent to the destination Email address.  This prevents the sending of duplicate bookmark messages during successive runs of the program.

Once all the bookmarks in the Firefox or Netscape bookmark file (or the IE Favorites folder) have been sent to the destination Email address, this program will send only those bookmarks that have been added or changed since the last time the program was run.

Don't overwhelm the SMTP server

Because the number of messages that could be sent the first time the program is run could overwhelm the SMTP server, (causing your ISP to erroneously conclude that you are distributing SPAM email), the program allows the user to specify the index of the first bookmark to be sent and the maximum number of bookmarks to be sent during any particular run. 

For example, if values of 0 and 100 are used the first time the program is run, it can be expected that 100 messages will be sent (assuming that there are at least 100 non-duplicate bookmarks).  These are the messages required to send bookmarks with indices of 0 through 99.

If values of 0 and 200 are used the second time the program is run, it can be expected that 100 or more messages will be sent (assuming that there are at least 200 non-duplicate bookmarks).  This will consist of any new bookmarks in the index range of 0 through 99 plus all of the bookmarks in the index range of 100 through 199.

Controlled by command-line parameters

This program is controlled using command-line parameters making it very easy to run periodically using a batch file or a script.

(Once you create the batch file, all that you need to do to run the program is to start the batch file.  If you put a shortcut to the batch file on your desktop, you can run the program simply by double-clicking the shortcut icon.)

The required command-line parameters

The following values must be provided as command-line parameters.  Although all command-line parameters are provided as strings, several of the parameters must be convertible to the int and boolean types shown below.

  • String destAdr:  Email address to which bookmarks are to be sent.
  • String smtpServer:  An SMTP server that can be used to send the messages.
  • String bkMrkPath:  Path to the folder containing the Firefox or Netscape bookmark file, (which is an HTML file) or containing the IE Favorites files (which Microsoft refers to as Internet Shortcut files).
  • String bkMrkFile:  Name of the Firefox or Netscape bookmark file.  (Provide a dummy file name if you are processing IE Favorites.)
  • String dataOutPath:  Path to a folder where output files will be stored.  (Make sure this folder is on the path for your regular backups because you don't want to lose these files in case of a disk failure.)
  • String bkMrkCode:  Unique code that is prepended to the Subject of the Email message to identify the message as a bookmark message.
  • int lowBkMrkLimit:  First bookmark index to process.
  • int numToProc:  Number of bookmarks to process.
  • String browser:  Type of browser: F for Firefox, N for Navigator, or I for Internet Explorer.
  • boolean sendMsgs:  Specify true or false.  Messages will be sent on true.  Message will not be sent, but bookmark statistics will be displayed on false.

Program testing

This program was tested using J2SE 5.0 under WinXP.  J2SE 5.0 or later is required due to the use of generics.

The Bookmarks02a class

The class definition and the main method begin in Listing 1.

class Bookmarks02a{

  public static void main(String[] args){
    //Confirm correct number of command-line parameters.
    // If the number is not correct, display a usage msg
    // and terminate the program.
    if(args.length != 10){
      System.out.println("Command-line parameter error");
      System.out.println();
      System.out.println("Usage: java Bookmarks02a");
      System.out.println("followed by:");
      System.out.println("Destination Email address");
      System.out.println("SMTP Server");
      System.out.println("Bookmark path");
      System.out.println("Bookmark file");
      System.out.println("Output data path");
      System.out.println("Bookmark code");
      System.out.println("Low bookmark limit");
      System.out.println("Number bookmarks to process");
      System.out.println("Browser, F, N, or I");
      System.out.println("Permission to send messages");
      System.out.println();
      System.out.println("Terminating Program");
      System.exit(0);      
    }//end if

Listing 1

The code in Listing 1 confirms that the user has entered the correct number of command-line parameters.  The code prints out some usage information and terminates the program if the number of parameters is found to be in error.

Typical command-line parameters

Figure 1 shows a typical set of command-line parameters to extract up to 500 bookmarks from a Firefox browser, format them into Email messages, and send them to an account at Gmail.


java Bookmarks02a 
my.account@gmail.com 
mail.austincc.edu 
"C:/Documents and Settings/Owner/Application Data/
Mozilla/Firefox/Profiles/athy94h2.default/" 
bookmarks.html 
"C:/Documents and Settings/Owner/Application Data/
Mozilla/Firefox/Profiles/athy94h2.default/" 
Bk-Mrk 
0 
500 
F 
true
Figure 1

Because of publication width limitations, each command-line parameter is shown on a different line in Figure 1.  In addition, each of the two paths shown in boldface was so long that it was necessary to break it up and display it on two lines.

You should be able to correlate the command-line parameters shown in Figure 1 with the list of command-line parameters described earlier.

Saving the command-line parameters as named variables

The code in Listing 2 declares named variables and stores the command-line parameters in those variables.  In a couple of cases, the code also converts the type of the parameter from String to int or boolean.

    //Email address to which bookmarks are to be sent.
    String destAdr = args[0];
    //An SMTP server that can be used to send the messages.
    String smtpServer = args[1];
    //Path to the folder containing a Firefox bookmark
    // file or containing a multitude of IE .url files.
    String bkMrkPath = args[2];
    //Name of the Firefox bookmark file.  Just use a 
    // dummy name for this parameter when processing IE
    // Favorites
    String bkMrkFile = args[3];
    //Path to the folder where output files will be stored.
    String dataOutPath = args[4];
    //Unique code that identifies a bookmark msg to
    // destAdr.  This code is prepended onto the name of
    // the bookmark and the name is put in the Subject line
    // of the message.
    String bkMrkCode = args[5];
    //Index of first bookmark to process.
    int lowBkMrkLimit = Integer.parseInt(args[6]);
    //Number of bookmarks to process.
    int numToProc = Integer.parseInt(args[7]);
    //Type of browser: F for Firefox, N for Navigator,
    // or I for Internet Explorer.
    String browser = args[8];
    //Send msgs on true.  Just display statistics on false.
    boolean sendMsgs = false;//default value
    //End of command-line parameters
    
    //Convert the last parameter from String to  boolean
    // and overwrite default if appropriate.
    if(args[9].toUpperCase().equals("TRUE")){
      sendMsgs = true;
    }//end if

Listing 2

A new object of type Bookmarks02a

Listing 3 instantiates a new object of the Bookmarks02a class and stores the reference to that object in a reference variable named thisObj.

    Bookmarks02a thisObj = new Bookmarks02a();

Listing 3

A new bookmark history file

Listing 4 invokes the createHistFile method to create a new empty bookmark history file if it doesn't exist.  If it does exist, the createHistFile method returns quietly having done nothing.

    String bkMrkHistFile = 
                          dataOutPath + "BkMrkHistory.txt";
    thisObj.createHistFile(bkMrkHistFile);

Listing 4

The createHistFile method is so simple that it shouldn't require an explanation.  It consists of only one statement that invokes the createNewFile method on a new File object.  You can view the method in Listing 22 near the end of the lesson and read about the createNewFile method in the Sun documentation.

Create a TreeSet object

Listing 5 invokes the makeBkMrkHistList method to create a TreeSet object that encapsulates the bookmarks from the bookmark history file.

(The makeBkMrkHistList method will be discussed in more detail later.)

This collection is used to store the contents of the history file for processing.  New bookmarks are added to the object as the corresponding bookmarks are sent to the destination address, known as destAdr.  The final contents are written back out into a new history file when the program terminates.

    TreeSet <String>bkMrkHistList = 
                  thisObj.makeBkMrkHistList(bkMrkHistFile);

Listing 5

(Note the use of the generics syntax in Listing 5.)

The bookmark name and URL

Following this, the program gets the name and the URL for each of the bookmarks.  It encapsulates the name and the URL for each bookmark in an object of type Bookmark.

The program encapsulates all of the Bookmark objects in an object of type ArrayList for further processing.

A copy of the bookmark file

As a safety precaution, when processing bookmarks from a Firefox or Navigator browser, the program invokes a method named copyBkMrkFile to make a copy of the bookmark file.  (The copy is actually made in Listing 7 later.)  The program processes the copy instead of processing the original bookmark file in order to reduce the likelihood of inadvertently corrupting the original bookmark file.  You can view the copyBkMrkFile method in Listing 22 near the end of the lesson.  The code in the method is straightforward, and shouldn't require further explanation.

A list of Bookmark objects

Listing 6 instantiates the ArrayList object that is used to encapsulate all of the Bookmark objects awaiting final processing.

    ArrayList <Bookmark> theBookmarks = 
                                new ArrayList <Bookmark>();

Listing 6

Populating the ArrayList object

Depending on whether the bookmarks being processed were created by an IE or a Firefox browser, either the getIEBookmarks method or the getFireFoxBookmarks is called to extract the name and the URL from each bookmark.

As you will see in Part 2 of this lesson, the getIEBookmarks method is recursive.  As a result, it requires that one of its method parameters point to the ArrayList object created in Listing 6.  Execution of the getIEBookmarks method later causes the ArrayList object to be populated.

The getFireFoxBookmarks method on the other hand is not recursive.  It creates and populates a new ArrayList object internally.  When it returns, the ArrayList object that was created in Listing 6 is overwritten by the new ArrayList object created and populated by the getFireFoxBookmarks method.

Decide among Firefox, Netscape, and IE

The code in Listing 7 uses one of the command-line parameters to decide whether to invoke the getFireFoxBookmarks method or the getIEBookmarks to extract the bookmark information from the browser file(s) and to save the result in the ArrayList object.

    String tempBkMrkFile = "BkMrkTemp.html";
    if(browser.toUpperCase().equals("F")){
      //Process Firefox bookmarks.
      thisObj.copyBkMrkFile(bkMrkPath,bkMrkFile,
                                dataOutPath,tempBkMrkFile);
      theBookmarks = thisObj.getFireFoxBookmarks(
                                dataOutPath,tempBkMrkFile);
    }else if(browser.toUpperCase().equals("N")){
      //Process Netscape Navigator bookmarks.  Same format
      // as Firefox
      thisObj.copyBkMrkFile(bkMrkPath,bkMrkFile,
                                dataOutPath,tempBkMrkFile);
      theBookmarks = thisObj.getFireFoxBookmarks(
                                dataOutPath,tempBkMrkFile);
    }else if(browser.toUpperCase().equals("I")){
      //Process Inernet Explorer favorites.
      theBookmarks = thisObj.getIEBookmarks(
                                   bkMrkPath,theBookmarks);
    }else{
      System.out.println("Don't recognize browser");
      System.out.println("Terminating program");
      System.exit(0);
    }//end else

Listing 7

Firefox and Netscape bookmark formats are the same

As near as I can determine, the bookmark file format for Firefox and Netscape are the same.  Therefore, the code in Listing 7 invokes the same method regardless of whether the user specifies Firefox or Netscape Navigator in the command-line parameter.

(If I learn later that the Netscape format is different from the Firefox format, I will write a new method to accommodate the Netscape format and reference the new method in Listing 7.  This is also the place where you would need to modify the program if you were to develop a method to extract bookmark information from some browser other than the three supported by this program as written.)

The code in Listing 7 causes the bookmark information to be extracted from the browser file(s) and stored as objects of type Bookmark in the ArrayList object that was instantiated in Listing 6.  That ArrayList object is referred to by theBookmarks.

Very little browser-specific code

At this point, the bookmark information is ready to be processed, independently of the specific browser involved.  The only code in this program that is browser-specific is the code in Listing 7 and the two methods named getFireFoxBookmarks and getIEBookmarks.  These two methods extract bookmark information from the browser file(s).

I will explain these two methods in detail in Part 2 of this lesson.  For now, suffice it to say that the name and the URL for each bookmark has been encapsulated in an object of the inner class Bookmark, and those objects have been encapsulated in the ArrayList object referred to by theBookmarks.

Method stubs

The version of the program shown in Listing 22 contains stubs for these two methods.  These stubs are suitable for testing the overall operation of the program without a requirement to deal with actual bookmarks.  Each of the stubs creates dummy information for two different bookmarks and uses that information to return the required ArrayList object populated with Bookmark objects.

Process the list of bookmarks

Listing 8 invokes the processBkMrks method to process the ArrayList object by either

  • Extracting and sending bookmarks as messages to destAdr, or
  • Simply extracting and displaying the statistics of the bookmark data. 

    thisObj.processBkMrks(destAdr,
                          smtpServer,
                          bkMrkCode,
                          lowBkMrkLimit,
                          numToProc,
                          sendMsgs,
                          bkMrkHistList,
                          theBookmarks);


Listing 8

Update the history list

The TreeSet object created in Listing 5 will be updated to contain an identification of each bookmark that is sent to destAdr.  At the beginning of the program, the TreeSet object contains an identification of all bookmarks that were sent to destAdr during previous runs of the program.  At the termination of the current run, the TreeSet object contains an identification of all bookmarks that have been sent to destAdr up through the current run.  This procedure is used to avoid sending duplicate bookmark messages to destAdr during successive runs of the program.

I will explain the processBkMrks method in detail shortly.

Write a new bookmark history file

Immediately before the program terminates, Listing 9 invokes the writeBkMrkHistFile method to write a new bookmark history file containing the updated contents of the TreeSet object.

    thisObj.writeBkMrkHistFile(
                              bkMrkHistFile,bkMrkHistList);
  }// end main

Listing 9

I will explain the writeBkMrkHistFile method in detail later in this lesson.

Listing 9 also signals the end of the main method and the termination of the program.

The processBkMrks method

Listing 10 shows the beginning of the processBkMrks method.  This method processes bookmarks previously stored in the ArrayList object either by the method named getFireFoxBookmarks or by the method named getIEBookmarks(One, but not both, of these methods is always executed once during a run of the program.)

  void processBkMrks(String destAdr,
                     String smtpServer,
                     String bkMrkCode,
                     int lowBkMrkLimit,
                     int numToProc,
                     boolean  sendMsgs,
                     TreeSet <String>bkMrkHistList,
                     ArrayList <Bookmark> theBookmarks){

    int msgCounter = 0;
    String theName = null;
    String theUrl = null;

Listing 10

Send a message or display statistics

If the value of the command-line parameter sendMsgs is true, the processBkMrks method constructs an Email message for each bookmark and sends it to destAdr.  Otherwise the method simply displays statistics regarding the number of messages that would be sent if the value of sendMsgs were true.

Analyze statistics before you send messages

This latter option is provided to make it possible for the user to determine in advance how many messages would be sent for a given set of command-line parameters before actually sending the messages.

Don't upset your ISP

This option is useful to avoid overwhelming the SMTP server with an excessive number of messages in a given run of the program.  For example, when initially uploading your large library of bookmarks to the web mail server, it may be necessary for you to meter them out and send only a few hundred bookmarks each day to avoid upsetting your ISP.  Otherwise, your ISP may erroneously conclude that you are sending SPAM and disable your ability to send messages.

Bookmarks that are eligible for sending

The processBkMrks method processes the name and the URL for each bookmark for which data is stored in the ArrayList object referred to by theBookmarks.

The user specifies the index of the first bookmark that may be eligible for sending to destAdr along with the maximum number of bookmarks that may be eligible.

In the final analysis, a bookmark is deemed eligible for sending to destAdr only if:

  • It is within the range of bookmarks specified for eligibility and
  • If it is not contained in the bookmark history file.

Duplicates are not eligible for sending

If it is in the bookmark history file that means that it was previously sent to destAdr.  In this case, it is declared ineligible for sending to destAdr in order to avoid creating duplicate bookmark messages in the archives at destAdr.

A count of eligible messages

The counter that is declared and initialized in Listing 10 counts the number of bookmarks that are eligible for sending to destAdr according to the criteria given above.

Iterate on the ArrayList object

Listing 11 shows the beginning of a for loop that iterates on the ArrayList object.  Code inside the for loop processes each bookmark contained in the ArrayList object.

    for(int cnt = 0;cnt < theBookmarks.size();cnt++){
      theName = theBookmarks.get(cnt).bkMrkName;
      theUrl = theBookmarks.get(cnt).bkMrkUrl;

Listing 11

Listing 11 also shows the code that extracts the name and the URL for the bookmark that is being processed during each iteration of the loop.

Determine eligibility for sending

Listing 12 shows the beginning of an if statement that determines is a particular bookmark is eligible for sending to destAdr according to the criteria given earlier.

      if((cnt >= lowBkMrkLimit) && 
         (cnt < lowBkMrkLimit + numToProc) &&
         (!bkMrkHistList.contains(theName + theUrl))){
        //This bookmark is eligible for sending to destAdr.
        msgCounter++;
        //Display the names of the eligible bookmark
        // on the command-line screen.
        System.out.println(cnt + " " + theName);

Listing 12

If a the bookmark being processed during the current iteration is deemed to be eligible for sending to destAdr, a counter is incremented and the name of the bookmark is displayed on the command-line screen by the code in Listing 12.

Send messages, or simply display statistics

Listing 13 shows the beginning of an if statement that tests the value of sendMsgs to determine whether to actually send the messages to destAdr, or simply to display the statistics regarding eligible bookmarks.

        if(sendMsgs){
          //sendMsgs is true
          String message ="Subject: " + bkMrkCode + ":" + 
              theName + "nn" + " ... " + theUrl + "nn"
                               + "aiNy4^273destAdr274^";

Listing 13

The code in Listing 13 is executed if sendMsgs is true.  This code constructs the message.

The message format

The code in Listing 13 puts the name of the bookmark in the Subject line, prepended by the user input string identified by bkMrkCode concatenated with a colon character.

The first line of the message body begins with three dots.  Those three dots are followed by the bookmark URL.  The second line in the message body is left blank.  The third line in the message body contains a (hopefully) unique code.  The purpose of this code is to make the message identifiable by a search of the web mail archives without regard to the bookmark name or the URL.

(The code in the third line is not necessary, but may be handy for use with web mail archive maintenance.)

Numerous improvements are possible here

There are numerous improvements that you could make with regard to the format of the message.  For example, if you wanted to make the program interactive, you could display a GUI at this point and allow the user to enter a description and one or more keywords to become part of the body of the Email message.  This could prove to be extremely valuable later when you are using the web mail search capability to locate a particular bookmark.

(This is particularly true for Gmail, which doesn't appear to have a partial-word search capability.  For example, searching for PayPal on Gmail wouldn't find a bookmark named PayPalHome.)

Send the message to destAdr

The code in Listing 14 calls the sendBkMrk method in an attempt to send the Email message.  The sendBkMrk method returns true if the message was successfully sent, and returns false otherwise.  The returned value is saved in the variable named success.

          try{
            boolean success = true;
            //Disable the following statement to avoid
            // sending messages during testing.
            success = sendBkMrk(
                             destAdr,smtpServer,message);

Listing 14

(Sometimes it is useful to disable the actual sending of messages while testing modifications to the program.  You can disable the sending of messages by using comment indicators to disable the last statement in Listing 14.)

If the message was not sent successfully ...

Listing 15 throws an exception to display an error message and terminate the program if the message was not sent successfully.

            if(!success){
              throw new Exception(
                         "Unable to send: " + theName);
            }//end if

Listing 15

(This may be a little extreme.  You may want to modify the code so that the failure to successfully send a bookmark to destAdr prevents that particular bookmark from being entered into the history list, (see Listing 16) but doesn't terminate the program.  This deserves some thought.)

Update the book history list

Finally, Listing 16 updates the TreeSet object containing the history list by adding the bookmark that was just sent to destAdr.  This will prevent a duplicate of this bookmark message from being sent to destAdr during some future run of the program.

Note that the information that is used to identify a bookmark in the history list is the concatenation of the bookmark name and the URL.

            bkMrkHistList.add(theName + theUrl);
          }catch(Exception ex){
            ex.printStackTrace();
            System.exit(0);
          }//end catch
        }//end if
      }//end if
    }//end for loop

Listing 16

Listing 16 also terminates two if statements and the for loop that began in Listing 11.

Display summary information for the run

Listing 17 displays summary information for the run on the command-line screen.

    System.out.println(
              "Number eligible bookmarks = " + msgCounter);
    if(sendMsgs){
      //sendMgs is true.
      //All eligigle bookmarks will have been sent.
      System.out.println("Bookmarks sent = " + msgCounter);
    }else{
      //sendMsgs is false.
      System.out.println("Bookmarks sent = " + 0);
    }//end else
    
    System.out.println(
                   "Bookmark range = " + lowBkMrkLimit
                   + " to " + (lowBkMrkLimit + numToProc));
    System.out.println(
        "Total number bookmarks = " + theBookmarks.size());
  }//end processBkMrks

Listing 17

Typical summary information

Figure 2 shows a typical output produced by Figure 17 by running the program from Listing 22.  The stub method named getFireFoxBookmarks in Listing 22 generates the two dummy bookmarks shown in Figure 2 and returns them in the ArrayList object when invoked in Listing 7.  These two dummy bookmarks were sent to my web mail account when I ran the program that produced the output shown in Figure 2.

0 Dummy Firefox bookmark 1
1 Dummy Firefox bookmark 2
Number eligible bookmarks = 2
Bookmarks sent = 2
Bookmark range = 0 to 500
Total number bookmarks = 2
Figure 2

The sendBkMrk method

The sendBkMrk method is invoked in Listing 14 to send the bookmark message to the web mail account.  I have discussed this method, (or methods very similar to this one), in several earlier lessons and won't repeat that discussion here.  You can view the sendBkMrk method in its entirety in Listing 22 near the end of the lesson.

The makeBkMrkHistList method

The makeBkMrkHistList method, which begins in Listing 18, is used to convert the data in the history text file into a TreeSet object suitable for processing within the program.  The method also makes a primary backup and several secondary backups of the history text file before starting to process the bookmark history data.

  TreeSet <String> makeBkMrkHistList(String bkMrkHistFile){
    TreeSet <String> bkMrkHistList = new TreeSet<String>();

    //Read lines of text from text file and populate the
    // TreeSet object.
    try{
      BufferedReader inData = new BufferedReader(
                            new FileReader(bkMrkHistFile));
      String data; //temp holding area

      while((data = inData.readLine()) != null){
        bkMrkHistList.add(data);
      }//end while loop

      inData.close();//Close input file


Listing 18

Create and populate the TreeSet object

This method reads the history text file and causes each line of text in the file to become an element in the TreeSet object.

(Note that a TreeSet object contains no duplicates.  Therefore, if there are duplicates in the text file, they are eliminated at this point.)

Write the data into a backup text file

After creating the TreeSet object and populating it with the data from the text file, the method writes the data from the TreeSet object into a backup file with an extension of bakN, (where the value of N is explained below).

A new backup file with a unique name based on N is created each time the method is executed (the method is executed once each time the program is run).  Once the number of backup files reaches 5, the method automatically deletes the oldest file before creating a new backup file.  Thus the method automatically maintains a sequence of five backup files with extensions bak0 through bak5 with one number missing.

(In case it is necessary to use the backup files to manually restore the history text file, the age-order of the backup files should be determined by the modification date on the file and not by the name of the file.)

The code for the primary purpose

The code in the makeBkMrkHistList method that accomplishes the primary purpose of the method, (which is to populate a TreeSet object from the history text file), is shown in Listing 18.  This code is relatively straightforward, simply creating a TreeSet object, reading the text file one line at a time, and adding those lines to the TreeSet object.

Create the backup file

The remainder of the makeBkMrkHistList method, whose purpose is to create the backup file as described above, is shown in Listing 19.

      //Write a backup file before terminating the method.

      //First determine the name of the next backup file
      // allowed in the directory.
      int N = 0;
      File theFile = null;
      String baseFileName = bkMrkHistFile.
                substring(0,bkMrkHistFile.indexOf(".txt"));
      for(N = 0;N < 6;N++){
        theFile = new File(baseFileName + ".bak" + N);
        if(!(theFile.exists()))break;
      }//end for loop

      //Cause N to rotate from 0 through 5
      if(N == 5){//del file 0 for use next time
        new File(baseFileName + ".bak0").delete();
      }//end if
      else{//delete the next file in sequence
        if(new File(baseFileName + ".bak"
                                      + (N + 1)).exists()){
          new File(
                  baseFileName + ".bak"+ (N + 1)).delete();
        }//end if
      }//end else

      //Now write the output file
      DataOutputStream dataOut = new DataOutputStream(
                            new FileOutputStream(theFile));

      //Use an Iterator object to access the data
      // in the TreeSet object.
      Iterator <String>iter = bkMrkHistList.iterator();

      while(iter.hasNext()){
        data = iter.next();
        dataOut.writeBytes(data + "n");
      }//end while

      dataOut.close();
      
    }catch(Exception e){
      e.printStackTrace();
    }//end catch
    
    return bkMrkHistList;
  }//end makeBkMrkHistList

Listing 19

The code in Listing 19 is also relatively straightforward, so I won't provide a detailed explanation.

Return the populated TreeSet object

The last statement in Listing 19 returns a reference to the TreeSet object that was populated from the history text file in Listing 18.  This is the object that is used for determination of eligibility by the code in Listing 12.

The writeBkMrkHistFile method

As bookmarks are turned into Email messages and sent to destAdr, they are added to the TreeSet object, (shown in Listing 16).  The writeBkMrkHistFile method shown in its entirety in Listing 20 writes the final version of the TreeSet object into the history text file.  This causes the history text file to contain the identifications of the bookmarks that were sent to destAdr during the current run, in addition to the identifications of all the bookmarks that were sent to destAdr during previous runs.

  void writeBkMrkHistFile(String bkMrkHistFile,
                            TreeSet <String>bkMrkHistList){
    try{
      DataOutputStream dataOut = new DataOutputStream(
                      new FileOutputStream(bkMrkHistFile));

      //Use an iterator to access the data in
      // the TreeSet object.
      Iterator <String>iter = bkMrkHistList.iterator();
      String data;

      while(iter.hasNext()){
        data = iter.next();
        dataOut.writeBytes(data + "n");
      }//end while

      dataOut.close();
    }catch(Exception e){e.printStackTrace();}

  }//end writeBkMrkHistFile

Listing 20

The code in Listing 20 is straightforward, and shouldn't require further explanation.

The Bookmark class

Listing 21 shows the definition of an inner class named Bookmark.  An object of this class is used to encapsulate the name and the URL for a bookmark.  I will discuss this class in Part 2 of this lesson in conjunction with my discussion of the methods named getFireFoxBookmarks and getIEBookmarks.

  class Bookmark{
    String bkMrkName;
    String bkMrkUrl;
    
    Bookmark(String bkMrkName,String bkMrkUrl){
      this.bkMrkName = bkMrkName;
      this.bkMrkUrl = bkMrkUrl;
    }//end constructor
  }//end inner class Bookmark

Listing 21

Run the Program

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

For example, make changes to the two method stubs named getFireFoxBookmarks and getIEBookmarks, and observe the impact of those changes on the history text file and also on the inbox at the Email address to which you send the messages.

Make the program interactive, providing the capability for the user to enter keywords and a description into the body of the Email message that will be sent to your web mail account for each bookmark.

If you use a browser other than Firefox, Netscape, or IE, try modifying the code in Listing 7 to accommodate this new browser.  Then write a method stub to support those changes.

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

The time interval until Part 2 of this lesson is published would be a good time for you to clean up your existing bookmark library.  Then it will be clean when you use the code that I will provide in Part 2 to establish your portable bookmark library.  Delete the obsolete entries from your library and make the names more suitable for searching.  If you will be using Gmail, remember that Gmail doesn't have a partial-word search capability so use names like PayPal Home instead of PayPalHome.

Summary

In this lesson, I have taught you part of what you need to know 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.

What's Next?

This document is Part 1 of a two-part lesson.  Part 2 of this lesson will be published soon.  Be on the lookout for it.





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