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     */
017    package org.apache.commons.jxpath.ri.axes;
018    
019    import org.apache.commons.jxpath.ri.EvalContext;
020    import org.apache.commons.jxpath.ri.compiler.NodeTest;
021    import org.apache.commons.jxpath.ri.model.NodePointer;
022    
023    /**
024     * EvalContext that walks the "parent::" axis.
025     *
026     * @author Dmitri Plotnikov
027     * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
028     */
029    public class ParentContext extends EvalContext {
030        private NodeTest nodeTest;
031        private boolean setStarted = false;
032        private NodePointer currentNodePointer;
033    
034        /**
035         * Create a new ParentContext.
036         * @param parentContext parent context
037         * @param nodeTest test
038         */
039        public ParentContext(EvalContext parentContext, NodeTest nodeTest) {
040            super(parentContext);
041            this.nodeTest = nodeTest;
042        }
043    
044        public NodePointer getCurrentNodePointer() {
045            return currentNodePointer;
046        }
047    
048        public int getCurrentPosition() {
049            return 1;
050        }
051    
052        public int getDocumentOrder() {
053            return -1;
054        }
055    
056        public void reset() {
057            super.reset();
058            setStarted = false;
059        }
060    
061        public boolean setPosition(int position) {
062            super.setPosition(position);
063            return position == 1;
064        }
065    
066        public boolean nextNode() {
067            // Each set contains exactly one node: the parent
068            if (setStarted) {
069                return false;
070            }
071            setStarted = true;
072            NodePointer thisLocation = parentContext.getCurrentNodePointer();
073            currentNodePointer = thisLocation.getImmediateParentPointer();
074            while (currentNodePointer != null
075                && currentNodePointer.isContainer()) {
076                currentNodePointer = currentNodePointer.getImmediateParentPointer();
077            }
078            if (currentNodePointer != null
079                && currentNodePointer.testNode(nodeTest)) {
080                position++;
081                return true;
082            }
083            return false;
084        }
085    }