Search in sources :

Example 1 with CustomCollectionEditor

use of org.springframework.beans.propertyeditors.CustomCollectionEditor in project spring-framework by spring-projects.

the class SelectTagTests method withMultiListAndCustomEditor.

@Test
public void withMultiListAndCustomEditor() throws Exception {
    List list = new ArrayList();
    list.add(Country.COUNTRY_UK);
    list.add(Country.COUNTRY_AT);
    this.bean.setSomeList(list);
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(List.class, new CustomCollectionEditor(ArrayList.class) {

        @Override
        public String getAsText() {
            return getValue().toString();
        }
    });
    exposeBindingResult(errors);
    this.tag.setPath("someList");
    this.tag.setItems(Country.getCountries());
    this.tag.setItemValue("isoCode");
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    String output = getOutput();
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element rootElement = document.getRootElement();
    assertThat(rootElement.elements().size()).isEqualTo(2);
    Element selectElement = rootElement.element("select");
    assertThat(selectElement.getName()).isEqualTo("select");
    assertThat(selectElement.attribute("name").getValue()).isEqualTo("someList");
    List children = selectElement.elements();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
    Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
    assertThat(e.attribute("selected").getValue()).as("UK node not selected").isEqualTo("selected");
    e = (Element) selectElement.selectSingleNode("option[@value = 'AT']");
    assertThat(e.attribute("selected").getValue()).as("AT node not selected").isEqualTo("selected");
}
Also used : CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Example 2 with CustomCollectionEditor

use of org.springframework.beans.propertyeditors.CustomCollectionEditor in project spring-framework by spring-projects.

the class PropertyEditorRegistrySupport method createDefaultEditors.

/**
 * Actually register the default editors for this registry instance.
 */
private void createDefaultEditors() {
    this.defaultEditors = new HashMap<>(64);
    // Simple editors, without parameterization capabilities.
    // The JDK does not contain a default editor for any of these target types.
    this.defaultEditors.put(Charset.class, new CharsetEditor());
    this.defaultEditors.put(Class.class, new ClassEditor());
    this.defaultEditors.put(Class[].class, new ClassArrayEditor());
    this.defaultEditors.put(Currency.class, new CurrencyEditor());
    this.defaultEditors.put(File.class, new FileEditor());
    this.defaultEditors.put(InputStream.class, new InputStreamEditor());
    if (!shouldIgnoreXml) {
        this.defaultEditors.put(InputSource.class, new InputSourceEditor());
    }
    this.defaultEditors.put(Locale.class, new LocaleEditor());
    this.defaultEditors.put(Path.class, new PathEditor());
    this.defaultEditors.put(Pattern.class, new PatternEditor());
    this.defaultEditors.put(Properties.class, new PropertiesEditor());
    this.defaultEditors.put(Reader.class, new ReaderEditor());
    this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
    this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());
    this.defaultEditors.put(URI.class, new URIEditor());
    this.defaultEditors.put(URL.class, new URLEditor());
    this.defaultEditors.put(UUID.class, new UUIDEditor());
    this.defaultEditors.put(ZoneId.class, new ZoneIdEditor());
    // Default instances of collection editors.
    // Can be overridden by registering custom instances of those as custom editors.
    this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
    this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
    this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
    this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
    this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
    // Default editors for primitive arrays.
    this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
    this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());
    // The JDK does not contain a default editor for char!
    this.defaultEditors.put(char.class, new CharacterEditor(false));
    this.defaultEditors.put(Character.class, new CharacterEditor(true));
    // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
    this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
    this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
    // The JDK does not contain default editors for number wrapper types!
    // Override JDK primitive number editors with our own CustomNumberEditor.
    this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
    this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
    this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
    this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
    this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
    this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
    this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
    this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
    this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
    this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
    this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
    this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
    this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
    this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
    // Only register config value editors if explicitly requested.
    if (this.configValueEditorsActive) {
        StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
        this.defaultEditors.put(String[].class, sae);
        this.defaultEditors.put(short[].class, sae);
        this.defaultEditors.put(int[].class, sae);
        this.defaultEditors.put(long[].class, sae);
    }
}
Also used : FileEditor(org.springframework.beans.propertyeditors.FileEditor) SortedSet(java.util.SortedSet) Set(java.util.Set) CustomBooleanEditor(org.springframework.beans.propertyeditors.CustomBooleanEditor) InputStreamEditor(org.springframework.beans.propertyeditors.InputStreamEditor) LocaleEditor(org.springframework.beans.propertyeditors.LocaleEditor) InputSourceEditor(org.springframework.beans.propertyeditors.InputSourceEditor) SortedSet(java.util.SortedSet) ReaderEditor(org.springframework.beans.propertyeditors.ReaderEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) CharsetEditor(org.springframework.beans.propertyeditors.CharsetEditor) ZoneIdEditor(org.springframework.beans.propertyeditors.ZoneIdEditor) List(java.util.List) ArrayList(java.util.ArrayList) TimeZoneEditor(org.springframework.beans.propertyeditors.TimeZoneEditor) PropertiesEditor(org.springframework.beans.propertyeditors.PropertiesEditor) UUIDEditor(org.springframework.beans.propertyeditors.UUIDEditor) CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) CurrencyEditor(org.springframework.beans.propertyeditors.CurrencyEditor) Resource(org.springframework.core.io.Resource) URIEditor(org.springframework.beans.propertyeditors.URIEditor) ByteArrayPropertyEditor(org.springframework.beans.propertyeditors.ByteArrayPropertyEditor) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) CharArrayPropertyEditor(org.springframework.beans.propertyeditors.CharArrayPropertyEditor) BigDecimal(java.math.BigDecimal) PathEditor(org.springframework.beans.propertyeditors.PathEditor) CharacterEditor(org.springframework.beans.propertyeditors.CharacterEditor) BigInteger(java.math.BigInteger) CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) PatternEditor(org.springframework.beans.propertyeditors.PatternEditor) SortedMap(java.util.SortedMap) Collection(java.util.Collection) BigInteger(java.math.BigInteger) CustomMapEditor(org.springframework.beans.propertyeditors.CustomMapEditor) URLEditor(org.springframework.beans.propertyeditors.URLEditor)

Example 3 with CustomCollectionEditor

use of org.springframework.beans.propertyeditors.CustomCollectionEditor in project spring-framework by spring-projects.

the class DataBinderTests method testBindingStringArrayToIntegerSet.

@Test
void testBindingStringArrayToIntegerSet() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) {

        @Override
        protected Object convertElement(Object element) {
            return Integer.valueOf(element.toString());
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("set", new String[] { "10", "20", "30" });
    binder.bind(pvs);
    assertThat(binder.getBindingResult().getFieldValue("set")).isEqualTo(tb.getSet());
    boolean condition = tb.getSet() instanceof TreeSet;
    assertThat(condition).isTrue();
    assertThat(tb.getSet().size()).isEqualTo(3);
    assertThat(tb.getSet().contains(10)).isTrue();
    assertThat(tb.getSet().contains(20)).isTrue();
    assertThat(tb.getSet().contains(30)).isTrue();
    pvs = new MutablePropertyValues();
    pvs.add("set", null);
    binder.bind(pvs);
    assertThat(tb.getSet()).isNull();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) TreeSet(java.util.TreeSet) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Example 4 with CustomCollectionEditor

use of org.springframework.beans.propertyeditors.CustomCollectionEditor in project spring-framework by spring-projects.

the class DataBinderTests method testBindingNullToEmptyCollection.

@Test
void testBindingNullToEmptyCollection() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("set", null);
    binder.bind(pvs);
    boolean condition = tb.getSet() instanceof TreeSet;
    assertThat(condition).isTrue();
    assertThat(tb.getSet().isEmpty()).isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) TreeSet(java.util.TreeSet) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Example 5 with CustomCollectionEditor

use of org.springframework.beans.propertyeditors.CustomCollectionEditor in project Asqatasun by Asqatasun.

the class UserManagementController method initBinder.

@InitBinder
@Override
protected void initBinder(WebDataBinder binder) {
    super.initBinder(binder);
    binder.registerCustomEditor(Collection.class, "userList", new CustomCollectionEditor(Collection.class) {

        @Override
        protected Object convertElement(Object element) {
            Long id = null;
            if (element instanceof String && !((String) element).equals("")) {
                // From the JSP 'element' will be a String
                try {
                    id = Long.parseLong((String) element);
                } catch (NumberFormatException e) {
                    LoggerFactory.getLogger(this.getClass()).warn("Element was " + element);
                }
            } else if (element instanceof Long) {
                // From the database 'element' will be a Long
                id = (Long) element;
            }
            return id != null ? userDataService.read(id) : null;
        }
    });
}
Also used : CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) Collection(java.util.Collection)

Aggregations

CustomCollectionEditor (org.springframework.beans.propertyeditors.CustomCollectionEditor)5 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)2 StringReader (java.io.StringReader)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 SortedSet (java.util.SortedSet)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 ByteArrayPropertyEditor (org.springframework.beans.propertyeditors.ByteArrayPropertyEditor)1 CharArrayPropertyEditor (org.springframework.beans.propertyeditors.CharArrayPropertyEditor)1 CharacterEditor (org.springframework.beans.propertyeditors.CharacterEditor)1