Subversion Repository Public Repository

litesoft

Diff Revisions 958 vs 959 for /trunk/Java/ScarPlus/src/com/esotericsoftware/wildcard/RegexScanner.java

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