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