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.commons.jxpath.ri.model.dynamic;
18  
19  import java.util.Locale;
20  
21  import org.apache.commons.jxpath.DynamicPropertyHandler;
22  import org.apache.commons.jxpath.JXPathBeanInfo;
23  import org.apache.commons.jxpath.JXPathIntrospector;
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.model.NodePointer;
26  import org.apache.commons.jxpath.ri.model.NodePointerFactory;
27  import org.apache.commons.jxpath.ri.model.beans.NullPointer;
28  import org.apache.commons.jxpath.util.ValueUtils;
29  
30  /**
31   * Implements NodePointerFactory for Dynamic classes like Map.
32   *
33   * @author Dmitri Plotnikov
34   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
35   */
36  public class DynamicPointerFactory implements NodePointerFactory {
37  
38      /**
39       * Factory order constant.
40       */
41      public static final int DYNAMIC_POINTER_FACTORY_ORDER = 800;
42  
43      public int getOrder() {
44          return DYNAMIC_POINTER_FACTORY_ORDER;
45      }
46  
47      public NodePointer createNodePointer(
48          QName name,
49          Object bean,
50          Locale locale) {
51          JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
52          if (bi.isDynamic()) {
53              DynamicPropertyHandler handler =
54                  ValueUtils.getDynamicPropertyHandler(
55                      bi.getDynamicPropertyHandlerClass());
56              return new DynamicPointer(name, bean, handler, locale);
57          }
58          return null;
59      }
60  
61      public NodePointer createNodePointer(
62          NodePointer parent,
63          QName name,
64          Object bean) {
65          if (bean == null) {
66              return new NullPointer(parent, name);
67          }
68  
69          JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
70          if (bi.isDynamic()) {
71              DynamicPropertyHandler handler =
72                  ValueUtils.getDynamicPropertyHandler(
73                      bi.getDynamicPropertyHandlerClass());
74              return new DynamicPointer(parent, name, bean, handler);
75          }
76          return null;
77      }
78  }