|
@@ -1,119 +1,119 @@ |
1 |
|
- |
package com.esotericsoftware.wildcard; |
2 |
|
- |
|
3 |
|
- |
import com.esotericsoftware.utils.*; |
4 |
|
- |
|
5 |
|
- |
import java.io.*; |
6 |
|
- |
import java.util.*; |
7 |
|
- |
import java.util.regex.Pattern; |
8 |
|
- |
|
9 |
|
- |
class RegexScanner |
10 |
|
- |
{ |
11 |
|
- |
private final File rootDir; |
12 |
|
- |
private final List<Pattern> includePatterns; |
13 |
|
- |
private final List<String> matches = new ArrayList( 128 ); |
14 |
|
- |
|
15 |
|
- |
public RegexScanner( File rootDir, List<String> includes, List<String> excludes ) |
16 |
|
- |
{ |
17 |
|
- |
if ( rootDir == null ) |
18 |
|
- |
{ |
19 |
|
- |
throw new IllegalArgumentException( "rootDir cannot be null." ); |
20 |
|
- |
} |
21 |
|
- |
if ( !rootDir.exists() ) |
22 |
|
- |
{ |
23 |
|
- |
throw new IllegalArgumentException( "Directory does not exist: " + rootDir ); |
24 |
|
- |
} |
25 |
|
- |
if ( !rootDir.isDirectory() ) |
26 |
|
- |
{ |
27 |
|
- |
throw new IllegalArgumentException( "File must be a directory: " + rootDir ); |
28 |
|
- |
} |
29 |
|
- |
try |
30 |
|
- |
{ |
31 |
|
- |
rootDir = rootDir.getCanonicalFile(); |
32 |
|
- |
} |
33 |
|
- |
catch ( IOException ex ) |
34 |
|
- |
{ |
35 |
|
- |
throw new WrappedIOException( "OS error determining canonical path: " + rootDir, ex ); |
36 |
|
- |
} |
37 |
|
- |
this.rootDir = rootDir; |
38 |
|
- |
|
39 |
|
- |
if ( includes == null ) |
40 |
|
- |
{ |
41 |
|
- |
throw new IllegalArgumentException( "includes cannot be null." ); |
42 |
|
- |
} |
43 |
|
- |
if ( excludes == null ) |
44 |
|
- |
{ |
45 |
|
- |
throw new IllegalArgumentException( "excludes cannot be null." ); |
46 |
|
- |
} |
47 |
|
- |
|
48 |
|
- |
includePatterns = new ArrayList(); |
49 |
|
- |
for ( String include : includes ) |
50 |
|
- |
{ |
51 |
|
- |
includePatterns.add( Pattern.compile( include, Pattern.CASE_INSENSITIVE ) ); |
52 |
|
- |
} |
53 |
|
- |
|
54 |
|
- |
List<Pattern> excludePatterns = new ArrayList(); |
55 |
|
- |
for ( String exclude : excludes ) |
56 |
|
- |
{ |
57 |
|
- |
excludePatterns.add( Pattern.compile( exclude, Pattern.CASE_INSENSITIVE ) ); |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
scanDir( rootDir ); |
61 |
|
- |
|
62 |
|
- |
for ( Iterator matchIter = matches.iterator(); matchIter.hasNext(); ) |
63 |
|
- |
{ |
64 |
|
- |
String filePath = (String) matchIter.next(); |
65 |
|
- |
for ( Pattern exclude : excludePatterns ) |
66 |
|
- |
{ |
67 |
|
- |
if ( exclude.matcher( filePath ).matches() ) |
68 |
|
- |
{ |
69 |
|
- |
matchIter.remove(); |
70 |
|
- |
} |
71 |
|
- |
} |
72 |
|
- |
} |
73 |
|
- |
} |
74 |
|
- |
|
75 |
|
- |
private void scanDir( File dir ) |
76 |
|
- |
{ |
77 |
|
- |
for ( File file : dir.listFiles() ) |
78 |
|
- |
{ |
79 |
|
- |
for ( Pattern include : includePatterns ) |
80 |
|
- |
{ |
81 |
|
- |
String filePath = file.getPath().substring( rootDir.getPath().length() + 1 ); |
82 |
|
- |
if ( include.matcher( filePath ).matches() ) |
83 |
|
- |
{ |
84 |
|
- |
matches.add( filePath ); |
85 |
|
- |
break; |
86 |
|
- |
} |
87 |
|
- |
} |
88 |
|
- |
if ( file.isDirectory() ) |
89 |
|
- |
{ |
90 |
|
- |
scanDir( file ); |
91 |
|
- |
} |
92 |
|
- |
} |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public List<String> matches() |
96 |
|
- |
{ |
97 |
|
- |
return matches; |
98 |
|
- |
} |
99 |
|
- |
|
100 |
|
- |
public File rootDir() |
101 |
|
- |
{ |
102 |
|
- |
return rootDir; |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
public static void main( String[] args ) |
106 |
|
- |
{ |
107 |
|
- |
// System.out.println(new Paths("C:\\Java\\ls", "**")); |
108 |
|
- |
List<String> includes = new ArrayList(); |
109 |
|
- |
includes.add( "core[^T]+php" ); |
110 |
|
- |
// includes.add(".*/lavaserver/.*"); |
111 |
|
- |
List<String> excludes = new ArrayList(); |
112 |
|
- |
// excludes.add("website/**/doc**"); |
113 |
|
- |
long start = System.nanoTime(); |
114 |
|
- |
List<String> files = new RegexScanner( new File( "..\\website\\includes" ), includes, excludes ).matches(); |
115 |
|
- |
long end = System.nanoTime(); |
116 |
|
- |
System.out.println( files.toString().replaceAll( ", ", "\n" ).replaceAll( "[\\[\\]]", "" ) ); |
117 |
|
- |
System.out.println( (end - start) / 1000000f ); |
118 |
|
- |
} |
119 |
|
- |
} |
|
1 |
+ |
package com.esotericsoftware.wildcard; |
|
2 |
+ |
|
|
3 |
+ |
import com.esotericsoftware.utils.*; |
|
4 |
+ |
|
|
5 |
+ |
import java.io.*; |
|
6 |
+ |
import java.util.*; |
|
7 |
+ |
import java.util.regex.Pattern; |
|
8 |
+ |
|
|
9 |
+ |
class RegexScanner |
|
10 |
+ |
{ |
|
11 |
+ |
private final File rootDir; |
|
12 |
+ |
private final List<Pattern> includePatterns; |
|
13 |
+ |
private final List<String> matches = new ArrayList( 128 ); |
|
14 |
+ |
|
|
15 |
+ |
public RegexScanner( File rootDir, List<String> includes, List<String> excludes ) |
|
16 |
+ |
{ |
|
17 |
+ |
if ( rootDir == null ) |
|
18 |
+ |
{ |
|
19 |
+ |
throw new IllegalArgumentException( "rootDir cannot be null." ); |
|
20 |
+ |
} |
|
21 |
+ |
if ( !rootDir.exists() ) |
|
22 |
+ |
{ |
|
23 |
+ |
throw new IllegalArgumentException( "Directory does not exist: " + rootDir ); |
|
24 |
+ |
} |
|
25 |
+ |
if ( !rootDir.isDirectory() ) |
|
26 |
+ |
{ |
|
27 |
+ |
throw new IllegalArgumentException( "File must be a directory: " + rootDir ); |
|
28 |
+ |
} |
|
29 |
+ |
try |
|
30 |
+ |
{ |
|
31 |
+ |
rootDir = rootDir.getCanonicalFile(); |
|
32 |
+ |
} |
|
33 |
+ |
catch ( IOException ex ) |
|
34 |
+ |
{ |
|
35 |
+ |
throw new WrappedIOException( "OS error determining canonical path: " + rootDir, ex ); |
|
36 |
+ |
} |
|
37 |
+ |
this.rootDir = rootDir; |
|
38 |
+ |
|
|
39 |
+ |
if ( includes == null ) |
|
40 |
+ |
{ |
|
41 |
+ |
throw new IllegalArgumentException( "includes cannot be null." ); |
|
42 |
+ |
} |
|
43 |
+ |
if ( excludes == null ) |
|
44 |
+ |
{ |
|
45 |
+ |
throw new IllegalArgumentException( "excludes cannot be null." ); |
|
46 |
+ |
} |
|
47 |
+ |
|
|
48 |
+ |
includePatterns = new ArrayList(); |
|
49 |
+ |
for ( String include : includes ) |
|
50 |
+ |
{ |
|
51 |
+ |
includePatterns.add( Pattern.compile( include, Pattern.CASE_INSENSITIVE ) ); |
|
52 |
+ |
} |
|
53 |
+ |
|
|
54 |
+ |
List<Pattern> excludePatterns = new ArrayList(); |
|
55 |
+ |
for ( String exclude : excludes ) |
|
56 |
+ |
{ |
|
57 |
+ |
excludePatterns.add( Pattern.compile( exclude, Pattern.CASE_INSENSITIVE ) ); |
|
58 |
+ |
} |
|
59 |
+ |
|
|
60 |
+ |
scanDir( rootDir ); |
|
61 |
+ |
|
|
62 |
+ |
for ( Iterator matchIter = matches.iterator(); matchIter.hasNext(); ) |
|
63 |
+ |
{ |
|
64 |
+ |
String filePath = (String) matchIter.next(); |
|
65 |
+ |
for ( Pattern exclude : excludePatterns ) |
|
66 |
+ |
{ |
|
67 |
+ |
if ( exclude.matcher( filePath ).matches() ) |
|
68 |
+ |
{ |
|
69 |
+ |
matchIter.remove(); |
|
70 |
+ |
} |
|
71 |
+ |
} |
|
72 |
+ |
} |
|
73 |
+ |
} |
|
74 |
+ |
|
|
75 |
+ |
private void scanDir( File dir ) |
|
76 |
+ |
{ |
|
77 |
+ |
for ( File file : dir.listFiles() ) |
|
78 |
+ |
{ |
|
79 |
+ |
for ( Pattern include : includePatterns ) |
|
80 |
+ |
{ |
|
81 |
+ |
String filePath = file.getPath().substring( rootDir.getPath().length() + 1 ); |
|
82 |
+ |
if ( include.matcher( filePath ).matches() ) |
|
83 |
+ |
{ |
|
84 |
+ |
matches.add( filePath ); |
|
85 |
+ |
break; |
|
86 |
+ |
} |
|
87 |
+ |
} |
|
88 |
+ |
if ( file.isDirectory() ) |
|
89 |
+ |
{ |
|
90 |
+ |
scanDir( file ); |
|
91 |
+ |
} |
|
92 |
+ |
} |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
public List<String> matches() |
|
96 |
+ |
{ |
|
97 |
+ |
return matches; |
|
98 |
+ |
} |
|
99 |
+ |
|
|
100 |
+ |
public File rootDir() |
|
101 |
+ |
{ |
|
102 |
+ |
return rootDir; |
|
103 |
+ |
} |
|
104 |
+ |
|
|
105 |
+ |
public static void main( String[] args ) |
|
106 |
+ |
{ |
|
107 |
+ |
// System.out.println(new Paths("C:\\Java\\ls", "**")); |
|
108 |
+ |
List<String> includes = new ArrayList(); |
|
109 |
+ |
includes.add( "core[^T]+php" ); |
|
110 |
+ |
// includes.add(".*/lavaserver/.*"); |
|
111 |
+ |
List<String> excludes = new ArrayList(); |
|
112 |
+ |
// excludes.add("website/**/doc**"); |
|
113 |
+ |
long start = System.nanoTime(); |
|
114 |
+ |
List<String> files = new RegexScanner( new File( "..\\website\\includes" ), includes, excludes ).matches(); |
|
115 |
+ |
long end = System.nanoTime(); |
|
116 |
+ |
System.out.println( files.toString().replaceAll( ", ", "\n" ).replaceAll( "[\\[\\]]", "" ) ); |
|
117 |
+ |
System.out.println( (end - start) / 1000000f ); |
|
118 |
+ |
} |
|
119 |
+ |
} |