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.commons.lang3.StringUtils;
23  import org.apache.maven.archetype.catalog.Archetype;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.LinkedHashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * @author Olivier Lamy
33   * @since 2.1
34   */
35  public class ArchetypeSelectorUtils
36  {
37      private ArchetypeSelectorUtils()
38      {
39          // no constructor for utility class
40      }
41  
42      private static String extractGroupIdFromFilter( String filter )
43      {
44          return StringUtils.contains( filter, ':' ) ? StringUtils.substringBefore( filter, ":" ) : null;
45      }
46  
47      private static String extractArtifactIdFromFilter( String filter )
48      {
49          // if no : the full text is considered as artifactId content
50          return StringUtils.contains( filter, ':' ) ? StringUtils.substringAfter( filter, ":" ) : filter;
51      }
52  
53      /**
54       * apply some filtering on archetypes.
55       * currently only on artifactId contains filter
56       *
57       * @param archetypesPerCatalog
58       * @return
59       */
60      public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog(
61          Map<String, List<Archetype>> archetypesPerCatalog, String filter )
62      {
63          if ( archetypesPerCatalog == null || archetypesPerCatalog.isEmpty() )
64          {
65              return Collections.emptyMap();
66          }
67          Map<String, List<Archetype>> filtered =
68              new LinkedHashMap<>( archetypesPerCatalog.size() );
69  
70          for ( Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet() )
71          {
72              List<Archetype> archetypes = new ArrayList<>();
73  
74              for ( Archetype archetype : entry.getValue() )
75              {
76                  String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter( filter );
77                  String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter( filter );
78  
79                  if ( ( ( groupId == null ) || StringUtils.contains( archetype.getGroupId(), groupId ) )
80                      && StringUtils.contains( archetype.getArtifactId(), artifactId ) )
81                  {
82                      archetypes.add( archetype );
83                  }
84              }
85  
86              if ( !archetypes.isEmpty() )
87              {
88                  filtered.put( entry.getKey(), archetypes );
89              }
90          }
91  
92          return filtered;
93      }
94  
95  }