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


Chapter 7
Working with URLs in a Document

Working with URLs is a natural part of creating and using HTML pages on the World Wide Web, Internet, or company intranet. Each HTML document file has its own URL (universal resource locator), as does its work directory.

This chapter describes using Composer Plug-in methods to write plug-ins that can reference and use URLs.

Note:
For general information about using HTML in a document, see the Netscape HTML  Reference Guide.

[Top]


Getting Document and Directory URLs


The HTML document is represented by a Document object. The document name is a relative URL and contains HTML text and a work directory, each of which has its own URL. The plug-in has access to these URLs through Document class methods.

The Document.getBase method gets the base URL of the current document. Any relative URLs included in the document are relative to this one.

This method returns the document URL.

The Document.getWorkDirectoryURL method gets the work directory of the document and returns it as a URL. You place images and other local data files that you are inserting into the document in this directory.

This method returns the work directory for the current document as a URL.

The DocInfo sample plug-in, included in the Composer Plug-in Kit, shows how a plug-in can access document information. The plug-in prints out the base URL, the working directory, and the working directory URL. In this code fragment, the plug-in gets the document URL, the work directory, and its URL and returns them as strings. The strings are appended to the list of document statistics, which the plug-in displays in a dialog box.

      String getStats(Document document) throws IOException{
        DocStats stats = new DocStats(document);
        StringBuffer b = new StringBuffer();
        ...
        /* Get the URL of the current document. */
        b.append(formatProp("base", document.getBase().toString()));
        /* Get the work directory. */
        b.append(formatProp("workDir", document.getWorkDirectory().toString()));
        /* Get the work directory. */
        b.append(formatProp("workDirURL", document.getWorkDirectoryURL().toString()));

This code from DocInfo gets the base URL of the document and assigns it as a value to the base variable.

          URL base = document.getBase();

[Top]


Opening a URL For Editing


If you want a Composer plug-in to open an arbitrary URL for editing by the user, use the Document.editDocument method. You can call this method from your plug-in, or from any Java or JavaScript running in Communicator.

As a parameter, this method takes a string containing the URL of the document you want to open. The editDocument method operates asynchronously and does not return any status.

The EditHomePage sample shows the editDocument method in action. Its perform method calls editDocument with the URL of the Netscape Home Page.

    public boolean perform(Document document){
        Document.editDocument("http://www.netscape.com/index.html");
        return true;
    }
}

For the full text of this sample plug-in, see EditHomePage.java, in the source directory of the Composer Plug-in Kit.

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



Copyright © 1997 Netscape Communications Corporation