blob: 6202524408bb82edd97c5a558a49057c8e5c3e49 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.glob;
2
Stuart McCulloch81d48de2012-06-29 19:23:09 +00003import java.util.*;
Stuart McCulloch2286f232012-06-15 13:27:53 +00004import java.util.regex.*;
Stuart McCullochbb014372012-06-07 21:57:32 +00005
6public class Glob {
7
Stuart McCulloch2286f232012-06-15 13:27:53 +00008 private final String glob;
9 private final Pattern pattern;
10
Stuart McCullochbb014372012-06-07 21:57:32 +000011 public Glob(String globString) {
12 this.glob = globString;
13 this.pattern = Pattern.compile(convertGlobToRegEx(globString));
14 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000015
Stuart McCullochbb014372012-06-07 21:57:32 +000016 public Matcher matcher(CharSequence input) {
17 return pattern.matcher(input);
18 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000019
Stuart McCullochbb014372012-06-07 21:57:32 +000020 @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 McCullochbb014372012-06-07 21:57:32 +000029 boolean escaping = false;
30 int inCurlies = 0;
31 for (char currentChar : line.toCharArray()) {
32 switch (currentChar) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000033 case '*' :
34 if (escaping)
35 sb.append("\\*");
36 else
37 sb.append(".*");
Stuart McCullochbb014372012-06-07 21:57:32 +000038 escaping = false;
Stuart McCulloch2286f232012-06-15 13:27:53 +000039 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 McCullochbb014372012-06-07 21:57:32 +000097 }
98 }
99 return sb.toString();
100 }
Stuart McCulloch81d48de2012-06-29 19:23:09 +0000101
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 McCullochbb014372012-06-07 21:57:32 +0000118}