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.components.interactivity.InputHandler;
23  import org.codehaus.plexus.components.interactivity.OutputHandler;
24  import org.codehaus.plexus.components.interactivity.Prompter;
25  import org.codehaus.plexus.components.interactivity.PrompterException;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  import java.io.IOException;
29  import java.util.List;
30  
31  /**
32   * @author raphaelpieroni
33   * @plexus.component role-hint="archetype"
34   */
35  public class ArchetypePrompter
36      implements Prompter
37  {
38  
39      /**
40       * @plexus.requirement
41       */
42      private OutputHandler outputHandler;
43  
44      /**
45       * @plexus.requirement
46       */
47      private InputHandler inputHandler;
48  
49      public String prompt( String message )
50          throws PrompterException
51      {
52          writePrompt( message );
53  
54          return readLine();
55      }
56  
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      @SuppressWarnings( { "rawtypes", "unchecked" })
73      public String prompt( String message, List possibleValues, String defaultReply )
74          throws PrompterException
75      {
76          String formattedMessage = formatMessage( message, possibleValues, defaultReply );
77  
78          String line;
79  
80          do
81          {
82              writePrompt( formattedMessage );
83  
84              line = readLine();
85  
86              if ( StringUtils.isEmpty( line ) )
87              {
88                  line = defaultReply;
89              }
90  
91              if ( line != null && !possibleValues.contains( line ) )
92              {
93                  try
94                  {
95                      outputHandler.writeLine( "Invalid selection." );
96                  }
97                  catch ( IOException e )
98                  {
99                      throw new PrompterException( "Failed to present feedback", e );
100                 }
101             }
102         }
103         while ( line == null || !possibleValues.contains( line ) );
104 
105         return line;
106     }
107 
108     @SuppressWarnings( "rawtypes" )
109     public String prompt( String message, List possibleValues )
110         throws PrompterException
111     {
112         return prompt( message, possibleValues, null );
113     }
114 
115     public String promptForPassword( String message )
116         throws PrompterException
117     {
118         writePrompt( message );
119 
120         try
121         {
122             return inputHandler.readPassword();
123         }
124         catch ( IOException e )
125         {
126             throw new PrompterException( "Failed to read user response", e );
127         }
128     }
129 
130     private String formatMessage( String message, List<String> possibleValues, String defaultReply )
131     {
132         StringBuffer formatted = new StringBuffer( message.length() * 2 );
133 
134         formatted.append( message );
135         /*
136          * if ( possibleValues != null && !possibleValues.isEmpty() ) { formatted.append( " (" ); for ( Iterator it =
137          * possibleValues.iterator(); it.hasNext(); ) { String possibleValue = (String) it.next(); formatted.append(
138          * possibleValue ); if ( it.hasNext() ) { formatted.append( '/' ); } } formatted.append( ')' ); }
139          */
140 
141         if ( defaultReply != null )
142         {
143             formatted.append( defaultReply );
144         }
145 
146         return formatted.toString();
147     }
148 
149     private void writePrompt( String message )
150         throws PrompterException
151     {
152         showMessage( message + ": " );
153     }
154 
155     private String readLine()
156         throws PrompterException
157     {
158         try
159         {
160             return inputHandler.readLine();
161         }
162         catch ( IOException e )
163         {
164             throw new PrompterException( "Failed to read user response", e );
165         }
166     }
167 
168     public void showMessage( String message )
169         throws PrompterException
170     {
171         try
172         {
173             outputHandler.write( message );
174         }
175         catch ( IOException e )
176         {
177             throw new PrompterException( "Failed to show message", e );
178         }
179     }
180 
181 }