1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* libbrlapi - A library providing access to braille terminals for applications.
*
* Copyright (C) 2006-2025 by
* Samuel Thibault <Samuel.Thibault@ens-lyon.org>
* Sébastien Hinderer <Sebastien.Hinderer@ens-lyon.org>
*
* libbrlapi comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
package org.a11y.brlapi;
public abstract class Parse {
private Parse () {
super();
}
private final static KeywordMap<Boolean> booleanKeywords = new KeywordMap<>();
static {
Boolean FALSE = false;
Boolean TRUE = true;
booleanKeywords.put("false", FALSE);
booleanKeywords.put("true", TRUE);
booleanKeywords.put("off", FALSE);
booleanKeywords.put("on", TRUE);
booleanKeywords.put("no", FALSE);
booleanKeywords.put("yes", TRUE);
booleanKeywords.put("0", FALSE);
booleanKeywords.put("1", TRUE);
}
public static Boolean asBoolean (String description, String operand)
throws SyntaxException
{
Boolean value = booleanKeywords.get(operand);
if (value != null) return value;
throw new SyntaxException("%s is not a boolean: %s", description, operand);
}
public static void checkMinimum (String description, long value, long minimum)
throws SyntaxException
{
if (value < minimum) {
throw new SyntaxException(
"%s is less than %d: %d", description, minimum, value
);
}
}
public static void checkMaximum (String description, long value, long maximum)
throws SyntaxException
{
if (value > maximum) {
throw new SyntaxException(
"%s is greater than %d: %d", description, maximum, value
);
}
}
public static void checkRange (String description, long value, long minimum, long maximum)
throws SyntaxException
{
checkMinimum(description, value, minimum);
checkMaximum(description, value, maximum);
}
private static Number asNumber (String description, String operand, long minimum, long maximum)
throws SyntaxException
{
long value;
try {
value = Long.valueOf(operand);
checkRange(description, value, minimum, maximum);
return Long.valueOf(value);
} catch (NumberFormatException exception) {
throw new SyntaxException(
"%s is not an integer: %s", description, operand
);
}
}
public final static byte DEFAULT_RANGE_MINIMUM = 0;
public static long asLong (String description, String operand, long minimum, long maximum)
throws SyntaxException
{
return asNumber(description, operand, minimum, maximum).longValue();
}
public static long asLong (String description, String operand, long minimum)
throws SyntaxException
{
return asLong(description, operand, minimum, Long.MAX_VALUE);
}
public static long asLong (String description, String operand)
throws SyntaxException
{
return asLong(description, operand, DEFAULT_RANGE_MINIMUM);
}
public static int asInt (String description, String operand, int minimum, int maximum)
throws SyntaxException
{
return asNumber(description, operand, minimum, maximum).intValue();
}
public static int asInt (String description, String operand, int minimum)
throws SyntaxException
{
return asInt(description, operand, minimum, Integer.MAX_VALUE);
}
public static int asInt (String description, String operand)
throws SyntaxException
{
return asInt(description, operand, DEFAULT_RANGE_MINIMUM);
}
public static short asShort (String description, String operand, short minimum, short maximum)
throws SyntaxException
{
return asNumber(description, operand, minimum, maximum).shortValue();
}
public static short asShort (String description, String operand, short minimum)
throws SyntaxException
{
return asShort(description, operand, minimum, Short.MAX_VALUE);
}
public static short asShort (String description, String operand)
throws SyntaxException
{
return asShort(description, operand, DEFAULT_RANGE_MINIMUM);
}
public static byte asByte (String description, String operand, byte minimum, byte maximum)
throws SyntaxException
{
return asNumber(description, operand, minimum, maximum).byteValue();
}
public static byte asByte (String description, String operand, byte minimum)
throws SyntaxException
{
return asByte(description, operand, minimum, Byte.MAX_VALUE);
}
public static byte asByte (String description, String operand)
throws SyntaxException
{
return asByte(description, operand, DEFAULT_RANGE_MINIMUM);
}
public static byte asDots (String description, String operand)
throws SyntaxException
{
if (operand.equals("0")) return 0;
byte dots = 0;
String numbers = "12345678";
for (char number : operand.toCharArray()) {
int index = numbers.indexOf(number);
if (index < 0) {
throw new SyntaxException(
"%s contains an invalid dot number: %s (%c)", description, operand, number
);
}
int dot = 1 << index;
if ((dots & dot) != 0) {
throw new SyntaxException(
"%s contains a duplicate dot number: %s (%c)", description, operand, number
);
}
dots |= dot;
}
return dots;
}
public final static int MINIMUM_CURSOR_POSITION = 1;
public final static String NO_CURSOR = "no";
public final static String LEAVE_CURSOR = "leave";
public static int asCursorPosition (String operand) throws SyntaxException {
if (Strings.isAbbreviation(NO_CURSOR, operand)) {
return Constants.CURSOR_OFF;
}
if (Strings.isAbbreviation(LEAVE_CURSOR, operand)) {
return Constants.CURSOR_LEAVE;
}
return asInt(
WriteArguments.CURSOR_POSITION,
operand, MINIMUM_CURSOR_POSITION
);
}
public final static int MINIMUM_DISPLAY_NUMBER = 1;
public final static String DEFAULT_DISPLAY = "default";
public static int asDisplayNumber (String operand) throws SyntaxException {
if (Strings.isAbbreviation(DEFAULT_DISPLAY, operand)) {
return Constants.DISPLAY_DEFAULT;
}
return asInt(
WriteArguments.DISPLAY_NUMBER,
operand, MINIMUM_DISPLAY_NUMBER
);
}
}
|