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.jline;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.maven.api.annotations.Experimental;
25  import org.apache.maven.api.di.Named;
26  import org.apache.maven.api.di.Singleton;
27  import org.apache.maven.api.services.Prompter;
28  import org.apache.maven.api.services.PrompterException;
29  
30  @Experimental
31  @Named
32  @Singleton
33  public class DefaultPrompter implements Prompter {
34  
35      @Override
36      public String prompt(String message, List<String> possibleValues, String defaultReply) throws PrompterException {
37          return doPrompt(message, possibleValues, defaultReply, false);
38      }
39  
40      @Override
41      public String promptForPassword(String message) throws PrompterException {
42          return doPrompt(message, null, null, true);
43      }
44  
45      @Override
46      public void showMessage(String message) throws PrompterException {
47          doDisplay(message);
48      }
49  
50      private String doPrompt(String message, boolean password) {
51          try {
52              if (message != null) {
53                  if (!message.endsWith("\n")) {
54                      if (message.endsWith(":")) {
55                          message += " ";
56                      } else if (!message.endsWith(": ")) {
57                          message += ": ";
58                      }
59                  }
60                  int lastNl = message.lastIndexOf('\n');
61                  String begin = message.substring(0, lastNl + 1);
62                  message = message.substring(lastNl + 1);
63                  MessageUtils.terminal.writer().print(begin);
64                  MessageUtils.terminal.flush();
65              }
66              return MessageUtils.reader.readLine(message, password ? '*' : null);
67          } catch (Exception e) {
68              throw new PrompterException("Unable to prompt user", e);
69          }
70      }
71  
72      private void doDisplay(String message) {
73          try {
74              MessageUtils.terminal.writer().print(message);
75              MessageUtils.terminal.flush();
76          } catch (Exception e) {
77              throw new PrompterException("Unable to display message", e);
78          }
79      }
80  
81      private String doPrompt(String message, List<?> possibleValues, String defaultReply, boolean password) {
82          String formattedMessage = formatMessage(message, possibleValues, defaultReply);
83          String line;
84          do {
85              line = doPrompt(formattedMessage, password);
86              if (line == null && defaultReply == null) {
87                  throw new PrompterException("EOF");
88              }
89              if (line == null || line.isEmpty()) {
90                  line = defaultReply;
91              }
92              if (line != null && (possibleValues != null && !possibleValues.contains(line))) {
93                  doDisplay("Invalid selection.\n");
94              }
95          } while (line == null || (possibleValues != null && !possibleValues.contains(line)));
96          return line;
97      }
98  
99      private String formatMessage(String message, List<?> possibleValues, String defaultReply) {
100         if (message == null) {
101             return null;
102         }
103         StringBuilder formatted = new StringBuilder(message.length() * 2);
104         formatted.append(message);
105         if (possibleValues != null && !possibleValues.isEmpty()) {
106             formatted.append(" (");
107             for (Iterator<?> it = possibleValues.iterator(); it.hasNext(); ) {
108                 String possibleValue = String.valueOf(it.next());
109                 formatted.append(possibleValue);
110                 if (it.hasNext()) {
111                     formatted.append('/');
112                 }
113             }
114             formatted.append(')');
115         }
116         if (defaultReply != null) {
117             formatted.append(' ').append(defaultReply).append(": ");
118         }
119         return formatted.toString();
120     }
121 }