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.QName; 022import org.apache.commons.jxpath.ri.compiler.NodeNameTest; 023import org.apache.commons.jxpath.ri.compiler.NodeTest; 024import org.apache.commons.jxpath.ri.model.NodeIterator; 025import org.apache.commons.jxpath.ri.model.NodePointer; 026 027/** 028 * EvalContext that walks the "namespace::" axis. 029 */ 030public class NamespaceContext extends EvalContext { 031 032 private final NodeTest nodeTest; 033 private boolean setStarted; 034 private NodeIterator iterator; 035 private NodePointer currentNodePointer; 036 037 /** 038 * Constructs a new instance. 039 * 040 * @param parentContext represents the previous step on the path 041 * @param nodeTest is the name of the namespace we are looking for 042 */ 043 public NamespaceContext(final EvalContext parentContext, final NodeTest nodeTest) { 044 super(parentContext); 045 this.nodeTest = nodeTest; 046 } 047 048 @Override 049 public NodePointer getCurrentNodePointer() { 050 return currentNodePointer; 051 } 052 053 @Override 054 public boolean nextNode() { 055 super.setPosition(getCurrentPosition() + 1); 056 if (!setStarted) { 057 setStarted = true; 058 if (!(nodeTest instanceof NodeNameTest)) { 059 return false; 060 } 061 final NodeNameTest nodeNameTest = (NodeNameTest) nodeTest; 062 final QName testName = nodeNameTest.getNodeName(); 063 if (testName.getPrefix() != null) { 064 return false; 065 } 066 if (!nodeNameTest.isWildcard()) { 067 currentNodePointer = parentContext.getCurrentNodePointer().namespacePointer(testName.getName()); 068 return currentNodePointer != null; 069 } 070 iterator = parentContext.getCurrentNodePointer().namespaceIterator(); 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}