1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.util; 23 24 import java.awt.Rectangle; 25 import java.awt.Dimension; 26 import java.awt.Color; 27 28 29 /** 30 * A utility class which encodes certain common types into Strings. 31 * 32 * @see StringParser 33 */ 34 35 36 public class StringEncoder{ 37 38 39 /** 40 * Encodes the given Rectangle into a String in the following format: 41 * "<x>;<y>;<width>;<height>" where <value> is replaced with the appropriate 42 * value of the given Rectangle. 43 * 44 * @see StringParser#parseRectangle(String) 45 */ 46 encodeRectangle(Rectangle rect)47 public static String encodeRectangle(Rectangle rect){ 48 StringBuffer buf = new StringBuffer(); 49 buf.append(rect.x); 50 buf.append(";"); 51 buf.append(rect.y); 52 buf.append(";"); 53 buf.append(rect.width); 54 buf.append(";"); 55 buf.append(rect.height); 56 57 return buf.toString(); 58 } 59 60 61 62 /** 63 * Encodes the given RectDouble into a String in the following format: 64 * "<x>;<y>;<width>;<height>" where <value> is replaced with the appropriate 65 * value of the given RectDouble. 66 * 67 * @see StringParser#parseRectDouble(String) 68 */ 69 encodeRectDouble(RectDouble rect)70 public static String encodeRectDouble(RectDouble rect){ 71 StringBuffer buf = new StringBuffer(); 72 buf.append(rect.getX()); 73 buf.append(";"); 74 buf.append(rect.getY()); 75 buf.append(";"); 76 buf.append(rect.getWidth()); 77 buf.append(";"); 78 buf.append(rect.getHeight()); 79 80 return buf.toString(); 81 } 82 83 84 85 /** 86 * Encodes the given Dimension into a String in the following format: 87 * "<width>;<height>" where <value> is replaced with the appropriate 88 * value of the given Dimension. 89 * 90 * @see StringParser#parseDimension(String) 91 */ 92 encodeDimension(Dimension dim)93 public static String encodeDimension(Dimension dim){ 94 StringBuffer buf = new StringBuffer(); 95 buf.append(dim.width); 96 buf.append(";"); 97 buf.append(dim.height); 98 99 return buf.toString(); 100 } 101 102 103 104 /** 105 * Encodes the given Color into a String in RGB format where each component 106 * is encoded into 2 characters as a hexadecimal string. 107 */ 108 encodeColor(Color color)109 public static String encodeColor(Color color){ 110 return Integer.toHexString(color.getRGB()&0xffffff); 111 } 112 113 114 115 /** 116 * Encodes the specified int list into a space delimited string. 117 */ 118 encodeIntList(int [] arr)119 public static String encodeIntList(int [] arr){ 120 StringBuffer buf = new StringBuffer(); 121 for (int i = 0; i < arr.length; i++){ 122 buf.append(String.valueOf(arr[i])); 123 buf.append(" "); 124 } 125 126 if (arr.length != 0) 127 buf.setLength(buf.length() - 1); 128 129 return buf.toString(); 130 } 131 132 133 134 /** 135 * Escapes the specified string. 136 */ 137 encodeString(String s)138 public static String encodeString(String s){ 139 StringBuffer buf = new StringBuffer(); 140 141 for (int i = 0; i < s.length(); i++){ 142 char c = s.charAt(i); 143 switch (c){ 144 case '\n': buf.append("\\n"); break; 145 case '\r': buf.append("\\r"); break; 146 case '\t': buf.append("\\t"); break; 147 case '\\': buf.append("\\\\"); break; 148 default: 149 buf.append(c); 150 } 151 } 152 153 return buf.toString(); 154 } 155 156 157 158 } 159