KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > FileChecker


1 package org.enhydra.snapper.business;
2
3 import java.io.File JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6
7
8
9 /**
10  * FileChecker
11  * <p>
12  * Class that checks size, age and type of the file qued for indexing.
13  * Maximal <code>size</code> and <code>age</code> as well as file <code>types</code>
14  * allowed for the site beeing indexed are stored in the SITES table in the database.
15  *
16  * returns true if the file can
17  */

18
19 public class FileChecker {
20     long size;
21     Date JavaDoc age;
22     Calendar JavaDoc c1;
23     boolean doc, html, msg, pdf, rtf, txt, xls, ppt, pps, eml, zip, sxw, sxc, other;
24     
25     public FileChecker(){
26         this.size = -1;
27         this.age = new Date JavaDoc(0);
28         
29         }
30
31     public FileChecker(int tAge, int size, boolean doc, boolean html,
32             boolean msg, boolean pdf, boolean rtf, boolean txt, boolean xls,
33             boolean ppt, boolean pps, boolean eml, boolean zip, boolean sxw, boolean sxc, boolean other){
34         this.doc = doc;
35         this.html =html;
36         this.msg = msg;
37         this.pdf = pdf;
38         this.rtf = rtf;
39         this.txt = txt;
40         this.xls = xls;
41         this.ppt = ppt;
42         this.pps = pps;
43         this.eml = zip;
44         this.zip = zip;
45         this.sxw = sxw;
46         this.sxc = sxc;
47         this.other = other;
48         
49         c1 = Calendar.getInstance();
50         c1.setTimeInMillis(System.currentTimeMillis());
51         c1.add(Calendar.DATE, -tAge);
52         //System.out.println("c1: " + c1.getTime());
53

54         this.size = size * 1024;
55     }
56     
57     public boolean check(File JavaDoc file){
58         if (file.isDirectory()) return false;
59         String JavaDoc name = file.getName();
60         String JavaDoc type = null;
61         int i = name.lastIndexOf('.');
62         if (i > 0 && i < name.length() - 1) {
63             type = name.substring(i+1).toLowerCase();
64         }
65         else
66             type = "";
67         
68         Date JavaDoc fileAge = new Date JavaDoc(file.lastModified());
69
70         try{
71         if (fileAge.after(c1.getTime()) && (file.length() < size)){
72             
73         if (type.equals("doc")){
74                 if (doc == true)
75                     return true;
76                 }
77         else if (type.equals("html") || type.equals("htm") ){
78             if (html == true)
79                 return true;
80             }
81         
82         else if (type.equals("msg")){
83             if (msg == true)
84                 return true;
85             }
86         
87         else if (type.equals("pdf")){
88             if (pdf == true)
89                 return true;
90             }
91         
92         else if (type.equals("rtf")){
93             if (rtf == true)
94                 return true;
95             }
96         
97         else if (type.equals("txt")){
98             if (txt == true)
99                 return true;
100             }
101         
102         else if (type.equals("xls")){
103             if (xls == true)
104                 return true;
105             }
106
107         else if (type.equals("ppt")){
108             if (ppt == true)
109                 return true;
110             }
111         else if (type.equals("pps")){
112             if (pps == true)
113                 return true;
114             }
115         else if (type.equals("eml")){
116             if (eml == true)
117                 return true;
118             }
119         else if (type.equals("zip")){
120             if (zip == true)
121                 return true;
122             }
123         else if (type.equals("sxw")){
124             if (sxw == true)
125                 return true;
126             }
127         else if (type.equals("sxc")){
128             if (sxc == true)
129                 return true;
130             }
131         else if (other == true)
132                 return true;
133             
134         }
135             
136         } catch (Exception JavaDoc ex) {
137             System.out.println(ex);
138             //ex.printStackTrace();
139
}
140         
141             return false;
142         
143     }
144 }
Popular Tags