/*
 * $Header$
 * $Revision: 318 $
 * $Date: 2003-06-29 11:15:15 -0700 (Sun, 29 Jun 2003) $
 *
 * ====================================================================
 *
 * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions, and the disclaimer that follows 
 *    these conditions in the documentation and/or other materials 
 *    provided with the distribution.
 *
 * 3. The name "Jaxen" must not be used to endorse or promote products
 *    derived from this software without prior written permission.  For
 *    written permission, please contact license@jaxen.org.
 * 
 * 4. Products derived from this software may not be called "Jaxen", nor
 *    may "Jaxen" appear in their name, without prior written permission
 *    from the Jaxen Project Management (pm@jaxen.org).
 * 
 * In addition, we request (but do not require) that you include in the 
 * end-user documentation provided with the redistribution and/or in the 
 * software itself an acknowledgement equivalent to the following:
 *     "This product includes software developed by the
 *      Jaxen Project (http://www.jaxen.org/)."
 * Alternatively, the acknowledgment may be graphical using the logos 
 * available at http://www.jaxen.org/
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 * This software consists of voluntary contributions made by many 
 * individuals on behalf of the Jaxen Project and was originally 
 * created by bob mcwhirter <bob@werken.com> and 
 * James Strachan <jstrachan@apache.org>.  For more information on the 
 * Jaxen Project, please see <http://www.jaxen.org/>.
 * 
 * $Id: PrecedingSiblingAxisIterator.java 318 2003-06-29 18:15:15Z ssanders $
 */



package org.jaxen.util;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

import org.jaxen.Navigator;
import org.jaxen.UnsupportedAxisException;

public class PrecedingSiblingAxisIterator implements Iterator
{
    private Object    contextNode;
    private Navigator navigator;

    private Iterator  siblingIter;

    private Object    nextObj;

    public PrecedingSiblingAxisIterator(Object contextNode,
                                        Navigator navigator) throws UnsupportedAxisException
    {
        this.contextNode = contextNode;
        this.navigator   = navigator;

        init();

        stepAhead();
    }

    private void init() throws UnsupportedAxisException
    {
        Object parent = this.navigator.getParentNode( this.contextNode );

        List siblings = Collections.EMPTY_LIST;

        if ( parent != null )
        {
            Iterator childIter = this.navigator.getChildAxisIterator( parent );
            Object   eachChild = null;
            
            siblings = new LinkedList();
            
            while ( childIter.hasNext() )
            {
                eachChild = childIter.next();
                
                if ( eachChild == this.contextNode )
                {
                    break;
                }

                ((LinkedList)siblings).addFirst( eachChild );
            }
        }

        this.siblingIter = siblings.iterator();
    }

    public boolean hasNext()
    {
        return ( this.nextObj != null );
    }

    public Object next() throws NoSuchElementException
    {
        if ( ! hasNext() )
        {
            throw new NoSuchElementException();
        }

        Object obj = this.nextObj;

        this.nextObj = null;

        stepAhead();

        return obj;
    }

    public void remove() throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException();
    }

    private void stepAhead()
    {
        if ( this.nextObj != null )
        {
            return;
        }

        Object obj = null;

        while ( siblingIter.hasNext() )
        {
            obj = siblingIter.next();

            this.nextObj = obj;
            break;
            
/*
             
             This should iterate through all nodes, not necessarily just elements
             
            if ( this.navigator.isElement( obj ) )
            {
                this.nextObj = obj;
                break;
            }
*/             
        }
    }
}

@@ -22,19 +22,19 @@
 vision: 
-318
+579
  $%0A * $D
@@ -45,48 +45,48 @@
  200
-3-06-29 11:15:15 -0700 (Sun, 29 Jun
+5-04-08 06:51:08 -0700 (Fri, 08 Apr
  200
-3
+5
 ) $%0A
@@ -2840,41 +2840,39 @@
 ava 
-318
+579
  200
-3-06-29 18:15:15Z ssanders
+5-04-08 13:51:08Z elharo
  $%0A 
@@ -3293,17 +3293,16 @@
 ngIter;%0A
-%0A
     priv
@@ -3918,60 +3918,8 @@
  );%0A
-            Object   eachChild = null;%0A            %0A
     
@@ -4040,16 +4040,23 @@
         
+Object 
 eachChil
@@ -4128,12 +4128,16 @@
 hild
- == 
+.equals(
 this
@@ -4148,16 +4148,17 @@
 textNode
+)
  )%0A     
@@ -5122,283 +5122,8 @@
 eak;
-%0A            %0A/*%0A             %0A             This should iterate through all nodes, not necessarily just elements%0A             %0A            if ( this.navigator.isElement( obj ) )%0A            %7B%0A                this.nextObj = obj;%0A                break;%0A            %7D%0A*/        
     

Fix for Jaxen-47: use equals() to compare nodes rather than ==