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.util.ArrayList;
24  import java.util.Collection;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  /**
29   * A {@link PropertyEditor} which converts a {@link String} into
30   * a {@link Collection} and vice versa.
31   *
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class CollectionEditor extends AbstractPropertyEditor {
35      static final Pattern ELEMENT = Pattern.compile(
36              "([,\\s]+)|" + // Delimiter
37              "(?<=\")((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^\"])*)(?=\")|" +
38              "(?<=')((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^'])*)(?=')|" +
39              "((?:[^\\\\\\s'\",]|\\\\ |\\\\\"|\\\\')+)");
40      
41      private final Class<?> elementType;
42      
43      public CollectionEditor(Class<?> elementType) {
44          if (elementType == null) {
45              throw new NullPointerException("elementType");
46          }
47          
48          this.elementType = elementType;
49          getElementEditor();
50          setTrimText(false);
51      }
52  
53      private PropertyEditor getElementEditor() {
54          PropertyEditor e = PropertyEditorFactory.getInstance(elementType);
55          if (e == null) {
56              throw new IllegalArgumentException(
57                      "No " + PropertyEditor.class.getSimpleName() + 
58                      " found for " + elementType.getSimpleName() + '.');
59          }
60          return e;
61      }
62  
63      @Override
64      @SuppressWarnings("unchecked")
65      protected final String toText(Object value) {
66          StringBuilder buf = new StringBuilder();
67          for (Object v: (Collection) value) {
68              if (v == null) {
69                  v = defaultElement();
70              }
71              
72              PropertyEditor e = PropertyEditorFactory.getInstance(v);
73              if (e == null) {
74                  throw new IllegalArgumentException(
75                          "No " + PropertyEditor.class.getSimpleName() + 
76                          " found for " + v.getClass().getSimpleName() + '.');
77              }            
78              e.setValue(v);
79              // TODO normalize.
80              String s = e.getAsText();
81              buf.append(s);
82              buf.append(", ");
83          }
84          
85          // Remove the last delimiter.
86          if (buf.length() >= 2) {
87              buf.setLength(buf.length() - 2);
88          }
89          return buf.toString();
90      }
91  
92      @Override
93      protected final Object toValue(String text) throws IllegalArgumentException {
94          PropertyEditor e = getElementEditor();
95          Collection<Object> answer = newCollection();
96          Matcher m = ELEMENT.matcher(text);
97          boolean matchedDelimiter = true;
98  
99          while (m.find()) {
100             if (m.group(1) != null) {
101                 matchedDelimiter = true;
102                 continue;
103             }
104             
105             if (!matchedDelimiter) {
106                 throw new IllegalArgumentException("No delimiter between elements: " + text);
107             }
108 
109             // TODO escape here.
110             e.setAsText(m.group());
111             answer.add(e.getValue());
112             
113             matchedDelimiter = false;
114             if (m.group(2) != null || m.group(3) != null) {
115                 // Skip the last '"'.
116                 m.region(m.end() + 1, m.regionEnd());
117             }
118         }
119         
120         return answer;
121     }
122     
123     protected Collection<Object> newCollection() {
124         return new ArrayList<Object>();
125     }
126     
127     protected Object defaultElement() {
128         PropertyEditor e = PropertyEditorFactory.getInstance(elementType);
129         if (e == null) {
130             return null;
131         }
132         
133         if (e instanceof AbstractPropertyEditor) {
134             return ((AbstractPropertyEditor) e).defaultValue();
135         }
136 
137         return null;
138     }
139 }