/**
   * Perform the action of the plugin.
   *
   * @param document the current document.
   */
  public boolean perform(Document document) throws IOException {
    // Interact with the user to get the layer tag/

    Tag layerTag = getLayerTagFromUser(document);
    if (layerTag == null) return false; // User cancled.
    Tag endTag = new Tag(layerTag.getName(), false);
    // Get the output stream to hold the new document text.
    PrintWriter out = new PrintWriter(document.getOutput());
    // Create a lexical stream to tokenize the old document text.
    LexicalStream in = new LexicalStream(new SelectedHTMLReader(document.getInput(), out));
    for (; ; ) {
      // Get the next token of the document.
      Token token = in.next();
      if (token == null) break; //  Null means we've finished the document.
      else if (token instanceof Comment) {
        Comment comment = (Comment) token;
        if (comment.isSelectionStart()) {
          out.print(layerTag);
        } else if (comment.isSelectionEnd()) {
          out.print(comment);
          out.print(endTag);
          continue; // So comment isn't printed twice.
        }
      }
      out.print(token);
    }
    out.close();
    return true;
  }
 /**
  * Perform the action of the plugin. This plugin adds a "Netscape Now" button with a link to the
  * Netscape home page.
  *
  * @param document the current document.
  */
 public boolean perform(Document document) throws IOException {
   // Create a print stream for the new document text.
   PrintWriter out = new PrintWriter(document.getOutput());
   // Create a lexical stream to tokenize the old document text.
   LexicalStream in = new LexicalStream(new SelectedHTMLReader(document.getInput(), out));
   // Keep track of whether or not we are in the selected text.
   // At the beginning of the document we're outside the selection.
   // After we encounter the start-selection comment, we're inside
   // the selection.
   // After we encounter the end-selection comment, we're outside
   // the selection again.
   boolean inSelection = false;
   Comment selectionStart = null;
   for (; ; ) {
     // Get the next token of the document.
     Token token = in.next();
     if (token == null) break; //  Null means we've finished the document.
     else if (token instanceof Comment) {
       Comment comment = (Comment) token;
       if (comment.isSelectionStart()) {
         selectionStart = comment;
         inSelection = true;
         continue; // Don't print the selection start yet.
       } else if (comment.isSelectionEnd()) {
         inSelection = false;
         out.print(selectionStart);
         insertButton(document, out);
       } else { // don't output this generic comment.
         continue;
       }
     }
     out.print(token);
   }
   out.close();
   return true;
 }