001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.model.dynabeans;
017:
018: import java.util.Locale;
019:
020: import org.apache.commons.beanutils.DynaBean;
021: import org.apache.commons.jxpath.ri.QName;
022: import org.apache.commons.jxpath.ri.model.NodePointer;
023: import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
024: import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
025:
026: /**
027: * A Pointer that points to a DynaBean.
028: *
029: * @author Dmitri Plotnikov
030: * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:40 $
031: */
032: public class DynaBeanPointer extends PropertyOwnerPointer {
033: private QName name;
034: private DynaBean dynaBean;
035:
036: public DynaBeanPointer(QName name, DynaBean dynaBean, Locale locale) {
037: super (null, locale);
038: this .name = name;
039: this .dynaBean = dynaBean;
040: }
041:
042: /**
043: * @param name is the name given to the first node
044: */
045: public DynaBeanPointer(NodePointer parent, QName name,
046: DynaBean dynaBean) {
047: super (parent);
048: this .name = name;
049: this .dynaBean = dynaBean;
050: }
051:
052: public PropertyPointer getPropertyPointer() {
053: return new DynaBeanPropertyPointer(this , dynaBean);
054: }
055:
056: public QName getName() {
057: return name;
058: }
059:
060: /**
061: * Returns the bean itself
062: */
063: public Object getBaseValue() {
064: return dynaBean;
065: }
066:
067: public Object getImmediateNode() {
068: return dynaBean;
069: }
070:
071: public boolean isCollection() {
072: return false;
073: }
074:
075: /**
076: * Returns 1.
077: */
078: public int getLength() {
079: return 1;
080: }
081:
082: public boolean isLeaf() {
083: return false;
084: }
085:
086: public int hashCode() {
087: return name == null ? 0 : name.hashCode();
088: }
089:
090: public boolean equals(Object object) {
091: if (object == this ) {
092: return true;
093: }
094:
095: if (!(object instanceof DynaBeanPointer)) {
096: return false;
097: }
098:
099: 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: }
|