001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.util;
027:
028: import java.awt.*;
029:
030: /**
031: * Other utilities not worth their own class
032: */
033: public final class GeneralUtil {
034: /**
035: * This method will replace '&' with "&", '"' with """, '<' with "<" and '>' with ">".
036: *
037: * @param html html to encode
038: * @return encoded html
039: */
040: public static String encodeHTML(String html) {
041: // Does java have a method of doing this?
042: StringBuffer buf = new StringBuffer();
043: char ch;
044:
045: for (int i = 0; i < html.length(); i++) {
046: ch = html.charAt(i);
047:
048: switch (ch) {
049: case '&':
050:
051: // May be already encoded
052: if (((i + 5) < html.length())
053: && html.substring(i + 1, i + 5).equals("amp;")) {
054: buf.append(ch);
055: } else {
056: buf.append("&");
057: }
058:
059: break;
060:
061: case '"':
062: buf.append(""");
063:
064: break;
065:
066: case '<':
067: buf.append("<");
068:
069: break;
070:
071: case '>':
072: buf.append(">");
073:
074: break;
075:
076: default:
077: buf.append(ch);
078: }
079: }
080:
081: return buf.toString();
082: }
083:
084: /**
085: * Return a <code>Color</code> object given a string representation of it
086: *
087: * @param color
088: * @return string representation
089: * @throws IllegalArgumentException if string in bad format
090: */
091: public static Color stringToColor(String s) {
092: try {
093: return new Color(Integer.decode("0x" + s.substring(1, 3))
094: .intValue(), Integer.decode(
095: "0x" + s.substring(3, 5)).intValue(), Integer
096: .decode("0x" + s.substring(5, 7)).intValue());
097: } catch (Exception e) {
098: return null;
099: }
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: }
|