View Javadoc
1   package org.apache.maven.archetype.ui.generation;
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.apache.maven.archetype.ui.ArchetypeConfiguration;
23  import org.codehaus.plexus.component.annotations.Component;
24  import org.codehaus.plexus.component.annotations.Requirement;
25  import org.codehaus.plexus.components.interactivity.Prompter;
26  import org.codehaus.plexus.components.interactivity.PrompterException;
27  import org.codehaus.plexus.logging.AbstractLogEnabled;
28  
29  import java.util.regex.Pattern;
30  
31  @Component( role = ArchetypeGenerationQueryer.class, hint = "default" )
32  public class DefaultArchetypeGenerationQueryer
33      extends AbstractLogEnabled
34      implements ArchetypeGenerationQueryer
35  {
36      @Requirement
37      private Prompter prompter;
38  
39      @Override
40      public boolean confirmConfiguration( ArchetypeConfiguration archetypeConfiguration )
41          throws PrompterException
42      {
43          StringBuilder query = new StringBuilder( "Confirm properties configuration:\n" );
44  
45          for ( String property : archetypeConfiguration.getRequiredProperties() )
46          {
47              query.append( property + ": " + archetypeConfiguration.getProperty( property ) + "\n" );
48          }
49  
50          String answer = prompter.prompt( query.toString(), "Y" );
51  
52          return "Y".equalsIgnoreCase( answer );
53      }
54  
55      @Override
56      public String getPropertyValue( String requiredProperty, String defaultValue, Pattern validationRegex )
57          throws PrompterException
58      {
59          StringBuilder queryBuilder = new StringBuilder();
60          queryBuilder.append( "Define value for property '" );
61          queryBuilder.append( requiredProperty );
62          queryBuilder.append( '\'' );
63  
64          if ( validationRegex != null )
65          {
66              queryBuilder.append( " (should match expression '" );
67              queryBuilder.append( validationRegex );
68              queryBuilder.append( "')" );
69          }
70  
71          String query = queryBuilder.toString();
72          String answer;
73          boolean validAnswer = false;
74  
75          do
76          {
77              if ( ( defaultValue != null ) && !defaultValue.equals( "null" ) )
78              {
79                  answer = prompter.prompt( query, defaultValue );
80              }
81              else
82              {
83                  answer = prompter.prompt( query );
84              }
85  
86              if ( validationRegex == null || validationRegex.matcher( answer ).matches() )
87              {
88                  validAnswer = true;
89              }
90              else
91              {
92                  query = "Value does not match the expression, please try again";
93              }
94  
95          }
96          while ( !validAnswer );
97  
98          return answer;
99      }
100 
101     public void setPrompter( Prompter prompter )
102     {
103         this.prompter = prompter;
104     }
105 }