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 / util / Attributes.java < prev    next >
Encoding:
Java Source  |  1997-11-03  |  2.9 KB  |  126 lines

  1. /*
  2.  * @(#)Attributes.java 1.0 6/3/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7. package com.ms.xml.util;
  8.  
  9. import java.util.Vector;
  10. import java.util.Enumeration;
  11.  
  12. /**
  13.  * A class that encapsulates a list of attribute/value pairs.
  14.  *
  15.  * @version 1.0, 6/3/97
  16.  */
  17. public class Attributes extends ReadOnlyAttributes
  18. {
  19.  
  20.     /**
  21.      * Construct empty attributes collection.
  22.      */
  23.     public Attributes()
  24.     {
  25.     }
  26.  
  27.     /**
  28.      * Construct attributes collection with given number of empty slots.
  29.      * The collection will grow automatically
  30.      * if you add more than this number.
  31.      * @param elems the number of attributes to reserve initially.
  32.      */
  33.     public Attributes(int elems)
  34.     {
  35.         super(elems);
  36.     }
  37.  
  38.     /**
  39.      * Construct attributes collection by copying the passed attributes.
  40.      *
  41.      * @param   attrs   attributes to clone
  42.      */
  43.     public Attributes(ReadOnlyAttributes attrs)
  44.     {
  45.         Vector v = attrs.attributes;
  46.         if (v != null)
  47.         {
  48.             int l = v.size();
  49.             attributes = new Vector(l);
  50.             attributes.setSize(l);
  51.             for (int i = 0; i < l; i++)
  52.             {
  53.                 Attribute a = (Attribute)v.elementAt(i);
  54.                 attributes.setElementAt(new Attribute(a.name, a.getValue()), i);
  55.             }
  56.         }
  57.     }
  58.  
  59.     /**
  60.     * Construct attributes collection using the vector of attributes.
  61.     *
  62.     * @param   v   Attribute vector
  63.     */
  64.     public Attributes(Vector v)
  65.     {
  66.         super(v);
  67.     }
  68.  
  69.     /**
  70.      * Removed the named attribute from the collection.
  71.      */
  72.     public void remove(Name name)
  73.     {
  74.         Attribute a = lookup(name);
  75.         if (a != null) 
  76.         {
  77.             attributes.removeElement(a);
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Add a new attribute/value pair, or replace the value for
  83.      * attribute if it already exists.
  84.      * @return the previous value for the name attribute or null.
  85.      */
  86.     public Object put(Name name, Object value) 
  87.     {
  88.         Attribute a = lookup(name);
  89.         Object o = null;
  90.         if (a != null) 
  91.         {
  92.             o = a.getValue();
  93.             a.setValue(value);
  94.         } 
  95.         else 
  96.         {
  97.             attributes.addElement(new Attribute(name, value));
  98.         }
  99.         return o;
  100.     }
  101.  
  102.     /**
  103.      * Add a new attribute or replace the attribute
  104.      * if it already exists.
  105.      * @return the previous value for the name attribute or null.
  106.      */
  107.     public Object put(Attribute v) 
  108.     {
  109.         Object o = null;
  110.         Attribute a = lookup(v.getName());
  111.         if (a != null) 
  112.         {
  113.             o = a.getValue();
  114.             attributes.removeElement(a);
  115.         }
  116.         attributes.addElement(v);
  117.         return o;
  118.     }
  119.  
  120.     public void removeAll()
  121.     {
  122.         attributes.removeAllElements();
  123.     }
  124. }    
  125.  
  126.