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