View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.ui.generation;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.archetype.ui.ArchetypeConfiguration;
28  import org.codehaus.plexus.components.interactivity.Prompter;
29  import org.codehaus.plexus.components.interactivity.PrompterException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  @Named("default")
34  @Singleton
35  public class DefaultArchetypeGenerationQueryer implements ArchetypeGenerationQueryer {
36      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultArchetypeGenerationQueryer.class);
37  
38      @Inject
39      private Prompter prompter;
40  
41      @Override
42      public boolean confirmConfiguration(ArchetypeConfiguration archetypeConfiguration) throws PrompterException {
43          StringBuilder query = new StringBuilder("Confirm properties configuration:\n");
44  
45          for (String property : archetypeConfiguration.getRequiredProperties()) {
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      @Override
55      public String getPropertyValue(String requiredProperty, String defaultValue, Pattern validationRegex)
56              throws PrompterException {
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              queryBuilder.append(" (should match expression '");
64              queryBuilder.append(validationRegex);
65              queryBuilder.append("')");
66          }
67  
68          String query = queryBuilder.toString();
69          String answer;
70          boolean validAnswer = false;
71  
72          do {
73              if ((defaultValue != null) && !defaultValue.equals("null")) {
74                  answer = prompter.prompt(query, defaultValue);
75              } else {
76                  answer = prompter.prompt(query);
77              }
78  
79              if (validationRegex == null || validationRegex.matcher(answer).matches()) {
80                  validAnswer = true;
81              } else {
82                  query = "Value does not match the expression, please try again";
83              }
84  
85          } while (!validAnswer);
86  
87          return answer;
88      }
89  
90      public void setPrompter(Prompter prompter) {
91          this.prompter = prompter;
92      }
93  }