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.JXPathIntrospector;
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.model.NodeIterator;
26  import org.apache.commons.jxpath.ri.model.NodePointer;
27  import org.apache.commons.jxpath.ri.model.beans.PropertyIterator;
28  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
29  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
30  
31  /**
32   * A Pointer that points to an object with Dynamic Properties. It is used for the first element of a path; following elements will by of type
33   * {@link PropertyPointer}.
34   */
35  public class DynamicPointer extends PropertyOwnerPointer {
36  
37      private static final long serialVersionUID = -1842347025295904256L;
38  
39      /**
40       * Qualified name.
41       */
42      private final QName qName;
43  
44      /**
45       * Java bean.
46       */
47      private final Object bean;
48  
49      /**
50       * Dynamic property handler.
51       */
52      private final DynamicPropertyHandler handler;
53  
54      /**
55       * Constructs a new DynamicPointer.
56       *
57       * @param parent  parent pointer
58       * @param qName    property name
59       * @param bean    owning bean
60       * @param handler DynamicPropertyHandler
61       */
62      public DynamicPointer(final NodePointer parent, final QName qName, final Object bean, final DynamicPropertyHandler handler) {
63          super(parent);
64          this.qName = qName;
65          this.bean = bean;
66          this.handler = handler;
67      }
68  
69      /**
70       * Constructs a new DynamicPointer.
71       *
72       * @param qName    property name
73       * @param bean    owning bean
74       * @param handler DynamicPropertyHandler
75       * @param locale  Locale
76       */
77      public DynamicPointer(final QName qName, final Object bean, final DynamicPropertyHandler handler, final Locale locale) {
78          super(null, locale);
79          this.qName = qName;
80          this.bean = bean;
81          this.handler = handler;
82      }
83  
84      @Override
85      public String asPath() {
86          return parent == null ? "/" : super.asPath();
87      }
88  
89      @Override
90      public NodeIterator attributeIterator(final QName qName) {
91          return new DynamicAttributeIterator(this, qName);
92      }
93  
94      @Override
95      public NodeIterator createNodeIterator(final String property, final boolean reverse, final NodePointer startWith) {
96          return new PropertyIterator(this, property, reverse, startWith);
97      }
98  
99      @Override
100     public boolean equals(final Object object) {
101         if (object == this) {
102             return true;
103         }
104         if (!(object instanceof DynamicPointer)) {
105             return false;
106         }
107         final DynamicPointer other = (DynamicPointer) object;
108         if (bean != other.bean) {
109             return false;
110         }
111         return qName == other.qName || qName != null && qName.equals(other.qName);
112     }
113 
114     /**
115      * Returns the DP object iself.
116      *
117      * @return Object
118      */
119     @Override
120     public Object getBaseValue() {
121         return bean;
122     }
123 
124     /**
125      * Returns 1.
126      *
127      * @return int
128      */
129     @Override
130     public int getLength() {
131         return 1;
132     }
133 
134     @Override
135     public QName getName() {
136         return qName;
137     }
138 
139     @Override
140     public PropertyPointer getPropertyPointer() {
141         return new DynamicPropertyPointer(this, handler);
142     }
143 
144     @Override
145     public int hashCode() {
146         return System.identityHashCode(bean) + (qName == null ? 0 : qName.hashCode());
147     }
148 
149     @Override
150     public boolean isCollection() {
151         return false;
152     }
153 
154     @Override
155     public boolean isDynamicPropertyDeclarationSupported() {
156         return true;
157     }
158 
159     @Override
160     public boolean isLeaf() {
161         final Object value = getNode();
162         return value == null || JXPathIntrospector.getBeanInfo(value.getClass()).isAtomic();
163     }
164 }