View Javadoc

1   /*
2    * Copyright 2006-2007 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.mask;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  /**
21   * The maskTo element is to used hide the beginning part of the value for
22   * unauthorized users. The number of leading characters to hide and the
23   * replacement character can be specified.
24   * 
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   */
27  public class MaskFormatterSubString implements MaskFormatter {
28      private static final long serialVersionUID = -876112522775686636L;
29      
30      protected String maskCharacter = "*";
31      protected int maskLength;
32  
33      public String maskValue(Object value) {
34          if (value == null) {
35              return null;
36          }
37  
38          // TODO: MOVE TO UNIT TEST: move this validation into the unit tests
39          if (maskCharacter == null) {
40              throw new RuntimeException("Mask character not specified. Check DD maskTo attribute.");
41          }
42  
43          String strValue = value.toString();
44          if (strValue.length() < maskLength) {
45          	return StringUtils.repeat(maskCharacter, maskLength);
46          }
47          if(maskLength >0){
48          	return StringUtils.repeat(maskCharacter, maskLength) + strValue.substring(maskLength);
49          }else{
50          	return strValue;
51          }
52      }
53  
54      /**
55       * Gets the maskCharacter attribute.
56       * 
57       * @return Returns the maskCharacter.
58       */
59      public String getMaskCharacter() {
60          return maskCharacter;
61      }
62  
63      /**
64       * Specify the character with which to mask the original value.
65       *
66       * @param maskCharacter for masking values
67       */
68      public void setMaskCharacter(String maskCharacter) {
69          this.maskCharacter = maskCharacter;
70      }
71  
72      /**
73       * Gets the maskLength attribute.
74       * 
75       * @return Returns the maskLength.
76       */
77      public int getMaskLength() {
78          return maskLength;
79      }
80  
81      /**
82       * Set the number of characters to mask at the beginning of the string.
83       * 
84       * @param maskLength The maskLength to set.
85       */
86      public void setMaskLength(int maskLength) {
87          this.maskLength = maskLength;
88      }
89  
90  
91  }