View Javadoc

1   package org.jaxen.util;
2   
3   /*
4    * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/util/AncestorOrSelfAxisIterator.java,v 1.14 2006/11/09 18:20:12 elharo Exp $
5    * $Revision: 1.14 $
6    * $Date: 2006/11/09 18:20:12 $
7    *
8    * ====================================================================
9    *
10   * Copyright 2000-2005 bob mcwhirter & James Strachan.
11   * All rights reserved.
12   *
13   *
14   * Redistribution and use in source and binary forms, with or without
15   * modification, are permitted provided that the following conditions are
16   * met:
17   * 
18   *   * Redistributions of source code must retain the above copyright
19   *     notice, this list of conditions and the following disclaimer.
20   * 
21   *   * Redistributions in binary form must reproduce the above copyright
22   *     notice, this list of conditions and the following disclaimer in the
23   *     documentation and/or other materials provided with the distribution.
24   * 
25   *   * Neither the name of the Jaxen Project nor the names of its
26   *     contributors may be used to endorse or promote products derived 
27   *     from this software without specific prior written permission.
28   * 
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
30   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * ====================================================================
42   * This software consists of voluntary contributions made by many
43   * individuals on behalf of the Jaxen Project and was originally
44   * created by bob mcwhirter <bob@werken.com> and
45   * James Strachan <jstrachan@apache.org>.  For more information on the
46   * Jaxen Project, please see <http://www.jaxen.org/>.
47   *
48   * $Id: AncestorOrSelfAxisIterator.java,v 1.14 2006/11/09 18:20:12 elharo Exp $
49  */
50  
51  import java.util.Iterator;
52  import java.util.NoSuchElementException;
53  
54  import org.jaxen.Navigator;
55  import org.jaxen.UnsupportedAxisException;
56  import org.jaxen.JaxenRuntimeException;
57  
58  /***
59   * <p>
60   * Represents the XPath <code>ancestor-or-self</code> axis. 
61   * The "<code>ancestor-or-self</code> axis contains the context node and 
62   * the ancestors of the context node; thus, the ancestor axis will 
63   * always include the root node."
64   * </p>
65   * 
66   * @version 1.2b12
67   */
68  public class AncestorOrSelfAxisIterator implements Iterator
69  {
70      
71      private Object    contextNode;
72      private Navigator navigator;
73  
74      /***
75       * Create a new <code>ancestor-or-self</code> axis iterator.
76       * 
77       * @param contextNode the node to start from
78       * @param navigator the object model specific navigator
79       */
80      public AncestorOrSelfAxisIterator(Object contextNode,
81                                        Navigator navigator)
82      {
83          // XXX should we throw a NullPointerException here if contextNode is null?
84          this.contextNode = contextNode;
85          this.navigator = navigator;
86      }
87  
88      /***
89       * Returns true if there are any nodes remaining 
90       * on the ancestor-or-self axis; false otherwise.
91       * 
92       * @return true if any ancestors or self remain
93       * 
94       * @see java.util.Iterator#hasNext()
95       */
96      public boolean hasNext()
97      {
98          return contextNode != null;
99      }
100 
101     /***
102      * Returns the next ancestor-or-self node.
103      * 
104      * @return the next ancestor-or-self node
105      * 
106      * @throws NoSuchElementException if no ancestors remain
107      * 
108      * @see java.util.Iterator#next()
109      */
110     public Object next()
111     {
112         try
113         {
114             if (hasNext()) {
115                 Object result = contextNode;
116                 contextNode = navigator.getParentNode(contextNode);
117                 return result;
118             }
119             throw new NoSuchElementException("Exhausted ancestor-or-self axis");
120         }
121         catch (UnsupportedAxisException e)
122         {
123             throw new JaxenRuntimeException(e);
124         }
125     }
126 
127     /***
128      * This operation is not supported.
129      * 
130      * @throws UnsupportedOperationException always
131      */
132     public void remove()
133     {
134         throw new UnsupportedOperationException();
135     }
136     
137 }