1 16 19 package com.sun.org.apache.xml.internal.dtm.ref; 20 21 import com.sun.org.apache.xml.internal.dtm.DTM; 22 23 35 public class ExpandedNameTable 36 { 37 38 39 private ExtendedType[] m_extendedTypes; 40 41 42 private static int m_initialSize = 128; 43 44 45 private int m_nextType; 48 49 public static final int ELEMENT = ((int)DTM.ELEMENT_NODE) ; 51 public static final int ATTRIBUTE = ((int)DTM.ATTRIBUTE_NODE) ; 52 public static final int TEXT = ((int)DTM.TEXT_NODE) ; 53 public static final int CDATA_SECTION = ((int)DTM.CDATA_SECTION_NODE) ; 54 public static final int ENTITY_REFERENCE = ((int)DTM.ENTITY_REFERENCE_NODE) ; 55 public static final int ENTITY = ((int)DTM.ENTITY_NODE) ; 56 public static final int PROCESSING_INSTRUCTION = ((int)DTM.PROCESSING_INSTRUCTION_NODE) ; 57 public static final int COMMENT = ((int)DTM.COMMENT_NODE) ; 58 public static final int DOCUMENT = ((int)DTM.DOCUMENT_NODE) ; 59 public static final int DOCUMENT_TYPE = ((int)DTM.DOCUMENT_TYPE_NODE) ; 60 public static final int DOCUMENT_FRAGMENT =((int)DTM.DOCUMENT_FRAGMENT_NODE) ; 61 public static final int NOTATION = ((int)DTM.NOTATION_NODE) ; 62 public static final int NAMESPACE = ((int)DTM.NAMESPACE_NODE) ; 63 64 66 ExtendedType hashET = new ExtendedType(-1, "", ""); 67 68 69 private static ExtendedType[] m_defaultExtendedTypes; 70 71 75 private static float m_loadFactor = 0.75f; 76 77 81 private static int m_initialCapacity = 203; 82 83 87 private int m_capacity; 88 89 94 private int m_threshold; 95 96 100 private HashEntry[] m_table; 101 102 105 static { 106 m_defaultExtendedTypes = new ExtendedType[DTM.NTYPES]; 107 108 for (int i = 0; i < DTM.NTYPES; i++) 109 { 110 m_defaultExtendedTypes[i] = new ExtendedType(i, "", ""); 111 } 112 } 113 114 117 public ExpandedNameTable() 118 { 119 m_capacity = m_initialCapacity; 120 m_threshold = (int)(m_capacity * m_loadFactor); 121 m_table = new HashEntry[m_capacity]; 122 123 initExtendedTypes(); 124 } 125 126 127 131 private void initExtendedTypes() 132 { 133 m_extendedTypes = new ExtendedType[m_initialSize]; 134 for (int i = 0; i < DTM.NTYPES; i++) { 135 m_extendedTypes[i] = m_defaultExtendedTypes[i]; 136 m_table[i] = new HashEntry(m_defaultExtendedTypes[i], i, i, null); 137 } 138 139 m_nextType = DTM.NTYPES; 140 } 141 142 154 public int getExpandedTypeID(String namespace, String localName, int type) 155 { 156 return getExpandedTypeID(namespace, localName, type, false); 157 } 158 159 177 public int getExpandedTypeID(String namespace, String localName, int type, boolean searchOnly) 178 { 179 if (null == namespace) 180 namespace = ""; 181 if (null == localName) 182 localName = ""; 183 184 int hash = type + namespace.hashCode() + localName.hashCode(); 186 187 hashET.redefine(type, namespace, localName, hash); 189 190 int index = hash % m_capacity; 192 if (index < 0) 193 index = -index; 194 195 for (HashEntry e = m_table[index]; e != null; e = e.next) 198 { 199 if (e.hash == hash && e.key.equals(hashET)) 200 return e.value; 201 } 202 203 if (searchOnly) 204 { 205 return DTM.NULL; 206 } 207 208 if (m_nextType > m_threshold) { 210 rehash(); 211 index = hash % m_capacity; 212 if (index < 0) 213 index = -index; 214 } 215 216 ExtendedType newET = new ExtendedType(type, namespace, localName, hash); 218 219 if (m_extendedTypes.length == m_nextType) { 221 ExtendedType[] newArray = new ExtendedType[m_extendedTypes.length * 2]; 222 System.arraycopy(m_extendedTypes, 0, newArray, 0, 223 m_extendedTypes.length); 224 m_extendedTypes = newArray; 225 } 226 227 m_extendedTypes[m_nextType] = newET; 228 229 HashEntry entry = new HashEntry(newET, m_nextType, hash, m_table[index]); 232 m_table[index] = entry; 233 234 return m_nextType++; 235 } 236 237 243 private void rehash() 244 { 245 int oldCapacity = m_capacity; 246 HashEntry[] oldTable = m_table; 247 248 int newCapacity = 2 * oldCapacity + 1; 249 m_capacity = newCapacity; 250 m_threshold = (int)(newCapacity * m_loadFactor); 251 252 m_table = new HashEntry[newCapacity]; 253 for (int i = oldCapacity-1; i >=0 ; i--) 254 { 255 for (HashEntry old = oldTable[i]; old != null; ) 256 { 257 HashEntry e = old; 258 old = old.next; 259 260 int newIndex = e.hash % newCapacity; 261 if (newIndex < 0) 262 newIndex = -newIndex; 263 264 e.next = m_table[newIndex]; 265 m_table[newIndex] = e; 266 } 267 } 268 } 269 270 279 public int getExpandedTypeID(int type) 280 { 281 return type; 282 } 283 284 290 public String getLocalName(int ExpandedNameID) 291 { 292 return m_extendedTypes[ExpandedNameID].getLocalName(); 293 } 294 295 301 public final int getLocalNameID(int ExpandedNameID) 302 { 303 if (m_extendedTypes[ExpandedNameID].getLocalName().equals("")) 305 return 0; 306 else 307 return ExpandedNameID; 308 } 309 310 311 318 public String getNamespace(int ExpandedNameID) 319 { 320 String namespace = m_extendedTypes[ExpandedNameID].getNamespace(); 321 return (namespace.equals("") ? null : namespace); 322 } 323 324 330 public final int getNamespaceID(int ExpandedNameID) 331 { 332 if (m_extendedTypes[ExpandedNameID].getNamespace().equals("")) 334 return 0; 335 else 336 return ExpandedNameID; 337 } 338 339 345 public final short getType(int ExpandedNameID) 346 { 347 return (short)m_extendedTypes[ExpandedNameID].getNodeType(); 349 } 350 351 356 public int getSize() 357 { 358 return m_nextType; 359 } 360 361 366 public ExtendedType[] getExtendedTypes() 367 { 368 return m_extendedTypes; 369 } 370 371 376 private static final class HashEntry 377 { 378 ExtendedType key; 379 int value; 380 int hash; 381 HashEntry next; 382 383 protected HashEntry(ExtendedType key, int value, int hash, HashEntry next) 384 { 385 this.key = key; 386 this.value = value; 387 this.hash = hash; 388 this.next = next; 389 } 390 } 391 392 } 393 | Popular Tags |