KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > InputPrompt


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.deployment.cli;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26
27 import jline.ConsoleReader;
28
29 /**
30  * Prompts a user for input; optionally masking.
31  *
32  * <p>
33  * Uses the <a HREF="http://jline.sf.net">JLine</a> library to provide a rich user experence.
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class InputPrompt
38 {
39     private ConsoleReader reader;
40
41     public InputPrompt(final InputStream JavaDoc in, final Writer JavaDoc out) throws IOException JavaDoc {
42         this.reader = new ConsoleReader(in, out);
43     }
44
45     public InputPrompt(final InputStream JavaDoc in, final OutputStream JavaDoc out) throws IOException JavaDoc {
46         this(in, new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(out), true));
47     }
48
49     /**
50      * Displays the prompt, grabs the input.
51      */

52     public String JavaDoc getInput(final String JavaDoc prompt, final Character JavaDoc mask) throws IOException JavaDoc {
53         if (mask == null) {
54             return reader.readLine(prompt);
55         }
56         else {
57             return reader.readLine(prompt, mask);
58         }
59     }
60
61     public String JavaDoc getInput(final String JavaDoc prompt, final char mask) throws IOException JavaDoc {
62         return getInput(prompt, new Character JavaDoc(mask));
63     }
64
65     public String JavaDoc getInput(final String JavaDoc prompt) throws IOException JavaDoc {
66         return getInput(prompt, null);
67     }
68
69     /**
70      * Displays the prompt, grabs the input masking with '*'.
71      */

72     public String JavaDoc getPassword(final String JavaDoc prompt) throws IOException JavaDoc {
73         return getInput(prompt, '*');
74     }
75 }
Popular Tags