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.kns.datadictionary.validation.charlevel;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
20  import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
21  
22  import java.util.regex.Pattern;
23  
24  /**
25   * Pattern for matching any character in the given list (String)
26   * 
27   * @deprecated Use {@link org.kuali.rice.krad.datadictionary.validation.constraint.CharsetPatternConstraint}.
28   */
29  @Deprecated
30  public class CharsetValidationPattern extends CharacterLevelValidationPattern {
31      protected String validChars;
32  
33      /**
34       * @return String containing all valid chars for this charset
35       */
36      public String getValidChars() {
37          return validChars;
38      }
39  
40      /**
41       * @param validChars for this charset
42       */
43      public void setValidChars(String validChars) {
44          if (StringUtils.isEmpty(validChars)) {
45              throw new IllegalArgumentException("invalid (empty) validChars");
46          }
47  
48          this.validChars = validChars;
49      }
50  
51  
52      /**
53       * Escapes every special character I could think of, to limit potential misuse of this pattern.
54       * 
55       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
56       */
57      protected String getRegexString() {
58          if (StringUtils.isEmpty(validChars)) {
59              throw new IllegalStateException("validChars is empty");
60          }
61  
62          // filter out and escape chars which would confuse the pattern-matcher
63          Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
64          String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
65  
66          StringBuffer regexString = new StringBuffer("[");
67          regexString.append(filteredChars);
68          if (filteredChars.endsWith("\\")) {
69              regexString.append("\\");
70          }
71          regexString.append("]");
72  
73          return regexString.toString();
74      }
75  
76  
77      /**
78       * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
79       */
80      public void extendExportMap(ExportMap exportMap) {
81          exportMap.set("type", "charset");
82  
83          exportMap.set("validChars", getValidChars());
84      }
85  
86  	/**
87  	 * This overridden method ...
88  	 * 
89  	 * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#getValidationErrorMessageParameters(java.lang.String, java.lang.String)
90  	 */
91  	@Override
92  	public String[] getValidationErrorMessageParameters(String attributeLabel) {
93  		// build character list
94  		StringBuilder buf = new StringBuilder();
95  		for (int i = 0; i < validChars.length(); i++) {
96  			buf.append(validChars.charAt(i));
97  			if (i != validChars.length() - 1) {
98  				buf.append(", ");
99  			}
100 		}
101 		String characterList = buf.toString();
102 		
103 		if (getMaxLength() != -1) {
104 			return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList};
105 		}
106 		if (getExactLength() != -1) {
107 			return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList};
108 		}
109 		return new String[] {attributeLabel, "0", characterList};
110 	}
111 }