View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author raphaelpieroni
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          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
117          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
118          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
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 }