    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.NodeSet;
    020    import org.apache.commons.jxpath.ri.EvalContext;
    021    import org.apache.commons.jxpath.ri.model.NodePointer;
    022
    023    /**
    024     * A simple context that is based on a {@link NodeSet}.
    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 NodeSetContext extends EvalContext {
    030        private boolean startedSet = false;
    031        private NodeSet nodeSet;
    032
    033        /**
    034         * Create a new NodeSetContext.
    035         * @param parentContext parent context
    036         * @param nodeSet associated NodeSet
    037         */
    038        public NodeSetContext(EvalContext parentContext, NodeSet nodeSet) {
    039            super(parentContext);
    040            this.nodeSet = nodeSet;
    041        }
    042
    043        public NodeSet getNodeSet() {
    044            return nodeSet;
    045        }
    046
    047        public NodePointer getCurrentNodePointer() {
    048            if (position == 0 && !setPosition(1)) {
    049                return null;
    050            }
    051            return (NodePointer) nodeSet.getPointers().get(position - 1);
    052        }
    053
    054        public boolean setPosition(int position) {
    055            super.setPosition(position);
    056            return position >= 1 && position <= nodeSet.getPointers().size();
    057        }
    058
    059        public boolean nextSet() {
    060            if (startedSet) {
    061                return false;
    062            }
    063            startedSet = true;
    064            return true;
    065        }
    066
    067        public boolean nextNode() {
    068            return setPosition(position + 1);
    069        }
    070    }