| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.datadictionary.validation.charlevel; |
| 17 | |
|
| 18 | |
import java.util.regex.Pattern; |
| 19 | |
|
| 20 | |
import org.apache.commons.lang.StringUtils; |
| 21 | |
import org.kuali.rice.kns.datadictionary.exporter.ExportMap; |
| 22 | |
import org.kuali.rice.kns.datadictionary.validation.CharacterLevelValidationPattern; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 0 | public class CharsetValidationPattern extends CharacterLevelValidationPattern { |
| 30 | |
protected String validChars; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public String getValidChars() { |
| 36 | 0 | return validChars; |
| 37 | |
} |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public void setValidChars(String validChars) { |
| 43 | 0 | if (StringUtils.isEmpty(validChars)) { |
| 44 | 0 | throw new IllegalArgumentException("invalid (empty) validChars"); |
| 45 | |
} |
| 46 | |
|
| 47 | 0 | this.validChars = validChars; |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
protected String getRegexString() { |
| 57 | 0 | if (StringUtils.isEmpty(validChars)) { |
| 58 | 0 | throw new IllegalStateException("validChars is empty"); |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | 0 | Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])"); |
| 63 | 0 | String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1"); |
| 64 | |
|
| 65 | 0 | StringBuffer regexString = new StringBuffer("["); |
| 66 | 0 | regexString.append(filteredChars); |
| 67 | 0 | if (filteredChars.endsWith("\\")) { |
| 68 | 0 | regexString.append("\\"); |
| 69 | |
} |
| 70 | 0 | regexString.append("]"); |
| 71 | |
|
| 72 | 0 | return regexString.toString(); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void extendExportMap(ExportMap exportMap) { |
| 80 | 0 | exportMap.set("type", "charset"); |
| 81 | |
|
| 82 | 0 | exportMap.set("validChars", getValidChars()); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public String[] getValidationErrorMessageParameters(String attributeLabel) { |
| 92 | |
|
| 93 | 0 | StringBuilder buf = new StringBuilder(); |
| 94 | 0 | for (int i = 0; i < validChars.length(); i++) { |
| 95 | 0 | buf.append(validChars.charAt(i)); |
| 96 | 0 | if (i != validChars.length() - 1) { |
| 97 | 0 | buf.append(", "); |
| 98 | |
} |
| 99 | |
} |
| 100 | 0 | String characterList = buf.toString(); |
| 101 | |
|
| 102 | 0 | if (getMaxLength() != -1) { |
| 103 | 0 | return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList}; |
| 104 | |
} |
| 105 | 0 | if (getExactLength() != -1) { |
| 106 | 0 | return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList}; |
| 107 | |
} |
| 108 | 0 | return new String[] {attributeLabel, "0", characterList}; |
| 109 | |
} |
| 110 | |
} |