View Javadoc

1   /**
2    * Copyright 2005-2013 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.core.api.config.property.ConfigurationService;
20  import org.kuali.rice.krad.service.KRADServiceLocator;
21  import org.kuali.rice.krad.uif.UifConstants;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.regex.Pattern;
26  
27  /**
28   * Pattern for matching any character in the given list (String)
29   * 
30   * 
31   */
32  public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
33      protected String validChars;
34  
35      /**
36       * @return String containing all valid chars for this charset
37       */
38      public String getValidChars() {
39          return validChars;
40      }
41  
42      /**
43       * @param validChars for this charset
44       */
45      public void setValidChars(String validChars) {
46          if (StringUtils.isEmpty(validChars)) {
47              throw new IllegalArgumentException("invalid (empty) validChars");
48          }
49  
50          this.validChars = validChars;
51      }
52  
53  
54      /**
55       * Escapes every special character I could think of, to limit potential misuse of this pattern.
56       * 
57       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
58       */
59      protected String getRegexString() {
60          if (StringUtils.isEmpty(validChars)) {
61              throw new IllegalStateException("validChars is empty");
62          }
63  
64          // filter out and escape chars which would confuse the pattern-matcher
65          Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
66          String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
67  
68          StringBuffer regexString = new StringBuffer("[");
69          regexString.append(filteredChars);
70          if (filteredChars.endsWith("\\")) {
71              regexString.append("\\");
72          }
73          regexString.append("]");
74  
75          return regexString.toString();
76      }
77  
78  	/**
79  	 * 
80  	 * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
81  	 */
82  	@Override
83  	public String getLabelKey() {
84  		String labelKey = super.getLabelKey();
85  		if (StringUtils.isNotEmpty(labelKey)) {
86  			return labelKey;
87  		}
88  		return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
89  	}
90  
91      /**
92       * Parameters to be used in the string retrieved by this constraint's labelKey
93       * @return the validationMessageParams
94       */
95      public List<String> getValidationMessageParams() {
96          if(validationMessageParams == null){
97              validationMessageParams = new ArrayList<String>();
98              if (StringUtils.isNotBlank(validChars)) {
99                  validationMessageParams.add(validChars);
100             }
101             
102         }
103         return this.validationMessageParams;
104     }
105 
106 }