View Javadoc

1   /**
2    * Copyright 2005-2014 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-bean", 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       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
60       */
61      protected String getRegexString() {
62          if (StringUtils.isEmpty(validChars)) {
63              throw new IllegalStateException("validChars is empty");
64          }
65  
66          // filter out and escape chars which would confuse the pattern-matcher
67          Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
68          String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
69  
70          StringBuffer regexString = new StringBuffer("[");
71          regexString.append(filteredChars);
72          if (filteredChars.endsWith("\\")) {
73              regexString.append("\\");
74          }
75          regexString.append("]");
76  
77          return regexString.toString();
78      }
79  
80      /**
81       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
82       */
83      @Override
84      public String getMessageKey() {
85          String messageKey = super.getMessageKey();
86          if (StringUtils.isNotEmpty(messageKey)) {
87              return messageKey;
88          }
89  
90          return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
91      }
92  
93      /**
94       * Parameters to be used in the string retrieved by this constraint's messageKey
95       *
96       * @return the validationMessageParams
97       */
98      public List<String> getValidationMessageParams() {
99          if (validationMessageParams == null) {
100             validationMessageParams = new ArrayList<String>();
101             if (StringUtils.isNotBlank(validChars)) {
102                 validationMessageParams.add(validChars);
103             }
104 
105         }
106         return this.validationMessageParams;
107     }
108 
109     /**
110      * Validates different requirements of component compiling a series of reports detailing information on errors
111      * found in the component.  Used by the RiceDictionaryValidator.
112      *
113      * @param tracer Record of component's location
114      * @return A list of ErrorReports detailing errors found within the component and referenced within it
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 }