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    
018    package org.apache.geronimo.common.propertyeditor;
019    
020    import java.io.ByteArrayInputStream;
021    import java.io.IOException;
022    import java.util.Iterator;
023    import java.util.Properties;
024    import java.util.Map;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * A property editor for {@link java.util.Map}.
031     *
032     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
033     */
034    public class MapEditor
035       extends TextPropertyEditorSupport
036    {
037        private static final Log log = LogFactory.getLog(MapEditor.class);
038        /**
039         *
040         * @throws PropertyEditorException  An IOException occured.
041         */
042        public void setAsText(String text) {
043            try {
044                ByteArrayInputStream is = new ByteArrayInputStream(text == null? new byte[0]: text.getBytes());
045                Properties p = new Properties();
046                p.load(is);
047                
048                setValue((Map)p);
049            } catch (IOException e) {
050                throw new PropertyEditorException(e.getMessage(), e);
051            }
052        }
053    
054        public String getAsText() {
055            Map map = (Map) getValue();
056            if (!(map instanceof Properties)) {
057                Properties p = new Properties();
058                if(map != null) {
059                    if(!map.containsKey(null) && !map.containsValue(null)) {
060                        p.putAll(map);
061                    } else {
062                        // Map contains null keys or values.  Replace null with empty string.
063                        log.warn("Map contains null keys or values.  Replacing null values with empty string.");
064                        for(Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) {
065                            Map.Entry entry = (Map.Entry) itr.next();
066                            Object key = entry.getKey();
067                            Object value = entry.getValue();
068                            if(key == null) {
069                                key = "";
070                            }
071                            if(value == null) {
072                                value = "";
073                            }
074                            p.put(key, value);
075                        }
076                    }
077                    map = p;
078                }
079            }
080            PropertiesEditor pe = new PropertiesEditor();
081            pe.setValue(map);
082            return pe.getAsText();
083        }
084    }