View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package org.apache.maven.archetype.ui;
7   
8   import java.io.IOException;
9   import java.util.List;
10  import org.codehaus.plexus.components.interactivity.InputHandler;
11  import org.codehaus.plexus.components.interactivity.OutputHandler;
12  import org.codehaus.plexus.components.interactivity.Prompter;
13  import org.codehaus.plexus.components.interactivity.PrompterException;
14  import org.codehaus.plexus.util.StringUtils;
15  
16  /**
17   * @author raphaelpieroni
18   * @plexus.component role-hint="archetype"
19   */
20  public class ArchetypePrompter
21      implements Prompter
22  {
23  
24      /**
25       * @plexus.requirement
26       */
27      private OutputHandler outputHandler;
28  
29      /**
30       * @plexus.requirement
31       */
32      private InputHandler inputHandler;
33  
34      public String prompt( String message )
35          throws PrompterException
36      {
37          try
38          {
39              writePrompt( message );
40          }
41  
42          catch ( IOException e )
43          {
44              throw new PrompterException( "Failed to present prompt", e );
45          }
46  
47          try
48          {
49              return inputHandler.readLine();
50          }
51          catch ( IOException e )
52          {
53              throw new PrompterException( "Failed to read user response", e );
54          }
55      }
56  
57      public String prompt( String message, String defaultReply )
58          throws PrompterException
59      {
60          try
61          {
62              writePrompt( formatMessage( message, null, defaultReply ) );
63          }
64          catch ( IOException e )
65          {
66              throw new PrompterException( "Failed to present prompt", e );
67          }
68  
69          try
70          {
71              String line = inputHandler.readLine();
72  
73              if ( StringUtils.isEmpty( line ) )
74              {
75                  line = defaultReply;
76              }
77  
78              return line;
79          }
80          catch ( IOException e )
81          {
82              throw new PrompterException( "Failed to read user response", e );
83          }
84      }
85  
86      public String prompt( String message, List possibleValues, String defaultReply )
87          throws PrompterException
88      {
89          String formattedMessage = formatMessage( message, possibleValues, defaultReply );
90  
91          String line;
92  
93          do
94          {
95              try
96              {
97                  writePrompt( formattedMessage );
98              }
99              catch ( IOException e )
100             {
101                 throw new PrompterException( "Failed to present prompt", e );
102             }
103 
104             try
105             {
106                 line = inputHandler.readLine();
107             }
108             catch ( IOException e )
109             {
110                 throw new PrompterException( "Failed to read user response", e );
111             }
112 
113             if ( StringUtils.isEmpty( line ) )
114             {
115                 line = defaultReply;
116             }
117 
118             if ( line != null && !possibleValues.contains( line ) )
119             {
120                 try
121                 {
122                     outputHandler.writeLine( "Invalid selection." );
123                 }
124                 catch ( IOException e )
125                 {
126                     throw new PrompterException( "Failed to present feedback", e );
127                 }
128             }
129         }
130         while ( line == null || !possibleValues.contains( line ) );
131 
132         return line;
133     }
134 
135     public String prompt( String message, List possibleValues )
136         throws PrompterException
137     {
138         return prompt( message, possibleValues, null );
139     }
140 
141     public String promptForPassword( String message )
142         throws PrompterException
143     {
144         try
145         {
146             writePrompt( message );
147         }
148         catch ( IOException e )
149         {
150             throw new PrompterException( "Failed to present prompt", e );
151         }
152 
153         try
154         {
155             return inputHandler.readPassword();
156         }
157         catch ( IOException e )
158         {
159             throw new PrompterException( "Failed to read user response", e );
160         }
161     }
162 
163     private String formatMessage( String message, List possibleValues, String defaultReply )
164     {
165         StringBuffer formatted = new StringBuffer( message.length() * 2 );
166 
167         formatted.append( message );
168         /*
169          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
170          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
171          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
172          */
173 
174         if ( defaultReply != null )
175         {
176             formatted.append( defaultReply );
177         }
178 
179         return formatted.toString();
180     }
181 
182     private void writePrompt( String message )
183         throws IOException
184     {
185         outputHandler.write( message + ": " );
186     }
187 
188     public void showMessage( String message )
189         throws PrompterException
190     {
191         try
192         {
193             writePrompt( message );
194         }
195         catch ( IOException e )
196         {
197             throw new PrompterException( "Failed to present prompt", e );
198         }
199 
200     }
201 
202 }