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.dynamic;
017:
018: import java.util.Locale;
019:
020: import org.apache.commons.jxpath.DynamicPropertyHandler;
021: import org.apache.commons.jxpath.JXPathIntrospector;
022: import org.apache.commons.jxpath.ri.QName;
023: import org.apache.commons.jxpath.ri.model.NodeIterator;
024: import org.apache.commons.jxpath.ri.model.NodePointer;
025: import org.apache.commons.jxpath.ri.model.beans.PropertyIterator;
026: import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
027: import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
028:
029: /**
030: * A Pointer that points to an object with Dynamic Properties. It is used for
031: * the first element of a path; following elements will by of type
032: * PropertyPointer.
033: *
034: * @author Dmitri Plotnikov
035: * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:44 $
036: */
037: public class DynamicPointer extends PropertyOwnerPointer {
038: private QName name;
039: private Object bean;
040: private DynamicPropertyHandler handler;
041: private String[] names;
042:
043: public DynamicPointer(QName name, Object bean,
044: DynamicPropertyHandler handler, Locale locale) {
045: super (null, locale);
046: this .name = name;
047: this .bean = bean;
048: this .handler = handler;
049: }
050:
051: public DynamicPointer(NodePointer parent, QName name, Object bean,
052: DynamicPropertyHandler handler) {
053: super (parent);
054: this .name = name;
055: this .bean = bean;
056: this .handler = handler;
057: }
058:
059: public PropertyPointer getPropertyPointer() {
060: return new DynamicPropertyPointer(this , handler);
061: }
062:
063: public NodeIterator createNodeIterator(String property,
064: boolean reverse, NodePointer startWith) {
065: return new PropertyIterator(this , property, reverse, startWith);
066: }
067:
068: public NodeIterator attributeIterator(QName name) {
069: return new DynamicAttributeIterator(this , name);
070: }
071:
072: public QName getName() {
073: return name;
074: }
075:
076: public boolean isDynamicPropertyDeclarationSupported() {
077: return true;
078: }
079:
080: /**
081: * Returns the DP object iself.
082: */
083: public Object getBaseValue() {
084: return bean;
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 boolean isCollection() {
095: return false;
096: }
097:
098: /**
099: * Returns 1.
100: */
101: public int getLength() {
102: return 1;
103: }
104:
105: public String asPath() {
106: if (parent != null) {
107: return super .asPath();
108: }
109: return "/";
110: }
111:
112: public int hashCode() {
113: return System.identityHashCode(bean) + name.hashCode();
114: }
115:
116: public boolean equals(Object object) {
117: if (object == this ) {
118: return true;
119: }
120:
121: if (!(object instanceof DynamicPointer)) {
122: return false;
123: }
124:
125: DynamicPointer other = (DynamicPointer) object;
126: return bean == other.bean && name.equals(other.name);
127: }
128: }
|