1 9 package javolution.xml.pull; 10 11 import javolution.lang.PersistentReference; 12 import j2me.lang.CharSequence; 13 14 20 final class Namespaces { 21 22 25 private static final PersistentReference SIZE = new PersistentReference( 26 "javolution.xml.pull.Namespaces#SIZE", new Integer(64)); 27 28 31 private int[] _nspCounts = new int[((Integer) SIZE.get()).intValue()]; 32 33 36 private int _mapCount; 37 38 41 private CharSequenceImpl[] _namespaces = new CharSequenceImpl[((Integer) SIZE 42 .get()).intValue()]; 43 44 47 private int _depth; 48 49 52 private CharSequenceImpl _default = CharSequenceImpl.EMPTY; 53 54 57 public Namespaces() { 58 } 59 60 66 public int getNamespaceCount(int depth) { 67 if (depth > _depth) 68 return _nspCounts[_depth]; 69 return _nspCounts[depth]; 70 } 71 72 78 public CharSequenceImpl getNamespacePrefix(int pos) { 79 return _namespaces[pos << 1]; 80 } 81 82 88 public CharSequenceImpl getNamespaceUri(int pos) { 89 return _namespaces[(pos << 1) + 1]; 90 } 91 92 99 public CharSequenceImpl getNamespaceUri(CharSequence prefix) { 100 if (prefix == null) 101 return _default; 102 for (int i = _nspCounts[_depth] + _mapCount; i > 0;) { 103 CharSequenceImpl pfx = _namespaces[--i << 1]; 104 if ((pfx != null) && pfx.equals(prefix)) 105 return _namespaces[(i << 1) + 1]; 106 } 107 if (XML_PREFIX.equals(prefix)) 108 return XML_URI; 109 if (XMLNS_PREFIX.equals(prefix)) 110 return XMLNS_URI; 111 return null; 112 } 113 114 private static final CharSequenceImpl XML_PREFIX = new CharSequenceImpl( 115 "xml"); 116 117 private static final CharSequenceImpl XML_URI = new CharSequenceImpl( 118 "http://www.w3.org/XML/1998/namespace"); 119 120 private static final CharSequenceImpl XMLNS_PREFIX = new CharSequenceImpl( 121 "xmlns"); 122 123 private static final CharSequenceImpl XMLNS_URI = new CharSequenceImpl( 124 "http://www.w3.org/2000/xmlns/"); 125 126 135 public void map(CharSequenceImpl prefix, CharSequenceImpl uri) { 136 final int i = (_nspCounts[_depth] + _mapCount++) << 1; 137 if (i + 1 >= _namespaces.length) resize(); 138 _namespaces[i] = prefix; 139 _namespaces[i + 1] = uri; 140 if (prefix == null) { _default = uri; 142 } 143 } 144 145 148 public void flush() { 149 if (_mapCount != 0) { 150 push(); 151 pop(); 152 } 153 } 154 155 158 public void push() { 159 if (++_depth >= _nspCounts.length) resize(); 160 _nspCounts[_depth] = _nspCounts[_depth - 1] + _mapCount; 161 _mapCount = 0; 162 } 163 164 167 public void pop() { 168 _mapCount = 0; 169 final int oldCount = _nspCounts[_depth]; 170 final int newCount = _nspCounts[--_depth]; 171 for (int i = oldCount; i > newCount;) { 172 if (_namespaces[--i << 1] == null) { _default = CharSequenceImpl.EMPTY; 174 for (int j = i; j > 0;) { if (_namespaces[--j << 1] == null) { 176 _default = _namespaces[(j << 1) + 1]; 177 break; 178 } 179 } 180 } 181 } 182 } 183 184 187 public void reset() { 188 _depth = 0; 189 _nspCounts[0] = 0; 190 _default = CharSequenceImpl.EMPTY; 191 } 192 193 196 private void resize() { 197 final int size = _nspCounts.length; int[] tmp0 = new int[size * 2]; 199 System.arraycopy(_nspCounts, 0, tmp0, 0, size); 200 _nspCounts = tmp0; 201 CharSequenceImpl[] tmp1 = new CharSequenceImpl[size * 2]; 202 System.arraycopy(_namespaces, 0, tmp1, 0, size); 203 _namespaces = tmp1; 204 SIZE.set(new Integer(size * 2)); 205 } 206 } | Popular Tags |