home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / SmallCaps.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  5.0 KB  |  138 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 implements Netscape-style small caps.
  27.  * Shows how to do low-level parsing of html.
  28.  */
  29.  
  30. public class SmallCaps 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 SmallCaps());
  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 "Small Caps";
  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 "Capitalizes selected text like the Netscape Home Page.";
  63.     }
  64.  
  65.     /** Perform the action of the plugin. This plugin implements Netscape-style small caps.
  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(new SelectedHTMLReader(document.getInput(), out));
  74.         // Keep track of whether or not we are in the selected text.
  75.         // At the beginning of the document we're outside the selection.
  76.         // After we encounter the start-selection comment, we're inside
  77.         // the selection.
  78.         // After we encounter the end-selection comment, we're outside
  79.         // the selection again.
  80.         boolean inSelection = false;
  81.         for(;;){
  82.             // Get the next token of the document.
  83.             Token token = in.next();
  84.             if ( token == null ) break; //  Null means we've finished the document.
  85.             else if (token instanceof Comment ) {
  86.                 Comment comment = (Comment) token;
  87.                 if ( comment.isSelectionStart() ){
  88.                     inSelection = true;
  89.                 }
  90.                 else if (comment.isSelectionEnd() ){
  91.                     inSelection = false;
  92.                 }
  93.             }
  94.             else if ( inSelection ) {
  95.                 if ( token instanceof Text ) {
  96.                     Text text = (Text) token;
  97.                     smallCaps(text.getText(), out);
  98.                     continue; // Don't need to output the original token
  99.                 }
  100.             }
  101.             out.print(token);
  102.         }
  103.         out.close();
  104.         return true;
  105.     }
  106.  
  107.     /** Convert a string to small caps.
  108.     */
  109.     private void smallCaps(String string, PrintWriter out){
  110.         int len = string.length();
  111.         int state = 1;
  112.         Tag bigTag = new Tag("font");
  113.         bigTag.addAttribute("SIZE", "+2");
  114.         Tag fontEnd = new Tag("font", false);
  115.         for(int i = 0; i < len; i++ ){
  116.             char c = string.charAt(i);
  117.             c = Character.toUpperCase(c);
  118.             switch ( state ) {
  119.             case 0: out.print(c);
  120.                  if ( c == ' ' ) {
  121.                      state = 1;
  122.                  }
  123.                  break;
  124.             case 1: if ( c != ' ' ) {
  125.                      out.print(bigTag);
  126.                      out.print(c);
  127.                      out.print(fontEnd);
  128.                      state = 0;
  129.                 }
  130.                 else {
  131.                     out.print(c);
  132.                 }
  133.                 break;
  134.             }
  135.         }
  136.     }
  137. }
  138.