1 16 package org.apache.commons.jxpath.ri.axes; 17 18 import org.apache.commons.jxpath.ri.EvalContext; 19 import org.apache.commons.jxpath.ri.QName; 20 import org.apache.commons.jxpath.ri.compiler.NodeNameTest; 21 import org.apache.commons.jxpath.ri.compiler.NodeTest; 22 import org.apache.commons.jxpath.ri.model.NodeIterator; 23 import org.apache.commons.jxpath.ri.model.NodePointer; 24 25 31 public class NamespaceContext extends EvalContext { 32 private NodeTest nodeTest; 33 private boolean setStarted = false; 34 private NodeIterator iterator; 35 private NodePointer currentNodePointer; 36 37 41 public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) { 42 super(parentContext); 43 this.nodeTest = nodeTest; 44 } 45 46 public NodePointer getCurrentNodePointer() { 47 return currentNodePointer; 48 } 49 50 public void reset() { 51 setStarted = false; 52 iterator = null; 53 super.reset(); 54 } 55 56 public boolean setPosition(int position) { 57 if (position < getCurrentPosition()) { 58 reset(); 59 } 60 61 while (getCurrentPosition() < position) { 62 if (!nextNode()) { 63 return false; 64 } 65 } 66 return true; 67 } 68 69 public boolean nextNode() { 70 super.setPosition(getCurrentPosition() + 1); 71 if (!setStarted) { 72 setStarted = true; 73 if (!(nodeTest instanceof NodeNameTest)) { 74 return false; 75 } 76 77 NodeNameTest nodeNameTest = (NodeNameTest) nodeTest; 78 QName testName = nodeNameTest.getNodeName(); 79 if (testName.getPrefix() != null) { 80 return false; 81 } 82 if (nodeNameTest.isWildcard()) { 83 iterator = 84 parentContext.getCurrentNodePointer().namespaceIterator(); 85 } 86 else { 87 currentNodePointer = 88 parentContext.getCurrentNodePointer().namespacePointer( 89 testName.getName()); 90 return currentNodePointer != null; 91 } 92 } 93 94 if (iterator == null) { 95 return false; 96 } 97 if (!iterator.setPosition(iterator.getPosition() + 1)) { 98 return false; 99 } 100 currentNodePointer = iterator.getNodePointer(); 101 return true; 102 } 103 } | Popular Tags |