~ubuntu-branches/ubuntu/natty/icedtea-web/natty-security

« back to all changes in this revision

Viewing changes to netx/net/sourceforge/jnlp/config/BasicValueValidators.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-02-24 12:57:25 UTC
  • mto: (18.1.1 experimental) (19.1.1 natty-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20110224125725-8zq5v35r6o27w8ku
Tags: upstream-1.1~20110320
ImportĀ upstreamĀ versionĀ 1.1~20110320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* BasicValueCheckers.java
 
2
   Copyright (C) 2010 Red Hat, Inc.
 
3
 
 
4
This file is part of IcedTea.
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
02110-1301 USA.
 
19
 
 
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
 
23
combination.
 
24
 
 
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.
 
36
*/
 
37
 
 
38
package net.sourceforge.jnlp.config;
 
39
 
 
40
import static net.sourceforge.jnlp.runtime.Translator.R;
 
41
 
 
42
import java.net.URL;
 
43
import java.util.Arrays;
 
44
import java.util.Locale;
 
45
 
 
46
/**
 
47
 * Provides {@link ValueValidator} implementations for some common value types
 
48
 *
 
49
 * @see #getBooleanValidator()
 
50
 * @see #getFilePathValidator()
 
51
 * @see #getRangedIntegerValidator(int, int)
 
52
 * @see #getStringValidator(String[])
 
53
 * @see #getUrlValidator()
 
54
 */
 
55
public class BasicValueValidators {
 
56
 
 
57
    /**
 
58
     * Checks if a value is a valid boolean
 
59
     */
 
60
    private static class BooleanValidator implements ValueValidator {
 
61
 
 
62
        @Override
 
63
        public void validate(Object value) throws IllegalArgumentException {
 
64
            Object possibleValue = value;
 
65
 
 
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);
 
71
                }
 
72
            }
 
73
 
 
74
            if (!(possibleValue instanceof Boolean)) {
 
75
                throw new IllegalArgumentException();
 
76
            }
 
77
        }
 
78
 
 
79
        @Override
 
80
        public String getPossibleValues() {
 
81
            return R("VVPossibleBooleanValues", Boolean.TRUE.toString(), Boolean.FALSE.toString());
 
82
        }
 
83
    };
 
84
 
 
85
    /**
 
86
     * Checks if a value is a valid file path (not a valid file!). The actual
 
87
     * file may or may not exist
 
88
     */
 
89
    private static class FilePathValidator implements ValueValidator {
 
90
 
 
91
        @Override
 
92
        public void validate(Object value) throws IllegalArgumentException {
 
93
            if (value == null) {
 
94
                return;
 
95
            }
 
96
 
 
97
            Object possibleValue = value;
 
98
 
 
99
            if (!(possibleValue instanceof String)) {
 
100
                throw new IllegalArgumentException();
 
101
            }
 
102
 
 
103
            String possibleFile = (String) possibleValue;
 
104
            if (!(possibleFile.startsWith("/"))) {
 
105
                throw new IllegalArgumentException();
 
106
            }
 
107
 
 
108
        }
 
109
 
 
110
        @Override
 
111
        public String getPossibleValues() {
 
112
            return R("VVPossibleFileValues");
 
113
        }
 
114
 
 
115
    }
 
116
 
 
117
    /**
 
118
     * Checks that the value is an Integer or Long (or a String representation
 
119
     * of one) that is within a desired range).
 
120
     */
 
121
    private static class RangedIntegerValidator implements ValueValidator {
 
122
        private int low = 0;
 
123
        private int high = 0;
 
124
 
 
125
        public RangedIntegerValidator(int low, int high) {
 
126
            this.low = low;
 
127
            this.high = high;
 
128
        }
 
129
 
 
130
        @Override
 
131
        public void validate(Object value) throws IllegalArgumentException {
 
132
            Object possibleValue = value;
 
133
 
 
134
            long actualValue = 0;
 
135
            try {
 
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;
 
142
                } else {
 
143
                    throw new IllegalArgumentException("Must be an integer");
 
144
                }
 
145
            } catch (NumberFormatException e) {
 
146
                throw new IllegalArgumentException("Must be an integer");
 
147
 
 
148
            }
 
149
 
 
150
            if (actualValue < low || actualValue > high) {
 
151
                throw new IllegalArgumentException("Not in range from " + low + " to " + high);
 
152
            }
 
153
        }
 
154
 
 
155
        @Override
 
156
        public String getPossibleValues() {
 
157
            return R("VVPossibleRangedIntegerValues", low, high);
 
158
        }
 
159
 
 
160
    };
 
161
 
 
162
    /**
 
163
     * Checks that the value is one of the acceptable String values
 
164
     */
 
165
    private static class StringValueValidator implements ValueValidator {
 
166
        String[] options = null;
 
167
 
 
168
        public StringValueValidator(String[] acceptableOptions) {
 
169
            options = acceptableOptions;
 
170
        }
 
171
 
 
172
        @Override
 
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");
 
177
            }
 
178
 
 
179
            String stringVal = (String) possibleValue;
 
180
            boolean found = false;
 
181
            for (String knownVal : options) {
 
182
                if (knownVal.equals(stringVal)) {
 
183
                    found = true;
 
184
                    break;
 
185
                }
 
186
            }
 
187
 
 
188
            if (!found) {
 
189
                throw new IllegalArgumentException();
 
190
            }
 
191
        }
 
192
 
 
193
        @Override
 
194
        public String getPossibleValues() {
 
195
            return Arrays.toString(options);
 
196
        }
 
197
 
 
198
    }
 
199
 
 
200
    /**
 
201
     * Checks that the value is a URL
 
202
     */
 
203
    private static class UrlValidator implements ValueValidator {
 
204
 
 
205
        @Override
 
206
        public void validate(Object value) throws IllegalArgumentException {
 
207
            if (value == null) {
 
208
                return;
 
209
            }
 
210
            try {
 
211
                new URL((String) value);
 
212
            } catch (Exception e) {
 
213
                throw new IllegalArgumentException();
 
214
            }
 
215
        }
 
216
 
 
217
        @Override
 
218
        public String getPossibleValues() {
 
219
            return R("VVPossibleUrlValues");
 
220
        }
 
221
 
 
222
    }
 
223
 
 
224
    /**
 
225
     * @return a {@link ValueValidator} that can be used to check if an object is
 
226
     * a valid Boolean
 
227
     */
 
228
    public static ValueValidator getBooleanValidator() {
 
229
        return new BooleanValidator();
 
230
    }
 
231
 
 
232
    /**
 
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
 
235
     */
 
236
    public static ValueValidator getFilePathValidator() {
 
237
        return new FilePathValidator();
 
238
    }
 
239
 
 
240
    /**
 
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
 
246
     */
 
247
    public static ValueValidator getRangedIntegerValidator(int low, int high) {
 
248
        return new RangedIntegerValidator(low, high);
 
249
    }
 
250
 
 
251
    /**
 
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
 
255
     */
 
256
    public static ValueValidator getStringValidator(String[] validValues) {
 
257
        return new StringValueValidator(validValues);
 
258
    }
 
259
 
 
260
    /**
 
261
     * @return a {@link ValueValidator} that checks if an object represents a
 
262
     * valid url
 
263
     */
 
264
    public static ValueValidator getUrlValidator() {
 
265
        return new UrlValidator();
 
266
    }
 
267
 
 
268
}