View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Validation pattern for matching currency type. Extends the FloatingPointPatternConstraint and
28   * adds the currency prefix/suffix to the regex string for validation
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  
33  public class CurrencyPatternConstraint extends FloatingPointPatternConstraint {
34  
35      /**
36       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
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          // Regex special characters need to be escaped if they are part of the prefix/suffix
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       * retrieves a currency formatter instance and sets ParseBigDecimal to true
81       * to fix [KULEDOCS-742]
82       *
83       * @return CurrencyInstance
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  }