1 16 17 package org.springframework.web.servlet.tags.form; 18 19 import org.springframework.core.enums.LabeledEnum; 20 import org.springframework.util.CollectionUtils; 21 import org.springframework.util.ObjectUtils; 22 import org.springframework.web.servlet.support.BindStatus; 23 24 import java.beans.PropertyEditor ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 59 abstract class SelectedValueComparator { 60 61 66 public static boolean isSelected(BindStatus bindStatus, Object candidateValue) { 67 Object boundValue = getBoundValue(bindStatus); 68 69 if (boundValue == null) { 70 return (candidateValue == null); 71 } 72 73 boolean selected = false; 74 75 if (boundValue.getClass().isArray()) { 76 selected = collectionCompare(CollectionUtils.arrayToList(boundValue), candidateValue, bindStatus); 77 } 78 else if (boundValue instanceof Collection ) { 79 selected = collectionCompare((Collection ) boundValue, candidateValue, bindStatus); 80 } 81 else if (boundValue instanceof Map ) { 82 selected = mapCompare((Map ) boundValue, candidateValue, bindStatus); 83 } 84 85 if (!selected) { 86 if (ObjectUtils.nullSafeEquals(boundValue, candidateValue)) { 87 selected = true; 88 } 89 else { 90 selected = exhaustiveCompare(boundValue, candidateValue, bindStatus.getEditor()); 91 } 92 } 93 94 return selected; 95 } 96 97 98 private static boolean mapCompare(Map boundMap, Object candidateValue, BindStatus bindStatus) { 99 if (boundMap.containsKey(candidateValue)) { 100 return true; 101 } 102 else { 103 return exhaustiveCollectionCompare(boundMap.keySet(), candidateValue, bindStatus.getEditor()); 104 } 105 } 106 107 private static boolean collectionCompare(Collection boundCollection, Object candidateValue, BindStatus bindStatus) { 108 if (boundCollection.contains(candidateValue)) { 109 return true; 110 } 111 else { 112 return exhaustiveCollectionCompare(boundCollection, candidateValue, bindStatus.getEditor()); 113 } 114 } 115 116 private static Object getBoundValue(BindStatus bindStatus) { 117 if (bindStatus == null) { 118 return null; 119 } 120 if (bindStatus.getEditor() != null) { 121 Object editorValue = bindStatus.getEditor().getValue(); 122 if (editorValue != null) { 123 return editorValue; 124 } 125 } 126 return bindStatus.getValue(); 127 } 128 129 private static boolean exhaustiveCollectionCompare( 130 Collection collection, Object candidateValue, PropertyEditor propertyEditor) { 131 132 for (Iterator iterator = collection.iterator(); iterator.hasNext();) { 133 Object o = iterator.next(); 134 if (exhaustiveCompare(o, candidateValue, propertyEditor)) { 135 return true; 136 } 137 } 138 return false; 139 } 140 141 private static boolean exhaustiveCompare(Object boundValue, Object candidate, PropertyEditor propertyEditor) { 142 String candidateDisplayString = ObjectUtils.getDisplayString(candidate); 143 if (boundValue instanceof LabeledEnum) { 144 LabeledEnum labeledEnum = (LabeledEnum) boundValue; 145 String enumCodeAsString = ObjectUtils.getDisplayString(labeledEnum.getCode()); 146 if (enumCodeAsString.equals(candidateDisplayString)) { 147 return true; 148 } 149 String enumLabelAsString = ObjectUtils.getDisplayString(labeledEnum.getLabel()); 150 if (enumLabelAsString.equals(candidateDisplayString)) { 151 return true; 152 } 153 } 154 else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) { 155 return true; 156 } 157 else if (propertyEditor != null && candidate instanceof String ) { 158 159 Object originalValue = propertyEditor.getValue(); 161 String candidateAsString = (String ) candidate; 162 try { 163 propertyEditor.setAsText(candidateAsString); 164 if (ObjectUtils.nullSafeEquals(boundValue, propertyEditor.getValue())) { 165 return true; 166 } 167 } 168 finally { 169 propertyEditor.setValue(originalValue); 170 } 171 172 if (propertyEditor.getValue() != null) { 173 return ObjectUtils.nullSafeEquals(candidateAsString, propertyEditor.getAsText()); 174 } 175 } 176 return false; 177 } 178 179 } 180 | Popular Tags |