Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.glob; |
| 2 | |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 3 | import java.util.*; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 4 | import java.util.regex.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 5 | |
| 6 | public class Glob { |
| 7 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | private final String glob; |
| 9 | private final Pattern pattern; |
| 10 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | public Glob(String globString) { |
| 12 | this.glob = globString; |
| 13 | this.pattern = Pattern.compile(convertGlobToRegEx(globString)); |
| 14 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | public Matcher matcher(CharSequence input) { |
| 17 | return pattern.matcher(input); |
| 18 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 19 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 20 | @Override |
| 21 | public String toString() { |
| 22 | return glob; |
| 23 | } |
| 24 | |
| 25 | private static String convertGlobToRegEx(String line) { |
| 26 | line = line.trim(); |
| 27 | int strLen = line.length(); |
| 28 | StringBuilder sb = new StringBuilder(strLen); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | boolean escaping = false; |
| 30 | int inCurlies = 0; |
| 31 | for (char currentChar : line.toCharArray()) { |
| 32 | switch (currentChar) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | case '*' : |
| 34 | if (escaping) |
| 35 | sb.append("\\*"); |
| 36 | else |
| 37 | sb.append(".*"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | escaping = false; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | break; |
| 40 | case '?' : |
| 41 | if (escaping) |
| 42 | sb.append("\\?"); |
| 43 | else |
| 44 | sb.append('.'); |
| 45 | escaping = false; |
| 46 | break; |
| 47 | case '.' : |
| 48 | case '(' : |
| 49 | case ')' : |
| 50 | case '+' : |
| 51 | case '|' : |
| 52 | case '^' : |
| 53 | case '$' : |
| 54 | case '@' : |
| 55 | case '%' : |
| 56 | sb.append('\\'); |
| 57 | sb.append(currentChar); |
| 58 | escaping = false; |
| 59 | break; |
| 60 | case '\\' : |
| 61 | if (escaping) { |
| 62 | sb.append("\\\\"); |
| 63 | escaping = false; |
| 64 | } else |
| 65 | escaping = true; |
| 66 | break; |
| 67 | case '{' : |
| 68 | if (escaping) { |
| 69 | sb.append("\\{"); |
| 70 | } else { |
| 71 | sb.append('('); |
| 72 | inCurlies++; |
| 73 | } |
| 74 | escaping = false; |
| 75 | break; |
| 76 | case '}' : |
| 77 | if (inCurlies > 0 && !escaping) { |
| 78 | sb.append(')'); |
| 79 | inCurlies--; |
| 80 | } else if (escaping) |
| 81 | sb.append("\\}"); |
| 82 | else |
| 83 | sb.append("}"); |
| 84 | escaping = false; |
| 85 | break; |
| 86 | case ',' : |
| 87 | if (inCurlies > 0 && !escaping) { |
| 88 | sb.append('|'); |
| 89 | } else if (escaping) |
| 90 | sb.append("\\,"); |
| 91 | else |
| 92 | sb.append(","); |
| 93 | break; |
| 94 | default : |
| 95 | escaping = false; |
| 96 | sb.append(currentChar); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | return sb.toString(); |
| 100 | } |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 101 | |
| 102 | public void select(List<?> objects) { |
| 103 | for ( Iterator<?> i =objects.iterator(); i.hasNext(); ) { |
| 104 | String s = i.next().toString(); |
| 105 | if ( !matcher(s).matches()) |
| 106 | i.remove(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public static Pattern toPattern(String s) { |
| 111 | try { |
| 112 | return Pattern.compile( convertGlobToRegEx(s)); |
| 113 | } catch( Exception e) { |
| 114 | // ignore |
| 115 | } |
| 116 | return null; |
| 117 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 118 | } |