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