Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.skin.io;
23 
31 import java.awt.Color;
32 import java.io.IOException;
33 import java.io.LineNumberReader;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class ParseUtils {
42 
46  private ParseUtils() {
47  }
48 
55  public static int parseStat(@NotNull final String name) throws IOException {
56  try {
57  return StatsParser.parseStat(name);
58  } catch (final IllegalArgumentException ignored) {
59  // ignore
60  }
61 
62  throw new IOException("invalid stat name: "+name);
63  }
64 
71  @NotNull
72  public static Orientation parseOrientation(@NotNull final String name) throws IOException {
73  try {
75  } catch (final IllegalArgumentException ignored) {
76  // ignore
77  }
78 
79  throw new IOException("invalid orientation: "+name);
80  }
81 
88  @NotNull
89  public static Color parseColor(@NotNull final String name) throws IOException {
90  final Color color = parseColorNull(name);
91  if (color != null) {
92  return color;
93  }
94  throw new IOException("unknown color name "+name);
95  }
96 
102  @Nullable
103  public static Color parseColorNull(@NotNull final String name) {
104  final int pos = name.lastIndexOf('/');
105  if (pos == -1) {
106  return parseColorName(name);
107  }
108 
109  int alpha = 255;
110  try {
111  alpha = Math.round(255*NumberParser.parseFloat(name.substring(pos+1)));
112  } catch (final IOException ignored) {
113  // ignore
114  }
115  if (alpha < 0 || alpha > 255) {
116  return parseColorName(name);
117  }
118 
119  final String colorName = name.substring(0, pos);
120  final Color color = parseColorName(colorName);
121  if (color == null) {
122  return null;
123  }
124  if (alpha == 255) {
125  return color;
126  }
127 
128  return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
129  }
130 
136  @Nullable
137  private static Color parseColorName(@NotNull final String name) {
138  if (name.equals("BLACK")) {
139  return Color.BLACK;
140  }
141  if (name.equals("BLUE")) {
142  return Color.BLUE;
143  }
144  if (name.equals("DARK_GRAY")) {
145  return Color.DARK_GRAY;
146  }
147  if (name.equals("GRAY")) {
148  return Color.GRAY;
149  }
150  if (name.equals("WHITE")) {
151  return Color.WHITE;
152  }
153  if (name.length() == 7 && name.charAt(0) == '#' && name.charAt(1) != '-') {
154  try {
155  return new Color(Integer.parseInt(name.substring(1), 16));
156  } catch (final NumberFormatException ignored) {
157  // ignore
158  }
159  }
160  return null;
161  }
162 
172  @NotNull
173  public static String parseText(@NotNull final Args args, @NotNull final LineNumberReader lnr) throws IOException {
174  final StringBuilder text = new StringBuilder();
175  if (args.hasMore()) {
176  text.append(args.get());
177  while (args.hasMore()) {
178  text.append(' ');
179  text.append(args.get());
180  }
181  }
182  if (text.toString().equals("<<EOF")) {
183  text.setLength(0);
184  while (true) {
185  final String line = lnr.readLine();
186  if (line == null) {
187  throw new IOException("EOF");
188  }
189  if (line.equals("EOF")) {
190  break;
191  }
192  if (line.startsWith("#")) {
193  continue;
194  }
195 
196  text.append(line);
197  text.append('\n');
198  }
199  if (text.length() > 0) {
200  text.setLength(text.length()-1);
201  }
202  }
203 
204  return text.toString().replaceFirst("_$", " ");
205  }
206 
214  @NotNull
215  public static CheckBoxOption parseCheckBoxOption(@NotNull final String name, @NotNull final OptionManager optionManager) throws IOException {
216  try {
217  return optionManager.getCheckBoxOption(name);
218  } catch (final OptionException ex) {
219  throw new IOException(ex.getMessage(), ex);
220  }
221  }
222 
223 }
static Color parseColor(@NotNull final String name)
Parses a color name.
Definition: ParseUtils.java:89
static Orientation parseOrientation(@NotNull final String name)
Parses an orientation value.
Definition: ParseUtils.java:72
Utility class to parse stat names.
Utility class for parsing string parameters into values.
Definition: ParseUtils.java:41
static float parseFloat(@NotNull final String str)
Parses a float constant.
static int parseStat(@NotNull final String name)
Parses a stat value.
Definition: ParseUtils.java:55
static Color parseColorNull(@NotNull final String name)
Parses a color name, optionally followed by "/&lt;alpha&gt;".
static Color parseColorName(@NotNull final String name)
Parses a color name.
Utility class for parsing strings into numbers.
static Orientation parseOrientation(@NotNull final String name)
Converts an orientation name into an Orientation instance.
ParseUtils()
Private constructor to prevent instantiation.
Definition: ParseUtils.java:46
static CheckBoxOption parseCheckBoxOption(@NotNull final String name, @NotNull final OptionManager optionManager)
Parses a check box option name.
static String parseText(@NotNull final Args args, @NotNull final LineNumberReader lnr)
Concatenates trailing arguments into a string.
static int parseStat(@NotNull final String name)
Converts a stat name into a stat index.