001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath.ri.axes;
018
019import org.apache.commons.jxpath.ri.EvalContext;
020import org.apache.commons.jxpath.ri.QName;
021import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
022import org.apache.commons.jxpath.ri.compiler.NodeTest;
023import org.apache.commons.jxpath.ri.model.NodeIterator;
024import org.apache.commons.jxpath.ri.model.NodePointer;
025
026/**
027 * EvalContext that walks the "namespace::" axis.
028 *
029 * @author Dmitri Plotnikov
030 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
031 */
032public class NamespaceContext extends EvalContext {
033    private NodeTest nodeTest;
034    private boolean setStarted = false;
035    private NodeIterator iterator;
036    private NodePointer currentNodePointer;
037
038    /**
039     * @param parentContext represents the previous step on the path
040     * @param nodeTest is the name of the namespace we are looking for
041     */
042    public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
043        super(parentContext);
044        this.nodeTest = nodeTest;
045    }
046
047    public NodePointer getCurrentNodePointer() {
048        return currentNodePointer;
049    }
050
051    public void reset() {
052        setStarted = false;
053        iterator = null;
054        super.reset();
055    }
056
057    public boolean setPosition(int position) {
058        if (position < getCurrentPosition()) {
059            reset();
060        }
061
062        while (getCurrentPosition() < position) {
063            if (!nextNode()) {
064                return false;
065            }
066        }
067        return true;
068    }
069
070    public boolean nextNode() {
071        super.setPosition(getCurrentPosition() + 1);
072        if (!setStarted) {
073            setStarted = true;
074            if (!(nodeTest instanceof NodeNameTest)) {
075                return false;
076            }
077
078            NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
079            QName testName = nodeNameTest.getNodeName();
080            if (testName.getPrefix() != null) {
081                return false;
082            }
083            if (nodeNameTest.isWildcard()) {
084                iterator =
085                    parentContext.getCurrentNodePointer().namespaceIterator();
086            }
087            else {
088                currentNodePointer =
089                    parentContext.getCurrentNodePointer().namespacePointer(
090                            testName.getName());
091                return currentNodePointer != null;
092            }
093        }
094
095        if (iterator == null) {
096            return false;
097        }
098        if (!iterator.setPosition(iterator.getPosition() + 1)) {
099            return false;
100        }
101        currentNodePointer = iterator.getNodePointer();
102        return true;
103    }
104}