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