1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model.profile;
20
21 import java.util.List;
22
23 import org.apache.maven.api.services.InterpolatorException;
24 import org.apache.maven.api.services.ModelBuilderException;
25 import org.apache.maven.api.services.VersionParser;
26 import org.apache.maven.api.services.model.ProfileActivationContext;
27
28 import static org.apache.maven.internal.impl.model.profile.ConditionParser.toInt;
29
30
31
32
33
34
35 @SuppressWarnings("unused")
36 public class ConditionFunctions {
37 private final ProfileActivationContext context;
38 private final VersionParser versionParser;
39
40
41
42
43
44
45
46 public ConditionFunctions(ProfileActivationContext context, VersionParser versionParser) {
47 this.context = context;
48 this.versionParser = versionParser;
49 }
50
51
52
53
54
55
56
57
58 public Object length(List<Object> args) {
59 if (args.size() != 1) {
60 throw new IllegalArgumentException("length function requires exactly one argument");
61 }
62 String s = ConditionParser.toString(args.get(0));
63 return s.length();
64 }
65
66
67
68
69
70
71
72
73 public Object upper(List<Object> args) {
74 if (args.size() != 1) {
75 throw new IllegalArgumentException("upper function requires exactly one argument");
76 }
77 String s = ConditionParser.toString(args.get(0));
78 return s.toUpperCase();
79 }
80
81
82
83
84
85
86
87
88 public Object lower(List<Object> args) {
89 if (args.size() != 1) {
90 throw new IllegalArgumentException("lower function requires exactly one argument");
91 }
92 String s = ConditionParser.toString(args.get(0));
93 return s.toLowerCase();
94 }
95
96
97
98
99
100
101
102
103 public Object substring(List<Object> args) {
104 if (args.size() < 2 || args.size() > 3) {
105 throw new IllegalArgumentException("substring function requires 2 or 3 arguments");
106 }
107 String s = ConditionParser.toString(args.get(0));
108 int start = toInt(args.get(1));
109 int end = args.size() == 3 ? toInt(args.get(2)) : s.length();
110 return s.substring(start, end);
111 }
112
113
114
115
116
117
118
119
120 public Object indexOf(List<Object> args) {
121 if (args.size() != 2) {
122 throw new IllegalArgumentException("indexOf function requires exactly two arguments");
123 }
124 String s = ConditionParser.toString(args.get(0));
125 String substring = ConditionParser.toString(args.get(1));
126 return s.indexOf(substring);
127 }
128
129
130
131
132
133
134
135
136 public Object contains(List<Object> args) {
137 if (args.size() != 2) {
138 throw new IllegalArgumentException("contains function requires exactly two arguments");
139 }
140 String s = ConditionParser.toString(args.get(0));
141 String substring = ConditionParser.toString(args.get(1));
142 return s.contains(substring);
143 }
144
145
146
147
148
149
150
151
152 public Object matches(List<Object> args) {
153 if (args.size() != 2) {
154 throw new IllegalArgumentException("matches function requires exactly two arguments");
155 }
156 String s = ConditionParser.toString(args.get(0));
157 String regex = ConditionParser.toString(args.get(1));
158 return s.matches(regex);
159 }
160
161
162
163
164
165
166
167
168 public Object not(List<Object> args) {
169 if (args.size() != 1) {
170 throw new IllegalArgumentException("not function requires exactly one argument");
171 }
172 return !ConditionParser.toBoolean(args.get(0));
173 }
174
175
176
177
178
179
180
181
182 @SuppressWarnings("checkstyle:MethodName")
183 public Object if_(List<Object> args) {
184 if (args.size() != 3) {
185 throw new IllegalArgumentException("if function requires exactly three arguments");
186 }
187 boolean condition = ConditionParser.toBoolean(args.get(0));
188 return condition ? args.get(1) : args.get(2);
189 }
190
191
192
193
194
195
196
197
198
199
200 public Object exists(List<Object> args) {
201 if (args.size() != 1) {
202 throw new IllegalArgumentException("exists function requires exactly one argument");
203 }
204 String path = ConditionParser.toString(args.get(0));
205 return context.exists(path, true);
206 }
207
208
209
210
211
212
213
214
215
216
217 public Object missing(List<Object> args) {
218 if (args.size() != 1) {
219 throw new IllegalArgumentException("missing function requires exactly one argument");
220 }
221 String path = ConditionParser.toString(args.get(0));
222 return !context.exists(path, true);
223 }
224
225
226
227
228
229
230
231
232 public Object inrange(List<Object> args) {
233 if (args.size() != 2) {
234 throw new IllegalArgumentException("inrange function requires exactly two arguments");
235 }
236 String version = ConditionParser.toString(args.get(0));
237 String range = ConditionParser.toString(args.get(1));
238 return versionParser.parseVersionRange(range).contains(versionParser.parseVersion(version));
239 }
240 }