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      public boolean confirmConfiguration( ArchetypeConfiguration archetypeConfiguration )
40          throws PrompterException
41      {
42          StringBuilder query = new StringBuilder( "Confirm properties configuration:\n" );
43  
44          for ( String property : archetypeConfiguration.getRequiredProperties() )
45          {
46              query.append( property + ": " + archetypeConfiguration.getProperty( property ) + "\n" );
47          }
48  
49          String answer = prompter.prompt( query.toString(), "Y" );
50  
51          return "Y".equalsIgnoreCase( answer );
52      }
53  
54      public String getPropertyValue( String requiredProperty, String defaultValue, Pattern validationRegex )
55          throws PrompterException
56      {
57          StringBuilder queryBuilder = new StringBuilder();
58          queryBuilder.append( "Define value for property '" );
59          queryBuilder.append( requiredProperty );
60          queryBuilder.append( '\'' );
61  
62          if ( validationRegex != null )
63          {
64              queryBuilder.append( " (should match expression '" );
65              queryBuilder.append( validationRegex );
66              queryBuilder.append( "')" );
67          }
68  
69          String query = queryBuilder.toString();
70          String answer;
71          boolean validAnswer = false;
72  
73          do
74          {
75              if ( ( defaultValue != null ) && !defaultValue.equals( "null" ) )
76              {
77                  answer = prompter.prompt( query, defaultValue );
78              }
79              else
80              {
81                  answer = prompter.prompt( query );
82              }
83  
84              if ( validationRegex == null || validationRegex.matcher( answer ).matches() )
85              {
86                  validAnswer = true;
87              }
88              else
89              {
90                  query = "Value does not match the expression, please try again";
91              }
92  
93          }
94          while ( !validAnswer );
95  
96          return answer;
97      }
98  
99      public void setPrompter( Prompter prompter )
100     {
101         this.prompter = prompter;
102     }
103 }