1 /**
2  * 
3  */
4 /*
5  * Copyright (C) 2003  robert rowntree
6 
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16
17 * ref. http://www.gnu.org/copyleft/gpl.html
18*/
19package com.borneo.util;
20
21import java.io.File;
22import java.text.SimpleDateFormat;
23import java.text.ParseException;
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.Date;
28import java.util.TimeZone;
29
30import javax.xml.namespace.QName;
31
32import org.apache.axis.encoding.ser.DateDeserializer;
33import org.apache.log4j.Logger;
34import org.apache.log4j.PropertyConfigurator;
35
36// Referenced classes of package com.borneo.util:
37//            POProperty, MyFileFilter
38
39public class FileSelector
40{
41
42    public FileSelector()
43    {
44    }
45
46    public static Collection getFiles(String pattern)
47    {
48        return Arrays.asList((new File(POProperty.get("data.source.sourcedirectory"))).listFiles(new MyFileFilter(pattern)));
49    }
50
51    public static Collection getFiles(File fil, String pattern)
52    {
53        return Arrays.asList(fil.listFiles(new MyFileFilter(pattern)));
54    }
55
56    public static Collection getFiles(File fil, String pattern, Date adate)
57    {
58        MyFileFilter myFF = new MyFileFilter(pattern)  ;
59        myFF.setModDate(adate);
60        if (fil.exists())               
61            return Arrays.asList(fil.listFiles(myFF));
62        return new ArrayList();
63    }
64    
65    public static Collection getFiles(String filenm, String pattern)
66    {
67        return getFiles(new File(filenm), pattern);
68    }
69
70//TODO Implement a comparator on the Date of each file so that the 
71//  collection can be sorted on Date b4 being returned  
72    public static Collection getFiles(String filenm, String pattern, String value)
73    {
74        if(value.equalsIgnoreCase("") ){
75            return getFiles(new File(filenm), pattern);
76        }
77        else 
78        {return getFiles(new File(filenm), pattern, Dates.getISODate(value));} 
79    }
80    /** have to know which constructor to use for the MFF
81     * ie. handle prop discrepancies here regex vs filename
82     *    remember you CANT have both
83     * so introspect the size & Date args 
84     * handle data.source.filesize.filter of 20k (DEMO only)
85     * handle all the if statements from FileTopictype
86     * make the call, get the array , convert to List
87     *  delegate the sizeFilter to MFF class 
88     */ 
89    public static Collection getFilesFromProps (){      
90        MyFileFilter myFF = new MyFileFilter(PropertyCompar())  ;
91
92        if(! POProperty.get(
93            "data.source.lastfile"
94        ).equalsIgnoreCase("")){
95            myFF.setModDate(
96                Dates.getISODate(
97                    POProperty.get(
98                        "data.source.lastfile"
99                    )
00                )
01            );}         
02        if(! POProperty.get(
03            "data.source.filesize.filter"
04        ).equalsIgnoreCase("")){
05            myFF.setSize(
06                new Long(
07                    POProperty.get(
08                        "data.source.filesize.filter"
09                    )
10                ).longValue()
11            );}     
12        
13        // call always the same where some args may be empty string ""      
14        return Arrays.asList(
15            new File (
16                POProperty.get(
17                    "data.source.sourcedirectory"
18                )
19            ).listFiles(myFF)
20        );                   
21    }
22    private static String PropertyCompar (){
23        if (
24            ! POProperty.get("data.source.regex").equalsIgnoreCase("")      
25        &&       
26            POProperty.get("data.source.filename").equalsIgnoreCase("")
27        ){return POProperty.get("data.source.regex")  ;}
28        else {
29            if (
30                POProperty.get("data.source.regex").equalsIgnoreCase("")        
31            &&       
32            !   POProperty.get("data.source.filename").equalsIgnoreCase("")
33            ){return POProperty.get("data.source.filename");}       
34        }       
35        
36        return null;
37    }
38    public static void main(String args1[])
39    {System.out.println("convertd : "
40        + Dates.getISODate(args1[0]).toString()
41);
42    }
43
44    protected static Logger log;
45
46    static 
47    {
48        log = Logger.getLogger(com.borneo.util.FileSelector.class.getName());
49        PropertyConfigurator.configure(com.borneo.util.FileSelector.class.getClassLoader().getResource("config/log4j.properties"));
50    }
51}
52