home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / classes / com / ms / xml / om / SiblingEnumeration.java < prev    next >
Encoding:
Java Source  |  1997-08-27  |  4.1 KB  |  156 lines

  1. /*
  2.  * @(#)SiblingEnumeration.java 1.0 7/11/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. package com.ms.xml.om;
  9.  
  10. import com.ms.xml.util.EnumWrapper;
  11. import com.ms.xml.util.Name;
  12. import com.ms.xml.util.Queue;
  13. import java.util.Enumeration;
  14. import java.util.Stack;
  15.  
  16. /**
  17.  * An Enumeration for iterating over the siblings of 
  18.  * a given node in the XML tree
  19.  *
  20.  * @version 1.0, 8/27/97
  21.  * @see Element
  22.  * @see Name
  23.  */
  24. public class SiblingEnumeration implements Enumeration
  25. {   
  26.     /**
  27.      * Creates new iterator for iterating over all of the children
  28.      * of the given root node.
  29.      */
  30.     public SiblingEnumeration(Element node)
  31.     {
  32.         Initialize( node, true );
  33.     }
  34.     public SiblingEnumeration(Element node, boolean enumDir )
  35.     {
  36.         Initialize( node, enumDir );
  37.     }
  38.  
  39.     private void Initialize( Element node, boolean enumDir )
  40.     {
  41.         this.originalNode   = node;
  42.         this.enumerationDir = enumDir;
  43.         this.next           = null;
  44.         this.parent         = null;
  45.         this.siblings       = null;
  46.         this.siblingIndex   = 0;
  47.  
  48.         if( node != null )
  49.         {
  50.             this.parent = node.getParent();
  51.             if( this.parent!=null ) 
  52.             {
  53.                 if( !enumDir )  // backwards traversal
  54.                 {
  55.                     // locate the given node in the children list                              
  56.                     int i = 0;
  57.  
  58.                     Enumeration en = this.parent.getElements();
  59.                     while( en.hasMoreElements() && (Element)en.nextElement() != node )
  60.                         i++;   
  61.                     
  62.                     // If (i >= numElements) then node was not found.
  63.                     // This should never happen, since parent = node.getParent()
  64.                     this.siblingIndex = (i < this.parent.numElements() ) ? i : 0;
  65.                 }
  66.                 else // default to forward traversal
  67.                 {
  68.                     // Advance siblings enumerator to correct position
  69.                     this.siblings = this.parent.getElements();
  70.                     while( siblings.hasMoreElements() && (Element)siblings.nextElement() != node )
  71.                         ;   
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Reset the iterator so you can iterate through the elements again.
  79.      */
  80.     public void reset()
  81.     {
  82.         Initialize( originalNode, enumerationDir );
  83.     }
  84.  
  85.     /**
  86.      * Return whether or not there are any more matching elements.
  87.      * @return true if the next call to nextElement will return
  88.      * non null result.
  89.      */
  90.     public boolean hasMoreElements()
  91.     {       
  92.         if (next == null) 
  93.         {
  94.             next = next();
  95.         }
  96.         return (next != null) ? true : false;
  97.     }
  98.  
  99.     /**
  100.      * Return the next matching element.
  101.      * @return Element or null of there are no more matching elements.
  102.      */
  103.     public Object nextElement()
  104.     {
  105.         if (next != null) 
  106.         {
  107.             Element result = next;
  108.             next = null;
  109.             return result;
  110.         }
  111.         return next();
  112.     }
  113.  
  114.     /**
  115.      * Internal method for getting next element.
  116.      */
  117.     Element next()
  118.     {                
  119.         if( !enumerationDir ) // backwards traversal
  120.             return prevSibling();
  121.         else // default to forward traversal
  122.             return nextSibling();
  123.     }
  124.  
  125.     Element nextSibling()
  126.     {              
  127.         if (siblings != null) 
  128.         {
  129.             Element e = (Element)siblings.nextElement();
  130.             return e;
  131.         }
  132.         return null;
  133.     }
  134.  
  135.     Element prevSibling()
  136.     {
  137.         if( parent != null ) 
  138.         {
  139.             Element e = parent.getChild( --siblingIndex );
  140.             return e;
  141.         }
  142.         return null;
  143.     } 
  144.  
  145.     Element originalNode;
  146.  
  147.     Element next;
  148.  
  149.     // Sibling enumeration
  150.     Element parent;
  151.     Enumeration siblings;
  152.     int siblingIndex;  
  153.  
  154.     boolean enumerationDir;  // true = forwards  -  false = backwards
  155. }
  156.