Coverage Report - org.kuali.rice.krad.datadictionary.validation.charlevel.CharsetValidationPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
CharsetValidationPattern
0%
0/30
0%
0/14
3.2
 
 1  
 /*
 2  
  * Copyright 2005-2008 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.charlevel;
 17  
 
 18  
 import java.util.regex.Pattern;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
 22  
 import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
 23  
 
 24  
 /**
 25  
  * Pattern for matching any character in the given list (String)
 26  
  * 
 27  
  * 
 28  
  */
 29  0
 public class CharsetValidationPattern extends CharacterLevelValidationPattern {
 30  
     protected String validChars;
 31  
 
 32  
     /**
 33  
      * @return String containing all valid chars for this charset
 34  
      */
 35  
     public String getValidChars() {
 36  0
         return validChars;
 37  
     }
 38  
 
 39  
     /**
 40  
      * @param validChars for this charset
 41  
      */
 42  
     public void setValidChars(String validChars) {
 43  0
         if (StringUtils.isEmpty(validChars)) {
 44  0
             throw new IllegalArgumentException("invalid (empty) validChars");
 45  
         }
 46  
 
 47  0
         this.validChars = validChars;
 48  0
     }
 49  
 
 50  
 
 51  
     /**
 52  
      * Escapes every special character I could think of, to limit potential misuse of this pattern.
 53  
      * 
 54  
      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
 55  
      */
 56  
     protected String getRegexString() {
 57  0
         if (StringUtils.isEmpty(validChars)) {
 58  0
             throw new IllegalStateException("validChars is empty");
 59  
         }
 60  
 
 61  
         // filter out and escape chars which would confuse the pattern-matcher
 62  0
         Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
 63  0
         String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
 64  
 
 65  0
         StringBuffer regexString = new StringBuffer("[");
 66  0
         regexString.append(filteredChars);
 67  0
         if (filteredChars.endsWith("\\")) {
 68  0
             regexString.append("\\");
 69  
         }
 70  0
         regexString.append("]");
 71  
 
 72  0
         return regexString.toString();
 73  
     }
 74  
 
 75  
 
 76  
     /**
 77  
      * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
 78  
      */
 79  
     public void extendExportMap(ExportMap exportMap) {
 80  0
         exportMap.set("type", "charset");
 81  
 
 82  0
         exportMap.set("validChars", getValidChars());
 83  0
     }
 84  
 
 85  
         /**
 86  
          * This overridden method ...
 87  
          * 
 88  
          * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#getValidationErrorMessageParameters(java.lang.String, java.lang.String)
 89  
          */
 90  
         @Override
 91  
         public String[] getValidationErrorMessageParameters(String attributeLabel) {
 92  
                 // build character list
 93  0
                 StringBuilder buf = new StringBuilder();
 94  0
                 for (int i = 0; i < validChars.length(); i++) {
 95  0
                         buf.append(validChars.charAt(i));
 96  0
                         if (i != validChars.length() - 1) {
 97  0
                                 buf.append(", ");
 98  
                         }
 99  
                 }
 100  0
                 String characterList = buf.toString();
 101  
                 
 102  0
                 if (getMaxLength() != -1) {
 103  0
                         return new String[] {attributeLabel, String.valueOf(getMaxLength()), characterList};
 104  
                 }
 105  0
                 if (getExactLength() != -1) {
 106  0
                         return new String[] {attributeLabel, String.valueOf(getExactLength()), characterList};
 107  
                 }
 108  0
                 return new String[] {attributeLabel, "0", characterList};
 109  
         }
 110  
 }