KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > cmdui > PromptMode


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

16 package org.jmanage.cmdui;
17
18 import org.jmanage.cmdui.util.In;
19 import org.jmanage.cmdui.util.Out;
20
21 import java.io.IOException JavaDoc;
22
23 /**
24  *
25  * date: Feb 4, 2005
26  * @author Rakesh Kalra
27  */

28 public class PromptMode implements CommandConstants {
29
30     private final Command authenticatedCommand;
31
32     PromptMode(Command command){
33         this.authenticatedCommand = command;
34     }
35
36     public void start()
37         throws IOException JavaDoc {
38
39         while(true){
40             try {
41                 /* get input */
42                 Command command = getCommand();
43                 /* execute command */
44                 command.execute();
45             } catch (Exception JavaDoc e) {
46                 handleException(e);
47             }
48         }
49     }
50
51     private Command getCommand()
52         throws IOException JavaDoc, InvalidCommandException {
53         String JavaDoc line = readLine();
54         return Command.get(line, authenticatedCommand);
55     }
56
57     private String JavaDoc readLine() throws IOException JavaDoc{
58
59         String JavaDoc line = null;
60         while(line == null || line.length() == 0){
61             Out.print("jmanage>");
62             line = In.readLine();
63             if(line != null) line = line.trim();
64         }
65         return line;
66     }
67
68     private void handleException(Exception JavaDoc e){
69         if(e instanceof InvalidCommandException){
70             Out.println(e.getMessage());
71         }else{
72             e.printStackTrace();
73         }
74         Out.println();
75     }
76 }
77
Popular Tags