| MyFileFilter.java |
1 /**
2 *
3 * @author robert r.
4 * @version
5 * use the FilenameFilter interface and provide implementation for FnF.accepts()
6 * Rest of behavior comes from the RE class procured from java.util.regex package.
7 * The client has file namespace pattern reqmts and creates REF new with a regex as parm.
8 * Then the client calls listFiles(FnF) which dispatches callBacks to REF.accepts()
9 */
10/*
11 * Copyright (C) 2003 robert rowntree
12
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22
23 * ref. http://www.gnu.org/copyleft/gpl.html
24*/
25
26package com.borneo.util;
27
28import java.io.File;
29import java.io.FilenameFilter;
30import java.util.Date;
31import java.util.regex.Matcher;
32import java.util.regex.Pattern;
33import javax.servlet.http.HttpSession;
34import javax.servlet.http.HttpSessionEvent;
35import org.apache.log4j.*;
36
37public class MyFileFilter
38 implements FilenameFilter
39{
40 /**
41 * Constructs a default Filter for filenames only
42 * @param pattern a Regular Expression according to the rules in package util.regex
43 * @throws REException thrown when the string pattern was not a well formed RE. The util.regex includes an applet to test the regular expression strings.
44 */
45 public MyFileFilter(Object pattern)
46 {
47 myPattern = Pattern.compile(pattern.toString());
48 modDate = null;
49 }
50
51 public MyFileFilter(HttpSession session)
52 {
53 String str =
54 session.getId().substring(
55 new Integer(
56 POProperty.get(
57 "rename.session.id.disp.from"
58 )
59 ).intValue()
60 ,
61 new Integer(
62 POProperty.get(
63 "rename.session.id.disp.to"
64 )
65 )
66 .intValue()
67 ) + ".*";
68
69 myPattern = Pattern.compile(str);
70 modDate = null;
71 }
72 /** Constructs a filter from an HttpSession and 2 ints.
73 * @param HttpSession
74 * @param the start displacement in the sessionId to use
75 * @param the end displacement in the sessionId to use
76 * @throws REException indicates a string that was not a well formed RE
77 */
78 public MyFileFilter(Object arg0, int arg1, int arg2)
79 {
80 if(javax.servlet.http.HttpSessionEvent.class.isInstance(arg0))
81 {
82 HttpSessionEvent sessEvnt = (HttpSessionEvent)arg0;
83 myPattern = Pattern.compile(sessEvnt.getSession().getId().substring(arg1, arg2) + ".*");
84 modDate = null;
85 log.debug("creating sessionFileFilter: " + sessEvnt.getSession().getId().substring(arg1, arg2) + ".*");
86 }
87 }
88 /** Return true if this file matches the pattern in the RE instantiated
89 * by the client. If "this" had a date passed in to the constructor then modDate is active not null
90 * With an active modDate, you must also get a File instance from the parms f and fn
91 * and get the lastModified from that file for comparison with modDate.
92 * The file will pass the test if it's newer than modDate
93 * @see FileFilter#accept
94 * @param f the directory portion of the full pathname
95 * @param fn just the name.typ portion. does not include directory information.
96 * @return the pattern matched the name portion.
97 * the pattern matched on name and the lastModified asDate was greater than modDate instance variable
98 */
99 public boolean accept(File f, String fn)
00 {
01 if(log.isDebugEnabled())
02 log.debug("checking file : " + f + System.getProperty("file.separator") + fn);
03 if(f != null && fn != null)
04 {
05 Matcher m = myPattern.matcher(fn);
06 return (m.matches() && bymodDate(f, fn) && byfilsize(f, fn));
07 /* if(m.matches())
08 if(cmpdte != null)
09 return (new Date((new File(f, fn)).lastModified())).after(cmpdte);
10 else
11 return true;
12 */
13 }
14 return false;
15 }
16
17 private boolean bymodDate(File f, String fname){
18 if (modDate == null) {
19 return true;
20 }
21 else{
22 return (
23 new Date(
24 (new File(f, fname))
25 .lastModified())).after(modDate);
26 }
27 }
28
29 private boolean byfilsize(File f, String fname){
30 if (size == -1) {
31 return true;
32 }
33 else{
34 return new File(f, fname).length() < size;
35 }
36 }
37
38 protected static Logger log;
39 private Pattern myPattern;
40 // date last modified comparator
41 public Date modDate;
42 // size of the file comparator
43 public long size = -1;
44
45 static
46 {
47 log = Logger.getLogger(com.borneo.util.MyFileFilter.class.getName());
48 PropertyConfigurator.configure(com.borneo.util.MyFileFilter.class.getClassLoader().getResource("config/log4j.properties"));
49 }
50 /**
51 * @return
52 */
53 public Date getModDate() {
54 return modDate;
55 }
56
57 /**
58 * @return
59 */
60 public long getSize() {
61 return size;
62 }
63
64 /**
65 * @param date
66 */
67 public void setModDate(Date date) {
68 modDate = date;
69 }
70
71 /**
72 * @param l
73 */
74 public void setSize(long l) {
75 size = l;
76 }
77
78}
79