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.Compiler;
021import org.apache.commons.jxpath.ri.EvalContext;
022import org.apache.commons.jxpath.ri.QName;
023import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
024import org.apache.commons.jxpath.ri.compiler.NodeTest;
025import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
026import org.apache.commons.jxpath.ri.model.NodeIterator;
027import org.apache.commons.jxpath.ri.model.NodePointer;
028
029/**
030 * EvalContext that walks the "attribute::" axis.
031 */
032public class AttributeContext extends EvalContext {
033
034    private static final QName WILDCARD = new QName(null, "*");
035    private final NodeTest nodeTest;
036    private boolean setStarted;
037    private NodeIterator iterator;
038    private NodePointer currentNodePointer;
039
040    /**
041     * Constructs a new AttributeContext.
042     *
043     * @param parentContext represents the previous step on the path
044     * @param nodeTest      is the name of the attribute we are looking for
045     */
046    public AttributeContext(final EvalContext parentContext, final NodeTest nodeTest) {
047        super(parentContext);
048        this.nodeTest = nodeTest;
049    }
050
051    @Override
052    public NodePointer getCurrentNodePointer() {
053        return currentNodePointer;
054    }
055
056    @Override
057    public boolean nextNode() {
058        super.setPosition(getCurrentPosition() + 1);
059        if (!setStarted) {
060            setStarted = true;
061            QName qName;
062            if (nodeTest instanceof NodeNameTest) {
063                qName = ((NodeNameTest) nodeTest).getNodeName();
064            } else if (nodeTest instanceof NodeTypeTest && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
065                qName = WILDCARD;
066            } else {
067                iterator = null;
068                return false;
069            }
070            iterator = parentContext.getCurrentNodePointer().attributeIterator(qName);
071        }
072        if (iterator == null) {
073            return false;
074        }
075        if (!iterator.setPosition(iterator.getPosition() + 1)) {
076            return false;
077        }
078        currentNodePointer = iterator.getNodePointer();
079        return true;
080    }
081
082    @Override
083    public void reset() {
084        setStarted = false;
085        iterator = null;
086        super.reset();
087    }
088
089    @Override
090    public boolean setPosition(final int position) {
091        if (position < getCurrentPosition()) {
092            reset();
093        }
094        while (getCurrentPosition() < position) {
095            if (!nextNode()) {
096                return false;
097            }
098        }
099        return true;
100    }
101}