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.container;
017:
018: import java.util.Locale;
019:
020: import org.apache.commons.jxpath.Container;
021: import org.apache.commons.jxpath.ri.QName;
022: import org.apache.commons.jxpath.ri.compiler.NodeTest;
023: import org.apache.commons.jxpath.ri.model.NodeIterator;
024: import org.apache.commons.jxpath.ri.model.NodePointer;
025: import org.apache.commons.jxpath.util.ValueUtils;
026:
027: /**
028: * Transparent pointer to a Container. The getValue() method
029: * returns the contents of the container, rather than the container
030: * itself.
031: *
032: * @author Dmitri Plotnikov
033: * @version $Revision: 1.13 $ $Date: 2004/04/04 22:06:36 $
034: */
035: public class ContainerPointer extends NodePointer {
036: private Container container;
037: private NodePointer valuePointer;
038:
039: public ContainerPointer(Container container, Locale locale) {
040: super (null, locale);
041: this .container = container;
042: }
043:
044: public ContainerPointer(NodePointer parent, Container container) {
045: super (parent);
046: this .container = container;
047: }
048:
049: /**
050: * This type of node is auxiliary.
051: */
052: public boolean isContainer() {
053: return true;
054: }
055:
056: public QName getName() {
057: return null;
058: }
059:
060: public Object getBaseValue() {
061: return container;
062: }
063:
064: public boolean isCollection() {
065: Object value = getBaseValue();
066: return value != null && ValueUtils.isCollection(value);
067: }
068:
069: public int getLength() {
070: Object value = getBaseValue();
071: if (value == null) {
072: return 1;
073: }
074: return ValueUtils.getLength(value);
075: }
076:
077: public boolean isLeaf() {
078: return getValuePointer().isLeaf();
079: }
080:
081: public Object getImmediateNode() {
082: Object value = getBaseValue();
083: if (index != WHOLE_COLLECTION) {
084: if (index >= 0 && index < getLength()) {
085: return ValueUtils.getValue(value, index);
086: } else {
087: return null;
088: }
089: } else {
090: return ValueUtils.getValue(value);
091: }
092: }
093:
094: public void setValue(Object value) {
095: // TODO: what if this is a collection?
096: container.setValue(value);
097: }
098:
099: public NodePointer getImmediateValuePointer() {
100: if (valuePointer == null) {
101: Object value = getImmediateNode();
102: valuePointer = NodePointer.newChildNodePointer(this ,
103: getName(), value);
104: }
105: return valuePointer;
106: }
107:
108: public int hashCode() {
109: return System.identityHashCode(container) + index;
110: }
111:
112: public boolean equals(Object object) {
113: if (object == this ) {
114: return true;
115: }
116:
117: if (!(object instanceof ContainerPointer)) {
118: return false;
119: }
120:
121: ContainerPointer other = (ContainerPointer) object;
122: return container == other.container && index == other.index;
123: }
124:
125: public NodeIterator childIterator(NodeTest test, boolean reverse,
126: NodePointer startWith) {
127: return getValuePointer()
128: .childIterator(test, reverse, startWith);
129: }
130:
131: public NodeIterator attributeIterator(QName name) {
132: return getValuePointer().attributeIterator(name);
133: }
134:
135: public NodeIterator namespaceIterator() {
136: return getValuePointer().namespaceIterator();
137: }
138:
139: public NodePointer namespacePointer(String namespace) {
140: return getValuePointer().namespacePointer(namespace);
141: }
142:
143: public boolean testNode(NodeTest nodeTest) {
144: return getValuePointer().testNode(nodeTest);
145: }
146:
147: public int compareChildNodePointers(NodePointer pointer1,
148: NodePointer pointer2) {
149: return pointer1.getIndex() - pointer2.getIndex();
150: }
151:
152: public String getNamespaceURI(String prefix) {
153: return getValuePointer().getNamespaceURI(prefix);
154: }
155:
156: public String asPath() {
157: if (parent != null) {
158: return parent.asPath();
159: }
160: return "/";
161: }
162: }
|