    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.Pointer;
    021import org.apache.commons.jxpath.ri.EvalContext;
    022import org.apache.commons.jxpath.ri.compiler.NodeTest;
    023import org.apache.commons.jxpath.ri.model.NodePointer;
    024
    025/**
    026 * EvalContext that returns the current node from the parent context if the test succeeds.
    027 */
    028public class SelfContext extends EvalContext {
    029
    030    private final NodeTest nodeTest;
    031    private boolean startedSet;
    032    private NodePointer nodePointer;
    033
    034    /**
    035     * Constructs a new SelfContext.
    036     *
    037     * @param parentContext EvalContext
    038     * @param nodeTest      guard
    039     */
    040    public SelfContext(final EvalContext parentContext, final NodeTest nodeTest) {
    041        super(parentContext);
    042        this.nodeTest = nodeTest;
    043    }
    044
    045    @Override
    046    public NodePointer getCurrentNodePointer() {
    047        if (position == 0 && !setPosition(1)) {
    048            return null;
    049        }
    050        return nodePointer;
    051    }
    052
    053    @Override
    054    public Pointer getSingleNodePointer() {
    055        return parentContext.getSingleNodePointer();
    056    }
    057
    058    @Override
    059    public boolean nextNode() {
    060        return setPosition(getCurrentPosition() + 1);
    061    }
    062
    063    @Override
    064    public void reset() {
    065        super.reset();
    066        startedSet = false;
    067    }
    068
    069    @Override
    070    public boolean setPosition(final int position) {
    071        if (position != 1) {
    072            return false;
    073        }
    074        super.setPosition(position);
    075        if (!startedSet) {
    076            startedSet = true;
    077            nodePointer = parentContext.getCurrentNodePointer();
    078        }
    079        if (nodePointer == null) {
    080            return false;
    081        }
    082        return nodeTest == null || nodePointer.testNode(nodeTest);
    083    }
    084}