View Javadoc
1   /**
2    * Copyright 2005-2015 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.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22  import org.kuali.rice.krad.uif.UifConstants;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.regex.Pattern;
27  
28  /**
29   * Pattern for matching any character in the given list (String)
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @BeanTag(name = "charsetPatternConstraint", parent = "CharsetPatternConstraint")
34  public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
35      protected String validChars;
36  
37      /**
38       * @return String containing all valid chars for this charset
39       */
40      @BeanTagAttribute(name = "validChars")
41      public String getValidChars() {
42          return validChars;
43      }
44  
45      /**
46       * @param validChars for this charset
47       */
48      public void setValidChars(String validChars) {
49          if (StringUtils.isEmpty(validChars)) {
50              throw new IllegalArgumentException("invalid (empty) validChars");
51          }
52  
53          this.validChars = validChars;
54      }
55  
56      /**
57       * Escapes every special character I could think of, to limit potential misuse of this pattern.
58       *
59       * {@inheritDoc}
60       */
61      @Override
62      protected String getRegexString() {
63          if (StringUtils.isEmpty(validChars)) {
64              throw new IllegalStateException("validChars is empty");
65          }
66  
67          // filter out and escape chars which would confuse the pattern-matcher
68          Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
69          String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
70  
71          StringBuffer regexString = new StringBuffer("[");
72          regexString.append(filteredChars);
73          if (filteredChars.endsWith("\\")) {
74              regexString.append("\\");
75          }
76          regexString.append("]");
77  
78          return regexString.toString();
79      }
80  
81      /**
82       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
83       */
84      @Override
85      public String getMessageKey() {
86          String messageKey = super.getMessageKey();
87          if (StringUtils.isNotEmpty(messageKey)) {
88              return messageKey;
89          }
90  
91          return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
92      }
93  
94      /**
95       * Parameters to be used in the string retrieved by this constraint's messageKey
96       *
97       * @return the validationMessageParams
98       */
99      public List<String> getValidationMessageParams() {
100         if (validationMessageParams == null) {
101             validationMessageParams = new ArrayList<String>();
102             if (StringUtils.isNotBlank(validChars)) {
103                 validationMessageParams.add(validChars);
104             }
105 
106         }
107         return this.validationMessageParams;
108     }
109 
110     /**
111      * Validates different requirements of component compiling a series of reports detailing information on errors
112      * found in the component.  Used by the RiceDictionaryValidator.
113      *
114      * @param tracer Record of component's location
115      */
116     @Override
117     public void completeValidation(ValidationTrace tracer) {
118         tracer.addBean("CharsetPatternConstraint", getMessageKey());
119 
120         if (getValidChars() == null) {
121             String currentValues[] = {"validChars =" + getValidChars()};
122             tracer.createError("ValidChars must be set", currentValues);
123         }
124 
125         super.completeValidation(tracer.getCopy());
126     }
127 }