    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.dynabeans;
    17
    18  import java.util.Locale;
    19
    20  import org.apache.commons.beanutils.DynaBean;
    21  import org.apache.commons.jxpath.ri.QName;
    22  import org.apache.commons.jxpath.ri.model.NodePointer;
    23  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
    24  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
    25
    26
    27  /***
    28   * A Pointer that points to a DynaBean.
    29   *
    30   * @author Dmitri Plotnikov
    31   * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:40 $
    32   */
    33  public class DynaBeanPointer extends PropertyOwnerPointer {
    34      private QName name;
    35      private DynaBean dynaBean;
    36
    37      public DynaBeanPointer(QName name, DynaBean dynaBean, Locale locale) {
    38          super(null, locale);
    39          this.name = name;
    40          this.dynaBean = dynaBean;
    41      }
    42
    43      /***
    44       * @param name is the name given to the first node
    45       */
    46      public DynaBeanPointer(NodePointer parent, QName name, DynaBean dynaBean) {
    47          super(parent);
    48          this.name = name;
    49          this.dynaBean = dynaBean;
    50      }
    51
    52      public PropertyPointer getPropertyPointer() {
    53          return new DynaBeanPropertyPointer(this, dynaBean);
    54      }
    55
    56      public QName getName() {
    57          return name;
    58      }
    59
    60      /***
    61       * Returns the bean itself
    62       */
    63      public Object getBaseValue() {
    64          return dynaBean;
    65      }
    66
    67      public Object getImmediateNode() {
    68          return dynaBean;
    69      }
    70
    71      public boolean isCollection() {
    72          return false;
    73      }
    74
    75      /***
    76       * Returns 1.
    77       */
    78      public int getLength() {
    79          return 1;
    80      }
    81
    82      public boolean isLeaf() {
    83          return false;
    84      }
    85
    86      public int hashCode() {
    87          return name == null ? 0 : name.hashCode();
    88      }
    89
    90      public boolean equals(Object object) {
    91          if (object == this) {
    92              return true;
    93          }
    94
    95          if (!(object instanceof DynaBeanPointer)) {
    96              return false;
    97          }
    98
    99          DynaBeanPointer other = (DynaBeanPointer) object;
    100         if (parent != other.parent) {
    101             if (parent == null || !parent.equals(other.parent)) {
    102                 return false;
    103             }
    104         }
    105
    106         if ((name == null && other.name != null)
    107             || (name != null && !name.equals(other.name))) {
    108             return false;
    109         }
    110
    111         int iThis = (index == WHOLE_COLLECTION ? 0 : index);
    112         int iOther = (other.index == WHOLE_COLLECTION ? 0 : other.index);
    113         if (iThis != iOther) {
    114             return false;
    115         }
    116
    117         return dynaBean == other.dynaBean;
    118     }
    119
    120     /***
    121      * If there's a parent - parent's path, otherwise "/".
    122      */
    123     public String asPath() {
    124         if (parent != null) {
    125             return super.asPath();
    126         }
    127         return "/";
    128     }
    129 }