Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.util » [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   package com.sshtools.common.util;
   27   
   28   import java.awt;
   29   
   30   
   31   /**
   32    * Other utilities not worth their own class
   33    */
   34   public final class GeneralUtil {
   35       /**
   36   * This method will replace '&' with "&amp;", '"' with "&quot;", '<' with "&lt;" and '>' with "&gt;".
   37   *
   38   * @param html html to encode
   39   * @return encoded html
   40   */
   41       public static String encodeHTML(String html) {
   42           // Does java have a method of doing this?
   43           StringBuffer buf = new StringBuffer();
   44           char ch;
   45   
   46           for (int i = 0; i < html.length(); i++) {
   47               ch = html.charAt(i);
   48   
   49               switch (ch) {
   50               case '&':
   51   
   52                   //	May be already encoded
   53                   if (((i + 5) < html.length()) &&
   54                           html.substring(i + 1, i + 5).equals("amp;")) {
   55                       buf.append(ch);
   56                   } else {
   57                       buf.append("&amp;");
   58                   }
   59   
   60                   break;
   61   
   62               case '"':
   63                   buf.append("&quot;");
   64   
   65                   break;
   66   
   67               case '<':
   68                   buf.append("&lt;");
   69   
   70                   break;
   71   
   72               case '>':
   73                   buf.append("&gt;");
   74   
   75                   break;
   76   
   77               default:
   78                   buf.append(ch);
   79               }
   80           }
   81   
   82           return buf.toString();
   83       }
   84   
   85       /**
   86   * Return a <code>Color</code> object given a string representation of it
   87   *
   88   * @param color
   89   * @return string representation
   90   * @throws IllegalArgumentException if string in bad format
   91   */
   92       public static Color stringToColor(String s) {
   93           try {
   94               return new Color(Integer.decode("0x" + s.substring(1, 3)).intValue(),
   95                   Integer.decode("0x" + s.substring(3, 5)).intValue(),
   96                   Integer.decode("0x" + s.substring(5, 7)).intValue());
   97           } catch (Exception e) {
   98               return null;
   99           }
  100       }
  101   
  102       /**
  103   * Return a string representation of a color
  104   *
  105   * @param color
  106   * @return string representation
  107   */
  108       public static String colorToString(Color color) {
  109           if (color == null) {
  110               return "";
  111           }
  112   
  113           StringBuffer buf = new StringBuffer();
  114           buf.append('#');
  115           buf.append(numberToPaddedHexString(color.getRed(), 2));
  116           buf.append(numberToPaddedHexString(color.getGreen(), 2));
  117           buf.append(numberToPaddedHexString(color.getBlue(), 2));
  118   
  119           return buf.toString();
  120       }
  121   
  122       /**
  123   * Convert a number to a zero padded hex string
  124   *
  125   * @param int number
  126   * @return zero padded hex string
  127   * @throws IllegalArgumentException if number takes up more characters than
  128   *                                  <code>size</code>
  129   */
  130       public static String numberToPaddedHexString(int number, int size) {
  131           String s = Integer.toHexString(number);
  132   
  133           if (s.length() > size) {
  134               throw new IllegalArgumentException(
  135                   "Number too big for padded hex string");
  136           }
  137   
  138           StringBuffer buf = new StringBuffer();
  139   
  140           for (int i = 0; i < (size - s.length()); i++) {
  141               buf.append('0');
  142           }
  143   
  144           buf.append(s);
  145   
  146           return buf.toString();
  147       }
  148   }

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