Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CharsetPatternConstraint |
|
| 2.75;2.75 |
1 | /* | |
2 | * Copyright 2005-2008 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 | ||
20 | import java.util.regex.Pattern; | |
21 | ||
22 | /** | |
23 | * Pattern for matching any character in the given list (String) | |
24 | * | |
25 | * | |
26 | */ | |
27 | 0 | public class CharsetPatternConstraint extends ValidCharactersPatternConstraint { |
28 | protected String validChars; | |
29 | ||
30 | /** | |
31 | * @return String containing all valid chars for this charset | |
32 | */ | |
33 | public String getValidChars() { | |
34 | 0 | return validChars; |
35 | } | |
36 | ||
37 | /** | |
38 | * @param validChars for this charset | |
39 | */ | |
40 | public void setValidChars(String validChars) { | |
41 | 0 | if (StringUtils.isEmpty(validChars)) { |
42 | 0 | throw new IllegalArgumentException("invalid (empty) validChars"); |
43 | } | |
44 | ||
45 | 0 | this.validChars = validChars; |
46 | 0 | } |
47 | ||
48 | ||
49 | /** | |
50 | * Escapes every special character I could think of, to limit potential misuse of this pattern. | |
51 | * | |
52 | * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString() | |
53 | */ | |
54 | protected String getRegexString() { | |
55 | 0 | if (StringUtils.isEmpty(validChars)) { |
56 | 0 | throw new IllegalStateException("validChars is empty"); |
57 | } | |
58 | ||
59 | // filter out and escape chars which would confuse the pattern-matcher | |
60 | 0 | Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])"); |
61 | 0 | String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1"); |
62 | ||
63 | 0 | StringBuffer regexString = new StringBuffer("["); |
64 | 0 | regexString.append(filteredChars); |
65 | 0 | if (filteredChars.endsWith("\\")) { |
66 | 0 | regexString.append("\\"); |
67 | } | |
68 | 0 | regexString.append("]"); |
69 | ||
70 | 0 | return regexString.toString(); |
71 | } | |
72 | ||
73 | /** | |
74 | * | |
75 | * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey() | |
76 | */ | |
77 | @Override | |
78 | public String getLabelKey() { | |
79 | 0 | String labelKey = super.getLabelKey(); |
80 | 0 | if (StringUtils.isNotEmpty(labelKey)) { |
81 | 0 | return labelKey; |
82 | } | |
83 | 0 | StringBuilder key = new StringBuilder(""); |
84 | 0 | key.append("charsetPattern,"); |
85 | // TODO delyea: add in list of valid characters here? | |
86 | // StringBuilder validCharsBuf = new StringBuilder(); | |
87 | // for (int i = 0; i < getValidChars().length(); i++) { | |
88 | // validCharsBuf.append(getValidChars().charAt(i)); | |
89 | // if (i != getValidChars().length() - 1) { | |
90 | // validCharsBuf.append(", "); | |
91 | // } | |
92 | // } | |
93 | 0 | return key.toString(); |
94 | } | |
95 | ||
96 | // /** | |
97 | // * This overridden method ... | |
98 | // * | |
99 | // * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#getValidationErrorMessageParameters(java.lang.String, java.lang.String) | |
100 | // */ | |
101 | // @Override | |
102 | // public String[] getValidationErrorMessageParameters(String attributeLabel) { | |
103 | // // build character list | |
104 | // StringBuilder buf = new StringBuilder(); | |
105 | // for (int i = 0; i < validChars.length(); i++) { | |
106 | // buf.append(validChars.charAt(i)); | |
107 | // if (i != validChars.length() - 1) { | |
108 | // buf.append(", "); | |
109 | // } | |
110 | // } | |
111 | // String characterList = buf.toString(); | |
112 | // | |
113 | // if (getMaxLength() != -1) { | |
114 | // return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList}; | |
115 | // } | |
116 | // if (getExactLength() != -1) { | |
117 | // return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList}; | |
118 | // } | |
119 | // return new String[] {attributeLabel, "0", characterList}; | |
120 | // } | |
121 | ||
122 | } |