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 / ElementCollection.java < prev    next >
Encoding:
Java Source  |  1997-10-13  |  4.6 KB  |  151 lines

  1. /*
  2.  * @(#)ElementCollection.java 1.0 8/7/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 com.ms.xml.om.ElementCollection;
  12.  
  13. /**
  14.  * This class provides a collection interface to elements
  15.  * similar to the element collections found in the Internet Explorer 4.0
  16.  * Dynamic HTML object model.
  17.  *
  18.  * @version 1.0, 8/7/97
  19.  */
  20. public class ElementCollection
  21. {
  22.     /**
  23.      * Creates new collection object for given element.
  24.      */
  25.     public ElementCollection(Element root)
  26.     {
  27.         this.root = root;
  28.         this.items = new ElementEnumeration(root,null,Element.ELEMENT);
  29.         currentindex = 0; // not initialized
  30.         current = (Element)items.nextElement();
  31.         length = -1;
  32.     }
  33.  
  34.     /**
  35.      * Creates a new collection for iterating over the immediate children
  36.      * of the given root node that have matching tag names and/or
  37.      * element types.   
  38.      * @param root The root to form the collection around.
  39.      * @param tag The name of the tag; this parameter can be null if the name is not important. 
  40.      * @param type The element type. <code>Element.ELEMENT</code> is the most
  41.       * common. If
  42.      * the element type is not important, pass -1.
  43.      */
  44.     public ElementCollection(Element root, Name tag, int type)
  45.     {
  46.         this.root = root;
  47.         this.items = new ElementEnumeration(root, tag, type);
  48.         length = -1; // not initialized
  49.         currentindex = 0; // not initialized
  50.         current = (Element)items.nextElement();
  51.     }
  52.  
  53.     /**
  54.      * Retrieves the number of items in the collection.
  55.      * @return the item count.
  56.      */
  57.     public int getLength()
  58.     {
  59.         if (length == -1) 
  60.         {
  61.             items.reset();
  62.             length = 0;
  63.             while (items.hasMoreElements()) 
  64.             {
  65.                 items.nextElement();
  66.                 length++;
  67.             }
  68.             items.reset();
  69.             currentindex = 0; // not initialized
  70.             current = (Element)items.nextElement();
  71.         }
  72.         return length;
  73.     }
  74.  
  75.     /**
  76.      * Retrieves a named item or a collection of matching items.
  77.      * @param name The name of the item or collection of matching items.
  78.      * @return the requested item. Possible types of objects 
  79.      * returned are <code>Element</code>, <code>ElementCollection</code>, 
  80.      * or null.
  81.      */
  82.     public Object item(String name)
  83.     {
  84.         try {
  85.             int i = Integer.parseInt(name);
  86.             return getChild(i);
  87.         } catch (Exception e) {
  88.         }
  89.  
  90.         ElementCollection col = new ElementCollection(root,Name.create(name),Element.ELEMENT);
  91.         if (col.getLength() == 1) 
  92.         {
  93.             // only one match, so return it.
  94.             return col.getChild(0);
  95.         } 
  96.         // otherwise return the new collection.
  97.         return col;
  98.     }
  99.  
  100.     /**
  101.      * Retrieves a specified item from the collection by index.
  102.      * @param index  The index of the item in the element collection.
  103.      * @return the requested child node; returns null if not found.
  104.      */
  105.     public Element getChild(int index)
  106.     {
  107.         // This is an optimization to help sequential
  108.         // calls to item with sequentially increasing
  109.         // indexes be linear in performance.
  110.         if (currentindex == index) {
  111.             return current;
  112.         } else if (currentindex > index) {
  113.             // have to reset and start over.
  114.             currentindex = 0;
  115.             items.reset();
  116.         } 
  117.         // must not use else here since we change currentindex above.
  118.         if (currentindex < index) { 
  119.             while (currentindex < index && items.hasMoreElements())
  120.             {
  121.                 current = (Element)items.nextElement();
  122.                 currentindex++;
  123.             }
  124.         }
  125.         if (currentindex != index) {
  126.             // Still didn't find it.  Mustn't be there.
  127.             return null;
  128.         }
  129.         return current;
  130.     }
  131.  
  132.     /**
  133.      * Retrieves a specified item from the collection of matching items.
  134.      * @param name  The name of the matching items.
  135.      * @param index  The index of the specific matching item to return.
  136.      * @return the requested item if it is found; returns null if it is not 
  137.       * found.
  138.      */
  139.     public Element item(String name, int index)
  140.     {
  141.         ElementCollection col = new ElementCollection(root,Name.create(name),Element.ELEMENT);
  142.         return col.getChild(index);
  143.     }
  144.  
  145.     Element root;
  146.     ElementEnumeration items;
  147.     int length;
  148.     Element current;
  149.     int currentindex;
  150. }
  151.