KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > common > propertyeditor > MapEditor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.common.propertyeditor;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * A property editor for {@link java.util.Map}.
31  *
32  * @version $Rev: 479586 $ $Date: 2006-11-27 05:54:38 -0500 (Mon, 27 Nov 2006) $
33  */

34 public class MapEditor
35    extends TextPropertyEditorSupport
36 {
37     private static final Log log = LogFactory.getLog(MapEditor.class);
38     /**
39      *
40      * @throws PropertyEditorException An IOException occured.
41      */

42     public void setAsText(String JavaDoc text) {
43         try {
44             ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(text == null? new byte[0]: text.getBytes());
45             Properties JavaDoc p = new Properties JavaDoc();
46             p.load(is);
47             
48             setValue((Map JavaDoc)p);
49         } catch (IOException JavaDoc e) {
50             throw new PropertyEditorException(e);
51         }
52     }
53
54     public String JavaDoc getAsText() {
55         Map JavaDoc map = (Map JavaDoc) getValue();
56         if (!(map instanceof Properties JavaDoc)) {
57             Properties JavaDoc p = new Properties JavaDoc();
58             if(map != null) {
59                 if(!map.containsKey(null) && !map.containsValue(null)) {
60                     p.putAll(map);
61                 } else {
62                     // Map contains null keys or values. Replace null with empty string.
63
log.warn("Map contains null keys or values. Replacing null values with empty string.");
64                     for(Iterator JavaDoc itr = map.entrySet().iterator(); itr.hasNext(); ) {
65                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) itr.next();
66                         Object JavaDoc key = entry.getKey();
67                         Object JavaDoc value = entry.getValue();
68                         if(key == null) {
69                             key = "";
70                         }
71                         if(value == null) {
72                             value = "";
73                         }
74                         p.put(key, value);
75                     }
76                 }
77                 map = p;
78             }
79         }
80         PropertiesEditor pe = new PropertiesEditor();
81         pe.setValue(map);
82         return pe.getAsText();
83     }
84 }
85
Popular Tags