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 The Apache MINA Project (dev@mina.apache.org)
33   * @version $Revision: 601229 $, $Date: 2007-12-05 08:13:18 +0100 (mer, 05 déc 2007) $
34   */
35  public class ArrayEditor extends AbstractPropertyEditor {
36      private final Class<?> componentType;
37      
38      public ArrayEditor(Class<?> componentType) {
39          if (componentType == null) {
40              throw new NullPointerException("componentType");
41          }
42          
43          this.componentType = componentType;
44          getComponentEditor();
45          setTrimText(false);
46      }
47  
48      private PropertyEditor getComponentEditor() {
49          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
50          if (e == null) {
51              throw new IllegalArgumentException(
52                      "No " + PropertyEditor.class.getSimpleName() + 
53                      " found for " + componentType.getSimpleName() + '.');
54          }
55          return e;
56      }
57  
58      @Override
59      protected String toText(Object value) {
60          Class<?> componentType = value.getClass().getComponentType();
61          if (componentType == null) {
62              throw new IllegalArgumentException("not an array: " + value);
63          }
64          
65          PropertyEditor e = PropertyEditorFactory.getInstance(componentType);
66          if (e == null) {
67              throw new IllegalArgumentException(
68                      "No " + PropertyEditor.class.getSimpleName() + 
69                      " found for " + componentType.getSimpleName() + '.');
70          }
71          
72          StringBuilder buf = new StringBuilder();
73          for (int i = 0; i < Array.getLength(value); i ++) {
74              e.setValue(Array.get(value, i));
75              // TODO normalize.
76              String s = e.getAsText();
77              buf.append(s);
78              buf.append(", ");
79          }
80          
81          // Remove the last delimiter.
82          if (buf.length() >= 2) {
83              buf.setLength(buf.length() - 2);
84          }
85          return buf.toString();
86      }
87  
88      @Override
89      protected Object toValue(String text) throws IllegalArgumentException {
90          PropertyEditor e = getComponentEditor();
91          List<Object> values = new ArrayList<Object>();
92          Matcher m = CollectionEditor.ELEMENT.matcher(text);
93          boolean matchedDelimiter = true;
94  
95          while (m.find()) {
96              if (m.group(1) != null) {
97                  matchedDelimiter = true;
98                  continue;
99              }
100             
101             if (!matchedDelimiter) {
102                 throw new IllegalArgumentException("No delimiter between elements: " + text);
103             }
104 
105             // TODO escape here.
106             e.setAsText(m.group());
107             values.add(e.getValue());
108             
109             matchedDelimiter = false;
110             if (m.group(2) != null || m.group(3) != null) {
111                 // Skip the last '"'.
112                 m.region(m.end() + 1, m.regionEnd());
113             }
114         }
115         
116         Object answer = Array.newInstance(componentType, values.size());
117         for (int i = 0; i < Array.getLength(answer); i ++) {
118             Array.set(answer, i, values.get(i));
119         }
120         return answer;
121     }
122 }