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