View Javadoc

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.kuali.rice.core.api.config.property.ConfigurationService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.uif.UifConstants;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Parent abstract class that allows additional characters to be allowed in child constraint
27   * character sets, see AlphaNumericPatternConstraint, among others for example.
28   *
29   * By setting an allow flag to true you are allowing that character as a valid character in the set.
30   * AllowAll set to true will allow all characters which have a flag to be allowed in the set.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public abstract class AllowCharacterConstraint extends ValidCharactersPatternConstraint {
35      protected boolean allowWhitespace = false;
36      protected boolean omitNewline = false;
37      protected boolean allowUnderscore = false;
38      protected boolean allowPeriod = false;
39      protected boolean allowParenthesis = false;
40      protected boolean allowDollar = false;
41      protected boolean allowForwardSlash = false;
42      protected boolean allowDoubleQuote = false;
43      protected boolean allowApostrophe = false;
44      protected boolean allowComma = false;
45      protected boolean allowColon = false;
46      protected boolean allowSemiColon = false;
47      protected boolean allowQuestionMark = false;
48      protected boolean allowExclaimation = false;
49      protected boolean allowDash = false;
50      protected boolean allowPlus = false;
51      protected boolean allowEquals = false;
52      protected boolean allowAsterisk = false;
53      protected boolean allowAtSign = false;
54      protected boolean allowPercent = false;
55      protected boolean allowPound = false;
56      protected boolean allowGreaterThan = false;
57      protected boolean allowLessThan = false;
58      protected boolean allowBrackets = false;
59      protected boolean allowAmpersand = false;
60      protected boolean allowCurlyBraces = false;
61      protected boolean allowBackslash = false;
62      protected boolean allowAll = false;
63  
64      /**
65       * @return the allowWhitespace
66       */
67      public boolean isAllowWhitespace() {
68          return this.allowWhitespace;
69      }
70  
71      /**
72       * @param allowWhitespace the allowWhitespace to set
73       */
74      public void setAllowWhitespace(boolean allowWhitespace) {
75          this.allowWhitespace = allowWhitespace;
76      }
77  
78      /**
79       * @return the allowUnderscore
80       */
81      public boolean isAllowUnderscore() {
82          return this.allowUnderscore;
83      }
84  
85      /**
86       * @param allowUnderscore the allowUnderscore to set
87       */
88      public void setAllowUnderscore(boolean allowUnderscore) {
89          this.allowUnderscore = allowUnderscore;
90      }
91  
92      /**
93       * @return the allowPeriod
94       */
95      public boolean isAllowPeriod() {
96          return this.allowPeriod;
97      }
98  
99      /**
100      * @param allowPeriod the allowPeriod to set
101      */
102     public void setAllowPeriod(boolean allowPeriod) {
103         this.allowPeriod = allowPeriod;
104     }
105 
106     /**
107      * @return the allowParenthesis
108      */
109     public boolean isAllowParenthesis() {
110         return this.allowParenthesis;
111     }
112 
113     /**
114      * @param allowParenthesis the allowParenthesis to set
115      */
116     public void setAllowParenthesis(boolean allowParenthesis) {
117         this.allowParenthesis = allowParenthesis;
118     }
119 
120     /**
121      * @return the allowDollar
122      */
123     public boolean isAllowDollar() {
124         return this.allowDollar;
125     }
126 
127     /**
128      * @param allowDollar the allowDollar to set
129      */
130     public void setAllowDollar(boolean allowDollar) {
131         this.allowDollar = allowDollar;
132     }
133 
134     /**
135      * @return the allowForwardSlash
136      */
137     public boolean isAllowForwardSlash() {
138         return this.allowForwardSlash;
139     }
140 
141     /**
142      * @param allowForwardSlash the allowForwardSlash to set
143      */
144     public void setAllowForwardSlash(boolean allowForwardSlash) {
145         this.allowForwardSlash = allowForwardSlash;
146     }
147 
148     /**
149      * @return the allowDoubleQuote
150      */
151     public boolean isAllowDoubleQuote() {
152         return this.allowDoubleQuote;
153     }
154 
155     /**
156      * @param allowDoubleQuote the allowDoubleQuote to set
157      */
158     public void setAllowDoubleQuote(boolean allowDoubleQuote) {
159         this.allowDoubleQuote = allowDoubleQuote;
160     }
161 
162     /**
163      * @return the allowApostrophe
164      */
165     public boolean isAllowApostrophe() {
166         return this.allowApostrophe;
167     }
168 
169     /**
170      * @param allowApostrophe the allowApostrophe to set
171      */
172     public void setAllowApostrophe(boolean allowApostrophe) {
173         this.allowApostrophe = allowApostrophe;
174     }
175 
176     /**
177      * @return the allowComma
178      */
179     public boolean isAllowComma() {
180         return this.allowComma;
181     }
182 
183     /**
184      * @param allowComma the allowComma to set
185      */
186     public void setAllowComma(boolean allowComma) {
187         this.allowComma = allowComma;
188     }
189 
190     /**
191      * @return the allowColon
192      */
193     public boolean isAllowColon() {
194         return this.allowColon;
195     }
196 
197     /**
198      * @param allowColon the allowColon to set
199      */
200     public void setAllowColon(boolean allowColon) {
201         this.allowColon = allowColon;
202     }
203 
204     /**
205      * @return the allowSemiColon
206      */
207     public boolean isAllowSemiColon() {
208         return this.allowSemiColon;
209     }
210 
211     /**
212      * @param allowSemiColon the allowSemiColon to set
213      */
214     public void setAllowSemiColon(boolean allowSemiColon) {
215         this.allowSemiColon = allowSemiColon;
216     }
217 
218     /**
219      * @return the allowQuestionMark
220      */
221     public boolean isAllowQuestionMark() {
222         return this.allowQuestionMark;
223     }
224 
225     /**
226      * @param allowQuestionMark the allowQuestionMark to set
227      */
228     public void setAllowQuestionMark(boolean allowQuestionMark) {
229         this.allowQuestionMark = allowQuestionMark;
230     }
231 
232     /**
233      * @return the allowExclaimation
234      */
235     public boolean isAllowExclaimation() {
236         return this.allowExclaimation;
237     }
238 
239     /**
240      * @param allowExclaimation the allowExclaimation to set
241      */
242     public void setAllowExclaimation(boolean allowExclaimation) {
243         this.allowExclaimation = allowExclaimation;
244     }
245 
246     /**
247      * @return the allowDash
248      */
249     public boolean isAllowDash() {
250         return this.allowDash;
251     }
252 
253     /**
254      * @param allowDash the allowDash to set
255      */
256     public void setAllowDash(boolean allowDash) {
257         this.allowDash = allowDash;
258     }
259 
260     /**
261      * @return the allowPlus
262      */
263     public boolean isAllowPlus() {
264         return this.allowPlus;
265     }
266 
267     /**
268      * @param allowPlus the allowPlus to set
269      */
270     public void setAllowPlus(boolean allowPlus) {
271         this.allowPlus = allowPlus;
272     }
273 
274     /**
275      * @return the allowEquals
276      */
277     public boolean isAllowEquals() {
278         return this.allowEquals;
279     }
280 
281     /**
282      * @param allowEquals the allowEquals to set
283      */
284     public void setAllowEquals(boolean allowEquals) {
285         this.allowEquals = allowEquals;
286     }
287 
288     /**
289      * @return the allowAsterisk
290      */
291     public boolean isAllowAsterisk() {
292         return this.allowAsterisk;
293     }
294 
295     /**
296      * @param allowAsterisk the allowAsterisk to set
297      */
298     public void setAllowAsterisk(boolean allowAsterisk) {
299         this.allowAsterisk = allowAsterisk;
300     }
301 
302     /**
303      * @return the allowAtSign
304      */
305     public boolean isAllowAtSign() {
306         return this.allowAtSign;
307     }
308 
309     /**
310      * @param allowAtSign the allowAtSign to set
311      */
312     public void setAllowAtSign(boolean allowAtSign) {
313         this.allowAtSign = allowAtSign;
314     }
315 
316     /**
317      * @return the allowPercent
318      */
319     public boolean isAllowPercent() {
320         return this.allowPercent;
321     }
322 
323     /**
324      * @param allowPercent the allowPercent to set
325      */
326     public void setAllowPercent(boolean allowPercent) {
327         this.allowPercent = allowPercent;
328     }
329 
330     /**
331      * @return the allowPound
332      */
333     public boolean isAllowPound() {
334         return this.allowPound;
335     }
336 
337     /**
338      * @param allowPound the allowPound to set
339      */
340     public void setAllowPound(boolean allowPound) {
341         this.allowPound = allowPound;
342     }
343 
344     public boolean isAllowGreaterThan() {
345         return allowGreaterThan;
346     }
347 
348     public void setAllowGreaterThan(boolean allowGreaterThan) {
349         this.allowGreaterThan = allowGreaterThan;
350     }
351 
352     public boolean isAllowLessThan() {
353         return allowLessThan;
354     }
355 
356     public void setAllowLessThan(boolean allowLessThan) {
357         this.allowLessThan = allowLessThan;
358     }
359 
360     public boolean isAllowBrackets() {
361         return allowBrackets;
362     }
363 
364     public void setAllowBrackets(boolean allowBrackets) {
365         this.allowBrackets = allowBrackets;
366     }
367 
368     public boolean isAllowAmpersand() {
369         return allowAmpersand;
370     }
371 
372     public void setAllowAmpersand(boolean allowAmpersand) {
373         this.allowAmpersand = allowAmpersand;
374     }
375 
376     public boolean isAllowCurlyBraces() {
377         return allowCurlyBraces;
378     }
379 
380     public void setAllowCurlyBraces(boolean allowCurlyBraces) {
381         this.allowCurlyBraces = allowCurlyBraces;
382     }
383 
384     public boolean isAllowBackslash() {
385         return allowBackslash;
386     }
387 
388     public void setAllowBackslash(boolean allowBackslash) {
389         this.allowBackslash = allowBackslash;
390     }
391 
392     /**
393      * If true, this constraint will allow all symbols that have flags for them. Equivalent to
394      * setting each flag to true separately.
395      *
396      * @return the allowAll
397      */
398     public boolean isAllowAll() {
399         return this.allowAll;
400     }
401 
402     /**
403      * @param allowAll the allowAll to set
404      */
405     public void setAllowAll(boolean allowAll) {
406         this.allowAll = allowAll;
407     }
408 
409     public boolean isOmitNewline() {
410         return omitNewline;
411     }
412 
413     /**
414      * When set to true, omit new line characters from the set of valid characters.  This flag
415      * will only have an effect if the allowWhitespace flag is true, otherwise all whitespace
416      * including new lines characters are omitted.
417      *
418      * @param omitNewline
419      */
420     public void setOmitNewline(boolean omitNewline) {
421         this.omitNewline = omitNewline;
422     }
423 
424     /**
425      * This method returns the allowed set of characters allowed by this constraint, based on the
426      * flags set. This string is intended to be placed within the or set of a regex, ie between the
427      * [ ] symbols
428      *
429      * @return
430      */
431     protected String getAllowedCharacterRegex() {
432         StringBuilder regexString = new StringBuilder("");
433         if (allowWhitespace || allowAll) {
434             regexString.append("\\t\\v\\040");
435             if (!omitNewline) {
436                 regexString.append("\\f\\r\\n");
437             }
438         }
439         if (allowUnderscore || allowAll) {
440             regexString.append("_");
441         }
442         if (allowPeriod || allowAll) {
443             regexString.append(".");
444         }
445         if (allowParenthesis || allowAll) {
446             regexString.append("(");
447             regexString.append(")");
448         }
449         if (allowDollar || allowAll) {
450             regexString.append("$");
451         }
452         if (allowForwardSlash || allowAll) {
453             regexString.append("/");
454         }
455         if (allowDoubleQuote || allowAll) {
456             regexString.append("\\\"");
457         }
458         if (allowApostrophe || allowAll) {
459             regexString.append("'");
460         }
461         if (allowComma || allowAll) {
462             regexString.append(",");
463         }
464         if (allowColon || allowAll) {
465             regexString.append(":");
466         }
467         if (allowSemiColon || allowAll) {
468             regexString.append(";");
469         }
470         if (allowQuestionMark || allowAll) {
471             regexString.append("?");
472         }
473         if (allowExclaimation || allowAll) {
474             regexString.append("!");
475         }
476         if (allowDash || allowAll) {
477             regexString.append("\\-");
478         }
479         if (allowPlus || allowAll) {
480             regexString.append("+");
481         }
482         if (allowEquals || allowAll) {
483             regexString.append("=");
484         }
485         if (allowAsterisk || allowAll) {
486             regexString.append("*");
487         }
488         if (allowAtSign || allowAll) {
489             regexString.append("@");
490         }
491         if (allowPercent || allowAll) {
492             regexString.append("%");
493         }
494         if (allowPound || allowAll) {
495             regexString.append("#");
496         }
497         if (allowLessThan || allowAll) {
498             regexString.append("\\0074");
499         }
500         if (allowGreaterThan || allowAll) {
501             regexString.append("\\0076");
502         }
503         if (allowAmpersand || allowAll) {
504             regexString.append("\\0046");
505         }
506         if (allowBackslash || allowAll) {
507             regexString.append("\\0134");
508         }
509         if (allowCurlyBraces || allowAll) {
510             regexString.append("\\0173\\0175");
511         }
512         if (allowBrackets || allowAll) {
513             regexString.append("\\0133\\0135");
514         }
515         return regexString.toString();
516     }
517 
518     /**
519      * Generates a comma separated string of the allowed set of characters, for the {0} parameter to
520      * be used within its validation message
521      *
522      * @return the validationMessageParams
523      */
524     public List<String> getValidationMessageParams() {
525         if (validationMessageParams == null) {
526             validationMessageParams = new ArrayList<String>();
527             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
528             StringBuilder paramString = new StringBuilder("");
529 
530             if (allowWhitespace || allowAll) {
531                 paramString.append(", " + configService.getPropertyValueAsString(
532                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "whitespace"));
533                 if (!omitNewline) {
534                     paramString.append(", " + configService.getPropertyValueAsString(
535                             UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "newline"));
536                 }
537             }
538             if (allowUnderscore || allowAll) {
539                 paramString.append(", " + configService.getPropertyValueAsString(
540                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "underscore"));
541             }
542             if (allowPeriod || allowAll) {
543                 paramString.append(", " + configService.getPropertyValueAsString(
544                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "period"));
545             }
546             if (allowParenthesis || allowAll) {
547                 paramString.append(", " + configService.getPropertyValueAsString(
548                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "parenthesis"));
549             }
550             if (allowDollar || allowAll) {
551                 paramString.append(", " + configService.getPropertyValueAsString(
552                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "dollar"));
553             }
554             if (allowForwardSlash || allowAll) {
555                 paramString.append(", " + configService.getPropertyValueAsString(
556                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "forwardSlash"));
557             }
558             if (allowDoubleQuote || allowAll) {
559                 paramString.append(", " + configService.getPropertyValueAsString(
560                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "doubleQuote"));
561             }
562             if (allowApostrophe || allowAll) {
563                 paramString.append(", " + configService.getPropertyValueAsString(
564                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "apostrophe"));
565             }
566             if (allowComma || allowAll) {
567                 paramString.append(", " + configService.getPropertyValueAsString(
568                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "comma"));
569             }
570             if (allowColon || allowAll) {
571                 paramString.append(", " + configService.getPropertyValueAsString(
572                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "colon"));
573             }
574             if (allowSemiColon || allowAll) {
575                 paramString.append(", " + configService.getPropertyValueAsString(
576                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "semiColon"));
577             }
578             if (allowQuestionMark || allowAll) {
579                 paramString.append(", " + configService.getPropertyValueAsString(
580                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "questionMark"));
581             }
582             if (allowExclaimation || allowAll) {
583                 paramString.append(", " + configService.getPropertyValueAsString(
584                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "exclaimation"));
585             }
586             if (allowDash || allowAll) {
587                 paramString.append(", " + configService.getPropertyValueAsString(
588                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "dash"));
589             }
590             if (allowPlus || allowAll) {
591                 paramString.append(", " + configService.getPropertyValueAsString(
592                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "plus"));
593             }
594             if (allowEquals || allowAll) {
595                 paramString.append(", " + configService.getPropertyValueAsString(
596                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "equals"));
597             }
598             if (allowAsterisk || allowAll) {
599                 paramString.append(", " + configService.getPropertyValueAsString(
600                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "asterisk"));
601             }
602             if (allowAtSign || allowAll) {
603                 paramString.append(", " + configService.getPropertyValueAsString(
604                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "atSign"));
605             }
606             if (allowPercent || allowAll) {
607                 paramString.append(", " + configService.getPropertyValueAsString(
608                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "percent"));
609             }
610             if (allowPound || allowAll) {
611                 paramString.append(", " + configService.getPropertyValueAsString(
612                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "pound"));
613             }
614             if (allowLessThan || allowAll) {
615                 paramString.append(", " + configService.getPropertyValueAsString(
616                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "lessThan"));
617             }
618             if (allowGreaterThan || allowAll) {
619                 paramString.append(", " + configService.getPropertyValueAsString(
620                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "greaterThan"));
621             }
622             if (allowAmpersand || allowAll) {
623                 paramString.append(", " + configService.getPropertyValueAsString(
624                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "ampersand"));
625             }
626             if (allowBackslash || allowAll) {
627                paramString.append(", " + configService.getPropertyValueAsString(
628                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "backslash"));
629             }
630             if (allowCurlyBraces || allowAll) {
631                 paramString.append(", " + configService.getPropertyValueAsString(
632                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "curlyBraces"));
633             }
634             if (allowBrackets || allowAll) {
635                 paramString.append(", " + configService.getPropertyValueAsString(
636                         UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "brackets"));
637             }
638 
639             validationMessageParams.add(paramString.toString());
640         }
641         return this.validationMessageParams;
642     }
643 
644 }