1 | |
package org.apache.maven.archetype.ui.generation; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.lang.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 | |
|
33 | |
|
34 | |
|
35 | |
public class ArchetypeSelectorUtils |
36 | |
{ |
37 | |
private ArchetypeSelectorUtils() |
38 | 0 | { |
39 | |
|
40 | 0 | } |
41 | |
|
42 | |
private static String extractGroupIdFromFilter( String filter ) |
43 | |
{ |
44 | 2 | return StringUtils.contains( filter, ':' ) ? StringUtils.substringBefore( filter, ":" ) : null; |
45 | |
} |
46 | |
|
47 | |
private static String extractArtifactIdFromFilter( String filter ) |
48 | |
{ |
49 | |
|
50 | 2 | return StringUtils.contains( filter, ':' ) ? StringUtils.substringAfter( filter, ":" ) : filter; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog( |
61 | |
Map<String, List<Archetype>> archetypesPerCatalog, String filter ) |
62 | |
{ |
63 | 1 | if ( archetypesPerCatalog == null || archetypesPerCatalog.isEmpty() ) |
64 | |
{ |
65 | 0 | return Collections.emptyMap(); |
66 | |
} |
67 | 1 | Map<String, List<Archetype>> filtered = |
68 | |
new LinkedHashMap<String, List<Archetype>>( archetypesPerCatalog.size() ); |
69 | |
|
70 | 1 | for ( Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet() ) |
71 | |
{ |
72 | 1 | List<Archetype> archetypes = new ArrayList<Archetype>(); |
73 | |
|
74 | 1 | for ( Archetype archetype : entry.getValue() ) |
75 | |
{ |
76 | 2 | String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter( filter ); |
77 | 2 | String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter( filter ); |
78 | |
|
79 | 2 | if ( ( ( groupId == null ) || StringUtils.contains( archetype.getGroupId(), groupId ) ) |
80 | |
&& StringUtils.contains( archetype.getArtifactId(), artifactId ) ) |
81 | |
{ |
82 | 1 | archetypes.add( archetype ); |
83 | |
} |
84 | 2 | } |
85 | |
|
86 | 1 | if ( !archetypes.isEmpty() ) |
87 | |
{ |
88 | 1 | filtered.put( entry.getKey(), archetypes ); |
89 | |
} |
90 | 1 | } |
91 | |
|
92 | 1 | return filtered; |
93 | |
} |
94 | |
|
95 | |
} |