1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.jboss.modules.domain.matching;
17
18 import java.util.regex.MatchResult;
19
20 import de.smartics.maven.plugin.jboss.modules.domain.MatchContext;
21
22
23
24
25
26 public final class DoubleMatchContext implements MatchContext
27 {
28
29
30
31
32
33
34
35
36
37 private final boolean result;
38
39
40
41
42 private final MatchResult groupMatchResult;
43
44
45
46
47 private final MatchResult artifactMatchResult;
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public DoubleMatchContext(final boolean result,
62 final MatchContext groupIdContext, final MatchContext artifactIdContext)
63 {
64 this.result = result;
65 this.groupMatchResult =
66 groupIdContext != null && groupIdContext.isMatched() ? groupIdContext
67 .getMatchResult() : null;
68 this.artifactMatchResult =
69 artifactIdContext != null && artifactIdContext.isMatched()
70 ? artifactIdContext.getMatchResult() : null;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public boolean isMatched()
88 {
89 return result;
90 }
91
92
93
94
95
96
97
98 @Override
99 public MatchResult getMatchResult()
100 {
101 return artifactMatchResult;
102 }
103
104
105
106 @Override
107 public String translateName(final String input)
108 {
109 String translation = input;
110
111 if (isMatched())
112 {
113 if (artifactMatchResult != null)
114 {
115 translation = translate(artifactMatchResult, "$", translation);
116 }
117
118 if (groupMatchResult != null)
119 {
120 translation = translate(groupMatchResult, "$g", translation);
121 }
122 }
123
124 return translation;
125 }
126
127 private static String translate(final MatchResult matchResult,
128 final String delimiter, final String input)
129 {
130 String translation = input;
131 final int groupCount = matchResult.groupCount();
132 if (groupCount > 0)
133 {
134 for (int group = 1; group <= groupCount; group++)
135 {
136 final String replacement = matchResult.group(group);
137 translation = translation.replace(delimiter + group, replacement);
138 }
139 }
140 return translation;
141 }
142
143
144
145
146
147
148
149 public boolean hasGroupMatch()
150 {
151 if (artifactMatchResult != null && isMatched())
152 {
153 final int groupCount = artifactMatchResult.groupCount();
154 if (groupCount > 0)
155 {
156 return true;
157 }
158 }
159
160 if (groupMatchResult != null && isMatched())
161 {
162 final int groupCount = groupMatchResult.groupCount();
163 if (groupCount > 0)
164 {
165 return true;
166 }
167 }
168
169 return false;
170 }
171
172
173
174 }