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