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.util.StringTokenizer; 25 import java.awt.Rectangle; 26 import java.awt.Dimension; 27 import java.awt.Color; 28 29 30 /** 31 * A utility class which parses certain common types from Strings. 32 * 33 * @see StringEncoder 34 */ 35 36 public class StringParser{ 37 38 39 40 /** 41 * Parses the given String as a Rectangle object. The expected format is: 42 * "<x>;<y>;<width>;<height>" where <value> is replaced by an integer value. 43 * 44 * @throws FormatException if the given String is not in the expected wrong 45 * format. 46 * 47 * @see StringEncoder#encodeRectangle(Rectangle) 48 */ 49 parseRectangle(String rectString)50 public static Rectangle parseRectangle(String rectString){ 51 StringTokenizer tokenizer = new StringTokenizer(rectString, ";"); 52 if (tokenizer.countTokens() != 4) 53 throw new FormatException("Wrong Rectangle format: " + rectString); 54 55 try{ 56 int x = Integer.parseInt(tokenizer.nextToken()); 57 int y = Integer.parseInt(tokenizer.nextToken()); 58 int width = Integer.parseInt(tokenizer.nextToken()); 59 int height = Integer.parseInt(tokenizer.nextToken()); 60 return new Rectangle(x, y, width, height); 61 } catch (NumberFormatException e){ 62 throw new FormatException(e,"Wrong Rectangle format: " + rectString); 63 } 64 } 65 66 67 68 /** 69 * Parses the given String as a RectDouble object. The expected format is: 70 * "<x>;<y>;<width>;<height>" where <value> is replaced by a double value, 71 * parseable by {@link Double#valueOf(String)}. 72 * 73 * @throws FormatException if the given String is not in the expected wrong 74 * format. 75 * 76 * @see StringEncoder#encodeRectDouble(RectDouble) 77 */ 78 parseRectDouble(String rectString)79 public static RectDouble parseRectDouble(String rectString){ 80 StringTokenizer tokenizer = new StringTokenizer(rectString, ";"); 81 if (tokenizer.countTokens() != 4) 82 throw new FormatException("Wrong Rectangle format: " + rectString); 83 84 try{ 85 double x = Double.valueOf(tokenizer.nextToken()).doubleValue(); 86 double y = Double.valueOf(tokenizer.nextToken()).doubleValue(); 87 double width = Double.valueOf(tokenizer.nextToken()).doubleValue(); 88 double height = Double.valueOf(tokenizer.nextToken()).doubleValue(); 89 return new RectDouble(x, y, width, height); 90 } catch (NumberFormatException e){ 91 throw new FormatException(e,"Wrong Rectangle format: " + rectString); 92 } 93 } 94 95 96 97 /** 98 * Parses the given String as a Dimension object. The expected format is: 99 * "<width>;<height>" where <value> is replaced by an integer value. 100 * 101 * @throws FormatException if the given String is not in the expected wrong 102 * format. 103 * 104 * @see StringEncoder#encodeDimension(Dimension) 105 */ 106 parseDimension(String dimString)107 public static Dimension parseDimension(String dimString){ 108 StringTokenizer tokenizer = new StringTokenizer(dimString, ";"); 109 if (tokenizer.countTokens() != 2) 110 throw new FormatException("Wrong Dimension format: " + dimString); 111 112 try{ 113 int width = Integer.parseInt(tokenizer.nextToken()); 114 int height = Integer.parseInt(tokenizer.nextToken()); 115 return new Dimension(width, height); 116 } catch (NumberFormatException e){ 117 throw new FormatException(e,"Wrong Dimension format: " + dimString); 118 } 119 } 120 121 122 123 /** 124 * Parses the given string as a color. The expected format is a hexadecimal 125 * value in the range [0..0xffffff] in RGB format. 126 * 127 * @throws FormatException if the given String is not in the correct format. 128 */ 129 parseColor(String colorString)130 public static Color parseColor(String colorString){ 131 try{ 132 int colorInt = Integer.parseInt(colorString, 16); 133 if ((colorInt < 0) || (colorInt > 0xffffffL)) 134 throw new FormatException("Wrong Color format: " + colorString); 135 136 return new Color(colorInt); 137 } catch (NumberFormatException e){ 138 throw new FormatException(e, "Wrong Color format: " + colorString); 139 } 140 } 141 142 143 144 /** 145 * Parses the given string as a list of integers. 146 */ 147 parseIntList(String text)148 public static int [] parseIntList(String text){ 149 return TextUtilities.parseIntList(text, " "); 150 } 151 152 153 154 /** 155 * Unescapes the specified string. 156 */ 157 parseString(String s)158 public static String parseString(String s){ 159 StringBuffer buf = null; 160 for (int i = 0; i < s.length(); i++){ 161 char c = s.charAt(i); 162 if (c == '\\'){ 163 if (buf == null) 164 buf = new StringBuffer(s.substring(0, i)); // Initialize lazily. 165 c = s.charAt(++i); 166 switch (c){ 167 case 'n': buf.append('\n'); break; 168 case 'r': buf.append('\r'); break; 169 case 't': buf.append('\t'); break; 170 case '\\': buf.append('\\'); break; 171 default: 172 buf.append('\''); 173 buf.append(c); 174 } 175 } 176 else if (buf != null) 177 buf.append(c); 178 } 179 180 return buf == null ? s : buf.toString(); 181 } 182 183 } 184