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 | 13 | 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 | 0 | writePrompt( message ); |
53 | |
|
54 | 0 | return readLine(); |
55 | |
} |
56 | |
|
57 | |
public String prompt( String message, String defaultReply ) |
58 | |
throws PrompterException |
59 | |
{ |
60 | 0 | writePrompt( formatMessage( message, null, defaultReply ) ); |
61 | |
|
62 | 0 | String line = readLine(); |
63 | |
|
64 | 0 | if ( StringUtils.isEmpty( line ) ) |
65 | |
{ |
66 | 0 | line = defaultReply; |
67 | |
} |
68 | |
|
69 | 0 | return line; |
70 | |
} |
71 | |
|
72 | |
@SuppressWarnings( { "rawtypes", "unchecked" }) |
73 | |
public String prompt( String message, List possibleValues, String defaultReply ) |
74 | |
throws PrompterException |
75 | |
{ |
76 | 0 | String formattedMessage = formatMessage( message, possibleValues, defaultReply ); |
77 | |
|
78 | |
String line; |
79 | |
|
80 | |
do |
81 | |
{ |
82 | 0 | writePrompt( formattedMessage ); |
83 | |
|
84 | 0 | line = readLine(); |
85 | |
|
86 | 0 | if ( StringUtils.isEmpty( line ) ) |
87 | |
{ |
88 | 0 | line = defaultReply; |
89 | |
} |
90 | |
|
91 | 0 | if ( line != null && !possibleValues.contains( line ) ) |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | 0 | outputHandler.writeLine( "Invalid selection." ); |
96 | |
} |
97 | 0 | catch ( IOException e ) |
98 | |
{ |
99 | 0 | throw new PrompterException( "Failed to present feedback", e ); |
100 | 0 | } |
101 | |
} |
102 | |
} |
103 | 0 | while ( line == null || !possibleValues.contains( line ) ); |
104 | |
|
105 | 0 | return line; |
106 | |
} |
107 | |
|
108 | |
@SuppressWarnings( "rawtypes" ) |
109 | |
public String prompt( String message, List possibleValues ) |
110 | |
throws PrompterException |
111 | |
{ |
112 | 0 | return prompt( message, possibleValues, null ); |
113 | |
} |
114 | |
|
115 | |
public String promptForPassword( String message ) |
116 | |
throws PrompterException |
117 | |
{ |
118 | 0 | writePrompt( message ); |
119 | |
|
120 | |
try |
121 | |
{ |
122 | 0 | return inputHandler.readPassword(); |
123 | |
} |
124 | 0 | catch ( IOException e ) |
125 | |
{ |
126 | 0 | 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 | 0 | StringBuffer formatted = new StringBuffer( message.length() * 2 ); |
133 | |
|
134 | 0 | formatted.append( message ); |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 0 | if ( defaultReply != null ) |
142 | |
{ |
143 | 0 | formatted.append( defaultReply ); |
144 | |
} |
145 | |
|
146 | 0 | return formatted.toString(); |
147 | |
} |
148 | |
|
149 | |
private void writePrompt( String message ) |
150 | |
throws PrompterException |
151 | |
{ |
152 | 0 | showMessage( message + ": " ); |
153 | 0 | } |
154 | |
|
155 | |
private String readLine() |
156 | |
throws PrompterException |
157 | |
{ |
158 | |
try |
159 | |
{ |
160 | 0 | return inputHandler.readLine(); |
161 | |
} |
162 | 0 | catch ( IOException e ) |
163 | |
{ |
164 | 0 | 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 | 0 | outputHandler.write( message ); |
174 | |
} |
175 | 0 | catch ( IOException e ) |
176 | |
{ |
177 | 0 | throw new PrompterException( "Failed to show message", e ); |
178 | 0 | } |
179 | 0 | } |
180 | |
|
181 | |
} |