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.dynabeans;
19  
20  import java.util.Locale;
21  
22  import org.apache.commons.beanutils.LazyDynaBean;
23  import org.apache.commons.beanutils.LazyDynaClass;
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.PropertyPointer;
28  
29  /**
30   * Implemented in response to [JXPATH-144]. Optionally pluggable {@code NodePointerFactory} that returns a special type of {@code NodePointer} for
31   * {@code LazyDynaBean}s. The {@code PropertyPointer}s returned by these will respect {@link LazyDynaClass#isDynaProperty(String)} when determining
32   * {@link PropertyPointer#isActual()}.
33   *
34   * @since 1.4.0
35   */
36  public class StrictLazyDynaBeanPointerFactory implements NodePointerFactory {
37  
38      /**
39       * Pointer implementation.
40       */
41      private static final class StrictLazyDynaBeanPointer extends DynaBeanPointer {
42  
43          private static final long serialVersionUID = 1L;
44          private final LazyDynaBean lazyDynaBean;
45  
46          /**
47           * Constructs a new StrictLazyDynaBeanPointer instance.
48           *
49           * @param parent       pointer
50           * @param qName        is the name given to the first node
51           * @param lazyDynaBean pointed
52           */
53          public StrictLazyDynaBeanPointer(final NodePointer parent, final QName qName, final LazyDynaBean lazyDynaBean) {
54              super(parent, qName, lazyDynaBean);
55              this.lazyDynaBean = lazyDynaBean;
56          }
57  
58          /**
59           * Constructs a new StrictLazyDynaBeanPointer instance.
60           *
61           * @param qName        is the name given to the first node
62           * @param lazyDynaBean pointed
63           * @param locale       Locale
64           */
65          public StrictLazyDynaBeanPointer(final QName qName, final LazyDynaBean lazyDynaBean, final Locale locale) {
66              super(qName, lazyDynaBean, locale);
67              this.lazyDynaBean = lazyDynaBean;
68          }
69  
70          /**
71           * {@inheritDoc}
72           */
73          @Override
74          public PropertyPointer getPropertyPointer() {
75              return new DynaBeanPropertyPointer(this, lazyDynaBean) {
76  
77                  private static final long serialVersionUID = 1L;
78  
79                  @Override
80                  protected boolean isActualProperty() {
81                      return ((LazyDynaClass) lazyDynaBean.getDynaClass()).isDynaProperty(getPropertyName());
82                  }
83              };
84          }
85      }
86  
87      /**
88       * Constructs a new instance.
89       */
90      public StrictLazyDynaBeanPointerFactory() {
91          // empty
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public NodePointer createNodePointer(final NodePointer parent, final QName qName, final Object object) {
99          return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(parent, qName, (LazyDynaBean) object) : null;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public NodePointer createNodePointer(final QName qName, final Object object, final Locale locale) {
107         return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(qName, (LazyDynaBean) object, locale) : null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public int getOrder() {
115         return DynaBeanPointerFactory.DYNA_BEAN_POINTER_FACTORY_ORDER - 1;
116     }
117 }