[Contents] [Previous] [Next] [Last]
Adding images and text files amplifies the interest and impact of an HTML document. To incorporate these items, the page writer or content provider uses HTML tags.
This chapter shows how to use Composer Plug-in methods to write plug-ins that can add images and files to a document, or remove them, by either constructing or deleting HTML tags.
A Composer document is made up of HTML text and associated files. While a simple HTML document may not have any associated files, a complex document can include images, Java class files, sounds, fonts, and other data used by the document.
You can include external files in a document in either of two ways, each of which has its own behavior.
To insert images or text data files into an HTML document, the plug-in must construct the HTML code and supply the URL of the file to insert. To remove a file, the plug-in must be able to select its associated HTML code and delete it from the document.
This chapter discusses HTML tags in relationship to Composer Plug-ins. For information about the tags themselves, see the Netscape HTML Reference Guide.
[Top]
The Composer handles local image files differently from other types of local files. Image files follow these rules:
The IMG tag in this example includes the local image file, stars.jpg:
<IMG SRC="stars.jpg">
The IMG tag in this example includes this file as an absolute image file:
<IMG SRC="stars.jpg" NOSAVE>
Background images behave like other images. There is no IMG tag for the background image; it is attached to the document by default. You use the NOSAVE attribute in the BODY tag to make the background image absolute.
The AddButton sample, included in the Composer Plug-in Kit, adds a button in response to a menu command. You could extend this idea by writing a plug-in with a dialog box that allows the user to locate an image to add.
See "Adding an Image: Sample Code" for an extended example. For general information about HTML tags and their use, consult the Netscape HTML Reference Guide.
[Top]
The AddButton sample's perform method calls the insertButton method, which constructs the HTML tag that inserts the image in the document and gets the URL for the image. To start out, it creates two variables for the file name and URL.
void insertButton(Document document, PrintWriter out){ String fileName = "netnow3.gif"; String imgURLString = "http://home.netscape.com/comprod/ products/navigator/version_3.0/images/" + fileName;
The writeImageToDisk method takes the URL and writes it to the file, then copies the image to the work directory. The method writes the HTML tags for the button in the document. It creates a new Tag object and adds attributes to it until it has assembled the three tags that make up the complete HTML code to insert an image: <A>, <IMG>, </A>.
writeImageToDisk(document, imgURLString, fileName); try { /* Write the HTML for the button. */ /* Insert start tag anchor <A>. */ Tag anchor = new Tag("A"); anchor.addAttribute("HREF", "http://www.netscape.com"); /* Insert end tag anchor </A>. */ Tag anchorEnd = new Tag("A", false); /* Insert image tag <IMG>. */ Tag img = new Tag("IMG"); /* Define the URL of the file to insert. */ URL imgURL = new URL(document.getWorkDirectoryURL(), fileName); /* Add SRC attribute to IMG. */ /* Use the URL defined above. */ img.addAttribute("SRC", imgURL.toString()); /* Print tags to the page */ out.print(anchor); out.print(img); out.print(anchorEnd); } catch (Throwable t){ t.printStackTrace(); } }
Note:
To write to a file, your Composer Plug-in must be signed. See
Using
the JAR Packager and Netscape
Object Signing.
[Top]
To add a new data file to the Composer document, you must first create the file in the document work directory and then add a reference to it to the HTML text of the document. See the sample AddApplet for details.
Normal data files are absolute by default. To indicate that a data file is local, add a LOCALDATA property to the APPLET tag that explicitly names the file.
This example includes a local non-image file:
<APPLET CODE="foo.class" LOCALDATA="application/java-vm foo.class" WIDTH=250 HEIGHT=50>
This example includes a remote non-image file:
<APPLET CODE="foo.class" WIDTH=250 HEIGHT=50>
The value of the LOCALDATA property is the MIME type of the file, followed by the URL of the local data. For information about MIME types, see " Registering Plug-ins" in the Plug-in Guide.
To specify more than one local non-image file in the same tag, use the " + " character to separate the files, as in this example:
<APPLET CODE="foo.class" LOCALDATA="application/java-vm foo.class + application/java-vm bar.class" WIDTH=250 HEIGHT=50>
When using the LOCALDATA property of the APPLET tag, keep these points in mind:
You can use the same file in one place as a local and elsewhere as an absolute file without causing an error, but this is not considered usual practice. In this case, the file is treated as a local file.
For general information about HTML tags and their use, consult the Netscape HTML Reference Guide.
[Top]
To remove an image or local data file from a document, the Composer plug-in must search for and delete the tag text in the HTML document that inserts the file. This effectively removes the image.
This code fragment from DocInfo, which calculates simple statistics for a document, demonstrates finding HTML tags. It gets tokens and checks whether they are Tag objects equal to IMG. It counts these tags, effectively counting the images in a document.
class DocStats{ public DocStats(Document document) throws IOException{ LexicalStream s = new LexicalStream(document.getInput()); boolean inBody = false; String BODY = "BODY"; String IMG = "IMG"; String SRC = "SRC"; for(;;){ /* Get the next token of the document. */ Token token = s.next(); if ( token == null ) break; /* Returns null when the document is finished. */ /* Add to the total byte count. */ textBytes += token.toString().length(); if (token instanceof Tag ) { Tag tag = (Tag) token; boolean isOpen = tag.isOpen(); String tagName = tag.getName(); if ( tagName.equals(BODY) ) { inBody = isOpen; } else if ( isOpen ) { /* Check for images. */ if ( tagName.equals(IMG) ){ imageCount++; String src = tag.lookupAttribute(SRC); if ( src != null ) { try { URL base = document.getBase(); URL srcURL = new URL(base, src); imageBytes += getSize(srcURL); } catch(Throwable t){ System.err.println("Couldn't open " + src); t.printStackTrace(); }
After locating the tag text, you can delete it, thus effectively removing the image from the document. In this example, the TagStrip sample reads tags out but does not read them back into the document. You can use this approach to delete any Token objects from a document.
for(;;){ /* Get the next token of the document. */ Token token = in.next(); if ( token == null ) break; /* Returns null when the document is finished. */
else if (token instanceof Comment ) { Comment comment = (Comment) token; if ( comment.isSelectionStart() ){ inSelection = true; } else if (comment.isSelectionEnd() ){ inSelection = false; } else { /* Don't output this generic comment. */ continue; }
[Top] [Contents] [Previous] [Next] [Last]
Copyright © 1997 Netscape Communications Corporation