View Javadoc

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