1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }