Crossfire JXClient, Trunk  R20561
ExpressionParser.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 
25 import java.io.IOException;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class ExpressionParser {
35 
40  @NotNull
41  private static final String WIDTH = "WIDTH";
42 
47  @NotNull
48  private static final String HEIGHT = "HEIGHT";
49 
54  @NotNull
55  private static final String PREF_WIDTH = "PREF_WIDTH";
56 
61  @NotNull
62  private static final String PREF_HEIGHT = "PREF_HEIGHT";
63 
67  @NotNull
68  private static final Pattern PATTERN_EXPR = Pattern.compile("([0-9]+|"+WIDTH+"|"+HEIGHT+"|"+WIDTH+"/2|"+HEIGHT+"/2|"+PREF_WIDTH+"|"+PREF_HEIGHT+"|"+PREF_WIDTH+"/2|"+PREF_HEIGHT+"/2)([-+])(.+)");
69 
73  private ExpressionParser() {
74  }
75 
83  public static int parseInt(@NotNull final String str) throws IOException {
84  return parseExpression(str).evaluateConstant();
85  }
86 
94  @NotNull
95  public static Expression parseExpression(@NotNull final String str) throws IOException {
96  try {
97  return parseIntegerConstant(str);
98  } catch (final NumberFormatException ignored) {
99  // ignore
100  }
101 
102  Matcher matcher = PATTERN_EXPR.matcher(str);
103  if (!matcher.matches()) {
104  throw new IOException("invalid number: "+str);
105  }
106  Expression value;
107  try {
108  value = parseIntegerConstant(matcher.group(1));
109  while (true) {
110  final boolean negative = matcher.group(2).equals("-");
111  final String rest = matcher.group(3);
112 
113  matcher = PATTERN_EXPR.matcher(rest);
114  if (!matcher.matches()) {
115  final Expression expressionRest = parseIntegerConstant(rest);
116  value = new Expression(value, negative, expressionRest);
117  break;
118  }
119 
120  final Expression valueRest = parseIntegerConstant(matcher.group(1));
121  value = new Expression(value, negative, valueRest);
122  }
123  } catch (final NumberFormatException ex) {
124  throw new IOException("invalid number: "+str, ex);
125  }
126 
127  return value;
128  }
129 
136  @NotNull
137  private static Expression parseIntegerConstant(@NotNull final String str) {
138  try {
139  return new Expression(Integer.parseInt(str), 0, 0, 0, 0);
140  } catch (final NumberFormatException ex) {
141  if (str.equals(WIDTH)) {
142  return new Expression(0, 2, 0, 0, 0);
143  }
144 
145  if (str.equals(HEIGHT)) {
146  return new Expression(0, 0, 2, 0, 0);
147  }
148 
149  if (str.equals(WIDTH+"/2")) {
150  return new Expression(0, 1, 0, 0, 0);
151  }
152 
153  if (str.equals(HEIGHT+"/2")) {
154  return new Expression(0, 0, 1, 0, 0);
155  }
156 
157  if (str.equals(PREF_WIDTH)) {
158  return new Expression(0, 0, 0, 2, 0);
159  }
160 
161  if (str.equals(PREF_HEIGHT)) {
162  return new Expression(0, 0, 0, 0, 2);
163  }
164 
165  if (str.equals(PREF_WIDTH+"/2")) {
166  return new Expression(0, 0, 0, 1, 0);
167  }
168 
169  if (str.equals(PREF_HEIGHT+"/2")) {
170  return new Expression(0, 0, 0, 0, 1);
171  }
172 
173  throw ex;
174  }
175  }
176 
177 }
static final String HEIGHT
The identifier evaluating to the height in pixels of the current resolution.
static final String PREF_HEIGHT
The identifier evaluating to the preferred height in pixels of the current dialog.
static Expression parseExpression(@NotNull final String str)
Parses an integer constant.
static int parseInt(@NotNull final String str)
Parses an integer constant.
static final String PREF_WIDTH
The identifier evaluating to the preferred width in pixels of the current dialog. ...
An expression yielding an integer value derived from a screen resolution.
Definition: Expression.java:30
int evaluateConstant()
Evaluates the expression into a constant.
static final Pattern PATTERN_EXPR
Pattern to parse integer constants.
static Expression parseIntegerConstant(@NotNull final String str)
Parses an integer constant string.
ExpressionParser()
Private constructor to prevent instantiation.
static final String WIDTH
The identifier evaluating to the width in pixels of the current resolution.