1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.util.Folder; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 33 public class VirtualFolder implements Folder { 34 35 protected transient final Log log = LogFactory.getLog(VirtualFolder.class); 36 37 40 protected String name; 41 42 46 protected String path; 47 48 51 protected char delimiter = '/'; 52 53 58 public VirtualFolder(String name) { 59 this(name, '/'); 60 } 61 62 68 public VirtualFolder(String name, String path) { 69 this(name, path, '/'); 70 } 71 72 78 public VirtualFolder(String name, char delimiter) { 79 this.name = name; 80 this.delimiter = delimiter; 81 } 82 83 91 public VirtualFolder(String name, String path, char delimiter) { 92 this.name = name; 93 this.path = path; 94 this.delimiter = delimiter; 95 } 96 97 102 public String getName() { 103 return name; 104 } 105 106 111 public void setName(String name) { 112 this.name = name; 113 } 114 115 120 public String getPath() { 121 return path; 122 } 123 124 129 public void setPath(String path) { 130 this.path = path; 131 } 132 133 138 public char getDelimiter() { 139 return delimiter; 140 } 141 142 147 public void setDelimiter(char delimiter) { 148 this.delimiter = delimiter; 149 } 150 151 156 public String getFullPath() { 157 String fullPath = path; 158 if ( !name.trim().equals("..") ) { 159 fullPath += (path.length() > 1 ? String.valueOf(delimiter) : "") + name; 160 } 161 return fullPath; 162 } 163 164 171 public static List splitPath(String path, char delimiter) { 172 return splitPath(path, delimiter, true); 173 } 174 175 186 public static List splitPath(String path, char delimiter, boolean startFromDelim) { 187 String delimiterString = String.valueOf(delimiter); 188 List folders = new ArrayList (); 189 if ( path != null && path.length() > 0 ) { 190 StringTokenizer pathElements = new StringTokenizer (path, delimiterString, false); 191 String folderPath = startFromDelim ? delimiterString : ""; 192 while ( pathElements.hasMoreTokens() ) { 193 String folderName = pathElements.nextToken(); 194 VirtualFolder virtualFolder = new VirtualFolder(folderName, folderPath, delimiter); 195 folders.add(virtualFolder); 196 folderPath += (folderPath.length() > 1 ? delimiterString : "") + folderName; 197 } 198 } 199 return folders; 200 } 201 202 208 public static List splitPath(String path) { 209 return splitPath(path, '/'); 210 } 211 212 213 public boolean equals(Object o) { 214 if ( this == o ) { 215 return true; 216 } 217 if ( !(o instanceof Folder) ) { 218 return false; 219 } 220 221 final Folder folder = (Folder) o; 222 223 if ( name != null ? !name.equals(folder.getName()) : folder.getName() != null ) { 224 return false; 225 } 226 if ( path != null ? !path.equals(folder.getPath()) : folder.getPath() != null ) { 227 return false; 228 } 229 230 return true; 231 } 232 233 243 public int compareTo(Object o) { 244 if ( !(o instanceof Folder) ) { 245 throw new ClassCastException ("Cannot compare instance of " + getClass().getName() + " to the instance of " + o.getClass().getName()); 246 } 247 Folder f = (Folder) o; 248 return name.equalsIgnoreCase(f.getName()) ? path.compareToIgnoreCase(f.getPath()) : name.compareToIgnoreCase(f.getName()); 249 } 250 251 public int hashCode() { 252 int result; 253 result = (name != null ? name.hashCode() : 0); 254 result = 29 * result + (path != null ? path.hashCode() : 0); 255 return result; 256 } 257 258 public String toString() { 259 StringBuffer sb = new StringBuffer ("VirtualFolder ["); 260 sb.append("name=").append(this.name); 261 sb.append(", path=").append(this.path); 262 sb.append("]"); 263 return sb.toString(); 264 } 265 } 266 | Popular Tags |