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