org.apache.mina.integration.beans.MapEditor Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.mina.integration.beans;
import java.beans.PropertyEditor;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A {@link PropertyEditor} which converts a {@link String} into
* a {@link Collection} and vice versa.
*
* @author Apache MINA Project
*/
public class MapEditor extends AbstractPropertyEditor {
static final Pattern ELEMENT = Pattern.compile("([,\\s]+)|"
+ // Entry delimiter
"(\\s*=\\s*)|"
+ // Key-Value delimiter
"(?<=\")((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^\"])*)(?=\")|"
+ "(?<=')((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^'])*)(?=')|" + "((?:[^\\\\\\s'\",]|\\\\ |\\\\\"|\\\\')+)");
private final Class keyType;
private final Class valueType;
private static final String NO_VALUE = "No value {1} found for {2}.";
private static final String NO_KEY = "No key {1} found for {2}.";
/**
* Creates a new DateEditor instance
*
* @param keyType The key type
* @param valueType The value type
*/
public MapEditor(Class keyType, Class valueType) {
if (keyType == null) {
throw new IllegalArgumentException("keyType");
}
if (valueType == null) {
throw new IllegalArgumentException("valueType");
}
this.keyType = keyType;
this.valueType = valueType;
getKeyEditor();
getValueEditor();
setTrimText(false);
}
private PropertyEditor getKeyEditor() {
PropertyEditor e = PropertyEditorFactory.getInstance(keyType);
if (e == null) {
throw new IllegalArgumentException(MessageFormat.format(NO_KEY, PropertyEditor.class.getSimpleName(),
keyType.getSimpleName()));
}
return e;
}
private PropertyEditor getValueEditor() {
PropertyEditor e = PropertyEditorFactory.getInstance(valueType);
if (e == null) {
throw new IllegalArgumentException(MessageFormat.format(NO_VALUE, PropertyEditor.class.getSimpleName(),
valueType.getSimpleName()));
}
return e;
}
@Override
protected final String toText(Object value) {
StringBuilder buf = new StringBuilder();
for (Map.Entry entry : ((Map) value).entrySet()) {
Object ekey = entry.getKey();
Object evalue = entry.getValue();
PropertyEditor ekeyEditor = PropertyEditorFactory.getInstance(ekey);
if (ekeyEditor == null) {
throw new IllegalArgumentException(MessageFormat.format(NO_KEY, PropertyEditor.class.getSimpleName(),
ekey.getClass().getSimpleName()));
}
ekeyEditor.setValue(ekey);
PropertyEditor evalueEditor = PropertyEditorFactory.getInstance(evalue);
if (evalueEditor == null) {
throw new IllegalArgumentException(MessageFormat.format(NO_VALUE, PropertyEditor.class.getSimpleName(),
evalue.getClass().getSimpleName()));
}
ekeyEditor.setValue(ekey);
evalueEditor.setValue(evalue);
// TODO normalize.
String keyString = ekeyEditor.getAsText();
String valueString = evalueEditor.getAsText();
buf.append(keyString);
buf.append(" = ");
buf.append(valueString);
buf.append(", ");
}
// Remove the last delimiter.
if (buf.length() >= 2) {
buf.setLength(buf.length() - 2);
}
return buf.toString();
}
@Override
protected final Object toValue(String text) {
PropertyEditor keyEditor = getKeyEditor();
PropertyEditor valueEditor = getValueEditor();
Map