1 28 29 package com.caucho.xml.stream; 30 31 import javax.xml.namespace.QName ; 32 33 36 public class StaxIntern { 37 private static final int SIZE = 203; 38 39 private final Entry []_entries = new Entry[SIZE]; 40 41 private final NamespaceReaderContext _namespaceContext; 42 43 StaxIntern(NamespaceReaderContext namespaceContext) 44 { 45 _namespaceContext = namespaceContext; 46 } 47 48 Entry add(char []buffer, int offset, int length, int colon, 49 boolean isAttribute) 50 { 51 int hash = 0; 52 53 for (int i = length - 1; i >= 0; i--) { 54 hash = 37 * hash + buffer[offset + i]; 55 } 56 57 int bucket = (hash & 0x7fffffff) % SIZE; 58 59 Entry entry; 60 61 for (entry = _entries[bucket]; 62 entry != null; 63 entry = entry._next) { 64 if (entry.match(buffer, offset, length, isAttribute)) 65 return entry; 66 } 67 68 entry = new Entry(_entries[bucket], 69 buffer, offset, length, 70 colon, 71 isAttribute); 72 _entries[bucket] = entry; 73 74 return entry; 75 } 76 77 final class Entry { 78 final Entry _next; 79 80 final char []_buf; 81 final boolean _isAttribute; 82 83 final String _prefix; 84 final String _localName; 85 86 NamespaceBinding _namespace; 87 int _version; 88 QName _qName; 89 90 Entry(Entry next, 91 char []buf, int offset, int length, 92 int colon, 93 boolean isAttribute) 94 { 95 _next = next; 96 97 _buf = new char[length]; 98 System.arraycopy(buf, offset, _buf, 0, length); 99 100 _isAttribute = isAttribute; 101 102 if (colon > 0) { 103 _prefix = new String (buf, 0, colon); 104 _localName = new String (buf, colon + 1, length - colon - 1); 105 } 106 else { 107 _prefix = null; 108 _localName = new String (buf, 0, length); 109 } 110 111 if (_isAttribute) 112 _namespace = _namespaceContext.getAttributeNamespace(_prefix); 113 else 114 _namespace = _namespaceContext.getElementNamespace(_prefix); 115 116 fillQName(); 117 } 118 119 public final boolean match(char []buf, int offset, int length, 120 boolean isAttribute) 121 { 122 if (length != _buf.length || _isAttribute != isAttribute) 123 return false; 124 125 char []entryBuf = _buf; 126 127 for (length--; length >= 0; length--) { 128 if (entryBuf[length] != buf[offset + length]) 129 return false; 130 } 131 132 return true; 133 } 134 135 String getLocalName() 136 { 137 return _localName; 138 } 139 140 String getPrefix() 141 { 142 return _prefix; 143 } 144 145 QName getQName() 146 { 147 if (_version != _namespace.getVersion()) 148 fillQName(); 149 150 return _qName; 151 } 152 153 private void fillQName() 154 { 155 _version = _namespace.getVersion(); 156 157 String prefix = _prefix; 158 159 if (prefix == null) 160 prefix = ""; 161 162 _qName = new QName (_namespace.getUri(), _localName, prefix); 163 } 164 } 165 } 166 | Popular Tags |