View Javadoc
1   package org.apache.maven.archetype.ui;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author raphaelpieroni
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      @Override
48      public String prompt( String message )
49          throws PrompterException
50      {
51          writePrompt( message );
52  
53          return readLine();
54      }
55  
56      @Override
57      public String prompt( String message, String defaultReply )
58          throws PrompterException
59      {
60          writePrompt( formatMessage( message, null, defaultReply ) );
61  
62          String line = readLine();
63  
64          if ( StringUtils.isEmpty( line ) )
65          {
66              line = defaultReply;
67          }
68  
69          return line;
70      }
71  
72      @Override
73      @SuppressWarnings( { "rawtypes", "unchecked" } )
74      public String prompt( String message, List possibleValues, String defaultReply )
75          throws PrompterException
76      {
77          String formattedMessage = formatMessage( message, possibleValues, defaultReply );
78  
79          String line;
80  
81          do
82          {
83              writePrompt( formattedMessage );
84  
85              line = readLine();
86  
87              if ( StringUtils.isEmpty( line ) )
88              {
89                  line = defaultReply;
90              }
91  
92              if ( line != null && !possibleValues.contains( line ) )
93              {
94                  try
95                  {
96                      outputHandler.writeLine( "Invalid selection." );
97                  }
98                  catch ( IOException e )
99                  {
100                     throw new PrompterException( "Failed to present feedback", e );
101                 }
102             }
103         }
104         while ( line == null || !possibleValues.contains( line ) );
105 
106         return line;
107     }
108 
109     @Override
110     @SuppressWarnings( "rawtypes" )
111     public String prompt( String message, List possibleValues )
112         throws PrompterException
113     {
114         return prompt( message, possibleValues, null );
115     }
116 
117     @Override
118     public String promptForPassword( String message )
119         throws PrompterException
120     {
121         writePrompt( message );
122 
123         try
124         {
125             return inputHandler.readPassword();
126         }
127         catch ( IOException e )
128         {
129             throw new PrompterException( "Failed to read user response", e );
130         }
131     }
132 
133     private String formatMessage( String message, List<String> possibleValues, String defaultReply )
134     {
135         StringBuilder formatted = new StringBuilder( message.length() * 2 );
136 
137         formatted.append( message );
138         /*
139          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
140          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
141          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
142          */
143 
144         if ( defaultReply != null )
145         {
146             formatted.append( defaultReply );
147         }
148 
149         return formatted.toString();
150     }
151 
152     private void writePrompt( String message )
153         throws PrompterException
154     {
155         showMessage( message + ": " );
156     }
157 
158     private String readLine()
159         throws PrompterException
160     {
161         try
162         {
163             return inputHandler.readLine();
164         }
165         catch ( IOException e )
166         {
167             throw new PrompterException( "Failed to read user response", e );
168         }
169     }
170 
171     @Override
172     public void showMessage( String message )
173         throws PrompterException
174     {
175         try
176         {
177             outputHandler.write( message );
178         }
179         catch ( IOException e )
180         {
181             throw new PrompterException( "Failed to show message", e );
182         }
183     }
184 
185 }