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 org.apache.commons.jxpath.JXPathContext;
019: import org.apache.commons.jxpath.ri.QName;
020: import org.apache.commons.jxpath.ri.model.NodePointer;
021:
022: /**
023: * Used when there is a need to construct a Pointer for a collection element
024: * that does not exist. For example, if the path is "foo[3]", but the
025: * collection "foo" only has one element or is empty or is null, the
026: * NullElementPointer can be used to capture this situation without putting a
027: * regular NodePointer into an invalid state. Just create a NullElementPointer
028: * with index 2 (= 3 - 1) and a "foo" pointer as the parent.
029: *
030: * @author Dmitri Plotnikov
031: * @version $Revision: 1.17 $ $Date: 2004/03/25 03:49:50 $
032: */
033: public class NullElementPointer extends CollectionPointer {
034:
035: public NullElementPointer(NodePointer parent, int index) {
036: super (parent, (Object) null);
037: this .index = index;
038: }
039:
040: public QName getName() {
041: return null;
042: }
043:
044: public Object getBaseValue() {
045: return null;
046: }
047:
048: public Object getImmediateNode() {
049: return null;
050: }
051:
052: public boolean isLeaf() {
053: return true;
054: }
055:
056: public boolean isCollection() {
057: return false;
058: }
059:
060: public PropertyPointer getPropertyPointer() {
061: return new NullPropertyPointer(this );
062: }
063:
064: public NodePointer getValuePointer() {
065: return new NullPointer(this , getName());
066: }
067:
068: public void setValue(Object value) {
069: throw new UnsupportedOperationException(
070: "Collection element does not exist: " + this );
071: }
072:
073: public boolean isActual() {
074: return false;
075: }
076:
077: public boolean isContainer() {
078: return true;
079: }
080:
081: public NodePointer createPath(JXPathContext context) {
082: return parent.createChild(context, null, index);
083: }
084:
085: public NodePointer createPath(JXPathContext context, Object value) {
086: return parent.createChild(context, null, index, value);
087: }
088:
089: public int hashCode() {
090: return getImmediateParentPointer().hashCode() + index;
091: }
092:
093: public boolean equals(Object object) {
094: if (object == this ) {
095: return true;
096: }
097:
098: if (!(object instanceof NullElementPointer)) {
099: return false;
100: }
101:
102: NullElementPointer other = (NullElementPointer) object;
103: return getImmediateParentPointer() == other
104: .getImmediateParentPointer()
105: && index == other.index;
106: }
107:
108: public int getLength() {
109: return 0;
110: }
111:
112: public String asPath() {
113: StringBuffer buffer = new StringBuffer();
114: NodePointer parent = getImmediateParentPointer();
115: if (parent != null) {
116: buffer.append(parent.asPath());
117: }
118: if (index != WHOLE_COLLECTION) {
119: // Address the list[1][2] case
120: if (parent != null && parent.getIndex() != WHOLE_COLLECTION) {
121: buffer.append("/.");
122: } else if (parent != null
123: && parent.getImmediateParentPointer() != null
124: && parent.getImmediateParentPointer().getIndex() != WHOLE_COLLECTION) {
125: buffer.append("/.");
126: }
127: buffer.append("[").append(index + 1).append(']');
128: }
129:
130: return buffer.toString();
131: }
132: }
|