1 | 0 | package pt.digitalis.dif.dem.objects.parameters.constraints.impl; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
|
8 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
9 | |
import pt.digitalis.dif.dem.objects.parameters.constraints.AbstractParameterConstraint; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | 0 | public class ParameterConstraintRegexImpl extends AbstractParameterConstraint { |
16 | |
|
17 | |
|
18 | |
public final static String REGEXP_COMMA = "<<COMMA>>"; |
19 | |
|
20 | |
|
21 | |
public final static String REGEXP_LIST_END = "<<END_REG_EXP>>"; |
22 | |
|
23 | |
|
24 | |
public final static String REGEXP_LIST_SEPARATOR = "<<REG_EXP_SEP>>"; |
25 | |
|
26 | |
|
27 | |
public final static String REGEXP_LIST_START = "<<START_REG_EXP>>"; |
28 | |
|
29 | |
|
30 | |
@SuppressWarnings("serial") |
31 | 0 | final static private List<String> supportedClasses = new ArrayList<String>() { |
32 | |
|
33 | |
{ |
34 | 0 | add(String.class.getCanonicalName()); |
35 | |
} |
36 | |
}; |
37 | |
|
38 | |
|
39 | |
String lastTestedValue; |
40 | |
|
41 | |
|
42 | |
String regexJava; |
43 | |
|
44 | |
|
45 | |
String regexJavaScript; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void configureConstraint(String configurationString) |
51 | |
{ |
52 | 0 | String value = configurationString.substring(configurationString.indexOf("=") + 1); |
53 | |
|
54 | 0 | if (value.contains(REGEXP_LIST_SEPARATOR)) |
55 | |
{ |
56 | 0 | String[] values = value.split(REGEXP_LIST_SEPARATOR); |
57 | |
|
58 | 0 | regexJava = values[0].substring(REGEXP_LIST_START.length()); |
59 | 0 | regexJavaScript = values[1].substring(0, values[1].length() - REGEXP_LIST_END.length()); |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | 0 | regexJava = value; |
64 | 0 | regexJavaScript = "/^" + value + "$/"; |
65 | |
} |
66 | |
|
67 | 0 | regexJava = regexJava.replaceAll(REGEXP_COMMA, ","); |
68 | 0 | regexJavaScript = regexJavaScript.replaceAll(REGEXP_COMMA, ","); |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Override |
75 | |
protected Map<String, String> getErrorMessageValues(boolean parseValues) |
76 | |
{ |
77 | 0 | Map<String, String> substitutions = new HashMap<String, String>(); |
78 | |
|
79 | 0 | if (parseValues) |
80 | |
{ |
81 | 0 | substitutions.put("value", lastTestedValue); |
82 | |
} |
83 | 0 | substitutions.put("regex", regexJava); |
84 | |
|
85 | 0 | return substitutions; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public String getJavaScriptValidationCondition() |
92 | |
{ |
93 | 0 | return "(new RegExp(" + regexJavaScript + ").test(value))"; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public boolean isSupportedClass(Class<?> clazz) |
100 | |
{ |
101 | 0 | return supportedClasses.contains(clazz.getCanonicalName()); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public boolean validateConstraint(String value, IStageInstance stageInstance) |
109 | |
{ |
110 | 0 | lastTestedValue = value; |
111 | |
|
112 | 0 | return (value == null || value.matches(regexJava)); |
113 | |
} |
114 | |
} |