00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.skin.io;
00023
00024 import com.realtime.crossfire.jxclient.gui.gauge.Orientation;
00025 import com.realtime.crossfire.jxclient.gui.gauge.OrientationParser;
00026 import com.realtime.crossfire.jxclient.settings.options.CheckBoxOption;
00027 import com.realtime.crossfire.jxclient.settings.options.OptionException;
00028 import com.realtime.crossfire.jxclient.settings.options.OptionManager;
00029 import com.realtime.crossfire.jxclient.stats.StatsParser;
00030 import com.realtime.crossfire.jxclient.util.NumberParser;
00031 import java.awt.Color;
00032 import java.io.IOException;
00033 import java.io.LineNumberReader;
00034 import org.jetbrains.annotations.NotNull;
00035 import org.jetbrains.annotations.Nullable;
00036
00041 public class ParseUtils {
00042
00046 private ParseUtils() {
00047 }
00048
00055 public static int parseStat(@NotNull final String name) throws IOException {
00056 try {
00057 return StatsParser.parseStat(name);
00058 } catch (final IllegalArgumentException ignored) {
00059
00060 }
00061
00062 throw new IOException("invalid stat name: "+name);
00063 }
00064
00071 @NotNull
00072 public static Orientation parseOrientation(@NotNull final String name) throws IOException {
00073 try {
00074 return OrientationParser.parseOrientation(name);
00075 } catch (final IllegalArgumentException ignored) {
00076
00077 }
00078
00079 throw new IOException("invalid orientation: "+name);
00080 }
00081
00088 @NotNull
00089 public static Color parseColor(@NotNull final String name) throws IOException {
00090 final Color color = parseColorNull(name);
00091 if (color != null) {
00092 return color;
00093 }
00094 throw new IOException("unknown color name "+name);
00095 }
00096
00102 @Nullable
00103 public static Color parseColorNull(@NotNull final String name) {
00104 final int pos = name.lastIndexOf('/');
00105 if (pos == -1) {
00106 return parseColorName(name);
00107 }
00108
00109 int alpha = 255;
00110 try {
00111 alpha = Math.round(255*NumberParser.parseFloat(name.substring(pos+1)));
00112 } catch (final IOException ignored) {
00113
00114 }
00115 if (alpha < 0 || alpha > 255) {
00116 return parseColorName(name);
00117 }
00118
00119 final String colorName = name.substring(0, pos);
00120 final Color color = parseColorName(colorName);
00121 if (color == null) {
00122 return null;
00123 }
00124 if (alpha == 255) {
00125 return color;
00126 }
00127
00128 return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
00129 }
00130
00136 @Nullable
00137 private static Color parseColorName(@NotNull final String name) {
00138 if (name.equals("BLACK")) {
00139 return Color.BLACK;
00140 }
00141 if (name.equals("BLUE")) {
00142 return Color.BLUE;
00143 }
00144 if (name.equals("DARK_GRAY")) {
00145 return Color.DARK_GRAY;
00146 }
00147 if (name.equals("GRAY")) {
00148 return Color.GRAY;
00149 }
00150 if (name.equals("WHITE")) {
00151 return Color.WHITE;
00152 }
00153 if (name.length() == 7 && name.charAt(0) == '#' && name.charAt(1) != '-') {
00154 try {
00155 return new Color(Integer.parseInt(name.substring(1), 16));
00156 } catch (final NumberFormatException ignored) {
00157
00158 }
00159 }
00160 return null;
00161 }
00162
00172 @NotNull
00173 public static String parseText(@NotNull final Args args, @NotNull final LineNumberReader lnr) throws IOException {
00174 final StringBuilder text = new StringBuilder();
00175 if (args.hasMore()) {
00176 text.append(args.get());
00177 while (args.hasMore()) {
00178 text.append(' ');
00179 text.append(args.get());
00180 }
00181 }
00182 if (text.toString().equals("<<EOF")) {
00183 text.setLength(0);
00184 while (true) {
00185 final String line = lnr.readLine();
00186 if (line == null) {
00187 throw new IOException();
00188 }
00189 if (line.equals("EOF")) {
00190 break;
00191 }
00192 if (line.startsWith("#")) {
00193 continue;
00194 }
00195
00196 text.append(line);
00197 text.append('\n');
00198 }
00199 if (text.length() > 0) {
00200 text.setLength(text.length()-1);
00201 }
00202 }
00203
00204 return text.toString().replaceFirst("_$", " ");
00205 }
00206
00214 @NotNull
00215 public static CheckBoxOption parseCheckBoxOption(@NotNull final String name, @NotNull final OptionManager optionManager) throws IOException {
00216 try {
00217 return optionManager.getCheckBoxOption(name);
00218 } catch (final OptionException ex) {
00219 throw new IOException(ex.getMessage());
00220 }
00221 }
00222
00223 }