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