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  package org.apache.commons.jxpath.ri.axes;
18  
19  import org.apache.commons.jxpath.ri.EvalContext;
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
22  import org.apache.commons.jxpath.ri.compiler.NodeTest;
23  import org.apache.commons.jxpath.ri.model.NodeIterator;
24  import org.apache.commons.jxpath.ri.model.NodePointer;
25  
26  /**
27   * EvalContext that walks the "namespace::" axis.
28   *
29   * @author Dmitri Plotnikov
30   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
31   */
32  public class NamespaceContext extends EvalContext {
33      private NodeTest nodeTest;
34      private boolean setStarted = false;
35      private NodeIterator iterator;
36      private NodePointer currentNodePointer;
37  
38      /**
39       * @param parentContext represents the previous step on the path
40       * @param nodeTest is the name of the namespace we are looking for
41       */
42      public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
43          super(parentContext);
44          this.nodeTest = nodeTest;
45      }
46  
47      public NodePointer getCurrentNodePointer() {
48          return currentNodePointer;
49      }
50  
51      public void reset() {
52          setStarted = false;
53          iterator = null;
54          super.reset();
55      }
56  
57      public boolean setPosition(int position) {
58          if (position < getCurrentPosition()) {
59              reset();
60          }
61  
62          while (getCurrentPosition() < position) {
63              if (!nextNode()) {
64                  return false;
65              }
66          }
67          return true;
68      }
69  
70      public boolean nextNode() {
71          super.setPosition(getCurrentPosition() + 1);
72          if (!setStarted) {
73              setStarted = true;
74              if (!(nodeTest instanceof NodeNameTest)) {
75                  return false;
76              }
77  
78              NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
79              QName testName = nodeNameTest.getNodeName();
80              if (testName.getPrefix() != null) {
81                  return false;
82              }
83              if (nodeNameTest.isWildcard()) {
84                  iterator =
85                      parentContext.getCurrentNodePointer().namespaceIterator();
86              }
87              else {
88                  currentNodePointer =
89                      parentContext.getCurrentNodePointer().namespacePointer(
90                              testName.getName());
91                  return currentNodePointer != null;
92              }
93          }
94  
95          if (iterator == null) {
96              return false;
97          }
98          if (!iterator.setPosition(iterator.getPosition() + 1)) {
99              return false;
100         }
101         currentNodePointer = iterator.getNodePointer();
102         return true;
103     }
104 }