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