1 16 package org.apache.cocoon.portal.pluto.om.common; 17 18 import java.util.Collection ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 import javax.portlet.PreferencesValidator; 25 26 import org.apache.pluto.om.common.Preference; 27 import org.apache.pluto.om.common.PreferenceSet; 28 import org.apache.pluto.om.common.PreferenceSetCtrl; 29 import org.apache.pluto.util.StringUtils; 30 31 38 public class PreferenceSetImpl 39 implements PreferenceSet, PreferenceSetCtrl, java.io.Serializable { 40 41 private String castorPreferencesValidator; 42 private ClassLoader classLoader; 43 private Set preferences = new HashSet (); 44 45 48 public Preference get(String name) { 49 Iterator iterator = this.preferences.iterator(); 50 while (iterator.hasNext()) { 51 Preference preference = (Preference)iterator.next(); 52 if (preference.getName().equals(name)) { 53 return preference; 54 } 55 } 56 return null; 57 } 58 59 62 public Iterator iterator() { 63 return this.preferences.iterator(); 64 } 65 66 69 public PreferencesValidator getPreferencesValidator() { 70 if (this.classLoader == null) 71 throw new IllegalStateException ("Portlet class loader not yet available to load preferences validator."); 72 73 if (castorPreferencesValidator == null) 74 return null; 75 76 try { 77 Object validator = classLoader.loadClass(castorPreferencesValidator).newInstance(); 78 if (validator instanceof PreferencesValidator) { 79 return(PreferencesValidator)validator; 80 } 81 } catch (Exception ignore) { 82 } 84 85 return null; 86 } 87 88 91 public Preference add(String name, List values) { 92 PreferenceImpl preference = new PreferenceImpl(); 93 preference.setName(name); 94 preference.setValues(values); 95 96 this.preferences.add(preference); 97 98 return preference; 99 } 100 101 public boolean add(Preference preference) { 102 return this.preferences.add(preference); 103 } 104 105 108 public Preference remove(String name) { 109 Iterator iterator = this.iterator(); 110 while (iterator.hasNext()) { 111 Preference preference = (Preference)iterator.next(); 112 if (preference.getName().equals(name)) { 113 this.preferences.remove(preference); 114 return preference; 115 } 116 } 117 return null; 118 } 119 120 123 public void remove(Preference preference) { 124 this.preferences.remove(preference); 125 } 126 127 130 public String toString() { 131 StringBuffer buffer = new StringBuffer (50); 132 buffer.append(StringUtils.lineSeparator); 133 buffer.append(getClass().toString()); 134 buffer.append(": "); 135 Iterator iterator = this.iterator(); 136 while (iterator.hasNext()) { 137 buffer.append(((PreferenceImpl)iterator.next()).toString(2)); 138 } 139 return buffer.toString(); 140 } 141 142 143 145 public String getCastorPreferencesValidator() { 146 return castorPreferencesValidator; 147 } 148 149 public void setCastorPreferencesValidator(String castorPreferencesValidator) { 150 this.castorPreferencesValidator = castorPreferencesValidator; 151 } 152 153 public Set getPreferences() { 154 return this.preferences; 155 } 156 157 public void setClassLoader(ClassLoader loader) { 158 this.classLoader = loader; 159 } 160 161 165 public boolean addAll(Collection c) { 166 boolean changed = false; 167 Iterator it = c.iterator(); 168 while (it.hasNext()) { 169 changed = true; 170 PreferenceImpl pref = (PreferenceImpl) it.next(); 171 this.add(pref.getName(), pref.getClonedCastorValuesAsList()); 172 } 173 174 return changed; 175 } 176 177 } 178 | Popular Tags |