1 package net.killingar.actions; 2 3 import net.killingar.StringUtils; 4 5 import java.util.ArrayList ; 6 import java.util.Arrays ; 7 import java.util.Comparator ; 8 import java.util.Date ; 9 10 public class ListDirectory extends PathActionSupport 11 { 12 static class Dir 14 { 15 String name; 16 String relPath; 17 String type; 18 String url; 19 long size; 20 java.io.File file; 21 Date modified; 22 23 public Dir(java.io.File inFile, String 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 (inFile.lastModified()); 32 } 33 34 public String getName() { return name; } 35 public String getUrl() { return url; } 36 public String getRelativePath() { return relPath; } 37 public String getType() { return type; } 38 public long getSize() { return size; } 39 public java.io.File getFile() { return file; } 40 public Date getModified() { return modified; } 41 } 42 43 static class File extends Dir 44 { 45 public File(java.io.File inFile, String inRelPath) 46 { 47 super(inFile, inRelPath); 48 49 try 50 { 51 String 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 } 67 catch (Exception e){} 68 } 69 70 public String getBareName() { return name.substring(0, name.lastIndexOf('.')); } 71 } 72 73 static class SortComparator implements Comparator 74 { 75 boolean descending = true; 76 String sortOn = "name"; 77 78 public int compare(Object o1, Object 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 obj) 108 { 109 SortComparator s = (SortComparator)obj; 110 111 return sortOn.equals(s.sortOn) && descending == s.descending; 112 } 113 } 114 115 Object [] directories; 117 Object [] files; 118 long totalSize = 0; 119 long totalCount = 0; 120 SortComparator sortComparator = new SortComparator(); 121 String filter; 122 String url; 123 124 public Object [] getDirectories() { return directories; } 126 public Object [] getFiles() { return files; } 127 public String getPath() { return path; } 128 public long getTotalSize() { return totalSize; } 129 public long getTotalCount() { return totalCount; } 130 public String getSortOn() { return sortComparator.sortOn; } 131 public boolean getDescending() { return sortComparator.descending; } 132 public String getFilter() { return filter; } 133 public String getUrl() { return url; } 134 135 public void setSortOn(String in) { sortComparator.sortOn = in; } 137 public void setDescending(boolean in) { sortComparator.descending = in; } 138 public void setFilter(String in) { filter = in; } 139 140 public String doExecute() 142 { 143 String result = SUCCESS; 144 try 145 { 146 151 java.io.File [] f = new java.io.File (realPath).listFiles(); 152 153 ArrayList files = new ArrayList (); 154 ArrayList directories = new ArrayList (); 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) == '.')) { 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 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 |