001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.integration.beans;
021:
022: import java.beans.PropertyEditor;
023: import java.lang.reflect.Array;
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.regex.Matcher;
027:
028: /**
029: * A {@link PropertyEditor} which converts a {@link String} into
030: * a one-dimensional array and vice versa.
031: *
032: * @author The Apache MINA Project (dev@mina.apache.org)
033: * @version $Revision: 601229 $, $Date: 2007-12-05 00:13:18 -0700 (Wed, 05 Dec 2007) $
034: */
035: public class ArrayEditor extends AbstractPropertyEditor {
036: private final Class<?> componentType;
037:
038: public ArrayEditor(Class<?> componentType) {
039: if (componentType == null) {
040: throw new NullPointerException("componentType");
041: }
042:
043: this .componentType = componentType;
044: getComponentEditor();
045: setTrimText(false);
046: }
047:
048: private PropertyEditor getComponentEditor() {
049: PropertyEditor e = PropertyEditorFactory
050: .getInstance(componentType);
051: if (e == null) {
052: throw new IllegalArgumentException("No "
053: + PropertyEditor.class.getSimpleName()
054: + " found for " + componentType.getSimpleName()
055: + '.');
056: }
057: return e;
058: }
059:
060: @Override
061: protected String toText(Object value) {
062: Class<?> componentType = value.getClass().getComponentType();
063: if (componentType == null) {
064: throw new IllegalArgumentException("not an array: " + value);
065: }
066:
067: PropertyEditor e = PropertyEditorFactory
068: .getInstance(componentType);
069: if (e == null) {
070: throw new IllegalArgumentException("No "
071: + PropertyEditor.class.getSimpleName()
072: + " found for " + componentType.getSimpleName()
073: + '.');
074: }
075:
076: StringBuilder buf = new StringBuilder();
077: for (int i = 0; i < Array.getLength(value); i++) {
078: e.setValue(Array.get(value, i));
079: // TODO normalize.
080: String s = e.getAsText();
081: buf.append(s);
082: buf.append(", ");
083: }
084:
085: // Remove the last delimiter.
086: if (buf.length() >= 2) {
087: buf.setLength(buf.length() - 2);
088: }
089: return buf.toString();
090: }
091:
092: @Override
093: protected Object toValue(String text)
094: throws IllegalArgumentException {
095: PropertyEditor e = getComponentEditor();
096: List<Object> values = new ArrayList<Object>();
097: Matcher m = CollectionEditor.ELEMENT.matcher(text);
098: boolean matchedDelimiter = true;
099:
100: while (m.find()) {
101: if (m.group(1) != null) {
102: matchedDelimiter = true;
103: continue;
104: }
105:
106: if (!matchedDelimiter) {
107: throw new IllegalArgumentException(
108: "No delimiter between elements: " + text);
109: }
110:
111: // TODO escape here.
112: e.setAsText(m.group());
113: values.add(e.getValue());
114:
115: matchedDelimiter = false;
116: if (m.group(2) != null || m.group(3) != null) {
117: // Skip the last '"'.
118: m.region(m.end() + 1, m.regionEnd());
119: }
120: }
121:
122: Object answer = Array.newInstance(componentType, values.size());
123: for (int i = 0; i < Array.getLength(answer); i++) {
124: Array.set(answer, i, values.get(i));
125: }
126: return answer;
127: }
128: }
|