1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.finder; |
18 | |
|
19 | |
import java.util.Collection; |
20 | |
import java.io.File; |
21 | |
import java.util.Map; |
22 | |
import java.util.Iterator; |
23 | |
import org.apache.commons.finder.filters.PathFilter; |
24 | |
import org.apache.commons.finder.filters.RegexFilter; |
25 | |
import org.apache.commons.finder.filters.SizeFilter; |
26 | |
|
27 | |
import org.apache.commons.io.IOCase; |
28 | |
import org.apache.commons.io.filefilter.WildcardFileFilter; |
29 | |
import org.apache.commons.io.filefilter.IOFileFilter; |
30 | |
import org.apache.commons.io.filefilter.AgeFileFilter; |
31 | |
import org.apache.commons.io.filefilter.NotFileFilter; |
32 | |
import org.apache.commons.io.filefilter.AndFileFilter; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class FindingFilter extends AndFileFilter { |
47 | |
|
48 | |
private static final long MINUTE = 60000L; |
49 | |
private static final long DAY = MINUTE * 60L * 24L; |
50 | |
|
51 | |
|
52 | |
private boolean daystart; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public FindingFilter(Map options) { |
60 | 15 | super(); |
61 | 15 | this.daystart = options.containsKey(Finder.DAYSTART); |
62 | 15 | Collection entries = options.entrySet(); |
63 | 15 | Iterator itr = entries.iterator(); |
64 | 45 | while(itr.hasNext()) { |
65 | 30 | Map.Entry entry = (Map.Entry)itr.next(); |
66 | 30 | if( entry.getKey().equals(Finder.DAYSTART) ) { |
67 | 0 | continue; |
68 | |
} |
69 | 30 | Object key = entry.getKey(); |
70 | 30 | if (key instanceof IOFileFilter) { |
71 | 8 | addFileFilter((IOFileFilter)key); |
72 | 8 | } else { |
73 | 22 | IOFileFilter filter = createFilter(key.toString(), entry.getValue()); |
74 | 22 | if (filter != null) { |
75 | 22 | addFileFilter(filter); |
76 | |
} |
77 | |
} |
78 | 30 | } |
79 | 15 | } |
80 | |
|
81 | |
private IOFileFilter createFilter(String option, Object argument) { |
82 | |
|
83 | 22 | if (argument instanceof IOFileFilter) { |
84 | 0 | return (IOFileFilter)argument; |
85 | |
} |
86 | |
|
87 | 22 | boolean invert = false; |
88 | 22 | if( option.startsWith(Finder.NOT) ) { |
89 | 15 | invert = true; |
90 | |
|
91 | 15 | option = option.substring(Finder.NOT.length()); |
92 | |
} |
93 | 22 | if( option.equals(Finder.MIN) ) { |
94 | 0 | int minutes = toInt(argument); |
95 | 0 | return new AgeFileFilter((long)minutes * MINUTE, invert); |
96 | |
} |
97 | 22 | if( option.equals(Finder.NEWER) ) { |
98 | 0 | File refFile = argument instanceof File ? (File)argument : new File(argument.toString()); |
99 | 0 | return new AgeFileFilter(refFile, invert); |
100 | |
} |
101 | 22 | if( option.equals(Finder.TIME) ) { |
102 | 0 | int days = toInt(argument); |
103 | 0 | return new AgeFileFilter((long)days * DAY, invert); |
104 | |
} |
105 | 22 | if( option.equals(Finder.SIZE) ) { |
106 | 1 | IOFileFilter filter = new SizeFilter(argument.toString()); |
107 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
108 | |
} |
109 | 21 | if( option.equals(Finder.NAME) ) { |
110 | 1 | IOFileFilter filter = new WildcardFileFilter(argument.toString(), IOCase.SENSITIVE); |
111 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
112 | |
} |
113 | 20 | if( option.equals(Finder.INAME) ) { |
114 | 1 | IOFileFilter filter = new WildcardFileFilter(argument.toString(), IOCase.INSENSITIVE); |
115 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
116 | |
} |
117 | 19 | if( option.equals(Finder.PATH) ) { |
118 | 16 | IOFileFilter filter = new PathFilter(argument.toString(), false); |
119 | 16 | return (invert ? new NotFileFilter(filter) : filter); |
120 | |
} |
121 | 3 | if( option.equals(Finder.IPATH) ) { |
122 | 1 | IOFileFilter filter = new PathFilter(argument.toString(), true); |
123 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
124 | |
} |
125 | 2 | if( option.equals(Finder.REGEX) ) { |
126 | 1 | IOFileFilter filter = new RegexFilter(argument.toString(), false); |
127 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
128 | |
} |
129 | 1 | if( option.equals(Finder.IREGEX) ) { |
130 | 1 | IOFileFilter filter = new RegexFilter(argument.toString(), true); |
131 | 1 | return (invert ? new NotFileFilter(filter) : filter); |
132 | |
} |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | |
public boolean isDaystartConfigured() { |
137 | 0 | return this.daystart; |
138 | |
} |
139 | |
|
140 | |
private int toInt(Object argument) { |
141 | 0 | if (argument instanceof Number) { |
142 | 0 | return((Number)argument).intValue(); |
143 | |
} else { |
144 | |
try { |
145 | 0 | return Integer.parseInt(argument.toString()); |
146 | 0 | } catch(NumberFormatException nfe) { |
147 | 0 | throw new IllegalArgumentException("Argument " + argument + " must be an integer."); |
148 | |
} |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
} |