1
/* BasicValueCheckers.java
2
Copyright (C) 2010 Red Hat, Inc.
4
This file is part of IcedTea.
6
IcedTea is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, version 2.
10
IcedTea is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with IcedTea; see the file COPYING. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
Linking this library statically or dynamically with other modules is
21
making a combined work based on this library. Thus, the terms and
22
conditions of the GNU General Public License cover the whole
25
As a special exception, the copyright holders of this library give you
26
permission to link this library with independent modules to produce an
27
executable, regardless of the license terms of these independent
28
modules, and to copy and distribute the resulting executable under
29
terms of your choice, provided that you also meet, for each linked
30
independent module, the terms and conditions of the license of that
31
module. An independent module is a module which is not derived from
32
or based on this library. If you modify this library, you may extend
33
this exception to your version of the library, but you are not
34
obligated to do so. If you do not wish to do so, delete this
35
exception statement from your version.
38
package net.sourceforge.jnlp.config;
40
import static net.sourceforge.jnlp.runtime.Translator.R;
43
import java.util.Arrays;
44
import java.util.Locale;
47
* Provides {@link ValueValidator} implementations for some common value types
49
* @see #getBooleanValidator()
50
* @see #getFilePathValidator()
51
* @see #getRangedIntegerValidator(int, int)
52
* @see #getStringValidator(String[])
53
* @see #getUrlValidator()
55
public class BasicValueValidators {
58
* Checks if a value is a valid boolean
60
private static class BooleanValidator implements ValueValidator {
63
public void validate(Object value) throws IllegalArgumentException {
64
Object possibleValue = value;
66
if (possibleValue instanceof String) {
67
String lower = ((String) possibleValue).toLowerCase(Locale.ENGLISH);
68
if (lower.equals(Boolean.TRUE.toString())
69
|| (lower.equals(Boolean.FALSE.toString()))) {
70
possibleValue = Boolean.valueOf(lower);
74
if (!(possibleValue instanceof Boolean)) {
75
throw new IllegalArgumentException();
80
public String getPossibleValues() {
81
return R("VVPossibleBooleanValues", Boolean.TRUE.toString(), Boolean.FALSE.toString());
86
* Checks if a value is a valid file path (not a valid file!). The actual
87
* file may or may not exist
89
private static class FilePathValidator implements ValueValidator {
92
public void validate(Object value) throws IllegalArgumentException {
97
Object possibleValue = value;
99
if (!(possibleValue instanceof String)) {
100
throw new IllegalArgumentException();
103
String possibleFile = (String) possibleValue;
104
if (!(possibleFile.startsWith("/"))) {
105
throw new IllegalArgumentException();
111
public String getPossibleValues() {
112
return R("VVPossibleFileValues");
118
* Checks that the value is an Integer or Long (or a String representation
119
* of one) that is within a desired range).
121
private static class RangedIntegerValidator implements ValueValidator {
123
private int high = 0;
125
public RangedIntegerValidator(int low, int high) {
131
public void validate(Object value) throws IllegalArgumentException {
132
Object possibleValue = value;
134
long actualValue = 0;
136
if (possibleValue instanceof String) {
137
actualValue = Long.valueOf((String) possibleValue);
138
} else if (possibleValue instanceof Integer) {
139
actualValue = (Integer) possibleValue;
140
} else if (possibleValue instanceof Long) {
141
actualValue = (Long) possibleValue;
143
throw new IllegalArgumentException("Must be an integer");
145
} catch (NumberFormatException e) {
146
throw new IllegalArgumentException("Must be an integer");
150
if (actualValue < low || actualValue > high) {
151
throw new IllegalArgumentException("Not in range from " + low + " to " + high);
156
public String getPossibleValues() {
157
return R("VVPossibleRangedIntegerValues", low, high);
163
* Checks that the value is one of the acceptable String values
165
private static class StringValueValidator implements ValueValidator {
166
String[] options = null;
168
public StringValueValidator(String[] acceptableOptions) {
169
options = acceptableOptions;
173
public void validate(Object value) throws IllegalArgumentException {
174
Object possibleValue = value;
175
if (!(possibleValue instanceof String)) {
176
throw new IllegalArgumentException("Must be a string");
179
String stringVal = (String) possibleValue;
180
boolean found = false;
181
for (String knownVal : options) {
182
if (knownVal.equals(stringVal)) {
189
throw new IllegalArgumentException();
194
public String getPossibleValues() {
195
return Arrays.toString(options);
201
* Checks that the value is a URL
203
private static class UrlValidator implements ValueValidator {
206
public void validate(Object value) throws IllegalArgumentException {
211
new URL((String) value);
212
} catch (Exception e) {
213
throw new IllegalArgumentException();
218
public String getPossibleValues() {
219
return R("VVPossibleUrlValues");
225
* @return a {@link ValueValidator} that can be used to check if an object is
228
public static ValueValidator getBooleanValidator() {
229
return new BooleanValidator();
233
* @return a {@link ValueValidator} that can be used to check if an object is
234
* a String containing a valid file path or not
236
public static ValueValidator getFilePathValidator() {
237
return new FilePathValidator();
241
* Returns a {@link ValueValidator} that checks if an object represents a
242
* valid integer (it is a Integer or Long or a String representation of
243
* one), within the given range. The values are inclusive.
244
* @param low the lowest valid value
245
* @param high the highest valid value
247
public static ValueValidator getRangedIntegerValidator(int low, int high) {
248
return new RangedIntegerValidator(low, high);
252
* Returns a {@link ValueValidator} that checks if an object is a string from
253
* one of the provided Strings.
254
* @param validValues an array of Strings which are considered valid
256
public static ValueValidator getStringValidator(String[] validValues) {
257
return new StringValueValidator(validValues);
261
* @return a {@link ValueValidator} that checks if an object represents a
264
public static ValueValidator getUrlValidator() {
265
return new UrlValidator();