Coverage Report - org.kuali.rice.krad.datadictionary.validation.constraint.CharsetPatternConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
CharsetPatternConstraint
60%
15/25
33%
4/12
2.8
 
 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.constraint;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 20  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 21  
 import org.kuali.rice.krad.uif.UifConstants;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 /**
 28  
  * Pattern for matching any character in the given list (String)
 29  
  * 
 30  
  * 
 31  
  */
 32  8
 public class CharsetPatternConstraint extends ValidCharactersPatternConstraint {
 33  
     protected String validChars;
 34  
 
 35  
     /**
 36  
      * @return String containing all valid chars for this charset
 37  
      */
 38  
     public String getValidChars() {
 39  0
         return validChars;
 40  
     }
 41  
 
 42  
     /**
 43  
      * @param validChars for this charset
 44  
      */
 45  
     public void setValidChars(String validChars) {
 46  8
         if (StringUtils.isEmpty(validChars)) {
 47  0
             throw new IllegalArgumentException("invalid (empty) validChars");
 48  
         }
 49  
 
 50  8
         this.validChars = validChars;
 51  8
     }
 52  
 
 53  
 
 54  
     /**
 55  
      * Escapes every special character I could think of, to limit potential misuse of this pattern.
 56  
      * 
 57  
      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
 58  
      */
 59  
     protected String getRegexString() {
 60  4
         if (StringUtils.isEmpty(validChars)) {
 61  0
             throw new IllegalStateException("validChars is empty");
 62  
         }
 63  
 
 64  
         // filter out and escape chars which would confuse the pattern-matcher
 65  4
         Pattern filteringChars = Pattern.compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
 66  4
         String filteredChars = filteringChars.matcher(validChars).replaceAll("\\\\$1");
 67  
 
 68  4
         StringBuffer regexString = new StringBuffer("[");
 69  4
         regexString.append(filteredChars);
 70  4
         if (filteredChars.endsWith("\\")) {
 71  0
             regexString.append("\\");
 72  
         }
 73  4
         regexString.append("]");
 74  
 
 75  4
         return regexString.toString();
 76  
     }
 77  
 
 78  
         /**
 79  
          * 
 80  
          * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getLabelKey()
 81  
          */
 82  
         @Override
 83  
         public String getLabelKey() {
 84  4
                 String labelKey = super.getLabelKey();
 85  4
                 if (StringUtils.isNotEmpty(labelKey)) {
 86  0
                         return labelKey;
 87  
                 }
 88  4
                 return (UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "charsetPattern");
 89  
         }
 90  
 
 91  
     /**
 92  
      * Parameters to be used in the string retrieved by this constraint's labelKey
 93  
      * @return the validationMessageParams
 94  
      */
 95  
     public List<String> getValidationMessageParams() {
 96  0
         if(validationMessageParams == null){
 97  0
             validationMessageParams = new ArrayList<String>();
 98  0
             if (StringUtils.isNotBlank(validChars)) {
 99  0
                 validationMessageParams.add(validChars);
 100  
             }
 101  
             
 102  
         }
 103  0
         return this.validationMessageParams;
 104  
     }
 105  
 
 106  
 }