[Contents] [Previous] [Next] [Last]  


Chapter 5
Reading and Writing Raw HTML

You can create plug-ins that allow the web page writer to edit the raw HTML of a document, thus extending the flexibility of the Composer window.

This chapter describes using the Composer Plug-in methods that read and write raw HTML, or HTML that is not tokenized.

The EditRaw sample, included in the Composer Plug-in Kit, uses a dialog box to provide raw HTML editing.


Reading HTML from the Document


The Document.getText method gets the complete text of the document as a string. Use getText if you want to perform string-based operations on the raw HTML code of the document. If you plan to tokenize the HTML, use Document.getInput.

The following methods copy raw HTML from the document to the dialog box, where the user can edit it, and return the user's changes to the document. These functions do not tokenize the text.

  /* Copy text from the document to the dialog box. */
  protected void copyTextFromDocument() {
      try {
          setText(document.getText());
      } catch(IOException e){
          System.err.println("Error reading document:");
          e.printStackTrace();
      }
  }

For the complete text of this sample, see EditRaw.java, included in the source directory of the Composer Plug-in API.

[Top]


Writing HTML to the Document


The Document.setText method sets the complete text of the document. Use setText if you want to perform string-based operations on the raw HTML code of the document. If you plan to tokenize the raw HTML code of the document, use Document.getOutput.

To write HTML back to the document, place the following code in the perform method of the plug-in.

        if ( result ) {
            document.setText(dialog.getText());
        }
        return result;

Use this method rather than document.getOutput if you have performed string-based operations on the raw HTML code of the document. Again, if you are processing tokenized HTML, use getOutput.

This code from the EditRaw sample plug-in, included in the Composer Plug-in Kit, demonstrates using the getInput, getText, and setText functions.

The perform method of this sample uses new to create a generic dialog box instance. The perform method sets up a dialog box, sets the size of the box, displays the dialog box, waits for the user to exit, disposes of the dialog box, and provides the output of the result string.

    public boolean perform(Document document) throws IOException{

       /* Create a dialog box */
       MyDialog dialog = new MyDialog("Edit Raw HTML", document);
        dialog.reshape(50,50,300,300);
        dialog.show();
        dialog.requestFocus();
        boolean result = dialog.waitForExit();
        dialog.dispose();

        /* Write the changed HTML back to the document. */
        if ( result ) {
            document.setText(dialog.getText());
        }
        return result;
    }

The sample creates a dialog box, represented by the MyDialog class, which has its own title and menu bar. For a description of this part of the sample, see "Displaying Dialog Boxes."

For the complete text of this sample, see EditRaw.java, included in the source directory of the Composer Plug-in API. For information about setting the selection, see "Selecting Raw HTML."

[Top] [Contents] [Previous] [Next] [Last]   



Copyright © 1997 Netscape Communications Corporation