-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathvalidators.xml
More file actions
202 lines (185 loc) · 7.5 KB
/
Copy pathvalidators.xml
File metadata and controls
202 lines (185 loc) · 7.5 KB
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
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<document>
<properties>
<author email="dev@commons.apache.org">commons-dev</author>
<title>CLI2 - Validators</title>
</properties>
<body>
<section name="Validators">
<p>
CLI2 provides a mechanism to validate argument values. The <code>Validator</code>
interface must be implemented to create an argument validator. This interface has
a single method <code>validate(java.util.List values) throws InvalidArgumentException</code>.
</p>
<p>
CLI2 has some standard validators included. They validate the following:
</p>
<ul>
<li>Java class</li>
<li>date</li>
<li>enumeration</li>
<li>file path</li>
<li>number</li>
<li>URL</li>
</ul>
<subsection name="ClassValidator">
<p>
The <code>ClassValidator</code> validates a value using three criteria:
</p>
<ol>
<li>the value adheres to the Java Language Specification rules</li>
<li>the class specified by the name is loadable</li>
<li>the class specified by the name can be instantiated</li>
</ol>
<source>// 1.
ClassValidator validator = new ClassValidator();
// 2.
validator.setLoadable(true);
// 3.
validator.setLoadable(true);</source>
<p>
TODO: add section about values being replaced with class instances
</p>
</subsection>
<subsection name="DateValidator">
<p>
The <code>DateValidator</code> validates values against a
<code>java.text.DateFormat</code>. There are three helper methods
that create built-in validators.
</p>
<ol>
<li><code>getDateInstance</code> returns a validator for the default
date format for the default locale.
</li>
<li><code>getTimeInstance</code> returns a validator for the default
time format for the default locale.
</li>
<li><code>getDateTimeInstance</code> returns a validator for the default
date and time format for the default locale.
</li>
</ol>
<p>
A <code>DateValidator</code> can also be created by passing your
a custom <code>DateFormat</code> into the constructor.
</p>
<source>DateFormat myFormat = DateFormat.getDateInstance( DateFormat.LONG, Locale.UK );
DateValidator myValidator = new DateValidator( myFormat );</source>
<p>
In addition to basic format checking you can also check if the date/time specified
is before/after a specific date/time. The lower bound is set using the
<code>setMinimum( java.util.Date )</code>, and the upper bound is set using
the <code>setMaximum( java.util.Date )</code>.
</p>
<p>
TODO: add section about values being replaced with date instances
</p>
</subsection>
<subsection name="EnumValidator">
<p>
The <code>EnumValidator</code> validates values against a list of
allowed string values. The values that are allowed are specified in
a <code>java.util.Set</code> and passed in to the constructor.
</p>
<source>Set enumSet = new TreeSet();
enumSet.add("red");
enumSet.add("green");
enumSet.add("blue");
EnumValidator validator = new EnumValidator( enumSet );</source>
</subsection>
<subsection name="FileValidator">
<p>
The <code>FileValidator</code> validates that values represent
existing files. You can also specify a combination of the
following additional criteria:
</p>
<ol>
<li>value is a file</li>
<li>value is a directory</li>
<li>the file is writable</li>
<li>the file is readable</li>
<li>the file is hidden</li>
</ol>
<p>
Each of the criteria listed here are specified using the appropriate
setter.
</p>
<p>
There are three helper methods to create validators:
</p>
<ol>
<li><code>getExistingInstance</code> returns a validator that ensures
a value represents an existing file.
</li>
<li><code>getExistingFileInstance</code> returns a validator that ensures
a value represents an existing file that is not a directory.
</li>
<li><code>getExistingDirectoryInstance</code> returns a validator that ensures
a value represents an existing file this is a directory.
</li>
</ol>
<source>// validate that the value represents a file that exists
FileValidator validator = FileValidator.getExistingInstance();
// ensure it's not a hidden file
validator.setHidden( false );
// ensure it's a writable file
validator.setWritable( true );</source>
</subsection>
<subsection name="NumberValidator">
<p>
The <code>NumberValidator</code> validates that values adhere to
certain rules like the following:
</p>
<ol>
<li><code>getCurrencyInstance</code> returns a validator for the default
currency format for the default locale.
</li>
<li><code>getPercentInstance</code> returns a validator for the default
percent format for the default locale.
</li>
<li><code>getIntegerInstance</code> returns a validator for the default
integer format for the default locale.
</li>
<li><code>getNumberInstance</code> returns a validator for the default
number format for the default locale.
</li>
</ol>
<p>
A <code>NumberValidator</code> can also be created by passing your
a custom <code>NumberFormat</code> into the constructor.
</p>
<source>NumberFormat myFormat = NumberFormat.getCurrencyInstance( Locale.UK );
NumberValidator myValidator = new NumberValidator( myFormat );</source>
<p>
In addition to basic format checking you can also check if the number specified
is less than or greater than a specific number. The lower bound is set using
the <code>setMinimum( Number )</code>, and the upper bound is set using
the <code>setMaximum( Number )</code>.
</p>
</subsection>
<subsection name="UrlValidator">
<p>
A <code>UrlValidator</code> validates that values are URLs and if you
choose it will also validate the protocol is of the type you have
specified.
</p>
<source>UrlValidator validator = new UrlValidator();
// only accept https URLs
validator.setProtocol("https");</source>
</subsection>
</section>
</body>
</document>