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 / ElementEnumeration.java < prev    next >
Encoding:
Java Source  |  1997-12-01  |  3.3 KB  |  124 lines

  1. /*
  2.  * @(#)ElementEnumeration.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.Name;
  11. import java.util.Enumeration;
  12.  
  13. /**
  14.  * This class is a simple Enumeration for iterating over the immediate children
  15.  * of a given node in the XML tree. This is not a hierarchical
  16.  * iterator. It does not walk the entire tree.
  17.  *
  18.  * @version 1.0, 7/11/97
  19.  * @see Element
  20.  * @see Name
  21.  */
  22. public class ElementEnumeration implements Enumeration
  23. {
  24.     /**
  25.      * Creates a new enumerator for enumerating over all of the children
  26.      * of the given root node.
  27.      * @param root The element whose children are going to be enumerated. 
  28.      */
  29.     public ElementEnumeration(Element root)
  30.     {
  31.         this.root = root;
  32.         this.tag = null;
  33.         this.next = null;
  34.         this.children = (root != null)  ? root.getElements() : null;
  35.         this.type =  -1;
  36.     }
  37.  
  38.     /**
  39.      * Creates a new enumerator for enumerating over the immediate children
  40.      * of the given root node that have matching tag names and/or types.
  41.      * @param root The element whose children are going to be enumerated. 
  42.      
  43.      * @param tag The name of the tag; this parameter can be null if the name is not important. 
  44.      
  45.      * @param type The element type. <code>Element.ELEMENT</code> is the most common. If the
  46.      * element type is not important, pass -1.
  47.      */
  48.     public ElementEnumeration(Element root, Name tag, int type)
  49.     {
  50.         this.root = root;
  51.         this.tag = tag;
  52.         this.next = null;
  53.         this.children = (root != null) ? root.getElements() : null;
  54.         this.type = type;
  55.     }
  56.  
  57.     /**
  58.      * Determines if there are any more matching elements.
  59.      * @return true if the next call to <code>nextElement</code> will return
  60.      * a non-null result.
  61.      */
  62.     public boolean hasMoreElements()
  63.     {
  64.         if (next == null) 
  65.         {
  66.             next = getNext();
  67.         }
  68.         return (next != null) ? true : false;
  69.     }
  70.  
  71.     /**
  72.      * Retrieves the next matching element.
  73.      * @return <code>Element</code> or null if there are no more matching elements.
  74.      */
  75.     public Object nextElement()
  76.     {
  77.         if (next != null) 
  78.         {
  79.             Element result = next;
  80.             next = null;
  81.             return result;
  82.         }
  83.         return getNext();
  84.     }
  85.  
  86.     /**
  87.      * Resets the iterator so that you can iterate through the elements again.
  88.       * @return No return value.
  89.      */
  90.     public void reset()
  91.     {
  92.         children = (root != null) ? root.getElements() : null;
  93.         next = null;
  94.     }
  95.  
  96.     /**
  97.      * Internal method for getting next element.
  98.      */
  99.     Element getNext()
  100.     {
  101.         if (children != null) 
  102.         {
  103.             while (children.hasMoreElements()) 
  104.             {
  105.                 Element e = (Element)children.nextElement();
  106.                 if (tag == null && type == -1) 
  107.                     return e;
  108.                 if (type != -1 && e.getType() != type)
  109.                     continue;
  110.                 if (tag != null && e.getTagName() != tag)
  111.                     continue;
  112.                 return e;
  113.             }
  114.         }
  115.         return null;
  116.     }
  117.  
  118.     Element root;
  119.     Enumeration children;
  120.     Name tag;
  121.     Element next;
  122.     int type;
  123. }
  124.