1 31 32 package org.opencms.file; 33 34 import org.opencms.main.CmsRuntimeException; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.io.Serializable ; 38 import java.util.ArrayList ; 39 import java.util.Collections ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.RandomAccess ; 45 46 92 public class CmsProperty implements Serializable , Cloneable , Comparable { 93 94 98 public static final int DELETE_OPTION_DELETE_RESOURCE_VALUES = 3; 99 100 104 public static final int DELETE_OPTION_DELETE_STRUCTURE_AND_RESOURCE_VALUES = 1; 105 106 110 public static final int DELETE_OPTION_DELETE_STRUCTURE_VALUES = 2; 111 112 116 public static final String DELETE_VALUE = new String (""); 117 118 122 public static final int RESOURCE_RECORD_MAPPING = 2; 123 124 128 public static final int STRUCTURE_RECORD_MAPPING = 1; 129 130 131 public static final String TYPE_INDIVIDUAL = "individual"; 132 133 134 public static final String TYPE_SHARED = "shared"; 135 136 137 public static final char VALUE_LIST_DELIMITER = '|'; 138 139 140 private static final CmsProperty NULL_PROPERTY = new CmsProperty(); 141 142 143 private static final long serialVersionUID = 93613508924212782L; 144 145 149 private boolean m_autoCreatePropertyDefinition; 150 151 152 private boolean m_frozen; 153 154 155 private String m_name; 156 157 158 private String m_resourceValue; 159 160 161 private List m_resourceValueList; 162 163 164 private String m_structureValue; 165 166 167 private List m_structureValueList; 168 169 175 public CmsProperty() { 176 177 } 179 180 193 public CmsProperty(String name, String structureValue, String resourceValue) { 194 195 this(name, structureValue, resourceValue, true); 196 } 197 198 210 public CmsProperty(String name, String structureValue, String resourceValue, boolean autoCreatePropertyDefinition) { 211 212 m_name = name; 213 m_structureValue = structureValue; 214 m_resourceValue = resourceValue; 215 m_autoCreatePropertyDefinition = autoCreatePropertyDefinition; 216 } 217 218 221 static { 222 223 NULL_PROPERTY.m_frozen = true; 224 NULL_PROPERTY.m_name = ""; 225 } 226 227 238 public static final CmsProperty get(String name, List list) { 239 240 CmsProperty property = null; 241 242 if (list instanceof RandomAccess ) { 244 for (int i = 0, n = list.size(); i < n; i++) { 245 property = (CmsProperty)list.get(i); 246 if (property.m_name.equals(name)) { 247 return property; 248 } 249 } 250 } else { 251 Iterator i = list.iterator(); 252 while (i.hasNext()) { 253 property = (CmsProperty)i.next(); 254 if (property.m_name.equals(name)) { 255 return property; 256 } 257 } 258 } 259 260 return NULL_PROPERTY; 261 } 262 263 268 public static final CmsProperty getNullProperty() { 269 270 return NULL_PROPERTY; 271 } 272 273 286 public static final List setAutoCreatePropertyDefinitions(List list, boolean value) { 287 288 CmsProperty property; 289 290 if (list instanceof RandomAccess ) { 292 for (int i = 0, n = list.size(); i < n; i++) { 293 property = (CmsProperty)list.get(i); 294 property.m_autoCreatePropertyDefinition = value; 295 } 296 } else { 297 Iterator i = list.iterator(); 298 while (i.hasNext()) { 299 property = (CmsProperty)i.next(); 300 property.m_autoCreatePropertyDefinition = value; 301 } 302 } 303 304 return list; 305 } 306 307 319 public static final List setFrozen(List list) { 320 321 CmsProperty property; 322 323 if (list instanceof RandomAccess ) { 325 for (int i = 0, n = list.size(); i < n; i++) { 326 property = (CmsProperty)list.get(i); 327 if (!property.isFrozen()) { 328 property.setFrozen(true); 329 } 330 } 331 } else { 332 Iterator i = list.iterator(); 333 while (i.hasNext()) { 334 property = (CmsProperty)i.next(); 335 if (!property.isFrozen()) { 336 property.setFrozen(true); 337 } 338 } 339 } 340 341 return list; 342 } 343 344 355 public static List toList(Map map) { 356 357 String name = null; 358 String value = null; 359 CmsProperty property = null; 360 List properties = null; 361 Object [] names = null; 362 363 if ((map == null) || (map.size() == 0)) { 364 return Collections.EMPTY_LIST; 365 } 366 367 properties = new ArrayList (map.size()); 368 names = map.keySet().toArray(); 369 for (int i = 0; i < names.length; i++) { 370 name = (String )names[i]; 371 value = (String )map.get(name); 372 373 property = new CmsProperty(); 374 property.m_name = name; 375 property.m_structureValue = value; 376 properties.add(property); 377 } 378 379 return properties; 380 } 381 382 393 public static Map toMap(List list) { 394 395 Map result = null; 396 String name = null; 397 String value = null; 398 CmsProperty property = null; 399 400 if ((list == null) || (list.size() == 0)) { 401 return Collections.EMPTY_MAP; 402 } 403 404 result = new HashMap (); 405 406 if (list instanceof RandomAccess ) { 408 for (int i = 0, n = list.size(); i < n; i++) { 409 property = (CmsProperty)list.get(i); 410 name = property.m_name; 411 value = property.getValue(); 412 result.put(name, value); 413 } 414 } else { 415 Iterator i = list.iterator(); 416 while (i.hasNext()) { 417 property = (CmsProperty)i.next(); 418 name = property.m_name; 419 value = property.getValue(); 420 result.put(name, value); 421 } 422 } 423 424 return result; 425 } 426 427 433 public boolean autoCreatePropertyDefinition() { 434 435 return m_autoCreatePropertyDefinition; 436 } 437 438 445 public Object clone() { 446 447 return cloneAsProperty(); 448 } 449 450 457 public CmsProperty cloneAsProperty() { 458 459 if (this == NULL_PROPERTY) { 460 return NULL_PROPERTY; 462 } 463 464 CmsProperty clone = new CmsProperty(); 465 466 clone.m_name = m_name; 467 clone.m_structureValue = m_structureValue; 468 clone.m_structureValueList = m_structureValueList; 469 clone.m_resourceValue = m_resourceValue; 470 clone.m_resourceValueList = m_resourceValueList; 471 clone.m_autoCreatePropertyDefinition = m_autoCreatePropertyDefinition; 472 474 return clone; 475 } 476 477 485 public int compareTo(Object obj) { 486 487 if (obj == this) { 488 return 0; 489 } 490 if (obj instanceof CmsProperty) { 491 return m_name.compareTo(((CmsProperty)obj).m_name); 492 } 493 return 0; 494 } 495 496 504 public boolean deleteResourceValue() { 505 506 return isDeleteResourceValue(); 507 } 508 509 517 public boolean deleteStructureValue() { 518 519 return isDeleteStructureValue(); 520 } 521 522 530 public boolean equals(Object obj) { 531 532 if (obj == this) { 533 return true; 534 } 535 if (obj instanceof CmsProperty) { 536 return ((CmsProperty)obj).m_name.equals(m_name); 537 } 538 return false; 539 } 540 541 548 public String getKey() { 549 550 return getName(); 551 } 552 553 558 public String getName() { 559 560 return m_name; 561 } 562 563 568 public String getResourceValue() { 569 570 return m_resourceValue; 571 } 572 573 582 public List getResourceValueList() { 583 584 if ((m_resourceValueList == null) && (m_resourceValue != null)) { 585 m_resourceValueList = createListFromValue(m_resourceValue); 587 m_resourceValueList = Collections.unmodifiableList(m_resourceValueList); 588 } 589 return m_resourceValueList; 590 } 591 592 597 public String getStructureValue() { 598 599 return m_structureValue; 600 } 601 602 611 public List getStructureValueList() { 612 613 if ((m_structureValueList == null) && (m_structureValue != null)) { 614 m_structureValueList = createListFromValue(m_structureValue); 616 m_structureValueList = Collections.unmodifiableList(m_structureValueList); 617 } 618 return m_structureValueList; 619 } 620 621 629 public String getValue() { 630 631 return (m_structureValue != null) ? m_structureValue : m_resourceValue; 632 } 633 634 645 public String getValue(String defaultValue) { 646 647 if (this == CmsProperty.NULL_PROPERTY) { 648 return defaultValue; 650 } 651 652 return (m_structureValue != null) ? m_structureValue : ((m_resourceValue != null) ? m_resourceValue 655 : defaultValue); 656 } 657 658 670 public List getValueList() { 671 672 return (m_structureValue != null) ? getStructureValueList() : getResourceValueList(); 673 } 674 675 686 public List getValueList(List defaultValue) { 687 688 if (this == CmsProperty.NULL_PROPERTY) { 689 return defaultValue; 691 } 692 693 return (m_structureValue != null) ? getStructureValueList() 696 : ((m_resourceValue != null) ? getResourceValueList() : defaultValue); 697 } 698 699 702 public int hashCode() { 703 704 StringBuffer strBuf = new StringBuffer (); 705 706 strBuf.append(m_name); 707 strBuf.append("_"); 708 strBuf.append(m_structureValue); 709 strBuf.append("_"); 710 strBuf.append(m_resourceValue); 711 712 return strBuf.toString().hashCode(); 713 } 714 715 722 public boolean isDeleteResourceValue() { 723 724 return (m_resourceValue == DELETE_VALUE) || ((m_resourceValue != null) && (m_resourceValue.length() == 0)); 725 } 726 727 734 public boolean isDeleteStructureValue() { 735 736 return (m_structureValue == DELETE_VALUE) || ((m_structureValue != null) && (m_structureValue.length() == 0)); 737 } 738 739 744 public boolean isFrozen() { 745 746 return m_frozen; 747 } 748 749 758 public boolean isIdentical(CmsProperty property) { 759 760 boolean isEqual; 761 762 if (m_name == null) { 764 isEqual = (property.getName() == null); 765 } else { 766 isEqual = m_name.equals(property.getName()); 767 } 768 769 if (m_structureValue == null) { 771 isEqual &= (property.getStructureValue() == null); 772 } else { 773 isEqual &= m_structureValue.equals(property.getStructureValue()); 774 } 775 776 if (m_resourceValue == null) { 778 isEqual &= (property.getResourceValue() == null); 779 } else { 780 isEqual &= m_resourceValue.equals(property.getResourceValue()); 781 } 782 783 return isEqual; 784 } 785 786 791 public boolean isNullProperty() { 792 793 return NULL_PROPERTY.equals(this); 794 } 795 796 802 public void setAutoCreatePropertyDefinition(boolean value) { 803 804 checkFrozen(); 805 m_autoCreatePropertyDefinition = value; 806 } 807 808 816 public void setFrozen(boolean frozen) { 817 818 if (!frozen) { 819 checkFrozen(); 820 } 821 m_frozen = frozen; 822 } 823 824 831 public void setKey(String name) { 832 833 checkFrozen(); 834 setName(name); 835 } 836 837 842 public void setName(String name) { 843 844 checkFrozen(); 845 m_name = name; 846 } 847 848 853 public void setResourceValue(String resourceValue) { 854 855 checkFrozen(); 856 m_resourceValue = resourceValue; 857 m_resourceValueList = null; 858 } 859 860 868 public void setResourceValueList(List valueList) { 869 870 checkFrozen(); 871 if (valueList != null) { 872 m_resourceValueList = new ArrayList (valueList); 873 m_resourceValueList = Collections.unmodifiableList(m_resourceValueList); 874 m_resourceValue = createValueFromList(m_resourceValueList); 875 } else { 876 m_resourceValueList = null; 877 m_resourceValue = null; 878 } 879 } 880 881 886 public void setStructureValue(String structureValue) { 887 888 checkFrozen(); 889 m_structureValue = structureValue; 890 m_structureValueList = null; 891 } 892 893 901 public void setStructureValueList(List valueList) { 902 903 checkFrozen(); 904 if (valueList != null) { 905 m_structureValueList = new ArrayList (valueList); 906 m_structureValueList = Collections.unmodifiableList(m_structureValueList); 907 m_structureValue = createValueFromList(m_structureValueList); 908 } else { 909 m_structureValueList = null; 910 m_structureValue = null; 911 } 912 } 913 914 925 public void setValue(String value, String type) { 926 927 checkFrozen(); 928 setAutoCreatePropertyDefinition(true); 929 if (TYPE_SHARED.equalsIgnoreCase(type)) { 930 setResourceValue(value); 932 } else { 933 setStructureValue(value); 935 } 936 } 937 938 943 public String toString() { 944 945 StringBuffer strBuf = new StringBuffer (); 946 947 strBuf.append("[").append(getClass().getName()).append(": "); 948 strBuf.append("name: '").append(m_name).append("'"); 949 strBuf.append(", value: '").append(getValue()).append("'"); 950 strBuf.append(", structure value: '").append(m_structureValue).append("'"); 951 strBuf.append(", resource value: '").append(m_resourceValue).append("'"); 952 strBuf.append(", frozen: ").append(m_frozen); 953 strBuf.append("]"); 954 955 return strBuf.toString(); 956 } 957 958 961 private void checkFrozen() { 962 963 if (m_frozen) { 964 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_PROPERTY_FROZEN_1, toString())); 965 } 966 } 967 968 977 private List createListFromValue(String value) { 978 979 if (value == null) { 980 return null; 981 } 982 return CmsStringUtil.splitAsList(value, VALUE_LIST_DELIMITER); 983 } 984 985 992 private String createValueFromList(List valueList) { 993 994 if (valueList == null) { 995 return null; 996 } 997 StringBuffer result = new StringBuffer (valueList.size() * 32); 998 Iterator i = valueList.iterator(); 999 while (i.hasNext()) { 1000 result.append(i.next().toString()); 1001 if (i.hasNext()) { 1002 result.append(VALUE_LIST_DELIMITER); 1003 } 1004 } 1005 return result.toString(); 1006 } 1007} | Popular Tags |