1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import javax.servlet.jsp.JspException ; 24 25 import org.springframework.beans.BeanWrapper; 26 import org.springframework.beans.BeanWrapperImpl; 27 import org.springframework.util.Assert; 28 import org.springframework.util.CollectionUtils; 29 import org.springframework.web.servlet.support.BindStatus; 30 31 58 final class OptionWriter { 59 60 private final ValueFormatter valueFormatter = new ValueFormatter(); 61 62 private final Object optionSource; 63 64 private final BindStatus bindStatus; 65 66 private final String valueProperty; 67 68 private final String labelProperty; 69 70 private final boolean htmlEscape; 71 72 73 82 public OptionWriter( 83 Object optionSource, BindStatus bindStatus, String valueProperty, String labelProperty, boolean htmlEscape) { 84 85 Assert.notNull(optionSource, "'optionSource' must not be null"); 86 Assert.notNull(bindStatus, "'bindStatus' must not be null"); 87 this.optionSource = optionSource; 88 this.bindStatus = bindStatus; 89 this.valueProperty = valueProperty; 90 this.labelProperty = labelProperty; 91 this.htmlEscape = htmlEscape; 92 } 93 94 98 public void writeOptions(TagWriter tagWriter) throws JspException { 99 if (this.optionSource.getClass().isArray()) { 100 renderFromArray(tagWriter); 101 } 102 else if (this.optionSource instanceof Collection ) { 103 renderFromCollection(tagWriter); 104 } 105 else if (this.optionSource instanceof Map ) { 106 renderFromMap(tagWriter); 107 } 108 else { 109 throw new JspException ( 110 "Type [" + this.optionSource.getClass().getName() + "] is not valid for option items"); 111 } 112 } 113 114 118 private void renderFromArray(TagWriter tagWriter) throws JspException { 119 doRenderFromCollection(CollectionUtils.arrayToList(this.optionSource), tagWriter); 120 } 121 122 127 private void renderFromMap(TagWriter tagWriter) throws JspException { 128 Map optionMap = (Map ) this.optionSource; 129 for (Iterator iterator = optionMap.entrySet().iterator(); iterator.hasNext();) { 130 Map.Entry entry = (Map.Entry ) iterator.next(); 131 renderOption(tagWriter, entry, entry.getKey().toString(), entry.getValue().toString()); 132 } 133 } 134 135 139 private void renderFromCollection(TagWriter tagWriter) throws JspException { 140 doRenderFromCollection((Collection ) this.optionSource, tagWriter); 141 } 142 143 149 private void doRenderFromCollection(Collection optionCollection, TagWriter tagWriter) throws JspException { 150 for (Iterator it = optionCollection.iterator(); it.hasNext();) { 151 Object item = it.next(); 152 BeanWrapper wrapper = new BeanWrapperImpl(item); 153 Object value = (this.valueProperty != null ? wrapper.getPropertyValue(this.valueProperty) : item); 154 Object label = (this.labelProperty != null ? wrapper.getPropertyValue(this.labelProperty) : item); 155 renderOption(tagWriter, item, value, label); 156 } 157 } 158 159 163 private void renderOption(TagWriter tagWriter, Object item, Object value, Object label) throws JspException { 164 tagWriter.startTag("option"); 165 166 String valueDisplayString = getDisplayString(value); 167 String labelDisplayString = getDisplayString(label); 168 169 tagWriter.writeAttribute("value", valueDisplayString); 171 172 if (isSelected(value) || isSelected(item)) { 173 tagWriter.writeAttribute("selected", "selected"); 174 } 175 tagWriter.appendValue(labelDisplayString); 176 tagWriter.endTag(); 177 } 178 179 183 private boolean isSelected(Object resolvedValue) { 184 return SelectedValueComparator.isSelected(this.bindStatus, resolvedValue); 185 } 186 187 192 private String getDisplayString(Object value) { 193 return this.valueFormatter.getDisplayString(value, this.bindStatus.getEditor(), this.htmlEscape); 194 } 195 196 } 197 | Popular Tags |