1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 import com.sun.org.apache.xerces.internal.xni.Augmentations; 61 import com.sun.org.apache.xerces.internal.xni.QName; 62 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 63 64 83 public class XMLAttributesImpl 84 implements XMLAttributes { 85 86 90 91 protected static final int TABLE_SIZE = 101; 92 93 97 protected static final int SIZE_LIMIT = 20; 98 99 103 105 106 protected boolean fNamespaces = true; 107 108 110 115 protected int fLargeCount = 1; 116 117 118 protected int fLength; 119 120 121 protected Attribute[] fAttributes = new Attribute[4]; 122 123 127 protected Attribute[] fAttributeTableView; 128 129 135 protected int[] fAttributeTableViewChainState; 136 137 140 protected int fTableViewBuckets; 141 142 145 protected boolean fIsTableViewConsistent; 146 147 151 152 public XMLAttributesImpl() { 153 this(TABLE_SIZE); 154 } 155 156 159 public XMLAttributesImpl(int tableSize) { 160 fTableViewBuckets = tableSize; 161 for (int i = 0; i < fAttributes.length; i++) { 162 fAttributes[i] = new Attribute(); 163 } 164 } 166 170 178 public void setNamespaces(boolean namespaces) { 179 fNamespaces = namespaces; 180 } 182 186 214 public int addAttribute(QName name, String type, String value) { 215 216 int index; 217 if (fLength < SIZE_LIMIT) { 218 index = name.uri != null && !name.uri.equals("") 219 ? getIndexFast(name.uri, name.localpart) 220 : getIndexFast(name.rawname); 221 222 if (index == -1) { 223 index = fLength; 224 if (fLength++ == fAttributes.length) { 225 Attribute[] attributes = new Attribute[fAttributes.length + 4]; 226 System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length); 227 for (int i = fAttributes.length; i < attributes.length; i++) { 228 attributes[i] = new Attribute(); 229 } 230 fAttributes = attributes; 231 } 232 } 233 } 234 else if (name.uri == null || 235 name.uri.length() == 0 || 236 (index = getIndexFast(name.uri, name.localpart)) == -1) { 237 238 246 if (!fIsTableViewConsistent || fLength == SIZE_LIMIT) { 247 prepareAndPopulateTableView(); 248 fIsTableViewConsistent = true; 249 } 250 251 int bucket = getTableViewBucket(name.rawname); 252 253 if (fAttributeTableViewChainState[bucket] != fLargeCount) { 256 index = fLength; 257 if (fLength++ == fAttributes.length) { 258 Attribute[] attributes = new Attribute[fAttributes.length << 1]; 259 System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length); 260 for (int i = fAttributes.length; i < attributes.length; i++) { 261 attributes[i] = new Attribute(); 262 } 263 fAttributes = attributes; 264 } 265 266 fAttributeTableViewChainState[bucket] = fLargeCount; 268 fAttributes[index].next = null; 269 fAttributeTableView[bucket] = fAttributes[index]; 270 } 271 else { 274 Attribute found = fAttributeTableView[bucket]; 276 while (found != null) { 277 if (found.name.rawname == name.rawname) { 278 break; 279 } 280 found = found.next; 281 } 282 if (found == null) { 284 index = fLength; 285 if (fLength++ == fAttributes.length) { 286 Attribute[] attributes = new Attribute[fAttributes.length << 1]; 287 System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length); 288 for (int i = fAttributes.length; i < attributes.length; i++) { 289 attributes[i] = new Attribute(); 290 } 291 fAttributes = attributes; 292 } 293 294 fAttributes[index].next = fAttributeTableView[bucket]; 296 fAttributeTableView[bucket] = fAttributes[index]; 297 } 298 else { 300 index = getIndexFast(name.rawname); 301 } 302 } 303 } 304 305 Attribute attribute = fAttributes[index]; 307 attribute.name.setValues(name); 308 attribute.type = type; 309 attribute.value = value; 310 attribute.nonNormalizedValue = value; 311 attribute.specified = false; 312 313 return index; 315 316 } 318 322 public void removeAllAttributes() { 323 fLength = 0; 324 } 326 334 public void removeAttributeAt(int attrIndex) { 335 fIsTableViewConsistent = false; 336 if (attrIndex < fLength - 1) { 337 Attribute removedAttr = fAttributes[attrIndex]; 338 System.arraycopy(fAttributes, attrIndex + 1, 339 fAttributes, attrIndex, fLength - attrIndex - 1); 340 fAttributes[fLength-1] = removedAttr; 343 } 344 fLength--; 345 } 347 353 public void setName(int attrIndex, QName attrName) { 354 fAttributes[attrIndex].name.setValues(attrName); 355 } 357 364 public void getName(int attrIndex, QName attrName) { 365 attrName.setValues(fAttributes[attrIndex].name); 366 } 368 381 public void setType(int attrIndex, String attrType) { 382 fAttributes[attrIndex].type = attrType; 383 } 385 394 public void setValue(int attrIndex, String attrValue) { 395 Attribute attribute = fAttributes[attrIndex]; 396 attribute.value = attrValue; 397 attribute.nonNormalizedValue = attrValue; 398 } 400 407 public void setNonNormalizedValue(int attrIndex, String attrValue) { 408 if (attrValue == null) { 409 attrValue = fAttributes[attrIndex].value; 410 } 411 fAttributes[attrIndex].nonNormalizedValue = attrValue; 412 } 414 421 public String getNonNormalizedValue(int attrIndex) { 422 String value = fAttributes[attrIndex].nonNormalizedValue; 423 return value; 424 } 426 434 public void setSpecified(int attrIndex, boolean specified) { 435 fAttributes[attrIndex].specified = specified; 436 } 438 443 public boolean isSpecified(int attrIndex) { 444 return fAttributes[attrIndex].specified; 445 } 447 451 459 public int getLength() { 460 return fLength; 461 } 463 483 public String getType(int index) { 484 if (index < 0 || index >= fLength) { 485 return null; 486 } 487 return getReportableType(fAttributes[index].type); 488 } 490 501 public String getType(String qname) { 502 int index = getIndex(qname); 503 return index != -1 ? getReportableType(fAttributes[index].type) : null; 504 } 506 519 public String getValue(int index) { 520 if (index < 0 || index >= fLength) { 521 return null; 522 } 523 return fAttributes[index].value; 524 } 526 537 public String getValue(String qname) { 538 int index = getIndex(qname); 539 return index != -1 ? fAttributes[index].value : null; 540 } 542 546 562 public String getName(int index) { 563 if (index < 0 || index >= fLength) { 564 return null; 565 } 566 return fAttributes[index].name.rawname; 567 } 569 573 580 public int getIndex(String qName) { 581 for (int i = 0; i < fLength; i++) { 582 Attribute attribute = fAttributes[i]; 583 if (attribute.name.rawname != null && 584 attribute.name.rawname.equals(qName)) { 585 return i; 586 } 587 } 588 return -1; 589 } 591 600 public int getIndex(String uri, String localPart) { 601 for (int i = 0; i < fLength; i++) { 602 Attribute attribute = fAttributes[i]; 603 if (attribute.name.localpart != null && 604 attribute.name.localpart.equals(localPart) && 605 ((uri==attribute.name.uri) || 606 (uri!=null && attribute.name.uri!=null && attribute.name.uri.equals(uri)))) 607 { 608 return i; 609 } 610 } 611 return -1; 612 } 614 623 public String getLocalName(int index) { 624 if (!fNamespaces) { 625 return ""; 626 } 627 if (index < 0 || index >= fLength) { 628 return null; 629 } 630 return fAttributes[index].name.localpart; 631 } 633 642 public String getQName(int index) { 643 if (index < 0 || index >= fLength) { 644 return null; 645 } 646 String rawname = fAttributes[index].name.rawname; 647 return rawname != null ? rawname : ""; 648 } 650 663 public String getType(String uri, String localName) { 664 if (!fNamespaces) { 665 return null; 666 } 667 int index = getIndex(uri, localName); 668 return index != -1 ? getType(index) : null; 669 } 682 public int getIndexFast(String qName) { 683 for (int i = 0; i < fLength; ++i) { 684 Attribute attribute = fAttributes[i]; 685 if (attribute.name.rawname == qName) { 686 return i; 687 } 688 } 689 return -1; 690 } 692 719 public void addAttributeNS(QName name, String type, String value) { 720 int index = fLength; 721 if (fLength++ == fAttributes.length) { 722 Attribute[] attributes; 723 if (fLength < SIZE_LIMIT) { 724 attributes = new Attribute[fAttributes.length + 4]; 725 } 726 else { 727 attributes = new Attribute[fAttributes.length << 1]; 728 } 729 System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length); 730 for (int i = fAttributes.length; i < attributes.length; i++) { 731 attributes[i] = new Attribute(); 732 } 733 fAttributes = attributes; 734 } 735 736 Attribute attribute = fAttributes[index]; 738 attribute.name.setValues(name); 739 attribute.type = type; 740 attribute.value = value; 741 attribute.nonNormalizedValue = value; 742 attribute.specified = false; 743 744 attribute.augs.removeAllItems(); 746 } 747 748 760 public QName checkDuplicatesNS() { 761 if (fLength <= SIZE_LIMIT) { 763 for (int i = 0; i < fLength - 1; ++i) { 764 Attribute att1 = fAttributes[i]; 765 for (int j = i + 1; j < fLength; ++j) { 766 Attribute att2 = fAttributes[j]; 767 if (att1.name.localpart == att2.name.localpart && 768 att1.name.uri == att2.name.uri) { 769 return att2.name; 770 } 771 } 772 } 773 } 774 else { 776 fIsTableViewConsistent = false; 779 780 prepareTableView(); 781 782 Attribute attr; 783 int bucket; 784 785 for (int i = fLength - 1; i >= 0; --i) { 786 attr = fAttributes[i]; 787 bucket = getTableViewBucket(attr.name.localpart, attr.name.uri); 788 789 if (fAttributeTableViewChainState[bucket] != fLargeCount) { 792 fAttributeTableViewChainState[bucket] = fLargeCount; 793 attr.next = null; 794 fAttributeTableView[bucket] = attr; 795 } 796 else { 799 Attribute found = fAttributeTableView[bucket]; 801 while (found != null) { 802 if (found.name.localpart == attr.name.localpart && 803 found.name.uri == attr.name.uri) { 804 return attr.name; 805 } 806 found = found.next; 807 } 808 809 attr.next = fAttributeTableView[bucket]; 811 fAttributeTableView[bucket] = attr; 812 } 813 } 814 } 815 return null; 816 } 817 818 832 public int getIndexFast(String uri, String localPart) { 833 for (int i = 0; i < fLength; ++i) { 834 Attribute attribute = fAttributes[i]; 835 if (attribute.name.localpart == localPart && 836 attribute.name.uri == uri) { 837 return i; 838 } 839 } 840 return -1; 841 } 843 849 private String getReportableType(String type) { 850 851 if (type.charAt(0) == '(') { 852 return "NMTOKEN"; 853 } 854 return type; 855 } 856 857 865 protected int getTableViewBucket(String qname) { 866 return (qname.hashCode() & 0x7FFFFFFF) % fTableViewBuckets; 867 } 868 869 878 protected int getTableViewBucket(String localpart, String uri) { 879 if (uri == null) { 880 return (localpart.hashCode() & 0x7FFFFFFF) % fTableViewBuckets; 881 } 882 else { 883 return ((localpart.hashCode() + uri.hashCode()) 884 & 0x7FFFFFFF) % fTableViewBuckets; 885 } 886 } 887 888 891 protected void cleanTableView() { 892 if (++fLargeCount < 0) { 893 if (fAttributeTableViewChainState != null) { 895 for (int i = fTableViewBuckets - 1; i >= 0; --i) { 896 fAttributeTableViewChainState[i] = 0; 897 } 898 } 899 fLargeCount = 1; 900 } 901 } 902 903 906 protected void prepareTableView() { 907 if (fAttributeTableView == null) { 908 fAttributeTableView = new Attribute[fTableViewBuckets]; 909 fAttributeTableViewChainState = new int[fTableViewBuckets]; 910 } 911 else { 912 cleanTableView(); 913 } 914 } 915 916 921 protected void prepareAndPopulateTableView() { 922 prepareTableView(); 923 Attribute attr; 925 int bucket; 926 for (int i = 0; i < fLength; ++i) { 927 attr = fAttributes[i]; 928 bucket = getTableViewBucket(attr.name.rawname); 929 if (fAttributeTableViewChainState[bucket] != fLargeCount) { 930 fAttributeTableViewChainState[bucket] = fLargeCount; 931 attr.next = null; 932 fAttributeTableView[bucket] = attr; 933 } 934 else { 935 attr.next = fAttributeTableView[bucket]; 937 fAttributeTableView[bucket] = attr; 938 } 939 } 940 } 941 942 943 948 public String getPrefix(int index) { 949 if (index < 0 || index >= fLength) { 950 return null; 951 } 952 String prefix = fAttributes[index].name.prefix; 953 return prefix != null ? prefix : ""; 955 } 957 964 public String getURI(int index) { 965 if (index < 0 || index >= fLength) { 966 return null; 967 } 968 String uri = fAttributes[index].name.uri; 969 return uri; 970 } 972 983 public String getValue(String uri, String localName) { 984 int index = getIndex(uri, localName); 985 return index != -1 ? getValue(index) : null; 986 } 988 989 996 public Augmentations getAugmentations (String uri, String localName) { 997 int index = getIndex(uri, localName); 998 return index != -1 ? fAttributes[index].augs : null; 999 } 1000 1001 1010 public Augmentations getAugmentations(String qName){ 1011 int index = getIndex(qName); 1012 return index != -1 ? fAttributes[index].augs : null; 1013 } 1014 1015 1016 1017 1023 public Augmentations getAugmentations (int attributeIndex){ 1024 if (attributeIndex < 0 || attributeIndex >= fLength) { 1025 return null; 1026 } 1027 return fAttributes[attributeIndex].augs; 1028 } 1029 1030 1036 public void setAugmentations(int attrIndex, Augmentations augs) { 1037 fAttributes[attrIndex].augs = augs; 1038 } 1039 1040 1046 public void setURI(int attrIndex, String uri) { 1047 fAttributes[attrIndex].name.uri = uri; 1048 } 1050 1054 1059 static class Attribute { 1060 1061 1065 1067 1068 public QName name = new QName(); 1069 1070 1071 public String type; 1072 1073 1074 public String value; 1075 1076 1077 public String nonNormalizedValue; 1078 1079 1080 public boolean specified; 1081 1082 1087 public Augmentations augs = new AugmentationsImpl(); 1088 1089 1091 1092 public Attribute next; 1093 1094 } 1096} | Popular Tags |