1 /**
2 * Copyright 2005-2012 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.krad.uif.UifConstants;
20
21 /**
22 * Pattern for matching any printable character
23 */
24 public class AnyCharacterPatternConstraint extends ValidCharactersPatternConstraint {
25 protected boolean allowWhitespace = false;
26 protected boolean omitNewline = false;
27
28 /**
29 * @return allowWhitespace
30 */
31 public boolean getAllowWhitespace() {
32 return allowWhitespace;
33 }
34
35 /**
36 * @param allowWhitespace
37 */
38 public void setAllowWhitespace(boolean allowWhitespace) {
39 this.allowWhitespace = allowWhitespace;
40 }
41
42 /**
43 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
44 */
45 protected String getRegexString() {
46 StringBuffer regexString = new StringBuffer("[");
47
48 regexString.append("\\x21-\\x7E");
49 if (allowWhitespace) {
50 regexString.append("\\t\\v\\040");
51 if (!omitNewline) {
52 regexString.append("\\f\\r\\n");
53 }
54 }
55
56 regexString.append("]");
57
58 return regexString.toString();
59 }
60
61 /**
62 * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
63 */
64 @Override
65 public String getLabelKey() {
66 String labelKey = super.getLabelKey();
67 if (StringUtils.isNotEmpty(labelKey)) {
68 return labelKey;
69 }
70 if (!allowWhitespace) {
71 return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "noWhitespace";
72 } else {
73 return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "anyCharacterPattern";
74 }
75 }
76
77 public boolean isOmitNewline() {
78 return omitNewline;
79 }
80
81 /**
82 * When set to true, omit new line characters from the set of valid characters. This flag
83 * will only have an effect if the allowWhitespace flag is true, otherwise all whitespace
84 * including new lines characters are omitted.
85 * @param omitNewline
86 */
87 public void setOmitNewline(boolean omitNewline) {
88 this.omitNewline = omitNewline;
89 }
90 }