home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / AddButton.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  6.3 KB  |  161 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.test.plugin.composer;
  20.  
  21. import java.io.*;
  22. import java.net.*;
  23. import netscape.plugin.composer.*;
  24. import netscape.plugin.composer.io.*;
  25.  
  26. /** Sample Plugin that adds a "Netscape Now" button to the document.
  27.  * Shows how to add images to the document.
  28.  */
  29.  
  30. public class AddButton extends Plugin {
  31.     /** Test the plugin. Not required for normal operation of the plugin.
  32.      * You can use this to run the plugin from the command line:
  33.      * java -classpath <your-class-path> <your-plugin-name> args...
  34.      * where args... are passed on to the Test class.
  35.      * You can remove this code before shipping your plugin.
  36.      */
  37.     static public void main(String[] args) {
  38.         Test.perform(args, new AddButton());
  39.     }
  40.     /** Get the human readable name of the plugin. Defaults to the name of the plugin class.
  41.      * @return the human readable name of the plugin.
  42.      */
  43.     public String getName()
  44.     {
  45.         return "Netscape Button";
  46.     }
  47.  
  48.     /** Get the human readable category of the plugin. Defaults to the name of the plugin class.
  49.      * @return the human readable category of the plugin.
  50.      */
  51.     public String getCategory()
  52.     {
  53.         return "Insert";
  54.     }
  55.  
  56.     /** Get the human readable hint for the plugin. This is a one-sentence description of
  57.      * what the plugin does. Defaults to the name of the plugin class.
  58.      * @return the human readable hint for the plugin.
  59.      */
  60.     public String getHint()
  61.     {
  62.         return "Inserts a 'Netscape Now' image that is linked to the Netscape home page.";
  63.     }
  64.  
  65.     /** Perform the action of the plugin. This plugin adds a "Netscape Now" button
  66.      * with a link to the Netscape home page.
  67.      *
  68.      * @param document the current document.
  69.      */
  70.     public boolean perform(Document document) throws IOException{
  71.         // Create a print stream for the new document text.
  72.         PrintWriter out = new PrintWriter(document.getOutput());
  73.         // Create a lexical stream to tokenize the old document text.
  74.         LexicalStream in = new LexicalStream(new SelectedHTMLReader(document.getInput(), out));
  75.         // Keep track of whether or not we are in the selected text.
  76.         // At the beginning of the document we're outside the selection.
  77.         // After we encounter the start-selection comment, we're inside
  78.         // the selection.
  79.         // After we encounter the end-selection comment, we're outside
  80.         // the selection again.
  81.         boolean inSelection = false;
  82.         Comment selectionStart = null;
  83.         for(;;){
  84.             // Get the next token of the document.
  85.             Token token = in.next();
  86.             if ( token == null ) break; //  Null means we've finished the document.
  87.             else if (token instanceof Comment ) {
  88.                 Comment comment = (Comment) token;
  89.                 if ( comment.isSelectionStart() ){
  90.                     selectionStart = comment;
  91.                     inSelection = true;
  92.                     continue; // Don't print the selection start yet.
  93.                 }
  94.                 else if (comment.isSelectionEnd() ){
  95.                     inSelection = false;
  96.                     out.print(selectionStart);
  97.                     insertButton(document, out);
  98.                 }
  99.                 else { // don't output this generic comment.
  100.                     continue;
  101.                 }
  102.             }
  103.             out.print(token);
  104.         }
  105.         out.close();
  106.         return true;
  107.     }
  108.  
  109.     void insertButton(Document document, PrintWriter out){
  110.         String fileName = "netnow3.gif";
  111.         String imgURLString = "http://home.netscape.com/comprod/products/navigator/version_3.0/images/"
  112.             + fileName;
  113.  
  114.         writeImageToDisk(document, imgURLString, fileName);
  115.  
  116.         try {
  117.             // Write the HTML for the button
  118.             Tag anchor = new Tag("A");
  119.             anchor.addAttribute("HREF", "http://www.netscape.com");
  120.             Tag anchorEnd = new Tag("A", false);
  121.             Tag img = new Tag("IMG");
  122.             URL imgURL = new URL(document.getWorkDirectoryURL(), fileName);
  123.             img.addAttribute("SRC", imgURL.toString());
  124.             out.print(anchor);
  125.             out.print(img);
  126.             out.print(anchorEnd);
  127.         } catch (Throwable t){
  128.             t.printStackTrace();
  129.         }
  130.     }
  131.  
  132.     void writeImageToDisk(Document document, String imgURLString, String fileName){
  133.         // We could just reference the image on the netscape site,
  134.         // but the point of this example is to show how to write
  135.         // a file to the disk.
  136.  
  137.         // In order to do this, we need to have the ability access files.
  138.         netscape.security.PrivilegeManager.enablePrivilege("UniversalFileAccess");
  139.         // And the ability to connect to an arbitrary URL.
  140.         netscape.security.PrivilegeManager.enablePrivilege("UniversalConnect");
  141.  
  142.         // Copy the image to the work directory.
  143.         File outFile = new File(document.getWorkDirectory(), fileName);
  144.         if ( ! outFile.exists() ){
  145.             try {
  146.                 InputStream in = new URL(imgURLString).openStream();
  147.                 FileOutputStream outStream = new FileOutputStream(outFile);
  148.                 int chunkSize = 1024;
  149.                 byte[] bytes = new byte[chunkSize];
  150.                 int readSize;
  151.                 while ( (readSize = in.read(bytes)) >= 0 ){
  152.                     outStream.write(bytes, 0, readSize);
  153.                 }
  154.                 outStream.close();
  155.             } catch (Exception e) {
  156.                 e.printStackTrace();
  157.             }
  158.         }
  159.     }
  160. }
  161.