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.Collection;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Properties;
027: import java.util.Set;
028:
029: /**
030: * A factory that creates a new {@link PropertyEditor} which is appropriate for
031: * the specified object or class.
032: *
033: * @author The Apache MINA Project (dev@mina.apache.org)
034: * @version $Rev: 601247 $, $Date: 2007-12-05 01:42:41 -0700 (Wed, 05 Dec 2007) $
035: */
036: public final class PropertyEditorFactory {
037: @SuppressWarnings("unchecked")
038: public static PropertyEditor getInstance(Object object) {
039: if (object == null) {
040: return new NullEditor();
041: }
042:
043: if (object instanceof Collection) {
044: Class<?> elementType = null;
045: for (Object e : (Collection) object) {
046: if (e != null) {
047: elementType = e.getClass();
048: break;
049: }
050: }
051:
052: if (elementType != null) {
053: if (object instanceof Set) {
054: return new SetEditor(elementType);
055: }
056:
057: if (object instanceof List) {
058: return new ListEditor(elementType);
059: }
060:
061: return new CollectionEditor(elementType);
062: }
063: }
064:
065: if (object instanceof Map) {
066: Class<?> keyType = null;
067: Class<?> valueType = null;
068: for (Object entry : ((Map) object).entrySet()) {
069: Map.Entry e = (Map.Entry) entry;
070: if (e.getKey() != null && e.getValue() != null) {
071: keyType = e.getKey().getClass();
072: valueType = e.getValue().getClass();
073: break;
074: }
075: }
076:
077: if (keyType != null && valueType != null) {
078: return new MapEditor(keyType, valueType);
079: }
080: }
081:
082: return getInstance(object.getClass());
083: }
084:
085: // parent type / property name / property type
086: public static PropertyEditor getInstance(Class<?> type) {
087: if (type == null) {
088: throw new NullPointerException("type");
089: }
090:
091: if (type.isEnum()) {
092: return new EnumEditor(type);
093: }
094:
095: if (type.isArray()) {
096: return new ArrayEditor(type.getComponentType());
097: }
098:
099: if (Collection.class.isAssignableFrom(type)) {
100: if (Set.class.isAssignableFrom(type)) {
101: return new SetEditor(String.class);
102: }
103:
104: if (List.class.isAssignableFrom(type)) {
105: return new ListEditor(String.class);
106: }
107:
108: return new CollectionEditor(String.class);
109: }
110:
111: if (Map.class.isAssignableFrom(type)) {
112: return new MapEditor(String.class, String.class);
113: }
114:
115: if (Properties.class.isAssignableFrom(type)) {
116: return new PropertiesEditor();
117: }
118:
119: type = filterPrimitiveType(type);
120:
121: try {
122: return (PropertyEditor) PropertyEditorFactory.class
123: .getClassLoader().loadClass(
124: PropertyEditorFactory.class.getPackage()
125: .getName()
126: + '.'
127: + type.getSimpleName()
128: + "Editor").newInstance();
129: } catch (Exception e) {
130: return null;
131: }
132: }
133:
134: private static Class<?> filterPrimitiveType(Class<?> type) {
135: if (type.isPrimitive()) {
136: if (type == boolean.class) {
137: type = Boolean.class;
138: }
139: if (type == byte.class) {
140: type = Byte.class;
141: }
142: if (type == char.class) {
143: type = Character.class;
144: }
145: if (type == double.class) {
146: type = Double.class;
147: }
148: if (type == float.class) {
149: type = Float.class;
150: }
151: if (type == int.class) {
152: type = Integer.class;
153: }
154: if (type == long.class) {
155: type = Long.class;
156: }
157: if (type == short.class) {
158: type = Short.class;
159: }
160: }
161: return type;
162: }
163:
164: private PropertyEditorFactory() {
165: }
166: }
|