1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 Eric Lafortune (eric (at) graphics.cornell.edu) 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 2 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package proguard.util; 22 23 import java.io.File; 24 25 /** 26 * This StringParser can create StringMatcher instances for regular expressions 27 * matching file names. The regular expressions can contain the following 28 * wildcards: 29 * '?' for a single regular file name character, 30 * '*' for any number of regular file name characters, and 31 * '**' for any number of regular file name characters or directory separator 32 * characters (always including '/'). 33 * 34 * @author Eric Lafortune 35 */ 36 public class FileNameParser implements StringParser 37 { 38 // Implementations for StringParser. 39 40 public StringMatcher parse(String regularExpression) 41 { 42 int index; 43 StringMatcher nextMatcher = new EmptyStringMatcher(); 44 45 // Look for wildcards. 46 for (index = 0; index < regularExpression.length(); index++) 47 { 48 // Is there a '**' wildcard? 49 if (regularExpression.regionMatches(index, "**", 0, 2)) 50 { 51 // Create a matcher for the wildcard and, recursively, for the 52 // remainder of the string. 53 nextMatcher = 54 new VariableStringMatcher(null, 55 null, 56 0, 57 Integer.MAX_VALUE, 58 parse(regularExpression.substring(index + 2))); 59 break; 60 } 61 62 // Is there a '*' wildcard? 63 else if (regularExpression.charAt(index) == '*') 64 { 65 // Create a matcher for the wildcard and, recursively, for the 66 // remainder of the string. 67 nextMatcher = 68 new VariableStringMatcher(null, 69 new char[] { File.pathSeparatorChar, '/' }, 70 0, 71 Integer.MAX_VALUE, 72 parse(regularExpression.substring(index + 1))); 73 break; 74 } 75 76 // Is there a '?' wildcard? 77 else if (regularExpression.charAt(index) == '?') 78 { 79 // Create a matcher for the wildcard and, recursively, for the 80 // remainder of the string. 81 nextMatcher = 82 new VariableStringMatcher(null, 83 new char[] { File.pathSeparatorChar, '/' }, 84 1, 85 1, 86 parse(regularExpression.substring(index + 1))); 87 break; 88 } 89 } 90 91 // Return a matcher for the fixed first part of the regular expression, 92 // if any, and the remainder. 93 return index != 0 ? 94 (StringMatcher)new FixedStringMatcher(regularExpression.substring(0, index), nextMatcher) : 95 (StringMatcher)nextMatcher; 96 } 97 98 99 /** 100 * A main method for testing file name matching. 101 */ 102 public static void main(String[] args) 103 { 104 try 105 { 106 System.out.println("Regular expression ["+args[0]+"]"); 107 FileNameParser parser = new FileNameParser(); 108 StringMatcher matcher = parser.parse(args[0]); 109 for (int index = 1; index < args.length; index++) 110 { 111 String string = args[index]; 112 System.out.print("String ["+string+"]"); 113 System.out.println(" -> match = "+matcher.matches(args[index])); 114 } 115 } 116 catch (Exception ex) 117 { 118 ex.printStackTrace(); 119 } 120 } 121 } 122