View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.axes;
19  
20  import org.apache.commons.jxpath.ri.Compiler;
21  import org.apache.commons.jxpath.ri.EvalContext;
22  import org.apache.commons.jxpath.ri.QName;
23  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
24  import org.apache.commons.jxpath.ri.compiler.NodeTest;
25  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
26  import org.apache.commons.jxpath.ri.model.NodeIterator;
27  import org.apache.commons.jxpath.ri.model.NodePointer;
28  
29  /**
30   * EvalContext that walks the "attribute::" axis.
31   */
32  public class AttributeContext extends EvalContext {
33  
34      private static final QName WILDCARD = new QName(null, "*");
35      private final NodeTest nodeTest;
36      private boolean setStarted;
37      private NodeIterator iterator;
38      private NodePointer currentNodePointer;
39  
40      /**
41       * Constructs a new AttributeContext.
42       *
43       * @param parentContext represents the previous step on the path
44       * @param nodeTest      is the name of the attribute we are looking for
45       */
46      public AttributeContext(final EvalContext parentContext, final NodeTest nodeTest) {
47          super(parentContext);
48          this.nodeTest = nodeTest;
49      }
50  
51      @Override
52      public NodePointer getCurrentNodePointer() {
53          return currentNodePointer;
54      }
55  
56      @Override
57      public boolean nextNode() {
58          super.setPosition(getCurrentPosition() + 1);
59          if (!setStarted) {
60              setStarted = true;
61              QName qName;
62              if (nodeTest instanceof NodeNameTest) {
63                  qName = ((NodeNameTest) nodeTest).getNodeName();
64              } else if (nodeTest instanceof NodeTypeTest && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
65                  qName = WILDCARD;
66              } else {
67                  iterator = null;
68                  return false;
69              }
70              iterator = parentContext.getCurrentNodePointer().attributeIterator(qName);
71          }
72          if (iterator == null) {
73              return false;
74          }
75          if (!iterator.setPosition(iterator.getPosition() + 1)) {
76              return false;
77          }
78          currentNodePointer = iterator.getNodePointer();
79          return true;
80      }
81  
82      @Override
83      public void reset() {
84          setStarted = false;
85          iterator = null;
86          super.reset();
87      }
88  
89      @Override
90      public boolean setPosition(final int position) {
91          if (position < getCurrentPosition()) {
92              reset();
93          }
94          while (getCurrentPosition() < position) {
95              if (!nextNode()) {
96                  return false;
97              }
98          }
99          return true;
100     }
101 }