1 22 23 package org.meshcms.util; 24 25 import java.io.*; 26 import java.util.*; 27 28 71 public class Path implements Comparable , Serializable, Cloneable { 72 protected String pathName; 73 protected String [] elements; 74 75 public static final Path ROOT = new Path(); 76 77 80 public Path() { 81 this(null, null, null); 82 } 83 84 89 public Path(Object o) { 90 this(o, null, null); 91 } 92 93 99 public Path(Object o1, Object o2) { 100 this(o1, o2, null); 101 } 102 103 110 public Path(Object o1, Object o2, Object o3) { 111 List list = new ArrayList(); 112 addObjectToList(list, o1); 113 addObjectToList(list, o2); 114 addObjectToList(list, o3); 115 116 for (int i = 0; i < list.size(); i++) { 117 String s = (String ) list.get(i); 118 119 if (s.equals("") || s.equals(".")) { 120 list.remove(i--); 121 } else if (s.equals("..")) { 122 if (i > 0 && !"..".equals(list.get(i - 1))) { 123 list.remove(i--); 124 list.remove(i--); 125 } 126 } 127 } 128 129 elements = (String []) list.toArray(new String [list.size()]); 130 pathName = Utils.generateList(elements, "/"); 131 } 132 133 protected void addObjectToList(List list, Object o) { 134 if (o == null) { 135 } else if (o instanceof Path) { 137 Path p = (Path) o; 138 139 for (int i = 0; i < p.getElementCount(); i++) { 140 list.add(p.getElementAt(i)); 141 } 142 } else if (o instanceof String []) { 143 String [] s = (String []) o; 144 145 for (int i = 0; i < s.length; i++) { 146 addObjectToList(list, s[i]); 147 } 148 } else if (o instanceof Collection) { 149 Iterator i = ((Collection) o).iterator(); 150 151 while (i.hasNext()) { 152 addObjectToList(list, i.next()); 153 } 154 } else { StringTokenizer st = new StringTokenizer(o.toString(), "\\/"); 156 157 while (st.hasMoreTokens()) { 158 list.add(st.nextToken()); 159 } 160 } 161 } 162 163 171 public Path add(Object o) { 172 return new Path(this, o); 173 } 174 175 184 public Path add(Object o1, Object o2) { 185 return new Path(this, o1, o2); 186 } 187 188 194 public Path getParent() { 195 return new Path(this, ".."); 196 } 197 198 206 public Path getPartial(int count) { 207 if (count < 0) { 208 throw new IllegalArgumentException ("Negative level not allowed"); 209 } 210 211 if (count == 0) { 212 return ROOT; 213 } 214 215 if (count >= elements.length) { 216 return this; 217 } 218 219 String [] partial = new String [count]; 220 System.arraycopy(elements, 0, partial, 0, count); 221 return new Path(partial); 222 } 223 224 231 public Path getCommonPath(Path other) { 232 return commonPart(this, other); 233 } 234 235 241 public boolean isRelative() { 242 return elements.length > 0 && elements[0].equals(".."); 243 } 244 245 250 public boolean isRoot() { 251 return elements.length == 0; 252 } 253 254 268 public boolean isChildOf(Path parent) { 269 if (parent == null) { 270 return false; 271 } 272 273 int level = parent.getElementCount(); 274 275 if (elements.length != level + 1) { 276 return false; 277 } 278 279 for (int i = 0; i < level; i++) { 280 if (!elements[i].equals(parent.getElementAt(i))) { 281 return false; 282 } 283 } 284 285 return true; 286 } 287 288 302 public boolean isContainedIn(Path root) { 303 if (root == null) { 304 return false; 305 } 306 307 if (root.isRelative()) { 308 throw new IllegalArgumentException ("Root path can't be relative"); 309 } 310 311 int level = root.getElementCount(); 312 313 if (level > elements.length) { 314 return false; 315 } 316 317 for (int i = 0; i < level; i++) { 318 if (!elements[i].equals(root.getElementAt(i))) { 319 return false; 320 } 321 } 322 323 return true; 324 } 325 326 337 public Path getRelativeTo(Object root) { 338 Path rootPath = (root instanceof Path) ? (Path) root : new Path(root); 339 340 if (rootPath.isRelative()) { 341 throw new IllegalArgumentException ("Root path can't be negative"); 342 } 343 344 int i0 = 0; 345 int i1 = 0; 346 347 List list = new ArrayList(); 348 349 while (i0 < rootPath.getElementCount() && i1 < elements.length && 350 rootPath.getElementAt(i0).equals(elements[i1])) { 351 i0++; 352 i1++; 353 } 354 355 while (i0++ < rootPath.getElementCount()) { 356 list.add(".."); 357 } 358 359 while (i1 < elements.length - 1) { 360 list.add(elements[i1++]); 361 } 362 363 if (i1 == elements.length - 1) { 364 list.add(elements[i1]); 365 } 366 367 return new Path(list); 368 } 369 370 377 public File getFile(File parent) { 378 return elements.length == 0 ? parent : new File(parent, pathName); 379 } 380 381 390 public int getElementCount() { 391 return elements.length; 392 } 393 394 402 public String getElementAt(int index) { 403 return elements[index]; 404 } 405 406 412 public String getLastElement() { 413 return elements.length == 0 ? "" : elements[elements.length - 1]; 414 } 415 416 420 public String toString() { 421 return pathName; 422 } 423 424 430 public String getAsLink() { 431 return elements.length == 0 ? "" : '/' + pathName; 432 } 433 434 438 public int compareTo(Object o) { 439 return compareTo(new Path(o)); 440 } 441 442 452 public int compareTo(Path other) { 453 int level = other.getElementCount(); 454 int result; 455 456 for (int i = 0; i < elements.length; i++) { 457 if (i >= level) { 458 return 1; 459 } 460 461 result = elements[i].compareTo(other.getElementAt(i)); 462 463 if (result != 0) { 464 return result; 465 } 466 } 467 468 return level > elements.length ? -1 : 0; 469 } 470 471 474 public int hashCode() { 475 return pathName.hashCode(); 476 } 477 478 482 public boolean equals(Object o) { 483 if (o == null || !(o instanceof Path)) { 484 return false; 485 } 486 487 return pathName.equals(o.toString()); 488 } 489 490 498 public static Path commonPart(Path p1, Path p2) { 499 int n = Math.min(p1.getElementCount(), p2.getElementCount()); 500 501 for (int i = 0; i < n; i++) { 502 if (!p1.getElementAt(i).equals(p2.getElementAt(i))) { 503 return p1.getPartial(i); 504 } 505 } 506 507 return p1; 508 } 509 510 518 public Path successor() { 519 return (elements.length == 0) ? new Path("\0") : getParent().add(getLastElement() + '\0'); 520 } 521 522 protected Object clone() throws CloneNotSupportedException { 523 return new Path(this); 525 } 526 527 public Path replace(int index, String element) { 528 int n = elements.length; 529 530 if (index < 0 || index >= n) { 531 throw new IllegalArgumentException ("index out of range"); 532 } 533 534 if (Utils.isNullOrEmpty(element)) { 535 throw new IllegalArgumentException ("element value is missing or empty"); 536 } 537 538 String [] elms = new String [n]; 539 System.arraycopy(elements, 0, elms, 0, n); 540 elms[index] = element; 541 return new Path(elms); 542 } 543 544 547 public String [] getElements() { 548 String [] result = new String [elements.length]; 549 System.arraycopy(elements, 0, result, 0, elements.length); 550 return result; 551 } 552 } 553 | Popular Tags |