View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Filters that can check whether to include a file in the response or not.
36   * <p>
37   * This is where most of the find functionality occurs. Nearly every option 
38   * to find is mapped to a FileFilter, which are then chained together inside 
39   * this class.
40   * 
41   * @author Henri Yandell
42   * @author Martin Kompf
43   * @version $Id: FindingFilter.java 437543 2006-08-28 05:47:51Z bayard $
44   * @since 1.1
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      /** The daystart flag. */
52      private boolean daystart;
53  
54      /**
55       * Constructor.
56       * 
57       * @param options  the options to use
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              // knows that option is a String. Bad. Needs an Enum?
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 }