View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.integration.beans;
21  
22  import java.beans.PropertyEditor;
23  import java.lang.reflect.Array;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.regex.Matcher;
27  
28  /**
29   * A {@link PropertyEditor} which converts a {@link String} into
30   * a one-dimensional array and vice versa.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class ArrayEditor extends AbstractPropertyEditor {
35      private final Class<?> componentType;
36  
37      public ArrayEditor(Class<?> componentType) {
38          if (componentType == null) {
39              throw new IllegalArgumentException("componentType");
40          }
41  
42          this.componentType = componentType;
43          getComponentEditor();
44          setTrimText(false);
45      }
46  
47      private PropertyEditor getComponentEditor() {
48          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
49          if (e == null) {
50              throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for "
51                      + componentType.getSimpleName() + '.');
52          }
53          return e;
54      }
55  
56      @Override
57      protected String toText(Object value) {
58          Class<?> componentType = value.getClass().getComponentType();
59          if (componentType == null) {
60              throw new IllegalArgumentException("not an array: " + value);
61          }
62  
63          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
64          if (e == null) {
65              throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for "
66                      + componentType.getSimpleName() + '.');
67          }
68  
69          StringBuilder buf = new StringBuilder();
70          for (int i = 0; i < Array.getLength(value); i++) {
71              e.setValue(Array.get(value, i));
72              // TODO normalize.
73              String s = e.getAsText();
74              buf.append(s);
75              buf.append(", ");
76          }
77  
78          // Remove the last delimiter.
79          if (buf.length() >= 2) {
80              buf.setLength(buf.length() - 2);
81          }
82          return buf.toString();
83      }
84  
85      @Override
86      protected Object toValue(String text) throws IllegalArgumentException {
87          PropertyEditor e = getComponentEditor();
88          List<Object> values = new ArrayList<Object>();
89          Matcher m = CollectionEditor.ELEMENT.matcher(text);
90          boolean matchedDelimiter = true;
91  
92          while (m.find()) {
93              if (m.group(1) != null) {
94                  matchedDelimiter = true;
95                  continue;
96              }
97  
98              if (!matchedDelimiter) {
99                  throw new IllegalArgumentException("No delimiter between elements: " + text);
100             }
101 
102             // TODO escape here.
103             e.setAsText(m.group());
104             values.add(e.getValue());
105 
106             matchedDelimiter = false;
107             if (m.group(2) != null || m.group(3) != null) {
108                 // Skip the last '"'.
109                 m.region(m.end() + 1, m.regionEnd());
110             }
111         }
112 
113         Object answer = Array.newInstance(componentType, values.size());
114         for (int i = 0; i < Array.getLength(answer); i++) {
115             Array.set(answer, i, values.get(i));
116         }
117         return answer;
118     }
119 }