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.axes;
017:
018: import org.apache.commons.jxpath.ri.EvalContext;
019: import org.apache.commons.jxpath.ri.QName;
020: import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
021: import org.apache.commons.jxpath.ri.compiler.NodeTest;
022: import org.apache.commons.jxpath.ri.model.NodeIterator;
023: import org.apache.commons.jxpath.ri.model.NodePointer;
024:
025: /**
026: * EvalContext that walks the "namespace::" axis.
027: *
028: * @author Dmitri Plotnikov
029: * @version $Revision: 1.10 $ $Date: 2004/03/25 05:42:01 $
030: */
031: public class NamespaceContext extends EvalContext {
032: private NodeTest nodeTest;
033: private boolean setStarted = false;
034: private NodeIterator iterator;
035: private NodePointer currentNodePointer;
036:
037: /**
038: * @param parentContext represents the previous step on the path
039: * @param nodeTest is the name of the namespace we are looking for
040: */
041: public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
042: super (parentContext);
043: this .nodeTest = nodeTest;
044: }
045:
046: public NodePointer getCurrentNodePointer() {
047: return currentNodePointer;
048: }
049:
050: public void reset() {
051: setStarted = false;
052: iterator = null;
053: super .reset();
054: }
055:
056: public boolean setPosition(int position) {
057: if (position < getCurrentPosition()) {
058: reset();
059: }
060:
061: while (getCurrentPosition() < position) {
062: if (!nextNode()) {
063: return false;
064: }
065: }
066: return true;
067: }
068:
069: public boolean nextNode() {
070: super .setPosition(getCurrentPosition() + 1);
071: if (!setStarted) {
072: setStarted = true;
073: if (!(nodeTest instanceof NodeNameTest)) {
074: return false;
075: }
076:
077: NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
078: QName testName = nodeNameTest.getNodeName();
079: if (testName.getPrefix() != null) {
080: return false;
081: }
082: if (nodeNameTest.isWildcard()) {
083: iterator = parentContext.getCurrentNodePointer()
084: .namespaceIterator();
085: } else {
086: currentNodePointer = parentContext
087: .getCurrentNodePointer().namespacePointer(
088: testName.getName());
089: return currentNodePointer != null;
090: }
091: }
092:
093: if (iterator == null) {
094: return false;
095: }
096: if (!iterator.setPosition(iterator.getPosition() + 1)) {
097: return false;
098: }
099: currentNodePointer = iterator.getNodePointer();
100: return true;
101: }
102: }
|