Coverage Report - org.apache.commons.finder.FindingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
FindingFilter
75%
45/60
66%
33/50
9
 
 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  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  
             // knows that option is a String. Bad. Needs an Enum?
 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  
 }