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.util.ArrayList;
024: import java.util.Collection;
025: import java.util.regex.Matcher;
026: import java.util.regex.Pattern;
027:
028: /**
029: * A {@link PropertyEditor} which converts a {@link String} into
030: * a {@link Collection} 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 CollectionEditor extends AbstractPropertyEditor {
036: static final Pattern ELEMENT = Pattern.compile("([,\\s]+)|"
037: + // Delimiter
038: "(?<=\")((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^\"])*)(?=\")|"
039: + "(?<=')((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^'])*)(?=')|"
040: + "((?:[^\\\\\\s'\",]|\\\\ |\\\\\"|\\\\')+)");
041:
042: private final Class<?> elementType;
043:
044: public CollectionEditor(Class<?> elementType) {
045: if (elementType == null) {
046: throw new NullPointerException("elementType");
047: }
048:
049: this .elementType = elementType;
050: getElementEditor();
051: setTrimText(false);
052: }
053:
054: private PropertyEditor getElementEditor() {
055: PropertyEditor e = PropertyEditorFactory
056: .getInstance(elementType);
057: if (e == null) {
058: throw new IllegalArgumentException("No "
059: + PropertyEditor.class.getSimpleName()
060: + " found for " + elementType.getSimpleName() + '.');
061: }
062: return e;
063: }
064:
065: @Override
066: @SuppressWarnings("unchecked")
067: protected final String toText(Object value) {
068: StringBuilder buf = new StringBuilder();
069: for (Object v : (Collection) value) {
070: if (v == null) {
071: v = defaultElement();
072: }
073:
074: PropertyEditor e = PropertyEditorFactory.getInstance(v);
075: if (e == null) {
076: throw new IllegalArgumentException("No "
077: + PropertyEditor.class.getSimpleName()
078: + " found for " + v.getClass().getSimpleName()
079: + '.');
080: }
081: e.setValue(v);
082: // TODO normalize.
083: String s = e.getAsText();
084: buf.append(s);
085: buf.append(", ");
086: }
087:
088: // Remove the last delimiter.
089: if (buf.length() >= 2) {
090: buf.setLength(buf.length() - 2);
091: }
092: return buf.toString();
093: }
094:
095: @Override
096: protected final Object toValue(String text)
097: throws IllegalArgumentException {
098: PropertyEditor e = getElementEditor();
099: Collection<Object> answer = newCollection();
100: Matcher m = ELEMENT.matcher(text);
101: boolean matchedDelimiter = true;
102:
103: while (m.find()) {
104: if (m.group(1) != null) {
105: matchedDelimiter = true;
106: continue;
107: }
108:
109: if (!matchedDelimiter) {
110: throw new IllegalArgumentException(
111: "No delimiter between elements: " + text);
112: }
113:
114: // TODO escape here.
115: e.setAsText(m.group());
116: answer.add(e.getValue());
117:
118: matchedDelimiter = false;
119: if (m.group(2) != null || m.group(3) != null) {
120: // Skip the last '"'.
121: m.region(m.end() + 1, m.regionEnd());
122: }
123: }
124:
125: return answer;
126: }
127:
128: protected Collection<Object> newCollection() {
129: return new ArrayList<Object>();
130: }
131:
132: protected Object defaultElement() {
133: PropertyEditor e = PropertyEditorFactory
134: .getInstance(elementType);
135: if (e == null) {
136: return null;
137: }
138:
139: if (e instanceof AbstractPropertyEditor) {
140: return ((AbstractPropertyEditor) e).defaultValue();
141: }
142:
143: return null;
144: }
145: }
|