1 16 package org.apache.commons.vfs.provider; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.FileType; 21 import org.apache.commons.vfs.NameScope; 22 import org.apache.commons.vfs.VFS; 23 24 29 public abstract class AbstractFileName 30 implements FileName 31 { 32 private final String scheme; 33 private final String absPath; 34 private FileType type; 35 36 private String uri; 38 private String baseName; 39 private String rootUri; 40 private String extension; 41 private String decodedAbsPath; 42 43 public AbstractFileName(final String scheme, 44 final String absPath, FileType type) 45 { 46 this.rootUri = null; 47 this.scheme = scheme; 48 this.type = type; 49 if (absPath != null && absPath.length() > 0) 50 { 51 if (absPath.length() > 1 && absPath.endsWith("/")) 52 { 53 this.absPath = absPath.substring(0, absPath.length() - 1); 54 } 55 else 56 { 57 this.absPath = absPath; 58 } 59 } 60 else 61 { 62 this.absPath = ROOT_PATH; 63 } 64 } 65 66 69 public int hashCode() 70 { 71 return (getRootURI().hashCode() ^ getPath().hashCode()); 72 } 73 74 77 public boolean equals(final Object obj) 78 { 79 final AbstractFileName name = (AbstractFileName) obj; 80 return (getRootURI().equals(name.getRootURI()) && getPath().equals(name.getPath())); 81 } 82 83 88 public int compareTo(Object obj) 89 { 90 final AbstractFileName name = (AbstractFileName) obj; 91 int ret = getRootURI().compareTo(name.getRootURI()); 92 if (ret != 0) 93 { 94 return ret; 95 } 96 97 try 99 { 100 return getPathDecoded().compareTo(name.getPathDecoded()); 101 } 102 catch (FileSystemException e) 103 { 104 throw new RuntimeException (e.getMessage()); 105 } 106 } 107 108 111 public String toString() 112 { 113 return getURI(); 114 } 115 116 119 public abstract FileName createName(String absPath, FileType type); 120 121 125 protected abstract void appendRootUri(StringBuffer buffer); 126 127 130 public String getBaseName() 131 { 132 if (baseName == null) 133 { 134 final int idx = getPath().lastIndexOf(SEPARATOR_CHAR); 135 if (idx == -1) 136 { 137 baseName = getPath(); 138 } 139 else 140 { 141 baseName = getPath().substring(idx + 1); 142 } 143 } 144 145 return baseName; 146 } 147 148 152 public String getPath() 153 { 154 if (VFS.isUriStyle()) 155 { 156 return absPath + getUriTrailer(); 157 } 158 return absPath; 159 } 160 161 protected String getUriTrailer() 162 { 163 return getType() == FileType.FOLDER ? "/" : ""; 164 } 165 166 public String getPathDecoded() throws FileSystemException 167 { 168 if (decodedAbsPath == null) 169 { 170 decodedAbsPath = UriParser.decode(getPath()); 171 } 172 173 return decodedAbsPath; 174 } 175 176 179 public FileName getParent() 180 { 181 final String parentPath; 182 final int idx = getPath().lastIndexOf(SEPARATOR_CHAR); 183 if (idx == -1 || idx == getPath().length() - 1) 184 { 185 return null; 187 } 188 else if (idx == 0) 189 { 190 parentPath = SEPARATOR; 192 } 193 else 194 { 195 parentPath = getPath().substring(0, idx); 196 } 197 return createName(parentPath, FileType.FOLDER); 198 } 199 200 203 public FileName getRoot() 204 { 205 FileName root = this; 206 while (root.getParent() != null) 207 { 208 root = root.getParent(); 209 } 210 211 return root; 212 } 213 214 217 public String getScheme() 218 { 219 return scheme; 220 } 221 222 225 public String getURI() 226 { 227 if (uri == null) 228 { 229 uri = createURI(); 230 } 231 return uri; 232 } 233 234 protected String createURI() 235 { 236 final StringBuffer buffer = new StringBuffer (); 237 appendRootUri(buffer); 238 buffer.append(getPath()); 239 return buffer.toString(); 240 } 241 242 245 public String getRelativeName(final FileName name) throws FileSystemException 246 { 247 final String path = name.getPath(); 248 249 final int basePathLen = getPath().length(); 251 final int pathLen = path.length(); 252 253 if (basePathLen == 1 && pathLen == 1) 255 { 256 return "."; 257 } 258 else if (basePathLen == 1) 259 { 260 return path.substring(1); 261 } 262 263 final int maxlen = Math.min(basePathLen, pathLen); 264 int pos = 0; 265 for (; pos < maxlen && getPath().charAt(pos) == path.charAt(pos); pos++) 266 { 267 } 268 269 if (pos == basePathLen && pos == pathLen) 270 { 271 return "."; 273 } 274 else if (pos == basePathLen && pos < pathLen && path.charAt(pos) == SEPARATOR_CHAR) 275 { 276 return path.substring(pos + 1); 278 } 279 280 final StringBuffer buffer = new StringBuffer (); 282 if (pathLen > 1 && (pos < pathLen || getPath().charAt(pos) != SEPARATOR_CHAR)) 283 { 284 pos = getPath().lastIndexOf(SEPARATOR_CHAR, pos); 286 buffer.append(path.substring(pos)); 287 } 288 289 buffer.insert(0, ".."); 292 pos = getPath().indexOf(SEPARATOR_CHAR, pos + 1); 293 while (pos != -1) 294 { 295 buffer.insert(0, "../"); 296 pos = getPath().indexOf(SEPARATOR_CHAR, pos + 1); 297 } 298 299 return buffer.toString(); 300 } 301 302 305 public String getRootURI() 306 { 307 if (rootUri == null) 308 { 309 final StringBuffer buffer = new StringBuffer (); 310 appendRootUri(buffer); 311 buffer.append(SEPARATOR_CHAR); 312 rootUri = buffer.toString(); 313 } 314 return rootUri; 315 } 316 317 320 public int getDepth() 321 { 322 final int len = getPath().length(); 323 if (len == 0 || (len == 1 && getPath().charAt(0) == SEPARATOR_CHAR)) 324 { 325 return 0; 326 } 327 int depth = 1; 328 for (int pos = 0; pos > -1 && pos < len; depth++) 329 { 330 pos = getPath().indexOf(SEPARATOR_CHAR, pos + 1); 331 } 332 return depth; 333 } 334 335 338 public String getExtension() 339 { 340 if (extension == null) 341 { 342 getBaseName(); 343 final int pos = baseName.lastIndexOf('.'); 344 if ((pos < 1) || (pos == baseName.length() - 1)) 350 { 351 extension = ""; 353 } 354 else 355 { 356 extension = baseName.substring(pos + 1); 357 } 358 } 359 return extension; 360 } 361 362 365 public boolean isAncestor(final FileName ancestor) 366 { 367 if (!ancestor.getRootURI().equals(getRootURI())) 368 { 369 return false; 370 } 371 return checkName(ancestor.getPath(), getPath(), NameScope.DESCENDENT); 372 } 373 374 377 public boolean isDescendent(final FileName descendent) 378 { 379 return isDescendent(descendent, NameScope.DESCENDENT); 380 } 381 382 385 public boolean isDescendent(final FileName descendent, 386 final NameScope scope) 387 { 388 if (!descendent.getRootURI().equals(getRootURI())) 389 { 390 return false; 391 } 392 return checkName(getPath(), descendent.getPath(), scope); 393 } 394 395 408 public FileType getType() 409 { 410 return type; 411 } 412 413 418 void setType(FileType type) throws FileSystemException 419 { 420 if (type != FileType.FOLDER && type != FileType.FILE) 421 { 422 throw new FileSystemException("vfs.provider/filename-type.error"); 423 } 424 425 this.type = type; 426 } 427 428 434 public static boolean checkName(final String basePath, 435 final String path, 436 final NameScope scope) 437 { 438 if (scope == NameScope.FILE_SYSTEM) 439 { 440 return true; 442 } 443 444 if (!path.startsWith(basePath)) 445 { 446 return false; 447 } 448 449 int baseLen = basePath.length(); 450 if (VFS.isUriStyle()) 451 { 452 baseLen--; 454 } 455 456 if (scope == NameScope.CHILD) 457 { 458 if (path.length() == baseLen 459 || (baseLen > 1 && path.charAt(baseLen) != SEPARATOR_CHAR) 460 || path.indexOf(SEPARATOR_CHAR, baseLen + 1) != -1) 461 { 462 return false; 463 } 464 } 465 else if (scope == NameScope.DESCENDENT) 466 { 467 if (path.length() == baseLen 468 || (baseLen > 1 && path.charAt(baseLen) != SEPARATOR_CHAR)) 469 { 470 return false; 471 } 472 } 473 else if (scope == NameScope.DESCENDENT_OR_SELF) 474 { 475 if (baseLen > 1 476 && path.length() > baseLen 477 && path.charAt(baseLen) != SEPARATOR_CHAR) 478 { 479 return false; 480 } 481 } 482 else if (scope != NameScope.FILE_SYSTEM) 483 { 484 throw new IllegalArgumentException (); 485 } 486 487 return true; 488 } 489 } 490 | Popular Tags |