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