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 super();
61 this.daystart = options.containsKey(Finder.DAYSTART);
62 Collection entries = options.entrySet();
63 Iterator itr = entries.iterator();
64 while(itr.hasNext()) {
65 Map.Entry entry = (Map.Entry)itr.next();
66 if( entry.getKey().equals(Finder.DAYSTART) ) {
67 continue;
68 }
69 Object key = entry.getKey();
70 if (key instanceof IOFileFilter) {
71 addFileFilter((IOFileFilter)key);
72 } else {
73 IOFileFilter filter = createFilter(key.toString(), entry.getValue());
74 if (filter != null) {
75 addFileFilter(filter);
76 }
77 }
78 }
79 }
80
81 private IOFileFilter createFilter(String option, Object argument) {
82
83 if (argument instanceof IOFileFilter) {
84 return (IOFileFilter)argument;
85 }
86
87 boolean invert = false;
88 if( option.startsWith(Finder.NOT) ) {
89 invert = true;
90
91 option = option.substring(Finder.NOT.length());
92 }
93 if( option.equals(Finder.MIN) ) {
94 int minutes = toInt(argument);
95 return new AgeFileFilter((long)minutes * MINUTE, invert);
96 }
97 if( option.equals(Finder.NEWER) ) {
98 File refFile = argument instanceof File ? (File)argument : new File(argument.toString());
99 return new AgeFileFilter(refFile, invert);
100 }
101 if( option.equals(Finder.TIME) ) {
102 int days = toInt(argument);
103 return new AgeFileFilter((long)days * DAY, invert);
104 }
105 if( option.equals(Finder.SIZE) ) {
106 IOFileFilter filter = new SizeFilter(argument.toString());
107 return (invert ? new NotFileFilter(filter) : filter);
108 }
109 if( option.equals(Finder.NAME) ) {
110 IOFileFilter filter = new WildcardFileFilter(argument.toString(), IOCase.SENSITIVE);
111 return (invert ? new NotFileFilter(filter) : filter);
112 }
113 if( option.equals(Finder.INAME) ) {
114 IOFileFilter filter = new WildcardFileFilter(argument.toString(), IOCase.INSENSITIVE);
115 return (invert ? new NotFileFilter(filter) : filter);
116 }
117 if( option.equals(Finder.PATH) ) {
118 IOFileFilter filter = new PathFilter(argument.toString(), false);
119 return (invert ? new NotFileFilter(filter) : filter);
120 }
121 if( option.equals(Finder.IPATH) ) {
122 IOFileFilter filter = new PathFilter(argument.toString(), true);
123 return (invert ? new NotFileFilter(filter) : filter);
124 }
125 if( option.equals(Finder.REGEX) ) {
126 IOFileFilter filter = new RegexFilter(argument.toString(), false);
127 return (invert ? new NotFileFilter(filter) : filter);
128 }
129 if( option.equals(Finder.IREGEX) ) {
130 IOFileFilter filter = new RegexFilter(argument.toString(), true);
131 return (invert ? new NotFileFilter(filter) : filter);
132 }
133 return null;
134 }
135
136 public boolean isDaystartConfigured() {
137 return this.daystart;
138 }
139
140 private int toInt(Object argument) {
141 if (argument instanceof Number) {
142 return((Number)argument).intValue();
143 } else {
144 try {
145 return Integer.parseInt(argument.toString());
146 } catch(NumberFormatException nfe) {
147 throw new IllegalArgumentException("Argument " + argument + " must be an integer.");
148 }
149 }
150 }
151
152 }