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