001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.propertyeditor;
018    
019    import java.beans.PropertyEditor;
020    import java.util.Map;
021    
022    /**
023     * @version $Rev: 6680 $ $Date: 2005-12-24T04:38:27.427468Z $
024     */
025    public abstract class AbstractMapConverter extends AbstractConverter {
026        private final PropertyEditor keyEditor;
027        private final PropertyEditor valueEditor;
028    
029        public AbstractMapConverter(Class type) {
030            super(type);
031            this.keyEditor = new StringEditor();
032            this.valueEditor = new StringEditor();
033        }
034    
035        protected AbstractMapConverter(Class type, PropertyEditor keyEditor, PropertyEditor valueEditor) {
036            super(type);
037            this.keyEditor = keyEditor;
038            this.valueEditor = valueEditor;
039        }
040    
041        /**
042         * Treats the text value of this property as an input stream that
043         * is converted into a Property bundle.
044         *
045         * @return a Properties object
046         * @throws PropertyEditorException An error occurred creating the Properties object.
047         */
048        protected final Object toObjectImpl(String text) {
049            Map map = CollectionUtil.toMap(text, keyEditor, valueEditor);
050            if (map == null) {
051                return null;
052            }
053            Map finalMap = createMap(map);
054            return finalMap;
055        }
056    
057        protected abstract Map createMap(Map map);
058    
059        protected final String toStringImpl(Object value) {
060            Map map = (Map) value;
061            String text = CollectionUtil.toString(map, keyEditor, valueEditor);
062            return text;
063        }
064    }