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 NullPointerException("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(
51                      "No " + PropertyEditor.class.getSimpleName() + 
52                      " found for " + componentType.getSimpleName() + '.');
53          }
54          return e;
55      }
56  
57      @Override
58      protected String toText(Object value) {
59          Class<?> componentType = value.getClass().getComponentType();
60          if (componentType == null) {
61              throw new IllegalArgumentException("not an array: " + value);
62          }
63          
64          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
65          if (e == null) {
66              throw new IllegalArgumentException(
67                      "No " + PropertyEditor.class.getSimpleName() + 
68                      " found for " + componentType.getSimpleName() + '.');
69          }
70          
71          StringBuilder buf = new StringBuilder();
72          for (int i = 0; i < Array.getLength(value); i ++) {
73              e.setValue(Array.get(value, i));
74              // TODO normalize.
75              String s = e.getAsText();
76              buf.append(s);
77              buf.append(", ");
78          }
79          
80          // Remove the last delimiter.
81          if (buf.length() >= 2) {
82              buf.setLength(buf.length() - 2);
83          }
84          return buf.toString();
85      }
86  
87      @Override
88      protected Object toValue(String text) throws IllegalArgumentException {
89          PropertyEditor e = getComponentEditor();
90          List<Object> values = new ArrayList<Object>();
91          Matcher m = CollectionEditor.ELEMENT.matcher(text);
92          boolean matchedDelimiter = true;
93  
94          while (m.find()) {
95              if (m.group(1) != null) {
96                  matchedDelimiter = true;
97                  continue;
98              }
99              
100             if (!matchedDelimiter) {
101                 throw new IllegalArgumentException("No delimiter between elements: " + text);
102             }
103 
104             // TODO escape here.
105             e.setAsText(m.group());
106             values.add(e.getValue());
107             
108             matchedDelimiter = false;
109             if (m.group(2) != null || m.group(3) != null) {
110                 // Skip the last '"'.
111                 m.region(m.end() + 1, m.regionEnd());
112             }
113         }
114         
115         Object answer = Array.newInstance(componentType, values.size());
116         for (int i = 0; i < Array.getLength(answer); i ++) {
117             Array.set(answer, i, values.get(i));
118         }
119         return answer;
120     }
121 }