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