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 / Name.java < prev    next >
Encoding:
Java Source  |  1997-11-05  |  4.2 KB  |  175 lines

  1. /*
  2.  * @(#)Name.java 1.0 6/3/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. package com.ms.xml.util;
  9.  
  10. import java.io.*;
  11. import java.util.Hashtable;
  12.  
  13. /**
  14.  * This is a general purpose name object to allow efficient
  15.  * sharing of duplicate names in the system.  It does this by
  16.  * creating a global HashTable of all names that have been 
  17.  * constructed.  Names are different from Atoms in that they can
  18.  * be qualified by a separate namespace string - so in effect
  19.  * that are compound atoms.
  20.  *
  21.  * @version 1.0, 6/3/97
  22.  */
  23. public class Name
  24. {
  25.  
  26.     /**
  27.      * Hash table for shared qualified names.
  28.      */
  29.     static Hashtable names = new Hashtable(500);
  30.  
  31.         /**
  32.      * Hash table for shared simple names.
  33.      */
  34.     static StringHashtable snames = new StringHashtable(500);
  35.  
  36.  
  37.     /**
  38.      * A name is a compound object containing two atoms.
  39.      */
  40.     Atom nameSpace;
  41.     String name;
  42.     int  hash;
  43.  
  44.     /**
  45.      * The constructor is private because all names should
  46.      * be constructed using the static Name.create() methods
  47.      */
  48.     Name(String name, Atom nameSpace, int hash)
  49.     {    
  50.         this.name = name;
  51.         this.nameSpace = nameSpace;
  52.         this.hash = hash;
  53.     }   
  54.  
  55.     public String getName()
  56.     {
  57.         return name;
  58.     }
  59.  
  60.     public Atom getNameSpace()
  61.     {
  62.         return nameSpace;
  63.     }
  64.  
  65.     public boolean equals(Object that) 
  66.     {
  67.         if (this == that) 
  68.         {
  69.             return true;
  70.         }
  71.         if (that == null || getClass() != that.getClass()) 
  72.         {
  73.             return false;
  74.         }
  75.  
  76.         Name t = (Name)that;
  77.         if (nameSpace != null) {
  78.             if (!nameSpace.equals(t.nameSpace))
  79.                 return false;
  80.         }
  81.         else if (t.nameSpace != null)
  82.             return false;
  83.         return this.name.equals(t.name);        
  84.     }
  85.     
  86.     /**
  87.      * Create an unqualified Name.
  88.      */    
  89.     public static Name create(String name)
  90.     {
  91.         if (name == null) return null;
  92.         Object o = snames.get(name);
  93.         if (o == null)
  94.         {
  95.             // add new name to simple names hash table.
  96.             int h = name.hashCode();
  97.             Name result = new Name(name, null, h);
  98.             snames.put(name, result);
  99.             return result;
  100.         }
  101.         // return existing name and drop 'result' on the floor.
  102.         return (Name)o;
  103.     }
  104.  
  105.     public static Name create(char val[], int offset, int len)
  106.     {
  107.         Object o = snames.get(val, offset, len);
  108.         if (o == null)
  109.         {
  110.             // add new name to simple names hash table.
  111.             String name = new String(val, offset, len);
  112.             int h = name.hashCode();
  113.             Name result = new Name(name, null, h);
  114.             snames.put(name, result);
  115.             return result;
  116.         }
  117.         // return existing name and drop 'result' on the floor.
  118.         return (Name)o;
  119.     }
  120.  
  121.     /**
  122.      * Create a Name object for the given name and namespace.
  123.      * The strings are case sensitive.
  124.      */    
  125.     public static Name create(String name, String ns)
  126.     {
  127.         if (name == null) return null;
  128.         if (ns == null) return create(name);        
  129.         return create(name,Atom.create(ns));
  130.     }
  131.  
  132.     /**
  133.      * Create a Name object for the given name and namespace
  134.      * where the name and Namespace are already Atoms.
  135.      * The strings are case sensitive.
  136.      */    
  137.     public static Name create(String name, Atom nameSpace)
  138.     {
  139.         if (name == null) return null;
  140.         if (nameSpace == null) return create(name);
  141.         
  142.         int h = name.hashCode() + nameSpace.hashCode();
  143.         Name result = new Name(name, nameSpace, h);
  144.         Object o = names.get(result);
  145.         if (o == null)
  146.         {
  147.             // add new name to hash table.
  148.             names.put(result, result);
  149.             return result;
  150.         }
  151.         else
  152.         {
  153.             // return existing name and drop 'result' on the floor.
  154.             return (Name)o;
  155.         }
  156.     }
  157.  
  158.  
  159.     public String toString()
  160.     {
  161.         if (nameSpace != null)
  162.             return nameSpace.toString() + ":" + name;
  163.         else return name;
  164.     }
  165.  
  166.     /**
  167.      * return the hash code for this name object
  168.      */
  169.     public int hashCode()
  170.     {
  171.         return hash;
  172.     }
  173.  
  174. }
  175.