KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > util > FileLister


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * foo.java
26  *
27  * Created on November 11, 2001, 12:09 AM
28  */

29
30 package com.sun.enterprise.config.backup.util;
31 import java.io.*;
32 import java.util.*;
33
34 /**
35  *
36  * @author bnevins
37  * @version
38  */

39 public abstract class FileLister
40 {
41     abstract protected boolean relativePath();
42
43     ///////////////////////////////////////////////////////////////////////////
44

45     FileLister(File root)
46     {
47         mainRoot = root;
48         fileList = new ArrayList();
49     }
50
51     /**
52      * Normally, we don't want a list of empty (leaf) directories. In case this
53      * is desired -- call this method. The default is to NOT keep empty directories
54      * in the list
55      */

56     public void keepEmptyDirectories()
57     {
58         keepEmpty = true;
59     }
60
61     ///////////////////////////////////////////////////////////////////////////
62

63     public String JavaDoc[] getFiles()
64     {
65         getFilesInternal(mainRoot);
66         String JavaDoc[] files = new String JavaDoc[fileList.size()];
67         
68         if(files.length <= 0)
69             return files;
70
71         int len = 0;
72         
73         if(relativePath())
74             len = mainRoot.getPath().length() + 1;
75         
76         for(int i = 0; i < files.length; i++)
77         {
78             files[i] = ((File)fileList.get(i)).getPath().substring(len).replace('\\', '/');
79         }
80         
81         Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
82         return files;
83     }
84
85     ///////////////////////////////////////////////////////////////////////////
86

87     private void getFilesInternal(File root)
88     {
89         File[] files = root.listFiles();
90         
91         for(int i = 0; i < files.length; i++)
92         {
93             if(files[i].isDirectory())
94             {
95                 getFilesInternal(files[i]);
96             }
97             else
98                 fileList.add(files[i]); // actual file
99
}
100         
101         // add empty directory, if the option is turned on
102
if(files.length <= 0 && keepEmpty)
103             fileList.add(root);
104     }
105
106     ///////////////////////////////////////////////////////////////////////////
107

108     private ArrayList fileList = null;
109     private File mainRoot = null;
110     private boolean keepEmpty = false;
111 }
112
Popular Tags