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