View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.mina.integration.ognl;
18  
19  import java.beans.PropertyEditor;
20  import java.lang.reflect.Member;
21  import java.util.Map;
22  
23  import ognl.OgnlContext;
24  import ognl.TypeConverter;
25  
26  import org.apache.mina.integration.beans.PropertyEditorFactory;
27  
28  /**
29   * {@link PropertyEditor}-based implementation of OGNL {@link TypeConverter}.
30   * This converter uses the {@link PropertyEditor} implementations in
31   * <tt>mina-integration-beans</tt> module to perform conversion.  To use this
32   * converter:
33   * <pre><code>
34   * OgnlContext ctx = Ognl.createDefaultContext(root);
35   * ctx.put(OgnlContext.TYPE_CONVERTER_CONTEXT_KEY, new PropertyTypeConverter());
36   * </code></pre>
37   * You can also override {@link #getPropertyEditor(Class, String, Class)}
38   * method to have more control over how an appropriate {@link PropertyEditor}
39   * is chosen.
40   * 
41   * @author The Apache MINA Project (dev@mina.apache.org)
42   * @version $Rev: 601229 $, $Date: 2007-12-05 08:13:18 +0100 (mer, 05 déc 2007) $
43   */
44  public class PropertyTypeConverter implements TypeConverter {
45      
46      @SuppressWarnings("unchecked")
47      public Object convertValue(Map ctx, Object target, Member member,
48              String attrName, Object value, Class toType) {
49          if (value == null) {
50              return null;
51          }
52  
53          if (attrName == null) {
54              // I don't know why but OGNL gives null attrName almost always.
55              // Fortunately, we can get the actual attrName with a tiny hack.
56              OgnlContext ognlCtx = (OgnlContext) ctx;
57              attrName = ognlCtx.getCurrentNode().toString().replaceAll(
58                      "[\" \']+", "");
59          }
60  
61          if (toType.isAssignableFrom(value.getClass())) {
62              return value;
63          }
64  
65          PropertyEditor e1 = getPropertyEditor(
66                  target.getClass(), attrName, value.getClass());
67          if (e1 == null) {
68              throw new IllegalArgumentException("Can't convert "
69                      + value.getClass().getSimpleName() + " to "
70                      + String.class.getSimpleName());
71          }
72          e1.setValue(value);
73  
74          PropertyEditor e2 = getPropertyEditor(
75                  target.getClass(), attrName, toType);
76          if (e2 == null) {
77              throw new IllegalArgumentException("Can't convert "
78                      + String.class.getSimpleName() + " to "
79                      + toType.getSimpleName());
80          }
81  
82          e2.setAsText(e1.getAsText());
83          return e2.getValue();
84      }
85      
86      @SuppressWarnings("unused")
87      protected PropertyEditor getPropertyEditor(Class<?> type, String attrName, Class<?> attrType) {
88          return PropertyEditorFactory.getInstance(attrType);
89      }
90  }