Author: jdillon
Date: Sat Sep 27 01:01:53 2008
New Revision: 699598
URL: http://svn.apache.org/viewvc?rev=699598&view=rev
Log:
Add prompt validation support
Modified:
geronimo/gshell/trunk/gshell-support/gshell-io/src/main/java/org/apache/geronimo/gshell/io/PromptReader.java
Modified: geronimo/gshell/trunk/gshell-support/gshell-io/src/main/java/org/apache/geronimo/gshell/io/PromptReader.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-io/src/main/java/org/apache/geronimo/gshell/io/PromptReader.java?rev=699598&r1=699597&r2=699598&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-io/src/main/java/org/apache/geronimo/gshell/io/PromptReader.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-io/src/main/java/org/apache/geronimo/gshell/io/PromptReader.java
Sat Sep 27 01:01:53 2008
@@ -49,25 +49,91 @@
this.mask = mask;
}
- //
- // TODO: Add a validator interface to simplify users trying to avoid null entry and such
- //
+ public String readLine(final String prompt, final Validator validator) throws IOException
{
+ assert prompt != null;
+ // validator may be null
+
+ String value;
+
+ while (true) {
+ value = reader.readLine(prompt);
+
+ if (validator == null) {
+ break;
+ }
+ else if (validator.isValid(value)) {
+ break;
+ }
+ }
+
+ return value;
+ }
public String readLine(final String prompt) throws IOException {
+ return readLine(prompt, null);
+ }
+
+ public String readLine(final String prompt, final char mask, final Validator validator)
throws IOException {
assert prompt != null;
+ // validator may be null
+
+ String value;
+
+ while (true) {
+ value = reader.readLine(prompt, mask);
- return reader.readLine(prompt);
+ if (validator == null) {
+ break;
+ }
+ else if (validator.isValid(value)) {
+ break;
+ }
+ }
+
+ return value;
}
public String readLine(final String prompt, final char mask) throws IOException {
+ return readLine(prompt, mask, null);
+ }
+
+ public String readPassword(final String prompt, final Validator validator) throws IOException
{
assert prompt != null;
+ // validator may be null
+
+ String value;
+
+ while (true) {
+ value = reader.readLine(prompt, mask);
+
+ if (validator == null) {
+ break;
+ }
+ else if (validator.isValid(value)) {
+ break;
+ }
+ }
- return reader.readLine(prompt, mask);
+ return value;
}
public String readPassword(final String prompt) throws IOException {
- assert prompt != null;
-
- return reader.readLine(prompt, mask);
+ return readPassword(prompt, null);
+ }
+
+ //
+ // Validator
+ //
+
+ /**
+ * Allows caller to customize the validation behavior when prompting.
+ */
+ public static interface Validator
+ {
+ /**
+ * Determin if the given value is valid. If the value is not valid then
+ * we will prompt the user again.
+ */
+ boolean isValid(String value);
}
}
|