Crossfire JXClient, Trunk
ParseUtils.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.skin.io;
24 
32 import java.awt.Color;
33 import java.io.IOException;
34 import java.io.LineNumberReader;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class ParseUtils {
43 
47  private ParseUtils() {
48  }
49 
56  public static int parseStat(@NotNull final String name) throws IOException {
57  try {
58  return StatsParser.parseStat(name);
59  } catch (final IllegalArgumentException ignored) {
60  // ignore
61  }
62 
63  throw new IOException("invalid stat name: "+name);
64  }
65 
72  @NotNull
73  public static Orientation parseOrientation(@NotNull final String name) throws IOException {
74  try {
76  } catch (final IllegalArgumentException ignored) {
77  // ignore
78  }
79 
80  throw new IOException("invalid orientation: "+name);
81  }
82 
89  @NotNull
90  public static Color parseColor(@NotNull final String name) throws IOException {
91  final Color color = parseColorNull(name);
92  if (color != null) {
93  return color;
94  }
95  throw new IOException("unknown color name "+name);
96  }
97 
103  @Nullable
104  public static Color parseColorNull(@NotNull final String name) {
105  final int pos = name.lastIndexOf('/');
106  if (pos == -1) {
107  return parseColorName(name);
108  }
109 
110  int alpha = 255;
111  try {
112  alpha = Math.round(255*NumberParser.parseFloat(name.substring(pos+1)));
113  } catch (final IOException ignored) {
114  // ignore
115  }
116  if (alpha < 0 || alpha > 255) {
117  return parseColorName(name);
118  }
119 
120  final String colorName = name.substring(0, pos);
121  final Color color = parseColorName(colorName);
122  if (color == null) {
123  return null;
124  }
125  if (alpha == 255) {
126  return color;
127  }
128 
129  return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
130  }
131 
137  @Nullable
138  private static Color parseColorName(@NotNull final String name) {
139  if (name.equals("BLACK")) {
140  return Color.BLACK;
141  }
142  if (name.equals("BLUE")) {
143  return Color.BLUE;
144  }
145  if (name.equals("DARK_GRAY")) {
146  return Color.DARK_GRAY;
147  }
148  if (name.equals("GRAY")) {
149  return Color.GRAY;
150  }
151  if (name.equals("LIGHT_GRAY")) {
152  return Color.LIGHT_GRAY;
153  }
154  if (name.equals("RED")) {
155  return Color.RED;
156  }
157  if (name.equals("WHITE")) {
158  return Color.WHITE;
159  }
160  if (name.length() == 7 && name.charAt(0) == '#' && name.charAt(1) != '-') {
161  try {
162  return new Color(Integer.parseInt(name.substring(1), 16));
163  } catch (final NumberFormatException ignored) {
164  // ignore
165  }
166  }
167  return null;
168  }
169 
179  @NotNull
180  public static String parseText(@NotNull final Args args, @NotNull final LineNumberReader lnr) throws IOException {
181  final StringBuilder text = new StringBuilder();
182  if (args.hasMore()) {
183  text.append(args.get());
184  while (args.hasMore()) {
185  text.append(' ');
186  text.append(args.get());
187  }
188  }
189  if (text.toString().equals("<<EOF")) {
190  text.setLength(0);
191  while (true) {
192  final String line = lnr.readLine();
193  if (line == null) {
194  throw new IOException("EOF");
195  }
196  if (line.equals("EOF")) {
197  break;
198  }
199  if (line.startsWith("#")) {
200  continue;
201  }
202 
203  text.append(line);
204  text.append('\n');
205  }
206  if (text.length() > 0) {
207  text.setLength(text.length()-1);
208  }
209  }
210 
211  return text.toString().replaceFirst("_$", " ");
212  }
213 
221  @NotNull
222  public static CheckBoxOption parseCheckBoxOption(@NotNull final String name, @NotNull final OptionManager optionManager) throws IOException {
223  try {
224  return optionManager.getCheckBoxOption(name);
225  } catch (final OptionException ex) {
226  throw new IOException(ex.getMessage(), ex);
227  }
228  }
229 
230 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.stats.StatsParser.parseStat
static int parseStat(@NotNull final String name)
Definition: StatsParser.java:140
com.realtime.crossfire.jxclient.skin.io.Args
Definition: Args.java:32
com.realtime.crossfire.jxclient.gui.gauge.OrientationParser.parseOrientation
static Orientation parseOrientation(@NotNull final String name)
Definition: OrientationParser.java:46
com.realtime.crossfire.jxclient.settings.options.OptionException
Definition: OptionException.java:31
com.realtime.crossfire.jxclient.skin.io.ParseUtils
Definition: ParseUtils.java:42
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseOrientation
static Orientation parseOrientation(@NotNull final String name)
Definition: ParseUtils.java:73
com.realtime.crossfire.jxclient.skin.io.ParseUtils.ParseUtils
ParseUtils()
Definition: ParseUtils.java:47
com.realtime.crossfire.jxclient.util.NumberParser.parseFloat
static float parseFloat(@NotNull final String str)
Definition: NumberParser.java:95
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseColorName
static Color parseColorName(@NotNull final String name)
Definition: ParseUtils.java:138
com.realtime.crossfire.jxclient.gui.gauge
Definition: AbstractOrientation.java:23
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.gui.gauge.OrientationParser
Definition: OrientationParser.java:31
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseText
static String parseText(@NotNull final Args args, @NotNull final LineNumberReader lnr)
Definition: ParseUtils.java:180
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseColor
static Color parseColor(@NotNull final String name)
Definition: ParseUtils.java:90
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire
com.realtime.crossfire.jxclient.settings.options
Definition: CheckBoxOption.java:23
com.realtime
com
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseColorNull
static Color parseColorNull(@NotNull final String name)
Definition: ParseUtils.java:104
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseStat
static int parseStat(@NotNull final String name)
Definition: ParseUtils.java:56
com.realtime.crossfire.jxclient.stats.StatsParser
Definition: StatsParser.java:33
com.realtime.crossfire.jxclient.settings.options.CheckBoxOption
Definition: CheckBoxOption.java:32
com.realtime.crossfire.jxclient.util.NumberParser
Definition: NumberParser.java:32
com.realtime.crossfire.jxclient.skin.io.ParseUtils.parseCheckBoxOption
static CheckBoxOption parseCheckBoxOption(@NotNull final String name, @NotNull final OptionManager optionManager)
Definition: ParseUtils.java:222
com.realtime.crossfire.jxclient.settings.options.OptionManager
Definition: OptionManager.java:36
com.realtime.crossfire.jxclient.gui.gauge.Orientation
Definition: Orientation.java:29
com.realtime.crossfire.jxclient.stats
Definition: ActiveSkillWatcher.java:23