1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.jboss.modules.descriptor;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import java.util.regex.PatternSyntaxException;
21
22 import org.apache.commons.lang.ObjectUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.sonatype.aether.artifact.Artifact;
25
26 import de.smartics.maven.plugin.jboss.modules.domain.MatchContext;
27 import de.smartics.maven.plugin.jboss.modules.domain.matching.DoubleMatchContext;
28 import de.smartics.maven.plugin.jboss.modules.domain.matching.SingleMatchContext;
29
30
31
32
33
34 public class ArtifactClusion
35 {
36
37
38
39
40
41
42
43
44
45 private String groupId;
46
47
48
49
50
51 private Pattern groupIdPattern;
52
53
54
55
56 private String artifactId;
57
58
59
60
61
62 private Pattern artifactIdPattern;
63
64
65
66
67
68
69
70
71 public ArtifactClusion()
72 {
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static ArtifactClusion create(final String groupId, final String artifactId)
91 {
92 final ArtifactClusion clusion = new ArtifactClusion();
93 clusion.setGroupId(groupId);
94 clusion.setArtifactId(artifactId);
95 return clusion;
96 }
97
98
99
100
101
102
103
104
105 public String getGroupId()
106 {
107 return groupId;
108 }
109
110
111
112
113
114
115 public void setGroupId(final String groupId)
116 {
117 if (StringUtils.isNotBlank(groupId))
118 {
119 this.groupId = groupId;
120 groupIdPattern = compilePattern(groupId);
121 }
122 }
123
124 private static Pattern compilePattern(final String pattern)
125 {
126 if (StringUtils.isNotBlank(pattern))
127 {
128 try
129 {
130 return Pattern.compile(pattern);
131 }
132 catch (final PatternSyntaxException e)
133 {
134
135 }
136 }
137 return null;
138 }
139
140
141
142
143
144
145 public String getArtifactId()
146 {
147 return artifactId;
148 }
149
150
151
152
153
154
155 public void setArtifactId(final String artifactId)
156 {
157 if (StringUtils.isNotBlank(artifactId))
158 {
159 this.artifactId = artifactId;
160 artifactIdPattern = compilePattern(artifactId);
161 }
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public MatchContext matches(final Artifact artifact)
175 {
176 final SingleMatchContext matchesGroupId =
177 matches(groupIdPattern, groupId, artifact.getGroupId());
178 if (matchesGroupId != null && !matchesGroupId.isMatched())
179 {
180 return new SingleMatchContext(false);
181 }
182 final SingleMatchContext matchesArtifactId =
183 matches(artifactIdPattern, artifactId, artifact.getArtifactId());
184
185 final boolean result =
186 (matchesGroupId != null && matchesGroupId.isMatched() && (matchesArtifactId == null || matchesArtifactId
187 .isMatched()))
188 || (matchesArtifactId != null && matchesArtifactId.isMatched());
189
190 final MatchContext context =
191 new DoubleMatchContext(result, matchesGroupId, matchesArtifactId);
192 return context;
193 }
194
195 private static SingleMatchContext matches(final Pattern pattern,
196 final String id, final String inputId)
197 {
198 if (pattern != null)
199 {
200 final Matcher matcher = pattern.matcher(inputId);
201 return new SingleMatchContext(matcher);
202 }
203
204 if (StringUtils.isNotBlank(id))
205 {
206 return new SingleMatchContext(id.equals(inputId));
207 }
208
209 return null;
210 }
211
212
213
214 @Override
215 public String toString()
216 {
217 return ObjectUtils.toString(groupId) + ':'
218 + ObjectUtils.toString(artifactId);
219 }
220 }