    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jxpath.ri.model.dynamic;
    17
    18  import java.util.Locale;
    19
    20  import org.apache.commons.jxpath.DynamicPropertyHandler;
    21  import org.apache.commons.jxpath.JXPathIntrospector;
    22  import org.apache.commons.jxpath.ri.QName;
    23  import org.apache.commons.jxpath.ri.model.NodeIterator;
    24  import org.apache.commons.jxpath.ri.model.NodePointer;
    25  import org.apache.commons.jxpath.ri.model.beans.PropertyIterator;
    26  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
    27  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
    28
    29  /***
    30   * A  Pointer that points to an object with Dynamic Properties. It is used for
    31   * the first element of a path; following elements will by of type
    32   * PropertyPointer.
    33   *
    34   * @author Dmitri Plotnikov
    35   * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:44 $
    36   */
    37  public class DynamicPointer extends PropertyOwnerPointer {
    38      private QName name;
    39      private Object bean;
    40      private DynamicPropertyHandler handler;
    41      private String[] names;
    42
    43      public DynamicPointer(QName name, Object bean,
    44              DynamicPropertyHandler handler, Locale locale)
    45      {
    46          super(null, locale);
    47          this.name = name;
    48          this.bean = bean;
    49          this.handler = handler;
    50      }
    51
    52      public DynamicPointer(NodePointer parent, QName name,
    53              Object bean, DynamicPropertyHandler handler)
    54      {
    55          super(parent);
    56          this.name = name;
    57          this.bean = bean;
    58          this.handler = handler;
    59      }
    60
    61      public PropertyPointer getPropertyPointer() {
    62          return new DynamicPropertyPointer(this, handler);
    63      }
    64
    65      public NodeIterator createNodeIterator(
    66                  String property, boolean reverse, NodePointer startWith)
    67      {
    68          return new PropertyIterator(this, property, reverse, startWith);
    69      }
    70
    71      public NodeIterator attributeIterator(QName name) {
    72          return new DynamicAttributeIterator(this, name);
    73      }
    74
    75      public QName getName() {
    76          return name;
    77      }
    78
    79      public boolean isDynamicPropertyDeclarationSupported() {
    80          return true;
    81      }
    82
    83      /***
    84       * Returns the DP object iself.
    85       */
    86      public Object getBaseValue() {
    87          return bean;
    88      }
    89
    90      public boolean isLeaf() {
    91          Object value = getNode();
    92          return value == null
    93              || JXPathIntrospector.getBeanInfo(value.getClass()).isAtomic();
    94      }
    95
    96      public boolean isCollection() {
    97          return false;
    98      }
    99
    100     /***
    101      * Returns 1.
    102      */
    103     public int getLength() {
    104         return 1;
    105     }
    106
    107     public String asPath() {
    108         if (parent != null) {
    109             return super.asPath();
    110         }
    111         return "/";
    112     }
    113
    114     public int hashCode() {
    115         return System.identityHashCode(bean) + name.hashCode();
    116     }
    117
    118     public boolean equals(Object object) {
    119         if (object == this) {
    120             return true;
    121         }
    122
    123         if (!(object instanceof DynamicPointer)) {
    124             return false;
    125         }
    126
    127         DynamicPointer other = (DynamicPointer) object;
    128         return bean == other.bean && name.equals(other.name);
    129     }
    130 }