| Home >> All >> com >> xpn >> xwiki >> [ objects Javadoc ] |
Source code: com/xpn/xwiki/objects/ListProperty.java
1 /** 2 * =================================================================== 3 * 4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details, published at 15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the 16 * root folder of this distribution. 17 18 * Created by 19 * User: Ludovic Dubost 20 * Date: 1 févr. 2004 21 * Time: 21:40:32 22 */ 23 package com.xpn.xwiki.objects; 24 25 import org.hibernate.collection.PersistentCollection; 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.ecs.filter.CharacterFilter; 28 import org.dom4j.Element; 29 import org.dom4j.dom.DOMElement; 30 31 import java.util.ArrayList; 32 import java.util.Iterator; 33 import java.util.List; 34 35 public class ListProperty extends BaseProperty { 36 protected List list = new ArrayList(); 37 38 public Object getValue() { 39 return getList(); 40 } 41 42 public void setValue(Object value) { 43 this.setList((List)value); 44 } 45 46 public String getTextValue() { 47 return toFormString(); 48 } 49 50 public String toText() { 51 if ((getList() instanceof PersistentCollection) 52 &&(!((PersistentCollection)getList()).wasInitialized())) 53 return ""; 54 else 55 return StringUtils.join(getList().toArray(), " "); 56 } 57 58 public String toSingleFormString() { 59 return super.toFormString(); 60 } 61 62 public String toFormString() { 63 CharacterFilter filter = new CharacterFilter(); 64 filter.addAttribute("|", "\\|"); 65 66 List list = getList(); 67 Iterator it = list.iterator(); 68 if (!it.hasNext()) { 69 return ""; 70 } 71 StringBuffer result = new StringBuffer(); 72 result.append(it.next()); 73 while (it.hasNext()) { 74 result.append("|"); 75 result.append(filter.process((String)it.next())); 76 } 77 return result.toString(); 78 } 79 80 public boolean equals(Object obj) { 81 if (!super.equals(obj)) 82 return false; 83 84 List list1 = (List) getValue(); 85 List list2 = (List) ((BaseProperty)obj).getValue(); 86 87 // If the collection was not yet initialized by Hibernate 88 // Let's use the super result.. 89 if ((list1 instanceof PersistentCollection) 90 &&(!((PersistentCollection)list1).wasInitialized())) 91 return true; 92 93 if ((list2 instanceof PersistentCollection) 94 &&(!((PersistentCollection)list2).wasInitialized())) 95 return true; 96 97 if (list1.size()!=list2.size()) 98 return false; 99 100 for (int i=0;i<list1.size();i++) { 101 Object obj1 = list1.get(i); 102 Object obj2 = list2.get(i); 103 104 if (!obj1.equals(obj2)) 105 return false; 106 } 107 return true; 108 } 109 110 111 public Object clone() { 112 ListProperty property = (ListProperty) super.clone(); 113 List list = new ArrayList(); 114 for (Iterator it=getList().iterator();it.hasNext();) { 115 list.add(it.next()); 116 } 117 property.setValue(list); 118 return property; 119 } 120 121 public List getList() { 122 return list; 123 } 124 125 public void setList(List list) { 126 if (list==null) 127 this.list = new ArrayList(); 128 else 129 this.list = list; 130 } 131 132 public Element toXML() { 133 Element el = new DOMElement(getName()); 134 List list = (List)getValue(); 135 for (int i=0;i<list.size();i++) { 136 String value = list.get(i).toString(); 137 Element vel = new DOMElement("value"); 138 vel.setText((value==null) ? "" : value.toString()); 139 el.add(vel); 140 } 141 return el; 142 } 143 144 // This is important.. Otherwise we can get a stackoverflow calling toXML() 145 public String toString() { 146 if ((getList() instanceof PersistentCollection) 147 &&(!((PersistentCollection)getList()).wasInitialized())) 148 return ""; 149 return toXMLString(); 150 } 151 152 }