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.EvalContext;
21  import org.apache.commons.jxpath.ri.QName;
22  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
23  import org.apache.commons.jxpath.ri.compiler.NodeTest;
24  import org.apache.commons.jxpath.ri.model.NodeIterator;
25  import org.apache.commons.jxpath.ri.model.NodePointer;
26  
27  /**
28   * EvalContext that walks the "namespace::" axis.
29   */
30  public class NamespaceContext extends EvalContext {
31  
32      private final NodeTest nodeTest;
33      private boolean setStarted;
34      private NodeIterator iterator;
35      private NodePointer currentNodePointer;
36  
37      /**
38       * Constructs a new instance.
39       *
40       * @param parentContext represents the previous step on the path
41       * @param nodeTest      is the name of the namespace we are looking for
42       */
43      public NamespaceContext(final EvalContext parentContext, final NodeTest nodeTest) {
44          super(parentContext);
45          this.nodeTest = nodeTest;
46      }
47  
48      @Override
49      public NodePointer getCurrentNodePointer() {
50          return currentNodePointer;
51      }
52  
53      @Override
54      public boolean nextNode() {
55          super.setPosition(getCurrentPosition() + 1);
56          if (!setStarted) {
57              setStarted = true;
58              if (!(nodeTest instanceof NodeNameTest)) {
59                  return false;
60              }
61              final NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
62              final QName testName = nodeNameTest.getNodeName();
63              if (testName.getPrefix() != null) {
64                  return false;
65              }
66              if (!nodeNameTest.isWildcard()) {
67                  currentNodePointer = parentContext.getCurrentNodePointer().namespacePointer(testName.getName());
68                  return currentNodePointer != null;
69              }
70              iterator = parentContext.getCurrentNodePointer().namespaceIterator();
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 }