1 package org.nuiton.eugene.models.extension.tagvalue;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.ImmutableSet;
26 import org.apache.commons.lang3.StringUtils;
27 import org.nuiton.eugene.models.object.ObjectModelElement;
28 import org.nuiton.eugene.models.object.ObjectModelPackage;
29
30 import java.util.Set;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34
35
36
37
38
39 public class TagValueUtil {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 protected static final Pattern TAG_VALUE_PATTERN =
55 Pattern.compile("^((?:[_a-zA-Z0-9]+\\.)+(?:_?[A-Z][_a-zA-Z0-9]*\\.)+)(?:(class|attribute|operation))\\.(?:([_a-zA-Z0-9]+)\\.)?(?:(tagvalue|tagValue)?)\\.((?:[_a-zA-Z0-9]+)+(?:\\.?[_a-zA-Z0-9]+)*)?$");
56
57
58
59
60
61 protected static final Pattern MODEL_TAG_VALUE_PATTERN = Pattern.compile(
62 "^model\\.(?:(tagvalue|tagValue)?)\\.((?:[_a-zA-Z0-9]+)+(?:\\.?[_a-zA-Z0-9]+)*)$");
63
64
65
66
67
68 protected static final Pattern PACKAGE_TAG_VALUE_PATTERN = Pattern.compile(
69 "^package\\.((?:[_a-zA-Z0-9]+\\.)+)(?:(tagvalue|tagValue)?)\\.((?:[_a-zA-Z0-9]+)+(?:\\.?[_a-zA-Z0-9]+)*)$");
70 protected static final Pattern PACKAGE_STEREOTYPE_PATTERN = Pattern
71 .compile("^package\\.((?:[_a-zA-Z0-9]+\\.)+)?(?:(stereotype)?)$");
72 protected static final Pattern STEREOTYPE_PATTERN = Pattern
73 .compile("^((?:[_a-zA-Z0-9]+\\.)+(?:_?[a-zA-Z][_a-zA-Z0-9]*\\.)+)(?:(class|attribute|operation))\\.(?:([_a-zA-Z0-9]+)\\.)?(?:(stereotype)?)$");
74
75 public static Matcher getModelMatcher(String key) throws InvalidTagValueSyntaxException {
76 Matcher matcher = MODEL_TAG_VALUE_PATTERN.matcher(key);
77 if (!matcher.find()) {
78 throw new InvalidTagValueSyntaxException();
79 }
80 return matcher;
81 }
82
83 public static Matcher getPackageMatcher(String key) throws InvalidTagValueSyntaxException {
84 Matcher matcher = PACKAGE_TAG_VALUE_PATTERN.matcher(key);
85 if (!matcher.find()) {
86 throw new InvalidTagValueSyntaxException();
87 }
88 return matcher;
89 }
90
91 public static Matcher getMatcher(String key) throws InvalidTagValueSyntaxException {
92 Matcher matcher = TAG_VALUE_PATTERN.matcher(key);
93 if (!matcher.find()) {
94 throw new InvalidTagValueSyntaxException();
95 }
96 return matcher;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public static String findTagValue(TagValueMetadata tagName, WithTagValuesOrStereotypes... elements) {
118 String result = findDirectTagValue(tagName, elements);
119
120 if (result != null) {
121 return result;
122 }
123
124 for (WithTagValuesOrStereotypes element : elements) {
125 if (element instanceof ObjectModelElement) {
126
127 ObjectModelElement declaringElement = ((ObjectModelElement) element).getDeclaringElement();
128 if (declaringElement != null) {
129 String value = findNotEmptyTagValue(tagName, declaringElement);
130 if (value != null) {
131 return value;
132 }
133 }
134 }
135 }
136 return tagName.getDefaultValue();
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static String findTagValue(String tagName, String defaultValue, WithTagValuesOrStereotypes... elements) {
159 String result = findDirectTagValue(tagName, elements);
160
161 if (result != null) {
162 return result;
163 }
164
165 for (WithTagValuesOrStereotypes element : elements) {
166 if (element instanceof ObjectModelElement) {
167
168 ObjectModelElement declaringElement = ((ObjectModelElement) element).getDeclaringElement();
169 if (declaringElement != null) {
170 String value = findNotEmptyTagValue(tagName, declaringElement);
171 if (value != null) {
172 return value;
173 }
174 }
175 }
176 }
177 return defaultValue;
178 }
179
180 public static boolean findBooleanTagValue(TagValueMetadata tagName, WithTagValuesOrStereotypes... elements) {
181 String value = findTagValue(tagName, elements);
182 return value != null && "true".equalsIgnoreCase(value);
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public static Boolean findNullableBooleanTagValue(TagValueMetadata tagName, WithTagValuesOrStereotypes... elements) {
202 String value = findTagValue(tagName, elements);
203 return value == null ? null : "true".equalsIgnoreCase(value);
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public static String findDirectTagValue(TagValueMetadata tagName, WithTagValuesOrStereotypes... elements) {
222
223 for (WithTagValuesOrStereotypes element : elements) {
224 String value = findNotEmptyTagValue(tagName, element);
225 if (value != null) {
226 return value;
227 }
228 }
229
230 return tagName.getDefaultValue();
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public static String findDirectTagValue(String tagName, WithTagValuesOrStereotypes... elements) {
249
250 for (WithTagValuesOrStereotypes element : elements) {
251 String value = findNotEmptyTagValue(tagName, element);
252 if (value != null) {
253 return value;
254 }
255 }
256
257 return null;
258 }
259
260 public static String findNotEmptyTagValue(TagValueMetadata tagName, WithTagValuesOrStereotypes element) {
261
262 String value = null;
263 if (element != null) {
264 if (element instanceof ObjectModelPackage) {
265 value = findNotEmptyTagValue(tagName, (ObjectModelPackage) element);
266 } else {
267 value = element.getTagValue(tagName.getName());
268 if (StringUtils.isEmpty(value)) {
269 value = null;
270 }
271 }
272 }
273 return value;
274
275 }
276
277 public static String findNotEmptyTagValue(String tagName, WithTagValuesOrStereotypes element) {
278
279 String value = null;
280 if (element != null) {
281 if (element instanceof ObjectModelPackage) {
282 value = findNotEmptyTagValue(tagName, (ObjectModelPackage) element);
283 } else {
284 value = element.getTagValue(tagName);
285 if (StringUtils.isEmpty(value)) {
286 value = null;
287 }
288 }
289 }
290 return value;
291
292 }
293
294
295 protected static String findNotEmptyTagValue(TagValueMetadata tagName, ObjectModelPackage element) {
296
297 String value = element.getTagValue(tagName.getName());
298 if (StringUtils.isEmpty(value)) {
299 value = null;
300 }
301 if (value == null && element.getParentPackage() != null) {
302 value = findNotEmptyTagValue(tagName, element.getParentPackage());
303 }
304 return value;
305
306 }
307
308 protected static String findNotEmptyTagValue(String tagName, ObjectModelPackage element) {
309
310 String value = element.getTagValue(tagName);
311 if (StringUtils.isEmpty(value)) {
312 value = null;
313 }
314 if (value == null && element.getParentPackage() != null) {
315 value = findNotEmptyTagValue(tagName, element.getParentPackage());
316 }
317 return value;
318
319 }
320
321 public static Matcher getStereotypeMatcher(String key) throws InvalidStereotypeSyntaxException {
322 Matcher matcher = STEREOTYPE_PATTERN.matcher(key);
323 if (!matcher.find()) {
324 throw new InvalidStereotypeSyntaxException();
325 }
326 return matcher;
327 }
328
329 public static Matcher getPackageStereotypeMatcher(String key) throws InvalidStereotypeSyntaxException {
330 Matcher matcher = PACKAGE_STEREOTYPE_PATTERN.matcher(key);
331 if (!matcher.find()) {
332 throw new InvalidStereotypeSyntaxException();
333 }
334 return matcher;
335 }
336
337 public static Set<String> getStereotypes(String value) {
338 String[] split = value.split("\\s*,\\s*");
339 return ImmutableSet.<String>builder().add(split).build();
340 }
341 }