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      /**
38       * Creates a new ArrayEditor instance
39       * 
40       * @param componentType The component type
41       */
42      public ArrayEditor(Class<?> componentType) {
43          if (componentType == null) {
44              throw new IllegalArgumentException("componentType");
45          }
46  
47          this.componentType = componentType;
48          getComponentEditor();
49          setTrimText(false);
50      }
51  
52      private PropertyEditor getComponentEditor() {
53          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
54          
55          if (e == null) {
56              throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for "
57                      + componentType.getSimpleName() + '.');
58          }
59          
60          return e;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      protected String toText(Object value) {
68          Class<?> componentType = value.getClass().getComponentType();
69          
70          if (componentType == null) {
71              throw new IllegalArgumentException("not an array: " + value);
72          }
73  
74          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
75          
76          if (e == null) {
77              throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for "
78                      + componentType.getSimpleName() + '.');
79          }
80  
81          StringBuilder buf = new StringBuilder();
82          
83          for (int i = 0; i < Array.getLength(value); i++) {
84              e.setValue(Array.get(value, i));
85              // TODO normalize.
86              String s = e.getAsText();
87              buf.append(s);
88              buf.append(", ");
89          }
90  
91          // Remove the last delimiter.
92          if (buf.length() >= 2) {
93              buf.setLength(buf.length() - 2);
94          }
95          
96          return buf.toString();
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     protected Object toValue(String text) throws IllegalArgumentException {
104         PropertyEditor e = getComponentEditor();
105         List<Object> values = new ArrayList<Object>();
106         Matcher m = CollectionEditor.ELEMENT.matcher(text);
107         boolean matchedDelimiter = true;
108 
109         while (m.find()) {
110             if (m.group(1) != null) {
111                 matchedDelimiter = true;
112                 continue;
113             }
114 
115             if (!matchedDelimiter) {
116                 throw new IllegalArgumentException("No delimiter between elements: " + text);
117             }
118 
119             // TODO escape here.
120             e.setAsText(m.group());
121             values.add(e.getValue());
122 
123             matchedDelimiter = false;
124             if (m.group(2) != null || m.group(3) != null) {
125                 // Skip the last '"'.
126                 
127                 m.region(m.end() + 1, m.regionEnd());
128             }
129         }
130 
131         Object answer = Array.newInstance(componentType, values.size());
132         
133         for (int i = 0; i < Array.getLength(answer); i++) {
134             Array.set(answer, i, values.get(i));
135         }
136         
137         return answer;
138     }
139 }