1 package org.apache.maven.archetype.ui;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.components.interactivity.InputHandler;
23 import org.codehaus.plexus.components.interactivity.OutputHandler;
24 import org.codehaus.plexus.components.interactivity.Prompter;
25 import org.codehaus.plexus.components.interactivity.PrompterException;
26 import org.codehaus.plexus.util.StringUtils;
27
28 import java.io.IOException;
29 import java.util.List;
30
31
32
33
34
35 public class ArchetypePrompter
36 implements Prompter
37 {
38
39
40
41
42 private OutputHandler outputHandler;
43
44
45
46
47 private InputHandler inputHandler;
48
49 public String prompt( String message )
50 throws PrompterException
51 {
52 writePrompt( message );
53
54 return readLine();
55 }
56
57 public String prompt( String message, String defaultReply )
58 throws PrompterException
59 {
60 writePrompt( formatMessage( message, null, defaultReply ) );
61
62 String line = readLine();
63
64 if ( StringUtils.isEmpty( line ) )
65 {
66 line = defaultReply;
67 }
68
69 return line;
70 }
71
72 @SuppressWarnings( { "rawtypes", "unchecked" })
73 public String prompt( String message, List possibleValues, String defaultReply )
74 throws PrompterException
75 {
76 String formattedMessage = formatMessage( message, possibleValues, defaultReply );
77
78 String line;
79
80 do
81 {
82 writePrompt( formattedMessage );
83
84 line = readLine();
85
86 if ( StringUtils.isEmpty( line ) )
87 {
88 line = defaultReply;
89 }
90
91 if ( line != null && !possibleValues.contains( line ) )
92 {
93 try
94 {
95 outputHandler.writeLine( "Invalid selection." );
96 }
97 catch ( IOException e )
98 {
99 throw new PrompterException( "Failed to present feedback", e );
100 }
101 }
102 }
103 while ( line == null || !possibleValues.contains( line ) );
104
105 return line;
106 }
107
108 @SuppressWarnings( "rawtypes" )
109 public String prompt( String message, List possibleValues )
110 throws PrompterException
111 {
112 return prompt( message, possibleValues, null );
113 }
114
115 public String promptForPassword( String message )
116 throws PrompterException
117 {
118 writePrompt( message );
119
120 try
121 {
122 return inputHandler.readPassword();
123 }
124 catch ( IOException e )
125 {
126 throw new PrompterException( "Failed to read user response", e );
127 }
128 }
129
130 private String formatMessage( String message, List<String> possibleValues, String defaultReply )
131 {
132 StringBuffer formatted = new StringBuffer( message.length() * 2 );
133
134 formatted.append( message );
135
136
137
138
139
140
141 if ( defaultReply != null )
142 {
143 formatted.append( defaultReply );
144 }
145
146 return formatted.toString();
147 }
148
149 private void writePrompt( String message )
150 throws PrompterException
151 {
152 showMessage( message + ": " );
153 }
154
155 private String readLine()
156 throws PrompterException
157 {
158 try
159 {
160 return inputHandler.readLine();
161 }
162 catch ( IOException e )
163 {
164 throw new PrompterException( "Failed to read user response", e );
165 }
166 }
167
168 public void showMessage( String message )
169 throws PrompterException
170 {
171 try
172 {
173 outputHandler.write( message );
174 }
175 catch ( IOException e )
176 {
177 throw new PrompterException( "Failed to show message", e );
178 }
179 }
180
181 }