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      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          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
135          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
136          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
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 }