|
@@ -3,32 +3,26 @@ |
3 |
3 |
|
import com.esotericsoftware.utils.*; |
4 |
4 |
|
import com.esotericsoftware.wildcard.support.*; |
5 |
5 |
|
|
6 |
|
- |
public class Pattern |
7 |
|
- |
{ |
|
6 |
+ |
public class Pattern { |
8 |
7 |
|
private final String mPattern; |
9 |
8 |
|
private final DirMatcher mDirMatcher; |
10 |
9 |
|
private final FileNameMatcher mFileNameMatcher; |
11 |
10 |
|
private final boolean mAllFiles; |
12 |
11 |
|
|
13 |
|
- |
public Pattern( String pattern ) |
14 |
|
- |
{ |
|
12 |
+ |
public Pattern( String pattern ) { |
15 |
13 |
|
mPattern = clean( pattern.replace( '\\', '/' ).trim() ); |
16 |
14 |
|
int lastSep = mPattern.lastIndexOf( '/' ); |
17 |
|
- |
if ( lastSep == -1 ) |
18 |
|
- |
{ |
|
15 |
+ |
if ( lastSep == -1 ) { |
19 |
16 |
|
mDirMatcher = CurrentDirMatcher.INSTANCE; |
20 |
17 |
|
mFileNameMatcher = createFileNameMatcher( mPattern ); |
21 |
|
- |
} |
22 |
|
- |
else |
23 |
|
- |
{ |
|
18 |
+ |
} else { |
24 |
19 |
|
mDirMatcher = createDirMatcher( mPattern.substring( 0, lastSep ).trim() ); |
25 |
20 |
|
mFileNameMatcher = createFileNameMatcher( mPattern.substring( lastSep + 1 ).trim() ); |
26 |
21 |
|
} |
27 |
22 |
|
mAllFiles = AllFileNameMatcher.INSTANCE == mFileNameMatcher; |
28 |
23 |
|
} |
29 |
24 |
|
|
30 |
|
- |
public static String clean( String pattern ) |
31 |
|
- |
{ |
|
25 |
+ |
public static String clean( String pattern ) { |
32 |
26 |
|
pattern = pattern.startsWith( "/" ) ? "." + pattern : "./" + pattern; |
33 |
27 |
|
String newPattern = pattern; |
34 |
28 |
|
newPattern = Util.replace( newPattern, "?*", "*" ); |
|
@@ -38,12 +32,10 @@ |
38 |
32 |
|
newPattern = Util.replace( newPattern, "/./", "//" ); |
39 |
33 |
|
newPattern = Util.replace( newPattern, "//", "/" ); |
40 |
34 |
|
|
41 |
|
- |
if ( newPattern.endsWith( "**" ) ) |
42 |
|
- |
{ |
|
35 |
+ |
if ( newPattern.endsWith( "**" ) ) { |
43 |
36 |
|
newPattern += "/"; |
44 |
37 |
|
} |
45 |
|
- |
if ( newPattern.endsWith( "/" ) ) |
46 |
|
- |
{ |
|
38 |
+ |
if ( newPattern.endsWith( "/" ) ) { |
47 |
39 |
|
newPattern += "*"; |
48 |
40 |
|
} |
49 |
41 |
|
newPattern = processNonSlashedStarStar( newPattern ); |
|
@@ -51,17 +43,13 @@ |
51 |
43 |
|
return newPattern.substring( 2 ); |
52 |
44 |
|
} |
53 |
45 |
|
|
54 |
|
- |
private static String processNonSlashedStarStar( String pPattern ) |
55 |
|
- |
{ |
|
46 |
+ |
private static String processNonSlashedStarStar( String pPattern ) { |
56 |
47 |
|
int from = 0; |
57 |
|
- |
for ( int at; -1 != (at = pPattern.indexOf( "**", from )); from = at + 1 ) |
58 |
|
- |
{ |
59 |
|
- |
if ( pPattern.charAt( at + 2 ) != '/' ) |
60 |
|
- |
{ |
|
48 |
+ |
for ( int at; -1 != (at = pPattern.indexOf( "**", from )); from = at + 1 ) { |
|
49 |
+ |
if ( pPattern.charAt( at + 2 ) != '/' ) { |
61 |
50 |
|
pPattern = pPattern.substring( 0, at + 2 ) + "/*" + pPattern.substring( at + 2 ); |
62 |
51 |
|
} |
63 |
|
- |
if ( pPattern.charAt( at - 1 ) != '/' ) |
64 |
|
- |
{ |
|
52 |
+ |
if ( pPattern.charAt( at - 1 ) != '/' ) { |
65 |
53 |
|
pPattern = pPattern.substring( 0, at ) + "*/" + pPattern.substring( at ); |
66 |
54 |
|
at += 2; |
67 |
55 |
|
} |
|
@@ -69,33 +57,26 @@ |
69 |
57 |
|
return pPattern; |
70 |
58 |
|
} |
71 |
59 |
|
|
72 |
|
- |
private DirMatcher createDirMatcher( String pDirPattern ) |
73 |
|
- |
{ |
74 |
|
- |
if ( "".equals( pDirPattern ) || ".".equals( pDirPattern ) ) |
75 |
|
- |
{ |
|
60 |
+ |
private DirMatcher createDirMatcher( String pDirPattern ) { |
|
61 |
+ |
if ( "".equals( pDirPattern ) || ".".equals( pDirPattern ) ) { |
76 |
62 |
|
return CurrentDirMatcher.INSTANCE; |
77 |
63 |
|
} |
78 |
|
- |
if ( "**".equals( pDirPattern ) ) |
79 |
|
- |
{ |
|
64 |
+ |
if ( "**".equals( pDirPattern ) ) { |
80 |
65 |
|
return AllDirMatcher.INSTANCE; |
81 |
66 |
|
} |
82 |
|
- |
if ( !pDirPattern.contains( "*" ) && !pDirPattern.contains( "?" ) ) |
83 |
|
- |
{ |
|
67 |
+ |
if ( !pDirPattern.contains( "*" ) && !pDirPattern.contains( "?" ) ) { |
84 |
68 |
|
return new ExactDirMatcher( pDirPattern ); |
85 |
69 |
|
} |
86 |
70 |
|
String[] zDirParts = FilePathPartMatcher.SLASH.split( pDirPattern, 0 ); |
87 |
71 |
|
return (zDirParts.length == 1) ? new WildSingleDirMatcher( pDirPattern ) : new WildMultiDirMatcher( zDirParts ); |
88 |
72 |
|
} |
89 |
73 |
|
|
90 |
|
- |
private static FileNameMatcher createFileNameMatcher( String pFileNamePattern ) |
91 |
|
- |
{ |
|
74 |
+ |
private static FileNameMatcher createFileNameMatcher( String pFileNamePattern ) { |
92 |
75 |
|
pFileNamePattern = pFileNamePattern.trim(); |
93 |
|
- |
if ( pFileNamePattern.equals( "*.*" ) || pFileNamePattern.equals( "*" ) ) |
94 |
|
- |
{ |
|
76 |
+ |
if ( pFileNamePattern.equals( "*.*" ) || pFileNamePattern.equals( "*" ) ) { |
95 |
77 |
|
return AllFileNameMatcher.INSTANCE; |
96 |
78 |
|
} |
97 |
|
- |
if ( !pFileNamePattern.contains( "*" ) && !pFileNamePattern.contains( "?" ) ) |
98 |
|
- |
{ |
|
79 |
+ |
if ( !pFileNamePattern.contains( "*" ) && !pFileNamePattern.contains( "?" ) ) { |
99 |
80 |
|
return new ExactFileNameMatcher( pFileNamePattern ); |
100 |
81 |
|
} |
101 |
82 |
|
return new WildFileNameMatcher( pFileNamePattern ); |
|
@@ -106,8 +87,7 @@ |
106 |
87 |
|
* |
107 |
88 |
|
* @param dirPath !null and path separators converted to '/' |
108 |
89 |
|
*/ |
109 |
|
- |
public boolean acceptableParentDirPath( String dirPath ) |
110 |
|
- |
{ |
|
90 |
+ |
public boolean acceptableParentDirPath( String dirPath ) { |
111 |
91 |
|
return mDirMatcher.acceptableParentDir( dirPath ); |
112 |
92 |
|
} |
113 |
93 |
|
|
|
@@ -116,8 +96,7 @@ |
116 |
96 |
|
* |
117 |
97 |
|
* @param dirPath !null and path separators converted to '/' |
118 |
98 |
|
*/ |
119 |
|
- |
public boolean acceptableDirPath( String dirPath ) |
120 |
|
- |
{ |
|
99 |
+ |
public boolean acceptableDirPath( String dirPath ) { |
121 |
100 |
|
return mDirMatcher.acceptable( dirPath ); |
122 |
101 |
|
} |
123 |
102 |
|
|
|
@@ -126,8 +105,7 @@ |
126 |
105 |
|
* |
127 |
106 |
|
* @param dirPath !null and path separators converted to '/' |
128 |
107 |
|
*/ |
129 |
|
- |
public boolean matchesDirPathAndChildren( String dirPath ) |
130 |
|
- |
{ |
|
108 |
+ |
public boolean matchesDirPathAndChildren( String dirPath ) { |
131 |
109 |
|
return mAllFiles && acceptableDirPath( dirPath ); |
132 |
110 |
|
} |
133 |
111 |
|
|
|
@@ -136,13 +114,11 @@ |
136 |
114 |
|
* |
137 |
115 |
|
* @param filePath !null and path separators converted to '/' |
138 |
116 |
|
*/ |
139 |
|
- |
public boolean matchesFilePath( String filePath ) |
140 |
|
- |
{ |
|
117 |
+ |
public boolean matchesFilePath( String filePath ) { |
141 |
118 |
|
String dirPath = ""; |
142 |
119 |
|
String fileName = filePath; |
143 |
120 |
|
int lastSep = filePath.lastIndexOf( '/' ); |
144 |
|
- |
if ( lastSep != -1 ) |
145 |
|
- |
{ |
|
121 |
+ |
if ( lastSep != -1 ) { |
146 |
122 |
|
dirPath = filePath.substring( 0, lastSep ); |
147 |
123 |
|
fileName = filePath.substring( lastSep + 1 ); |
148 |
124 |
|
} |
|
@@ -150,8 +126,7 @@ |
150 |
126 |
|
} |
151 |
127 |
|
|
152 |
128 |
|
@Override |
153 |
|
- |
public String toString() |
154 |
|
- |
{ |
|
129 |
+ |
public String toString() { |
155 |
130 |
|
return mPattern; |
156 |
131 |
|
} |
157 |
132 |
|
} |