Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.terminal » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   /**
   27    * SSHTools - Java SSH API The contents of this package has been derived from
   28    * the TelnetD library available from http://sourceforge.net/projects/telnetd
   29    * The original license of the source code is as follows: TelnetD library
   30    * (embeddable telnet daemon) Copyright (C) 2000 Dieter Wimberger This library
   31    * is free software; you can either redistribute it and/or modify it under the
   32    * terms of the GNU Lesser General Public License version 2.1,1999 as
   33    * published by the Free Software Foundation (see copy received along with the
   34    * library), or under the terms of the BSD-style license received along with
   35    * this library.
   36    */
   37   package com.sshtools.daemon.terminal;
   38   
   39   
   40   /**
   41    *
   42    *
   43    * @author $author$
   44    * @version $Revision: 1.13 $
   45    */
   46   public final class Colorizer {
   47       private static Object Self; //Singleton instance reference
   48       private static int testcount = 0;
   49       private static Colorizer myColorizer;
   50   
   51       //Constants
   52       private static final int 
   53       /*black*/ S = 30;
   54   
   55       //Constants
   56       private static final int s = 40;
   57   
   58       //Constants
   59       private static final int 
   60       /*red*/ R = 31;
   61   
   62       //Constants
   63       private static final int r = 41;
   64   
   65       //Constants
   66       private static final int 
   67       /*green*/ G = 32;
   68   
   69       //Constants
   70       private static final int g = 42;
   71   
   72       //Constants
   73       private static final int 
   74       /*yellow*/ Y = 33;
   75   
   76       //Constants
   77       private static final int y = 43;
   78   
   79       //Constants
   80       private static final int 
   81       /*blue*/ B = 34;
   82   
   83       //Constants
   84       private static final int b = 44;
   85   
   86       //Constants
   87       private static final int 
   88       /*magenta*/ M = 35;
   89   
   90       //Constants
   91       private static final int m = 45;
   92   
   93       //Constants
   94       private static final int 
   95       /*cyan*/ C = 36;
   96   
   97       //Constants
   98       private static final int c = 46;
   99   
  100       //Constants
  101       private static final int 
  102       /*white*/ W = 37;
  103   
  104       //Constants
  105       private static final int w = 47;
  106   
  107       //Constants
  108       private static final int 
  109       /*bold*/ f = 1;
  110   
  111       //Constants
  112       private static final int 
  113       /*!bold*/ d = 22;
  114   
  115       //Constants
  116       private static final int 
  117       /*italic*/ i = 3;
  118   
  119       //Constants
  120       private static final int 
  121       /*!italic*/ j = 23;
  122   
  123       //Constants
  124       private static final int 
  125       /*underlined*/ u = 4;
  126   
  127       //Constants
  128       private static final int 
  129       /*!underlined*/ v = 24;
  130   
  131       //Constants
  132       private static final int 
  133       /*blink*/ e = 5;
  134   
  135       //Constants
  136       private static final int 
  137       /*steady*/ n = 25;
  138   
  139       //Constants
  140       private static final int 
  141       /*hide*/ h = 8;
  142   
  143       //Constants
  144       private static final int 
  145       /*all out*/ a = 0;
  146       private int[] colortranslation; //translation table
  147       private int leng;
  148   
  149       private Colorizer() {
  150           colortranslation = new int[128];
  151           colortranslation[83] = S;
  152           colortranslation[82] = R;
  153           colortranslation[71] = G;
  154           colortranslation[89] = Y;
  155           colortranslation[66] = B;
  156           colortranslation[77] = M;
  157           colortranslation[67] = C;
  158           colortranslation[87] = W;
  159           colortranslation[115] = s;
  160           colortranslation[114] = r;
  161           colortranslation[103] = g;
  162           colortranslation[121] = y;
  163           colortranslation[98] = b;
  164           colortranslation[109] = m;
  165           colortranslation[99] = c;
  166           colortranslation[119] = w;
  167           colortranslation[102] = f;
  168           colortranslation[100] = d;
  169           colortranslation[105] = i;
  170           colortranslation[106] = j;
  171           colortranslation[117] = u;
  172           colortranslation[118] = v;
  173           colortranslation[101] = e;
  174           colortranslation[110] = n;
  175           colortranslation[104] = h;
  176           colortranslation[97] = a;
  177           Self = this;
  178       }
  179   
  180       //constructor
  181       public String colorize(String str, boolean support) {
  182           StringBuffer out = new StringBuffer(str.length() + 20);
  183           int parsecursor = 0;
  184           int foundcursor = 0;
  185           boolean done = false;
  186   
  187           while (!done) {
  188               foundcursor = str.indexOf(ColorHelper.MARKER_CODE, parsecursor);
  189   
  190               if (foundcursor != -1) {
  191                   out.append(str.substring(parsecursor, foundcursor));
  192   
  193                   if (support) {
  194                       out.append(addEscapeSequence(str.substring(foundcursor + 1,
  195                                   foundcursor + 2)));
  196                   }
  197   
  198                   parsecursor = foundcursor + 2;
  199               } else {
  200                   out.append(str.substring(parsecursor, str.length()));
  201                   done = true;
  202               }
  203           }
  204   
  205           /*
  206    * This will always add a "reset all" escape sequence
  207    * behind the input string.
  208    * Basically this is a good idea, because developers tend to
  209    * forget writing colored strings properly.
  210    */
  211           if (support) {
  212               out.append(addEscapeSequence("a"));
  213           }
  214   
  215           return out.toString();
  216       }
  217   
  218       //colorize
  219       private String addEscapeSequence(String attribute) {
  220           StringBuffer tmpbuf = new StringBuffer(10);
  221           byte[] tmpbytes = attribute.getBytes();
  222           int key = (int) tmpbytes[0];
  223           tmpbuf.append((char) 27);
  224           tmpbuf.append((char) 91);
  225           tmpbuf.append((new Integer(colortranslation[key])).toString());
  226           tmpbuf.append((char) 109);
  227   
  228           return tmpbuf.toString();
  229       }
  230   
  231       //addEscapeSequence
  232       public static Colorizer getReference() {
  233           if (Self != null) {
  234               return (Colorizer) Self;
  235           } else {
  236               return new Colorizer();
  237           }
  238       }
  239   
  240       //getReference
  241       private static void announceResult(boolean res) {
  242           if (res) {
  243               System.out.println("[#" + testcount + "] ok.");
  244           } else {
  245               System.out.println("[#" + testcount +
  246                   "] failed (see possible StackTrace).");
  247           }
  248       }
  249   
  250       //announceResult
  251       private static void announceTest(String what) {
  252           testcount++;
  253           System.out.println("Test #" + testcount + " [" + what + "]:");
  254       }
  255   
  256       //announceTest
  257       private static void bfcolorTest(String color) {
  258           System.out.println("->" +
  259               myColorizer.colorize(ColorHelper.boldcolorizeText("COLOR", color),
  260                   true) + "<-");
  261       }
  262   
  263       //bfcolorTest
  264       private static void fcolorTest(String color) {
  265           System.out.println("->" +
  266               myColorizer.colorize(ColorHelper.colorizeText("COLOR", color), true) +
  267               "<-");
  268       }
  269   
  270       //fcolorTest
  271       private static void bcolorTest(String color) {
  272           System.out.println("->" +
  273               myColorizer.colorize(ColorHelper.colorizeBackground("     ", color),
  274                   true) + "<-");
  275       }
  276   
  277       //bcolorTest
  278       public static void main(String[] args) {
  279           try {
  280               announceTest("Instantiation");
  281               myColorizer = Colorizer.getReference();
  282               announceResult(true);
  283               announceTest("Textcolor Tests");
  284               fcolorTest(ColorHelper.BLACK);
  285               fcolorTest(ColorHelper.RED);
  286               fcolorTest(ColorHelper.GREEN);
  287               fcolorTest(ColorHelper.YELLOW);
  288               fcolorTest(ColorHelper.BLUE);
  289               fcolorTest(ColorHelper.MAGENTA);
  290               fcolorTest(ColorHelper.CYAN);
  291               fcolorTest(ColorHelper.white);
  292               announceResult(true);
  293               announceTest("Bold textcolor Tests");
  294               bfcolorTest(ColorHelper.BLACK);
  295               bfcolorTest(ColorHelper.RED);
  296               bfcolorTest(ColorHelper.GREEN);
  297               bfcolorTest(ColorHelper.YELLOW);
  298               bfcolorTest(ColorHelper.BLUE);
  299               bfcolorTest(ColorHelper.MAGENTA);
  300               bfcolorTest(ColorHelper.CYAN);
  301               bfcolorTest(ColorHelper.white);
  302               announceResult(true);
  303               announceTest("Background Tests");
  304               bcolorTest(ColorHelper.BLACK);
  305               bcolorTest(ColorHelper.RED);
  306               bcolorTest(ColorHelper.GREEN);
  307               bcolorTest(ColorHelper.YELLOW);
  308               bcolorTest(ColorHelper.BLUE);
  309               bcolorTest(ColorHelper.MAGENTA);
  310               bcolorTest(ColorHelper.CYAN);
  311               bcolorTest(ColorHelper.white);
  312               announceResult(true);
  313               announceTest("Mixed Color Tests");
  314               System.out.println("->" +
  315                   myColorizer.colorize(ColorHelper.colorizeText("COLOR",
  316                           ColorHelper.white, ColorHelper.BLUE), true) + "<-");
  317               System.out.println("->" +
  318                   myColorizer.colorize(ColorHelper.colorizeText("COLOR",
  319                           ColorHelper.YELLOW, ColorHelper.GREEN), true) + "<-");
  320               System.out.println("->" +
  321                   myColorizer.colorize(ColorHelper.boldcolorizeText("COLOR",
  322                           ColorHelper.white, ColorHelper.BLUE), true) + "<-");
  323               System.out.println("->" +
  324                   myColorizer.colorize(ColorHelper.boldcolorizeText("COLOR",
  325                           ColorHelper.YELLOW, ColorHelper.GREEN), true) + "<-");
  326               announceResult(true);
  327               announceTest("Style Tests");
  328               System.out.println("->" +
  329                   myColorizer.colorize(ColorHelper.boldText("Bold"), true) +
  330                   "<-");
  331               System.out.println("->" +
  332                   myColorizer.colorize(ColorHelper.italicText("Italic"), true) +
  333                   "<-");
  334               System.out.println("->" +
  335                   myColorizer.colorize(ColorHelper.underlinedText("Underlined"),
  336                       true) + "<-");
  337               System.out.println("->" +
  338                   myColorizer.colorize(ColorHelper.blinkingText("Blinking"), true) +
  339                   "<-");
  340               announceResult(true);
  341               announceTest("Visible length test");
  342   
  343               String colorized = ColorHelper.boldcolorizeText("STRING",
  344                       ColorHelper.YELLOW);
  345               System.out.println("->" + myColorizer.colorize(colorized, true) +
  346                   "<-");
  347               System.out.println("Visible length=" +
  348                   ColorHelper.getVisibleLength(colorized));
  349               colorized = ColorHelper.boldcolorizeText("BANNER",
  350                       ColorHelper.white, ColorHelper.BLUE) +
  351                   ColorHelper.colorizeText("COLOR", ColorHelper.white,
  352                       ColorHelper.BLUE) + ColorHelper.underlinedText("UNDER");
  353               System.out.println("->" + myColorizer.colorize(colorized, true) +
  354                   "<-");
  355               System.out.println("Visible length=" +
  356                   ColorHelper.getVisibleLength(colorized));
  357               announceResult(true);
  358   
  359               if (false) {
  360                   throw new Exception(); //this will shut up jikes
  361               }
  362           } catch (Exception e) {
  363               announceResult(false);
  364               e.printStackTrace();
  365           }
  366       }
  367   
  368       //main (test routine)
  369   }
  370   
  371   
  372   //class Colorizer

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.terminal » [javadoc | source]