View Javadoc
1   /**
2    * Copyright 2005-2014 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       * {@inheritDoc}
50       */
51      @Override
52      protected String getRegexString() {
53          StringBuffer regexString = new StringBuffer("[");
54  
55          regexString.append("\\u0000-\\uFFFF");
56          if (allowWhitespace) {
57              regexString.append("\\t\\v\\040");
58              if (!omitNewline) {
59                  regexString.append("\\f\\r\\n");
60              }
61          }
62  
63          regexString.append("]");
64  
65          return regexString.toString();
66      }
67  
68      /**
69       * @see BaseConstraint#getMessageKey()
70       */
71      @Override
72      public String getMessageKey() {
73          String messageKey = super.getMessageKey();
74          if (StringUtils.isNotEmpty(messageKey)) {
75              return messageKey;
76          }
77  
78          if (!allowWhitespace) {
79              return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "noWhitespace";
80          } else {
81              return UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "utf8AnyCharacterPattern";
82          }
83      }
84  
85      @BeanTagAttribute(name = "omitNewline")
86      public boolean isOmitNewline() {
87          return omitNewline;
88      }
89  
90      /**
91       * When set to true, omit new line characters from the set of valid characters.  This flag
92       * will only have an effect if the allowWhitespace flag is true, otherwise all whitespace
93       * including new lines characters are omitted.
94       *
95       * @param omitNewline
96       */
97      public void setOmitNewline(boolean omitNewline) {
98          this.omitNewline = omitNewline;
99      }
100 }