KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > actions > ListDirectory


1 package net.killingar.actions;
2
3 import net.killingar.StringUtils;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Arrays JavaDoc;
7 import java.util.Comparator JavaDoc;
8 import java.util.Date JavaDoc;
9
10 public class ListDirectory extends PathActionSupport
11 {
12     // Types ---------------------------------------------------------
13
static class Dir
14     {
15         String JavaDoc name;
16         String JavaDoc relPath;
17         String JavaDoc type;
18         String JavaDoc url;
19         long size;
20         java.io.File JavaDoc file;
21         Date JavaDoc modified;
22
23         public Dir(java.io.File JavaDoc inFile, String JavaDoc inRelPath)
24         {
25             file = inFile;
26             name = inFile.getName();
27             relPath = inRelPath+"/"+name+(inFile.isDirectory()? "/" : "");
28             url = StringUtils.URLEncode(name)+(inFile.isDirectory()? "/" : "");
29             type = inFile.isDirectory()? "folder" : "file";
30             size = inFile.length();
31             modified = new Date JavaDoc(inFile.lastModified());
32         }
33
34         public String JavaDoc getName() { return name; }
35         public String JavaDoc getUrl() { return url; }
36         public String JavaDoc getRelativePath() { return relPath; }
37         public String JavaDoc getType() { return type; }
38         public long getSize() { return size; }
39         public java.io.File JavaDoc getFile() { return file; }
40         public Date JavaDoc getModified() { return modified; }
41     }
42
43     static class File extends Dir
44     {
45         public File(java.io.File JavaDoc inFile, String JavaDoc inRelPath)
46         {
47             super(inFile, inRelPath);
48
49             try
50             {
51                 String JavaDoc extension = name.substring(name.lastIndexOf(".")+1).toLowerCase();
52
53                      if (extension.equals("jpg")) { type = "pic"; }
54                 else if (extension.equals("jpeg")) { type = "pic"; }
55                 else if (extension.equals("gif")) { type = "pic"; }
56                 else if (extension.equals("png")) { type = "pic"; }
57                 else if (extension.equals("txt")) { type = "text"; }
58                 else if (extension.equals("doc")) { type = "doc"; }
59                 else if (extension.equals("zip")) { type = "zip"; }
60                 else if (extension.equals("rar")) { type = "zip"; }
61                 else if (extension.equals("html")) { type = "ie_html"; }
62                 else if (extension.equals("htm")) { type = "ie_html"; }
63                 else if (extension.equals("mp3")) { type = "mp3"; }
64                 else if (extension.equals("wav")) { type = "mp3"; }
65                 //else if (extension.equals("")) { type = ""; }
66
}
67             catch (Exception JavaDoc e){}
68         }
69
70         public String JavaDoc getBareName() { return name.substring(0, name.lastIndexOf('.')); }
71     }
72
73     static class SortComparator implements Comparator JavaDoc
74     {
75         boolean descending = true;
76         String JavaDoc sortOn = "name";
77
78         public int compare(Object JavaDoc o1, Object JavaDoc o2)
79         {
80             Dir d1 = (Dir)o1;
81             Dir d2 = (Dir)o2;
82
83             int c = 0;
84
85             if (sortOn.equals("name"))
86                 c = d1.getName().compareToIgnoreCase(d2.getName());
87             else if (sortOn.equals("type"))
88                 c = d1.getType().compareToIgnoreCase(d2.getType());
89             else if (sortOn.equals("size"))
90             {
91                 if (d1.getSize() == d2.getSize())
92                     c = 0;
93                 else if (d1.getSize() < d2.getSize())
94                     c = -1;
95                 else
96                     c = 1;
97             }
98             else if (sortOn.equals("modified"))
99                 c = d1.getModified().compareTo(d2.getModified());
100
101             if (descending)
102                 return c;
103             else
104                 return -c;
105         }
106
107         public boolean equals(Object JavaDoc obj)
108         {
109             SortComparator s = (SortComparator)obj;
110
111             return sortOn.equals(s.sortOn) && descending == s.descending;
112         }
113     }
114
115     // Attributes ----------------------------------------------------
116
Object JavaDoc[] directories;
117     Object JavaDoc[] files;
118     long totalSize = 0;
119     long totalCount = 0;
120     SortComparator sortComparator = new SortComparator();
121     String JavaDoc filter;
122     String JavaDoc url;
123
124     // getters
125
public Object JavaDoc[] getDirectories() { return directories; }
126     public Object JavaDoc[] getFiles() { return files; }
127     public String JavaDoc getPath() { return path; }
128     public long getTotalSize() { return totalSize; }
129     public long getTotalCount() { return totalCount; }
130     public String JavaDoc getSortOn() { return sortComparator.sortOn; }
131     public boolean getDescending() { return sortComparator.descending; }
132     public String JavaDoc getFilter() { return filter; }
133     public String JavaDoc getUrl() { return url; }
134
135     // setters
136
public void setSortOn(String JavaDoc in) { sortComparator.sortOn = in; }
137     public void setDescending(boolean in) { sortComparator.descending = in; }
138     public void setFilter(String JavaDoc in) { filter = in; }
139
140     // Implementation
141
public String JavaDoc doExecute()
142     {
143         String JavaDoc result = SUCCESS;
144         try
145         {
146             /*if (getPath().charAt(getPath().length()-1) != '/')
147             {
148                 url = getPath()+"/";
149                 result = "redirect";
150             }*/

151             java.io.File JavaDoc[] f = new java.io.File JavaDoc(realPath).listFiles();
152
153             ArrayList JavaDoc files = new ArrayList JavaDoc();
154             ArrayList JavaDoc directories = new ArrayList JavaDoc();
155
156             if (f != null)
157             {
158                 for (int i = 0; i < f.length; i++)
159                 {
160                     if (f[i].isDirectory())
161                     {
162                         if (
163                             !(f[i].getName().equalsIgnoreCase("WEB-INF")) &&
164                             !(f[i].getName().charAt(0) == '.'))// && (path.equals("") || path.equals("/"))))
165
{
166                             directories.add(new Dir(f[i], path));
167                         }
168                     }
169                     else
170                     {
171                         File fi = new File(f[i], path);
172                         if (filter == null || filter.equals(fi.getType()))
173                         {
174                             files.add(fi);
175                             totalSize += f[i].length();
176                             totalCount++;
177                         }
178                     }
179                 }
180             }
181
182             this.directories = directories.toArray();
183             this.files = files.toArray();
184
185             Arrays.sort(this.directories, sortComparator);
186             Arrays.sort(this.files, sortComparator);
187         }
188         catch (Exception JavaDoc e)
189         {
190             addErrorMessage("listing directory failed, exception thrown ("+e.toString()+")");
191             e.printStackTrace();
192
193             return ERROR;
194         }
195
196         return result;
197     }
198 }
199
Popular Tags