1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.*; 3 import org.xml.sax.Attributes ; 4 5 6 14 15 public final class AttributeCollection implements Attributes 16 { 17 18 22 private NamePool namePool; 23 private Object [] list = null; 24 private int used = 0; 25 26 private static int RECSIZE = 3; 27 private static int NAMECODE = 0; 28 private static int TYPE = 1; 29 private static int VALUE = 2; 30 31 34 35 public AttributeCollection (NamePool pool) { 36 namePool = pool; 37 list = null; 38 used = 0; 39 } 40 41 44 45 public AttributeCollection (NamePool pool, int n) { 46 namePool = pool; 47 list = new Object [n*RECSIZE]; 48 used = 0; 49 } 50 51 54 55 public AttributeCollection (AttributeCollection atts) { 56 this.namePool = atts.namePool; 57 this.list = new Object [atts.used]; 58 if (atts.used > 0 ) { 59 System.arraycopy(atts.list, 0, this.list, 0, atts.used); 60 } 61 this.used = atts.used; 62 } 63 64 67 68 public AttributeCollection (NamePool pool, Attributes atts) { 69 namePool = pool; 70 int len = atts.getLength(); 71 used = len*RECSIZE; 72 this.list = new Object [used]; 73 74 for (int a=0; a<len; a++) { 75 int j = a*RECSIZE; 76 String qname = atts.getQName(a); 77 String prefix = Name.getPrefix(qname); 78 String uri = atts.getURI(a); 79 String localName = atts.getLocalName(a); 80 int nameCode = namePool.allocate(prefix, uri, localName); 81 list[j+NAMECODE] = new Integer (nameCode); 82 list[j+TYPE] = atts.getType(a); 83 list[j+VALUE] = atts.getValue(a); 84 } 85 } 86 87 94 95 public void addAttribute (int nameCode, String type, String value) 96 { 97 if (list==null) { 98 list = new Object [5*RECSIZE]; 99 used = 0; 100 } 101 if (list.length == used) { 102 int newsize = (used==0 ? 5*RECSIZE : used*2); 103 Object [] newlist = new Object [newsize]; 104 System.arraycopy(list, 0, newlist, 0, used); 105 list = newlist; 106 } 107 list[used++] = new Integer (nameCode); 108 list[used++] = type; 109 list[used++] = value; 110 } 111 112 121 122 public void addAttribute (String prefix, String uri, String localName, String type, String value) 123 { 124 addAttribute(namePool.allocate(prefix, uri, localName), type, value); 125 } 126 127 133 134 public void setAttribute(String prefix, String uri, String localName, String type, String value) 135 { 136 int nameCode = namePool.allocate(prefix, uri, localName); 137 int offset = findByFingerprint(nameCode&0xfffff); 138 if (offset<0) { 139 addAttribute(prefix, uri, localName, type, value); 140 } else { 141 list[offset + NAMECODE] = new Integer (nameCode); 142 list[offset + TYPE] = type; 143 list[offset + VALUE] = value; 144 } 145 } 146 147 153 154 public void setAttribute(int nameCode, String type, String value) 155 { 156 int offset = findByFingerprint(nameCode&0xfffff); 157 if (offset<0) { 158 addAttribute(nameCode, type, value); 159 } else { 160 list[offset + NAMECODE] = new Integer (nameCode); 161 list[offset + TYPE] = type; 162 list[offset + VALUE] = value; 163 } 164 } 165 166 169 170 public void clear () 171 { 172 used = 0; 173 } 174 175 178 179 public void compact() { 180 if (used==0) { 181 list = null; 182 } else if (list.length > used) { 183 Object [] newlist = new Object [used]; 184 System.arraycopy(list, 0, newlist, 0, used); 185 list = newlist; 186 } 187 } 188 189 190 194 195 199 200 public int getLength () 201 { 202 return (list==null ? 0 : used / RECSIZE ); 203 } 204 205 212 213 public int getNameCode (int index) 214 { 215 int offset = index*RECSIZE; 216 if (list==null) return -1; 217 if (offset >= used) return -1; 218 219 return ((Integer )list[offset+NAMECODE]).intValue(); 220 } 221 222 229 230 public String getQName (int index) 231 { 232 int offset = index*RECSIZE; 233 if (list==null) return null; 234 if (offset >= used) return null; 235 return namePool.getDisplayName(getNameCode(index)); 236 } 237 238 245 246 public String getLocalName (int index) 247 { 248 if (list==null) return null; 249 if (index*RECSIZE >= used) return null; 250 return namePool.getLocalName(getNameCode(index)); 251 } 252 253 260 261 public String getURI (int index) 262 { 263 if (list==null) return null; 264 if (index*RECSIZE >= used) return null; 265 return namePool.getURI(getNameCode(index)); 266 } 267 268 269 270 278 279 public String getType (int index) 280 { 281 int offset = index*RECSIZE; 282 if (list==null) return null; 283 if (offset >= used) return null; 284 return (String )list[offset+TYPE]; 285 } 286 287 294 295 public String getType (String uri, String localname) 296 { 297 int offset = findByName(uri, localname); 298 return ( offset<0 ? null : (String )list[offset+TYPE]); 299 } 300 301 308 309 public String getValue (int index) { 310 int offset = index*RECSIZE; 311 if (list==null) return null; 312 if (offset >= used) return null; 313 return (String )list[offset+VALUE]; 314 } 315 316 323 324 public String getValue (String uri, String localname) 325 { 326 int offset = findByName(uri, localname); 327 return ( offset<0 ? null : (String )list[offset+VALUE]); 328 } 329 330 333 334 public String getValueByFingerprint(int fingerprint) { 335 int offset = findByFingerprint(fingerprint); 336 return ( offset<0 ? null : (String )list[offset+VALUE]); 337 } 338 339 345 346 public int getIndex (String name) 347 { 348 int offset = findByDisplayName(name); 349 return ( offset<0 ? -1 : offset / RECSIZE); 350 } 351 352 359 360 public int getIndex (String uri, String localname) 361 { 362 int offset = findByName(uri, localname); 363 return ( offset<0 ? -1 : offset / RECSIZE); 364 } 365 366 369 370 public int getIndexByFingerprint(int fingerprint) { 371 int offset = findByFingerprint(fingerprint); 372 return ( offset<0 ? -1 : offset / RECSIZE); 373 } 374 375 383 384 public String getType (String name) 385 { 386 int offset = findByDisplayName(name); 387 return ( offset<0 ? null : (String )list[offset+TYPE]); 388 } 389 390 391 396 397 public String getValue (String name) 398 { 399 int offset = findByDisplayName(name); 400 return ( offset<0 ? null : (String )list[offset+VALUE]); 401 } 402 403 407 411 412 private int findByName(String uri, String localName) { 413 if (namePool==null) return -1; int f = namePool.getFingerprint(uri, localName); 415 if (f==-1) return -1; 416 return findByFingerprint(f); 417 } 418 419 423 424 private int findByFingerprint(int fingerprint) { 425 if (list==null) return -1; 426 for (int i=0; i<used; i+=RECSIZE) { 427 if (fingerprint==(((Integer )list[i+NAMECODE]).intValue()&0xfffff)) { 428 return i; 429 } 430 } 431 return -1; 432 } 433 434 438 439 private int findByDisplayName(String qname) { 440 if (list==null) return -1; 441 String prefix = Name.getPrefix(qname); 442 if (prefix.equals("")) { 443 return findByName("", qname); 444 } else { 445 String localName = Name.getLocalName(qname); 446 for (int i=0; i<getLength(); i++) { 447 String lname=namePool.getLocalName(getNameCode(i)); 448 String ppref=namePool.getPrefix(getNameCode(i)); 449 if (localName.equals(lname) && prefix.equals(ppref)) { 450 return i; 451 } 452 } 453 return -1; 454 } 455 } 456 457 } 458 459 | Popular Tags |