View Javadoc

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  package org.apache.xbean.propertyeditor;
18  
19  import java.beans.PropertyEditor;
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  import java.util.Collection;
25  import java.util.Properties;
26  import java.util.Map;
27  import java.util.LinkedHashMap;
28  import java.io.ByteArrayInputStream;
29  import java.io.IOException;
30  import java.io.ByteArrayOutputStream;
31  
32  /**
33   * @version $Rev: 6680 $ $Date: 2005-12-24T04:38:27.427468Z $
34   */
35  public final class CollectionUtil {
36      public static List toList(String text, PropertyEditor componentEditor) {
37          if (text.length() == 0) {
38              return null;
39          }
40  
41          // text may be surrounded with [ and ]
42          if (text.startsWith("[") && text.endsWith("]")) {
43              text = text.substring(1, text.length() - 1).trim();
44          }
45  
46          List list = new LinkedList();
47  
48          if (text.length() > 0) {
49              StringTokenizer stok = new StringTokenizer(text, ",");
50              while (stok.hasMoreTokens()) {
51                  String innerText = stok.nextToken();
52                  Object value = componentToObject(innerText, componentEditor);
53                  list.add(value);
54              }
55          }
56  
57          return list;
58      }
59  
60      public static String toString(Collection values, PropertyEditor componentEditor) {
61          if (values.size() == 0) {
62              return "[]";
63          }
64  
65          StringBuffer result = new StringBuffer();
66          result.append("[");
67          int i = 0;
68          for (Iterator iterator = values.iterator(); iterator.hasNext();) {
69              Object object = iterator.next();
70              String text = componentToString(object, componentEditor);
71  
72              if (i > 0) {
73                  result.append(",");
74              }
75              result.append(text);
76              i++;
77          }
78          result.append("]");
79          return result.toString();
80      }
81  
82      public static final Map toMap(String text, PropertyEditor keyEditor, PropertyEditor valueEditor) {
83          Properties properties = new Properties();
84          try {
85              ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes());
86              properties.load(stream);
87          } catch (IOException e) {
88              // any errors here are just a property exception
89              throw new PropertyEditorException(e);
90          }
91  
92          // run the properties through the editors
93          Map map = new LinkedHashMap(properties.size());
94          for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
95              Map.Entry entry = (Map.Entry) iterator.next();
96              String keyText = (String) entry.getKey();
97              String valueText = (String) entry.getValue();
98  
99              Object keyObject = componentToObject(keyText, keyEditor);
100             Object valueObject = componentToObject(valueText, valueEditor);
101 
102             map.put(keyObject, valueObject);
103         }
104         return map;
105     }
106 
107     public static final String toString(Map map, PropertyEditor keyEditor, PropertyEditor valueEditor) {
108         // run the properties through the editors
109         Properties properties = new Properties();
110         for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
111             Map.Entry entry = (Map.Entry) iterator.next();
112             Object keyObject = entry.getKey();
113             Object valueObject = entry.getValue();
114 
115             String keyText = componentToString(keyObject, keyEditor);
116             String valueText = componentToString(valueObject, valueEditor);
117 
118             properties.setProperty(keyText, valueText);
119         }
120 
121         // write using the properties object
122         try {
123             ByteArrayOutputStream out = new ByteArrayOutputStream();
124             properties.store(out, null);
125             String text = new String(out.toByteArray());
126             return text;
127         } catch (IOException e) {
128             // any errors here are just a property exception
129             throw new PropertyEditorException(e);
130         }
131     }
132 
133     private static final String componentToString(Object value, PropertyEditor componentEditor) {
134         if (value == null) {
135             return null;
136         }
137         if (componentEditor instanceof Converter) {
138             Converter converter = (Converter) componentEditor;
139             Class type = converter.getType();
140             if (!type.isInstance(value)) {
141                 throw new PropertyEditorException("Value is not an instance of " + type.getSimpleName() + ": " + value.getClass().getName());
142             }
143             return converter.toString(value);
144         } else {
145             componentEditor.setValue(value);
146             String text = componentEditor.getAsText();
147             return text;
148         }
149     }
150 
151     private static final Object componentToObject(String text, PropertyEditor componentEditor) {
152         if (text == null) {
153             return null;
154         }
155 
156         if (componentEditor instanceof Converter) {
157             Converter converter = (Converter) componentEditor;
158             Object value = converter.toObject(text.trim());
159             return value;
160         } else {
161             componentEditor.setAsText(text);
162             Object value = componentEditor.getValue();
163             return value;
164         }
165     }
166 }