1 31 32 package org.opencms.file; 33 34 import org.opencms.main.CmsIllegalArgumentException; 35 import org.opencms.util.CmsStringUtil; 36 import org.opencms.util.CmsUUID; 37 38 import java.io.Serializable ; 39 import java.util.Comparator ; 40 41 52 public class CmsResource extends Object implements Cloneable , Serializable , Comparable { 53 54 60 public static final Comparator COMPARE_DATE_RELEASED = new Comparator () { 61 62 65 public int compare(Object o1, Object o2) { 66 67 if ((o1 == o2) || !(o1 instanceof CmsResource) || !(o2 instanceof CmsResource)) { 68 return 0; 69 } 70 71 CmsResource r1 = (CmsResource)o1; 72 CmsResource r2 = (CmsResource)o2; 73 74 long date1 = r1.getDateReleased(); 75 if (date1 == CmsResource.DATE_RELEASED_DEFAULT) { 76 date1 = r1.getDateLastModified(); 78 } 79 80 long date2 = r2.getDateReleased(); 81 if (date2 == CmsResource.DATE_RELEASED_DEFAULT) { 82 date2 = r2.getDateLastModified(); 84 } 85 86 return (date1 > date2) ? -1 : (date1 < date2) ? 1 : 0; 87 } 88 }; 89 90 93 public static final Comparator COMPARE_ROOT_PATH = new Comparator () { 94 95 98 public int compare(Object o1, Object o2) { 99 100 if ((o1 == o2) || !(o1 instanceof CmsResource) || !(o2 instanceof CmsResource)) { 101 return 0; 102 } 103 104 CmsResource r1 = (CmsResource)o1; 105 CmsResource r2 = (CmsResource)o2; 106 107 return r1.getRootPath().compareTo(r2.getRootPath()); 108 } 109 }; 110 111 114 public static final Comparator COMPARE_ROOT_PATH_IGNORE_CASE = new Comparator () { 115 116 119 public int compare(Object o1, Object o2) { 120 121 if ((o1 == o2) || !(o1 instanceof CmsResource) || !(o2 instanceof CmsResource)) { 122 return 0; 123 } 124 125 CmsResource r1 = (CmsResource)o1; 126 CmsResource r2 = (CmsResource)o2; 127 128 return r1.getRootPath().compareToIgnoreCase(r2.getRootPath()); 129 } 130 }; 131 132 135 public static final Comparator COMPARE_ROOT_PATH_IGNORE_CASE_FOLDERS_FIRST = new Comparator () { 136 137 140 public int compare(Object o1, Object o2) { 141 142 if ((o1 == o2) || !(o1 instanceof CmsResource) || !(o2 instanceof CmsResource)) { 143 return 0; 144 } 145 146 CmsResource r1 = (CmsResource)o1; 147 CmsResource r2 = (CmsResource)o2; 148 149 if (r1.isFolder() && !r2.isFolder()) { 150 return -1; 151 } else if (r2.isFolder() && !r1.isFolder()) { 152 return 1; 153 } 154 return r1.getRootPath().compareToIgnoreCase(r2.getRootPath()); 156 } 157 }; 158 159 160 public static final int COPY_AS_NEW = 1; 161 162 163 public static final int COPY_AS_SIBLING = 2; 164 165 166 public static final int COPY_PRESERVE_SIBLING = 3; 167 168 169 public static final long DATE_EXPIRED_DEFAULT = Long.MAX_VALUE; 170 171 172 public static final long DATE_RELEASED_DEFAULT = 0; 173 174 175 public static final int DELETE_PRESERVE_SIBLINGS = 0; 176 177 178 public static final int DELETE_REMOVE_SIBLINGS = 1; 179 180 181 public static final int FLAG_INTERNAL = 512; 182 183 184 public static final int FLAG_LABELED = 2; 185 186 187 public static final int FLAG_TEMPFILE = 1024; 188 189 190 public static final String NAME_CONSTRAINTS = "-._~$"; 191 192 193 public static final int STATE_CHANGED = 1; 194 195 196 public static final int STATE_DELETED = 3; 197 198 202 public static final int STATE_KEEP = 99; 203 204 205 public static final int STATE_NEW = 2; 206 207 208 public static final int STATE_UNCHANGED = 0; 209 210 211 public static final long TOUCH_DATE_UNCHANGED = -1; 212 213 214 public static final String VFS_FOLDER_CHANNELS = "/channels"; 215 216 217 public static final String VFS_FOLDER_SITES = "/sites"; 218 219 220 public static final String VFS_FOLDER_SYSTEM = "/system"; 221 222 223 private static final long serialVersionUID = 257325098790850498L; 224 225 226 protected int m_length; 227 228 229 private long m_dateCreated; 230 231 232 private long m_dateExpired; 233 234 235 private long m_dateLastModified; 236 237 238 private long m_dateReleased; 239 240 241 private int m_flags; 242 243 244 private boolean m_isFolder; 245 246 247 private boolean m_isTouched; 248 249 250 private int m_projectLastModified; 251 252 253 private CmsUUID m_resourceId; 254 255 256 private String m_rootPath; 257 258 259 private int m_siblingCount; 260 261 262 private int m_state; 263 264 265 private CmsUUID m_structureId; 266 267 268 private int m_typeId; 269 270 271 private CmsUUID m_userCreated; 272 273 274 private CmsUUID m_userLastModified; 275 276 296 public CmsResource( 297 CmsUUID structureId, 298 CmsUUID resourceId, 299 String rootPath, 300 int type, 301 boolean isFolder, 302 int flags, 303 int projectId, 304 int state, 305 long dateCreated, 306 CmsUUID userCreated, 307 long dateLastModified, 308 CmsUUID userLastModified, 309 long dateReleased, 310 long dateExpired, 311 int linkCount, 312 int size) { 313 314 m_structureId = structureId; 315 m_resourceId = resourceId; 316 m_rootPath = rootPath; 317 m_typeId = type; 318 m_isFolder = isFolder; 319 m_flags = flags; 320 m_projectLastModified = projectId; 321 m_state = state; 322 m_dateCreated = dateCreated; 323 m_userCreated = userCreated; 324 m_dateLastModified = dateLastModified; 325 m_userLastModified = userLastModified; 326 m_length = size; 327 m_siblingCount = linkCount; 328 m_dateReleased = dateReleased; 329 m_dateExpired = dateExpired; 330 m_isTouched = false; 331 } 332 333 345 public static void checkResourceName(String name) throws CmsIllegalArgumentException { 346 347 if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) { 348 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_BAD_RESOURCENAME_EMPTY_0, name)); 349 } 350 351 CmsStringUtil.checkName(name, NAME_CONSTRAINTS, Messages.ERR_BAD_RESOURCENAME_4, Messages.get()); 352 353 boolean onlydots = true; 355 String lastName = CmsResource.getName(name); 357 int l = lastName.length(); 358 for (int i = 0; i < l; i++) { 359 char c = lastName.charAt(i); 360 if ((c != '.') && (c != '/')) { 361 onlydots = false; 362 } 363 } 364 if (onlydots) { 365 throw new CmsIllegalArgumentException(Messages.get().container( 366 Messages.ERR_BAD_RESOURCENAME_DOTS_1, 367 lastName)); 368 } 369 } 370 371 387 public static String getFolderPath(String resource) { 388 389 return resource.substring(0, resource.lastIndexOf('/') + 1); 390 } 391 392 404 public static String getName(String resource) { 405 406 if ("/".equals(resource)) { 407 return "/"; 408 } 409 String parent = (resource.substring(0, resource.length() - 1)); 411 return resource.substring(parent.lastIndexOf('/') + 1); 413 } 414 415 427 public static String getParentFolder(String resource) { 428 429 if ("/".equals(resource)) { 430 return null; 431 } 432 String parent = (resource.substring(0, resource.length() - 1)); 434 return parent.substring(0, parent.lastIndexOf('/') + 1); 436 } 437 438 448 public static int getPathLevel(String resource) { 449 450 int level = -1; 451 int pos = 0; 452 while (resource.indexOf('/', pos) >= 0) { 453 pos = resource.indexOf('/', pos) + 1; 454 level++; 455 } 456 return level; 457 } 458 459 469 public static String getPathPart(String resource, int level) { 470 471 resource = getFolderPath(resource); 472 String result = null; 473 int pos = 0, count = 0; 474 if (level >= 0) { 475 while ((count < level) && (pos > -1)) { 477 count++; 478 pos = resource.indexOf('/', pos + 1); 479 } 480 } else { 481 pos = resource.length(); 483 while ((count > level) && (pos > -1)) { 484 count--; 485 pos = resource.lastIndexOf('/', pos - 1); 486 } 487 } 488 if (pos > -1) { 489 result = resource.substring(0, pos + 1); 491 } else { 492 result = (level < 0) ? "/" : resource; 494 } 495 return result; 496 } 497 498 504 public static boolean isFolder(String resource) { 505 506 return CmsStringUtil.isNotEmpty(resource) && (resource.charAt(resource.length() - 1) == '/'); 507 } 508 509 514 public Object clone() { 515 516 CmsResource clone = new CmsResource( 517 m_structureId, 518 m_resourceId, 519 m_rootPath, 520 m_typeId, 521 m_isFolder, 522 m_flags, 523 m_projectLastModified, 524 m_state, 525 m_dateCreated, 526 m_userCreated, 527 m_dateLastModified, 528 m_userLastModified, 529 m_dateReleased, 530 m_dateExpired, 531 m_siblingCount, 532 m_length); 533 534 if (isTouched()) { 535 clone.setDateLastModified(m_dateLastModified); 536 } 537 538 return clone; 539 } 540 541 544 public int compareTo(Object obj) { 545 546 if (obj == this) { 547 return 0; 548 } 549 if (obj instanceof CmsResource) { 550 return m_rootPath.compareTo(((CmsResource)obj).m_rootPath); 551 } 552 return 0; 553 } 554 555 558 public boolean equals(Object obj) { 559 560 if (obj == this) { 561 return true; 562 } 563 if (obj instanceof CmsResource) { 564 return ((CmsResource)obj).m_structureId.equals(m_structureId); 565 } 566 return false; 567 } 568 569 574 public long getDateCreated() { 575 576 return m_dateCreated; 577 } 578 579 584 public long getDateExpired() { 585 586 return m_dateExpired; 587 } 588 589 594 public long getDateLastModified() { 595 596 return m_dateLastModified; 597 } 598 599 604 public long getDateReleased() { 605 606 return m_dateReleased; 607 } 608 609 614 public int getFlags() { 615 616 return m_flags; 617 } 618 619 627 public int getLength() { 628 629 return m_isFolder ? -1 : m_length; 631 } 632 633 638 public String getName() { 639 640 String name = getName(m_rootPath); 641 if (name.charAt(name.length() - 1) == '/') { 642 return name.substring(0, name.length() - 1); 643 } else { 644 return name; 645 } 646 } 647 648 653 public int getProjectLastModified() { 654 655 return m_projectLastModified; 656 } 657 658 663 public CmsUUID getResourceId() { 664 665 return m_resourceId; 666 } 667 668 684 public String getRootPath() { 685 686 return m_rootPath; 687 } 688 689 697 public int getSiblingCount() { 698 699 return m_siblingCount; 700 } 701 702 709 public int getState() { 710 711 return m_state; 712 } 713 714 719 public CmsUUID getStructureId() { 720 721 return m_structureId; 722 } 723 724 729 public int getTypeId() { 730 731 return m_typeId; 732 } 733 734 739 public CmsUUID getUserCreated() { 740 741 return m_userCreated; 742 } 743 744 749 public CmsUUID getUserLastModified() { 750 751 return m_userLastModified; 752 } 753 754 757 public int hashCode() { 758 759 if (m_structureId != null) { 760 return m_structureId.hashCode(); 761 } 762 763 return CmsUUID.getNullUUID().hashCode(); 764 } 765 766 771 public boolean isFile() { 772 773 return !m_isFolder; 774 } 775 776 781 public boolean isFolder() { 782 783 return m_isFolder; 784 } 785 786 793 public boolean isInternal() { 794 795 return ((m_flags & FLAG_INTERNAL) > 0); 796 } 797 798 805 public boolean isLabeled() { 806 807 return ((m_flags & CmsResource.FLAG_LABELED) > 0); 808 } 809 810 815 public boolean isTouched() { 816 817 return m_isTouched; 818 } 819 820 825 public void setDateExpired(long time) { 826 827 m_dateExpired = time; 828 } 829 830 835 public void setDateLastModified(long time) { 836 837 m_isTouched = true; 838 m_dateLastModified = time; 839 } 840 841 846 public void setDateReleased(long time) { 847 848 m_dateReleased = time; 849 } 850 851 856 public void setFlags(int flags) { 857 858 m_flags = flags; 859 } 860 861 866 public void setState(int state) { 867 868 m_state = state; 869 } 870 871 876 public void setType(int type) { 877 878 m_typeId = type; 879 } 880 881 886 public void setUserLastModified(CmsUUID resourceLastModifiedByUserId) { 887 888 m_userLastModified = resourceLastModifiedByUserId; 889 } 890 891 894 public String toString() { 895 896 StringBuffer result = new StringBuffer (); 897 898 result.append("["); 899 result.append(this.getClass().getName()); 900 result.append(", path: "); 901 result.append(m_rootPath); 902 result.append(", structure id "); 903 result.append(m_structureId); 904 result.append(", resource id: "); 905 result.append(m_resourceId); 906 result.append(", type id: "); 907 result.append(m_typeId); 908 result.append(", folder: "); 909 result.append(m_isFolder); 910 result.append(", flags: "); 911 result.append(m_flags); 912 result.append(", project: "); 913 result.append(m_projectLastModified); 914 result.append(", state: "); 915 result.append(m_state); 916 result.append(", date created: "); 917 result.append(new java.util.Date (m_dateCreated)); 918 result.append(", user created: "); 919 result.append(m_userCreated); 920 result.append(", date lastmodified: "); 921 result.append(new java.util.Date (m_dateLastModified)); 922 result.append(", user lastmodified: "); 923 result.append(m_userLastModified); 924 result.append(", date released: "); 925 result.append(new java.util.Date (m_dateReleased)); 926 result.append(", date expired: "); 927 result.append(new java.util.Date (m_dateExpired)); 928 result.append(", size: "); 929 result.append(m_length); 930 result.append(" sibling count: "); 931 result.append(m_siblingCount); 932 result.append("]"); 933 934 return result.toString(); 935 } 936 937 } | Popular Tags |