1 package org.jaxen.util; 2 3 /* 4 * $Header$ 5 * $Revision$ 6 * $Date$ 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$ 49 */ 50 51 import org.jaxen.Navigator; 52 import org.jaxen.UnsupportedAxisException; 53 import org.jaxen.JaxenRuntimeException; 54 55 import java.util.Iterator; 56 import java.util.NoSuchElementException; 57 import java.util.ArrayList; 58 59 /** 60 * Represents the XPath <code>descendant</code> axis. 61 * The "<code>descendant</code> axis contains the descendants of the context node; 62 * a descendant is a child or a child of a child and so on; thus 63 * the descendant axis never contains attribute or namespace nodes." 64 * 65 * @version 1.2b12 66 */ 67 public class DescendantAxisIterator implements Iterator 68 { 69 70 private ArrayList stack = new ArrayList(); 71 private Iterator children; 72 private Navigator navigator; 73 74 /** 75 * Create a new <code>descendant</code> axis iterator. 76 * 77 * @param contextNode the node to start from 78 * @param navigator the object model specific navigator 79 */ 80 public DescendantAxisIterator(Object contextNode, 81 Navigator navigator) throws UnsupportedAxisException 82 { 83 this(navigator, navigator.getChildAxisIterator(contextNode)); 84 } 85 86 public DescendantAxisIterator(Navigator navigator, 87 Iterator iterator) 88 { 89 this.navigator = navigator; 90 this.children = iterator; 91 } 92 93 /** 94 * Returns true if there are any descendants remaining; false otherwise. 95 * 96 * @return true if any descendants remain; false otherwise 97 * 98 * @see java.util.Iterator#hasNext() 99 */ public boolean hasNext() 100 { 101 while (!children.hasNext()) 102 { 103 if (stack.isEmpty()) 104 { 105 return false; 106 } 107 children = (Iterator) stack.remove(stack.size()-1); 108 } 109 return true; 110 } 111 112 /** 113 * Returns the next descendant node. 114 * 115 * @return the next descendant node 116 * 117 * @throws NoSuchElementException if no descendants remain 118 * 119 * @see java.util.Iterator#next() 120 */ 121 public Object next() 122 { 123 try 124 { 125 if (hasNext()) 126 { 127 Object node = children.next(); 128 stack.add(children); 129 children = navigator.getChildAxisIterator(node); 130 return node; 131 } 132 throw new NoSuchElementException(); 133 } 134 catch (UnsupportedAxisException e) 135 { 136 throw new JaxenRuntimeException(e); 137 } 138 } 139 140 /** 141 * This operation is not supported. 142 * 143 * @throws UnsupportedOperationException always 144 */ 145 public void remove() 146 { 147 throw new UnsupportedOperationException(); 148 } 149 150 }