home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / Tableize.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  3.6 KB  |  97 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 netscape.plugin.composer.*;
  23. import netscape.plugin.composer.io.*;
  24. import java.awt.Color;
  25.  
  26. /** Sample Plugin that wraps selected text into a table.
  27.  * Shows how to do low-level parsing of html.
  28.  */
  29.  
  30. public class Tableize 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 Tableize());
  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 "Tableize";
  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 "Character Tools";
  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 "Puts the selected text into a table.";
  63.     }
  64.  
  65.     /** Perform the action of the plugin. This plugin wraps selected text into a table.
  66.      *
  67.      * @param document the current document.
  68.      */
  69.     public boolean perform(Document document) throws IOException{
  70.         // Get the output stream to hold the new document text.
  71.         PrintWriter out = new PrintWriter(document.getOutput());
  72.         // Create a lexical stream to tokenize the old document text.
  73.         LexicalStream in = new LexicalStream(document.getInput());
  74.         for(;;){
  75.             // Get the next token of the document.
  76.             Token token = in.next();
  77.             if ( token == null ) break; //  Null means we've finished the document.
  78.             else if (token instanceof Comment ) {
  79.                 Comment comment = (Comment) token;
  80.                 if ( comment.isSelectionStart() ){
  81.                     out.print(token);
  82.                     out.print("<TABLE BGCOLOR=#30c030><TR><TD>");
  83.                     continue;
  84.                 }
  85.                 else if (comment.isSelectionEnd() ){
  86.                     out.print("</TD></TR></TABLE>");
  87.                     out.print(token);
  88.                     continue;
  89.                 }
  90.             }
  91.             out.print(token);
  92.         }
  93.         out.close();
  94.         return true;
  95.     }
  96. }
  97.