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