01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
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: /**
26: * EvalContext that walks the "attribute::" axis.
27: *
28: * @author Dmitri Plotnikov
29: * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:38 $
30: */
31: public class AttributeContext extends EvalContext {
32: private NodeTest nodeTest;
33: private boolean setStarted = false;
34: private NodeIterator iterator;
35: private NodePointer currentNodePointer;
36:
37: /**
38: * @param parentContext represents the previous step on the path
39: * @param nameTest is the name of the attribute we are looking for
40: */
41: public AttributeContext(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: QName name = ((NodeNameTest) nodeTest).getNodeName();
77: iterator = parentContext.getCurrentNodePointer()
78: .attributeIterator(name);
79: }
80:
81: if (iterator == null) {
82: return false;
83: }
84: if (!iterator.setPosition(iterator.getPosition() + 1)) {
85: return false;
86: }
87: currentNodePointer = iterator.getNodePointer();
88: return true;
89: }
90: }
|