1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.constraint;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20 import org.kuali.rice.krad.uif.UifConstants;
21
22 import java.text.DecimalFormat;
23 import java.text.NumberFormat;
24 import java.util.HashSet;
25
26
27
28
29
30
31
32
33 public class CurrencyPatternConstraint extends FloatingPointPatternConstraint {
34
35
36
37
38 @Override
39 protected String getRegexString() {
40 StringBuilder regexString = new StringBuilder(super.getRegexString());
41
42 NumberFormat formatter = getCurrencyInstanceUsingParseBigDecimal();
43
44 if (!(formatter instanceof DecimalFormat)) {
45 return regexString.toString();
46 }
47
48 String prefix = ((DecimalFormat) formatter).getPositivePrefix();
49 String suffix = ((DecimalFormat) formatter).getPositiveSuffix();
50
51
52 if (prefix != null) {
53 StringBuilder escapedPrefix = new StringBuilder();
54 for (char c : prefix.toCharArray()) {
55 if (UifConstants.JS_REGEX_SPECIAL_CHARS.indexOf(c) != -1) {
56 escapedPrefix.append("\\");
57 }
58 escapedPrefix.append(c);
59 }
60
61 regexString.insert(0, "(" + escapedPrefix + ")?");
62 }
63
64 if (suffix != null) {
65 StringBuilder escapedSuffix = new StringBuilder();
66 for (char c : suffix.toCharArray()) {
67 if (UifConstants.JS_REGEX_SPECIAL_CHARS.indexOf(c) != -1) {
68 escapedSuffix.append("\\");
69 }
70 escapedSuffix.append(c);
71 }
72
73 regexString.append("(" + escapedSuffix + ")?");
74 }
75
76 return regexString.toString();
77 }
78
79
80
81
82
83
84
85 private NumberFormat getCurrencyInstanceUsingParseBigDecimal() {
86 NumberFormat formatter = NumberFormat.getCurrencyInstance();
87 if (formatter instanceof DecimalFormat) {
88 ((DecimalFormat) formatter).setParseBigDecimal(true);
89 }
90 return formatter;
91 }
92
93 }