Coverage Report - org.kuali.rice.kns.datadictionary.validation.CharacterLevelValidationPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
CharacterLevelValidationPattern
0%
0/45
0%
0/22
2.5
 
 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.kns.datadictionary.validation;
 17  
 
 18  
 import java.util.regex.Pattern;
 19  
 
 20  
 import org.kuali.rice.kns.datadictionary.exporter.ExportMap;
 21  
 import org.kuali.rice.kns.util.KNSConstants;
 22  
 
 23  
 /**
 24  
  * Abstraction of the regular expressions used to validate attribute values.
 25  
  * 
 26  
  * 
 27  
  */
 28  0
 abstract public class CharacterLevelValidationPattern extends ValidationPattern {
 29  
     protected Pattern regexPattern;
 30  
 
 31  0
     protected int maxLength = -1;
 32  0
     protected int exactLength = -1;
 33  
 
 34  
     /**
 35  
      * Sets maxLength parameter for the associated regex.
 36  
      * 
 37  
      * @param maxLength
 38  
      */
 39  
     public void setMaxLength(int maxLength) {
 40  0
         if (this.exactLength != -1) {
 41  0
             throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
 42  
         }
 43  
 
 44  0
         this.maxLength = maxLength;
 45  0
     }
 46  
 
 47  
     /**
 48  
      * @return current maxLength, or -1 if none has been set
 49  
      */
 50  
     public int getMaxLength() {
 51  0
         return maxLength;
 52  
     }
 53  
 
 54  
 
 55  
     /**
 56  
      * Sets exactLength parameter for the associated regex.
 57  
      * 
 58  
      * @param exactLength
 59  
      */
 60  
     public void setExactLength(int exactLength) {
 61  0
         if (this.maxLength != -1) {
 62  0
             throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
 63  
         }
 64  
 
 65  0
         this.exactLength = exactLength;
 66  0
     }
 67  
 
 68  
     /**
 69  
      * @return current exactLength, or -1 if none has been set
 70  
      */
 71  
     public int getExactLength() {
 72  0
         return exactLength;
 73  
     }
 74  
 
 75  
 
 76  
     /**
 77  
      * @return regular expression Pattern generated using the individual ValidationPattern subclass
 78  
      */
 79  
     final public Pattern getRegexPattern() {
 80  0
         if ( regexPattern == null ) {
 81  0
             String regexString = getRegexString();
 82  
     
 83  0
             StringBuffer completeRegex = new StringBuffer("^");
 84  0
             completeRegex.append(getRegexString());
 85  
     
 86  0
             if (maxLength != -1) {
 87  0
                 completeRegex.append("{0," + maxLength + "}");
 88  
             }
 89  0
             else if (exactLength != -1) {
 90  0
                 completeRegex.append("{" + exactLength + "}");
 91  
             }
 92  
             else {
 93  0
                 completeRegex.append("*");
 94  
             }
 95  
     
 96  0
             completeRegex.append("$");
 97  
     
 98  0
             regexPattern = Pattern.compile(completeRegex.toString());
 99  
         }
 100  0
         return regexPattern;
 101  
     }
 102  
 
 103  
 
 104  
     /**
 105  
      * @see org.kuali.rice.kns.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
 106  
      */
 107  
     final public ExportMap buildExportMap(String exportKey) {
 108  0
         ExportMap exportMap = new ExportMap(exportKey);
 109  
 
 110  0
         if (getMaxLength() != -1) {
 111  0
             exportMap.set("maxLength", Integer.toString(getMaxLength()));
 112  
         }
 113  0
         else if (getExactLength() != -1) {
 114  0
             exportMap.set("exactLength", Integer.toString(getExactLength()));
 115  
         }
 116  
 
 117  0
         extendExportMap(exportMap);
 118  
 
 119  0
         return exportMap;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Extends the given (parent class) exportMap as needed to represent subclass instances
 124  
      * 
 125  
      * @param exportMap
 126  
      */
 127  
     abstract public void extendExportMap(ExportMap exportMap);
 128  
 
 129  
         @Override
 130  
         public String[] getValidationErrorMessageParameters(String attributeLabel) {
 131  0
                 if (getMaxLength() != -1) {
 132  0
                         return new String[] {attributeLabel, String.valueOf(getMaxLength())};
 133  
                 }
 134  0
                 if (getExactLength() != -1) {
 135  0
                         return new String[] {attributeLabel, String.valueOf(getExactLength())};
 136  
                 }
 137  0
                 return new String[] {attributeLabel};
 138  
         }
 139  
 
 140  
         /**
 141  
          * This overridden method ...
 142  
          * 
 143  
          * @see org.kuali.rice.kns.datadictionary.validation.ValidationPattern#getValidationErrorMessageKey()
 144  
          */
 145  
         @Override
 146  
         public String getValidationErrorMessageKey() {
 147  0
                 StringBuilder buf = new StringBuilder();
 148  0
                 buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions());
 149  0
                 if (getMaxLength() != -1) {
 150  0
                         buf.append(".maxLength");
 151  
                 }
 152  0
                 if (getExactLength() != -1) {
 153  0
                         buf.append(".exactLength");
 154  
                 }        
 155  0
                 return buf.toString();
 156  
         }
 157  
         
 158  
         protected String getValidationErrorMessageKeyOptions() {
 159  0
                 return KNSConstants.EMPTY_STRING;
 160  
         }
 161  
 }