1 | |
package org.apache.maven.shared.utils.io; |
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 | 167 | { |
48 | 167 | regexPattern = SelectorUtils.isRegexPrefixedPattern( source ) ? source.substring( |
49 | |
SelectorUtils.REGEX_HANDLER_PREFIX.length(), |
50 | |
source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length() ) : null; |
51 | 167 | 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 | 167 | this.separator = separator; |
57 | 167 | tokenized = tokenizePathToString( this.source, separator ); |
58 | 167 | } |
59 | |
|
60 | |
|
61 | |
public boolean matchPath( String str, boolean isCaseSensitive ) |
62 | |
{ |
63 | 1 | if ( regexPattern != null ) |
64 | |
{ |
65 | 0 | return str.matches( regexPattern ); |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 1 | return SelectorUtils.matchAntPathPattern( this, str, separator, isCaseSensitive ); |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
boolean matchPath( String str, String[] strDirs, boolean isCaseSensitive ) |
74 | |
{ |
75 | 851 | if ( regexPattern != null ) |
76 | |
{ |
77 | 0 | return str.matches( regexPattern ); |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | 851 | return SelectorUtils.matchAntPathPattern( getTokenizedPathString(), strDirs, isCaseSensitive ); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public boolean matchPatternStart( @Nonnull String str, boolean isCaseSensitive ) |
86 | |
{ |
87 | 8 | if ( regexPattern != null ) |
88 | |
{ |
89 | |
|
90 | |
|
91 | 0 | return true; |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 8 | String altStr = source.replace( '\\', '/' ); |
96 | |
|
97 | 8 | return SelectorUtils.matchAntPathPatternStart( this, str, File.separator, isCaseSensitive ) |
98 | |
|| SelectorUtils.matchAntPathPatternStart( this, altStr, "/", isCaseSensitive ); |
99 | |
} |
100 | |
} |
101 | |
|
102 | |
public String[] getTokenizedPathString() |
103 | |
{ |
104 | 860 | return tokenized; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
public boolean startsWith( String string ) |
109 | |
{ |
110 | 9 | return source.startsWith( string ); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
static String[] tokenizePathToString( @Nonnull String path, @Nonnull String separator ) |
115 | |
{ |
116 | 290 | List<String> ret = new ArrayList<String>(); |
117 | 290 | StringTokenizer st = new StringTokenizer( path, separator ); |
118 | 814 | while ( st.hasMoreTokens() ) |
119 | |
{ |
120 | 524 | ret.add( st.nextToken() ); |
121 | |
} |
122 | 290 | return ret.toArray( new String[ret.size()] ); |
123 | |
} |
124 | |
|
125 | |
public static MatchPattern fromString( String source ) |
126 | |
{ |
127 | 167 | return new MatchPattern( source, File.separator ); |
128 | |
} |
129 | |
|
130 | |
} |