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