Coverage Report - org.mockftpserver.core.util.PatternUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
PatternUtil
88%
15/17
100%
10/10
6.667
 
 1  
 /*
 2  
  * Copyright 2008 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 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.mockftpserver.core.util;
 17  
 
 18  
 /**
 19  
  * Contains static utility methods related to pattern-matching and regular expressions.
 20  
  *
 21  
  * @author Chris Mair
 22  
  * @version $Revision: 240 $ - $Date: 2010-03-19 20:28:25 -0400 (Fri, 19 Mar 2010) $
 23  
  */
 24  
 public class PatternUtil {
 25  
 
 26  
     /**
 27  
      * Return true if the specified String contains one or more wildcard characters ('?' or '*')
 28  
      *
 29  
      * @param string - the String to check
 30  
      * @return true if the String contains wildcards
 31  
      */
 32  
     public static boolean containsWildcards(String string) {
 33  88
         return string.indexOf("*") != -1 || string.indexOf("?") != -1;
 34  
     }
 35  
 
 36  
     /**
 37  
      * Convert the specified String, optionally containing wildcards (? or *), to a regular expression String
 38  
      *
 39  
      * @param stringWithWildcards - the String to convert, optionally containing wildcards (? or *)
 40  
      * @return an equivalent regex String
 41  
      * @throws AssertionError - if the stringWithWildcards is null
 42  
      */
 43  
     public static String convertStringWithWildcardsToRegex(String stringWithWildcards) {
 44  35
         Assert.notNull(stringWithWildcards, "stringWithWildcards");
 45  
 
 46  35
         StringBuffer result = new StringBuffer();
 47  229
         for (int i = 0; i < stringWithWildcards.length(); i++) {
 48  194
             char ch = stringWithWildcards.charAt(i);
 49  194
             switch (ch) {
 50  
                 case '*':
 51  19
                     result.append(".*");
 52  19
                     break;
 53  
                 case '?':
 54  51
                     result.append('.');
 55  51
                     break;
 56  
                 case '$':
 57  
                 case '|':
 58  
                 case '[':
 59  
                 case ']':
 60  
                 case '(':
 61  
                 case ')':
 62  
                 case '.':
 63  
                 case ':':
 64  
                 case '{':
 65  
                 case '}':
 66  
                 case '\\':
 67  
                 case '^':
 68  
                 case '+':
 69  38
                     result.append('\\');
 70  38
                     result.append(ch);
 71  38
                     break;
 72  
                 default:
 73  86
                     result.append(ch);
 74  
             }
 75  
         }
 76  35
         return result.toString();
 77  
     }
 78  
 
 79  
     /**
 80  
      * Private constructor to prevent instantiation. All members are static.
 81  
      */
 82  0
     private PatternUtil() {
 83  0
     }
 84  
 
 85  
 }