1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.StringTokenizer;
23
24
25
26
27
28
29
30
31 public class MatchPattern {
32 private final String source;
33
34 private final String regexPattern;
35
36 private final String separator;
37
38 private final String[] tokenized;
39
40 private final char[][] tokenizedChar;
41
42 private MatchPattern(String source, String separator) {
43 regexPattern = SelectorUtils.isRegexPrefixedPattern(source)
44 ? source.substring(
45 SelectorUtils.REGEX_HANDLER_PREFIX.length(),
46 source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
47 : null;
48 this.source = SelectorUtils.isAntPrefixedPattern(source)
49 ? source.substring(
50 SelectorUtils.ANT_HANDLER_PREFIX.length(),
51 source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
52 : source;
53 this.separator = separator;
54 tokenized = tokenizePathToString(this.source, separator);
55 tokenizedChar = new char[tokenized.length][];
56 for (int i = 0; i < tokenized.length; i++) {
57 tokenizedChar[i] = tokenized[i].toCharArray();
58 }
59 }
60
61 public boolean matchPath(String str, boolean isCaseSensitive) {
62 if (regexPattern != null) {
63 return str.matches(regexPattern);
64 } else {
65 return SelectorUtils.matchAntPathPattern(this, str, separator, isCaseSensitive);
66 }
67 }
68
69 boolean matchPath(String str, char[][] strDirs, boolean isCaseSensitive) {
70 if (regexPattern != null) {
71 return str.matches(regexPattern);
72 } else {
73 return SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
74 }
75 }
76
77 public boolean matchPatternStart(String str, boolean isCaseSensitive) {
78 if (regexPattern != null) {
79
80
81 return true;
82 } else {
83 String altStr = str.replace('\\', '/');
84
85 return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive)
86 || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive);
87 }
88 }
89
90 public String[] getTokenizedPathString() {
91 return tokenized;
92 }
93
94 public char[][] getTokenizedPathChars() {
95 return tokenizedChar;
96 }
97
98 public boolean startsWith(String string) {
99 return source.startsWith(string);
100 }
101
102 static String[] tokenizePathToString(String path, String separator) {
103 List<String> ret = new ArrayList<String>();
104 StringTokenizer st = new StringTokenizer(path, separator);
105 while (st.hasMoreTokens()) {
106 ret.add(st.nextToken());
107 }
108 return ret.toArray(new String[0]);
109 }
110
111 static char[][] tokenizePathToCharArray(String path, String separator) {
112 String[] tokenizedName = tokenizePathToString(path, separator);
113 char[][] tokenizedNameChar = new char[tokenizedName.length][];
114 for (int i = 0; i < tokenizedName.length; i++) {
115 tokenizedNameChar[i] = tokenizedName[i].toCharArray();
116 }
117 return tokenizedNameChar;
118 }
119
120 public static MatchPattern fromString(String source) {
121 return new MatchPattern(source, File.separator);
122 }
123 }