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;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  import java.util.regex.Pattern;
27  
28  import org.apache.maven.archetype.common.Constants;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  public class ArchetypeConfiguration {
32      private String groupId;
33  
34      private String artifactId;
35  
36      private String version;
37  
38      private String name;
39  
40      private String goals;
41  
42      public String getDescription() {
43          return description;
44      }
45  
46      public void setDescription(String description) {
47          this.description = description;
48      }
49  
50      public String getUrl() {
51          return url;
52      }
53  
54      public void setUrl(String url) {
55          this.url = url;
56      }
57  
58      private String url;
59  
60      private String description;
61  
62      private List<String> requiredProperties;
63  
64      public void addRequiredProperty(String string) {
65          getRequiredProperties().add(string);
66      }
67  
68      public String getArtifactId() {
69          return artifactId;
70      }
71  
72      public String getGoals() {
73          return goals;
74      }
75  
76      public String getGroupId() {
77          return groupId;
78      }
79  
80      public String getName() {
81          return name;
82      }
83  
84      public List<String> getRequiredProperties() {
85          if (requiredProperties == null) {
86              requiredProperties = new ArrayList<>();
87          }
88  
89          return requiredProperties;
90      }
91  
92      public String getVersion() {
93          return version;
94      }
95  
96      public void removeRequiredProperty(String string) {
97          getRequiredProperties().remove(string);
98      }
99  
100     public void setArtifactId(String artifactId) {
101         this.artifactId = artifactId;
102     }
103 
104     public void setGoals(String goals) {
105         this.goals = goals;
106     }
107 
108     public void setGroupId(String groupId) {
109         this.groupId = groupId;
110     }
111 
112     public void setName(String name) {
113         this.name = name;
114     }
115 
116     public void setRequiredProperties(List<String> requiredProperties) {
117         this.requiredProperties = requiredProperties;
118     }
119 
120     public void setVersion(String version) {
121         this.version = version;
122     }
123 
124     public void reset() {
125         properties.clear();
126     }
127 
128     private Properties properties = new Properties();
129 
130     public void setProperty(String requiredProperty, String propertyValue) {
131         properties.setProperty(requiredProperty, propertyValue);
132     }
133 
134     public String getProperty(String property) {
135         return properties.getProperty(property, null);
136     }
137 
138     public Properties getProperties() {
139         return properties;
140     }
141 
142     public Properties toProperties() {
143         Properties result = new Properties();
144 
145         result.putAll(properties);
146 
147         result.setProperty(Constants.ARCHETYPE_GROUP_ID, StringUtils.isNotEmpty(getGroupId()) ? getGroupId() : "");
148         result.setProperty(
149                 Constants.ARCHETYPE_ARTIFACT_ID, StringUtils.isNotEmpty(getArtifactId()) ? getArtifactId() : "");
150         result.setProperty(Constants.ARCHETYPE_VERSION, StringUtils.isNotEmpty(getVersion()) ? getVersion() : "");
151 
152         if (StringUtils.isNotEmpty(getGoals())) {
153             result.setProperty(Constants.ARCHETYPE_POST_GENERATION_GOALS, getGoals());
154         }
155 
156         return result;
157     }
158 
159     public boolean isConfigured() {
160         for (String requiredProperty : getRequiredProperties()) {
161             if (StringUtils.isEmpty(properties.getProperty(requiredProperty))) {
162                 return false;
163             }
164         }
165 
166         return true;
167     }
168 
169     public boolean isConfigured(String requiredProperties) {
170         return StringUtils.isNotBlank(properties.getProperty(requiredProperties));
171     }
172 
173     private Properties defaultProperties = new Properties();
174 
175     public void setDefaultProperty(String requiredProperty, String propertyValue) {
176         defaultProperties.setProperty(requiredProperty, propertyValue);
177     }
178 
179     public String getDefaultValue(String requiredProperty) {
180         return defaultProperties.getProperty(requiredProperty, null);
181     }
182 
183     public Properties getDefaultValues() {
184         return defaultProperties;
185     }
186 
187     Map<String, Pattern> propertiesValidationPatterns = new HashMap<>();
188 
189     public void setPropertyValidationRegex(String requiredProperty, String regex) {
190         propertiesValidationPatterns.put(requiredProperty, Pattern.compile(regex));
191     }
192 
193     public Pattern getPropertyValidationRegex(String requiredProperty) {
194         return propertiesValidationPatterns.get(requiredProperty);
195     }
196 
197     public boolean validatePropertyValue(String property, String value) {
198         Pattern pattern = propertiesValidationPatterns.get(property);
199         if (pattern == null) {
200             return true;
201         }
202         return pattern.matcher(value).matches();
203     }
204 }