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.beans;
017:
018: import java.util.Locale;
019:
020: import org.apache.commons.jxpath.JXPathBeanInfo;
021: import org.apache.commons.jxpath.JXPathIntrospector;
022: import org.apache.commons.jxpath.ri.QName;
023: import org.apache.commons.jxpath.ri.model.NodePointer;
024:
025: /**
026: * A Pointer that points to a JavaBean or a collection. It is either
027: * the first element of a path or a pointer for a property value.
028: * Typically there is a BeanPropertyPointer between two BeanPointers
029: * in the chain.
030: *
031: * @author Dmitri Plotnikov
032: * @version $Revision: 1.13 $ $Date: 2004/02/29 14:17:41 $
033: */
034: public class BeanPointer extends PropertyOwnerPointer {
035: private QName name;
036: private Object bean;
037: private JXPathBeanInfo beanInfo;
038:
039: public BeanPointer(QName name, Object bean,
040: JXPathBeanInfo beanInfo, Locale locale) {
041: super (null, locale);
042: this .name = name;
043: this .bean = bean;
044: this .beanInfo = beanInfo;
045: }
046:
047: /**
048: * @param name is the name given to the first node
049: */
050: public BeanPointer(NodePointer parent, QName name, Object bean,
051: JXPathBeanInfo beanInfo) {
052: super (parent);
053: this .name = name;
054: this .bean = bean;
055: this .beanInfo = beanInfo;
056: }
057:
058: public PropertyPointer getPropertyPointer() {
059: return new BeanPropertyPointer(this , beanInfo);
060: }
061:
062: public QName getName() {
063: return name;
064: }
065:
066: /**
067: * Returns the bean itself
068: */
069: public Object getBaseValue() {
070: return bean;
071: }
072:
073: /**
074: * Returns false
075: */
076: public boolean isCollection() {
077: return false;
078: }
079:
080: /**
081: * Returns 1.
082: */
083: public int getLength() {
084: return 1;
085: }
086:
087: public boolean isLeaf() {
088: Object value = getNode();
089: return value == null
090: || JXPathIntrospector.getBeanInfo(value.getClass())
091: .isAtomic();
092: }
093:
094: public int hashCode() {
095: return name == null ? 0 : name.hashCode();
096: }
097:
098: public boolean equals(Object object) {
099: if (object == this ) {
100: return true;
101: }
102:
103: if (!(object instanceof BeanPointer)) {
104: return false;
105: }
106:
107: BeanPointer other = (BeanPointer) object;
108: if (parent != other.parent) {
109: if (parent == null || !parent.equals(other.parent)) {
110: return false;
111: }
112: }
113:
114: if ((name == null && other.name != null)
115: || (name != null && !name.equals(other.name))) {
116: return false;
117: }
118:
119: int iThis = (index == WHOLE_COLLECTION ? 0 : index);
120: int iOther = (other.index == WHOLE_COLLECTION ? 0 : other.index);
121: if (iThis != iOther) {
122: return false;
123: }
124:
125: if (bean instanceof Number || bean instanceof String
126: || bean instanceof Boolean) {
127: return bean.equals(other.bean);
128: }
129: return bean == other.bean;
130: }
131:
132: /**
133: * If the pointer has a parent, then parent's path.
134: * If the bean is null, "null()".
135: * If the bean is a primitive value, the value itself.
136: * Otherwise - an empty string.
137: */
138: public String asPath() {
139: if (parent != null) {
140: return super .asPath();
141: } else if (bean == null) {
142: return "null()";
143: } else if (bean instanceof Number) {
144: String string = bean.toString();
145: if (string.endsWith(".0")) {
146: string = string.substring(0, string.length() - 2);
147: }
148: return string;
149: } else if (bean instanceof Boolean) {
150: return ((Boolean) bean).booleanValue() ? "true()"
151: : "false()";
152: } else if (bean instanceof String) {
153: return "'" + bean + "'";
154: }
155: return "/";
156: }
157: }
|