/*
 * 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.
 */

package org.apache.geronimo.gshell.io;

import jline.ConsoleReader;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Helper to prompt a user for information.
 *
 * @version $Rev$ $Date$
 */
public class PromptReader
{
    private char mask = '*';

    private final ConsoleReader reader;

    public PromptReader(final IO io) throws IOException {
        assert io != null;
        
        this.reader = new ConsoleReader(io.inputStream, new PrintWriter(io.outputStream, true), /*bindings*/ null, io.getTerminal());
    }

    public char getMask() {
        return mask;
    }

    public void setMask(final char mask) {
        this.mask = mask;
    }

    //
    // TODO: Add a validator interface to simplify users trying to avoid null entry and such
    //

    public String readLine(final String prompt) throws IOException {
        assert prompt != null;

        return reader.readLine(prompt);
    }

    public String readLine(final String prompt, final char mask) throws IOException {
        assert prompt != null;

        return reader.readLine(prompt, mask);
    }

    public String readPassword(final String prompt) throws IOException {
        assert prompt != null;
        
        return reader.readLine(prompt, mask);
    }
}

@@ -1481,110 +1481,570 @@
     
-//%0A    // 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 %7B%0A        assert prompt != null;%0A        // validator may be null%0A%0A        String value;%0A%0A        while (true) %7B%0A            value = reader.readLine(prompt);%0A%0A            if (validator == null) %7B%0A                break;%0A            %7D%0A            else if (validator.isValid(value)) %7B%0A                break;%0A            %7D%0A        %7D%0A%0A        return value;%0A    %7D%0A%0A    public String readLine(final String prompt) throws IOException %7B%0A        return readLine(prompt, null);
 %0A    
-//
+%7D
 %0A%0A  
@@ -2079,32 +2079,76 @@
 al String prompt
+, final char mask, final Validator validator
 ) throws IOExcep
@@ -2189,218 +2189,932 @@
 ll;%0A
-%0A        return reader.readLine(prompt);%0A    %7D%0A%0A    public String readLine(final String prompt, final char mask) throws IOException %7B%0A        assert prompt != null;%0A%0A        return reader.readLine(prompt, mask)
+        // validator may be null%0A%0A        String value;%0A%0A        while (true) %7B%0A            value = reader.readLine(prompt, mask);%0A%0A            if (validator == null) %7B%0A                break;%0A            %7D%0A            else if (validator.isValid(value)) %7B%0A                break;%0A            %7D%0A        %7D%0A%0A        return value;%0A    %7D%0A%0A    public String readLine(final String prompt, final char mask) throws IOException %7B%0A        return readLine(prompt, mask, null);%0A    %7D%0A%0A    public String readPassword(final String prompt, final Validator validator) throws IOException %7B%0A        assert prompt != null;%0A        // validator may be null%0A%0A        String value;%0A%0A        while (true) %7B%0A            value = reader.readLine(prompt, mask);%0A%0A            if (validator == null) %7B%0A                break;%0A            %7D%0A            else if (validator.isValid(value)) %7B%0A                break;%0A            %7D%0A        %7D%0A%0A        return value
 ;%0A  
@@ -3203,83 +3203,399 @@
     
-assert 
+return readPassword(
 prompt
- !=
+,
  null
+)
 ;%0A    
+%7D%0A%0A
     
+//
 %0A    
-    return reader.readLine(prompt, mask
+// Validator%0A    //%0A%0A    /**%0A     * Allows caller to customize the validation behavior when prompting.%0A     */%0A    public static interface Validator%0A    %7B%0A        /**%0A         * Determin if the given value is valid.  If the value is not valid then%0A         * we will prompt the user again.%0A         */%0A        boolean isValid(String value
 );%0A 

Add prompt validation support