1 package javolution.xml.stream; 2 3 import j2me.lang.CharSequence; 4 import j2mex.realtime.MemoryArea; 5 import javolution.lang.Reusable; 6 import javolution.text.CharArray; 7 import javolution.text.Text; 8 import javolution.xml.sax.Attributes; 9 10 16 final class AttributesImpl implements Attributes, Reusable { 17 18 21 private CharArray[] _localNames = new CharArray[16]; 22 23 26 private CharArray[] _prefixes = new CharArray[16]; 27 28 31 private CharArray[] _qNames = new CharArray[16]; 32 33 36 private CharArray[] _values = new CharArray[16]; 37 38 41 private final NamespacesImpl _namespaces; 42 43 46 private int _length; 47 48 52 55 public AttributesImpl(NamespacesImpl namespaces) { 56 _namespaces = namespaces; 57 } 58 59 public int getLength() { 61 return _length; 62 } 63 64 public CharArray getURI(int index) { 66 return (index >= 0 && index < _length) ? _namespaces 67 .getNamespaceURI(_prefixes[index]) : null; 68 } 69 70 public CharArray getLocalName(int index) { 72 return (index >= 0 && index < _length) ? _localNames[index] : null; 73 } 74 75 public CharArray getPrefix(int index) { 77 return (index >= 0 && index < _length) ? _prefixes[index] : null; 78 } 79 80 public CharArray getQName(int index) { 82 return (index >= 0 && index < _length) ? _qNames[index] : null; 83 } 84 85 public CharArray getType(int index) { 87 return (index >= 0 && index < _length) ? CDATA : null; 88 } 89 90 private static final CharArray CDATA = new CharArray("CDATA"); 91 92 public CharArray getValue(int index) { 94 return (index >= 0 && index < _length) ? _values[index] : null; 95 } 96 97 public int getIndex(CharSequence uri, CharSequence localName) { 99 if (uri == null) 100 throw new IllegalArgumentException ( 101 "null namespace URI is not allowed"); 102 for (int i = 0; i < _length; i++) { 103 if (_localNames[i].equals(localName)) { 104 CharArray ns = _namespaces.getNamespaceURI(_prefixes[i]); 105 if ((ns != null) && ns.equals(uri)) 106 return i; 107 } 108 } 109 return -1; 110 } 111 112 public int getIndex(CharSequence qName) { 114 for (int i = 0; i < _length; i++) { 115 if (_qNames[i].equals(qName)) 116 return i; 117 } 118 return -1; 119 } 120 121 public CharArray getType(CharSequence uri, CharSequence localName) { 123 final int index = getIndex(uri, localName); 124 return (index >= 0) ? CDATA : null; 125 } 126 127 public CharArray getType(CharSequence qName) { 129 final int index = getIndex(qName); 130 return (index >= 0) ? CDATA : null; 131 } 132 133 public CharArray getValue(CharSequence uri, CharSequence localName) { 135 final int index = getIndex(uri, localName); 136 return (index >= 0) ? _values[index] : null; 137 } 138 139 public CharArray getValue(CharSequence qName) { 141 final int index = getIndex(qName); 142 return (index >= 0) ? _values[index] : null; 143 } 144 145 148 public void reset() { 149 _length = 0; 150 } 151 152 160 public void addAttribute(CharArray localName, CharArray prefix, 161 CharArray qName, CharArray value) { 162 if (_length >= _localNames.length) { 163 increaseCapacity(); 164 } 165 _localNames[_length] = localName; 166 _prefixes[_length] = prefix; 167 _qNames[_length] = qName; 168 _values[_length++] = value; 169 } 170 171 176 public String toString() { 177 Text text = Text.valueOf('['); 178 final Text equ = Text.valueOf('='); 179 final Text sep = Text.valueOf(", "); 180 for (int i = 0; i < _length;) { 181 text = text.concat(Text.valueOf(_qNames[i]).concat(equ).concat( 182 Text.valueOf(_values[i]))); 183 if (++i != _length) { 184 text = text.concat(sep); 185 } 186 } 187 return text.concat(Text.valueOf(']')).toString(); 188 } 189 190 private void increaseCapacity() { 191 MemoryArea.getMemoryArea(this).executeInArea(new Runnable () { 192 public void run() { 193 final int newCapacity = _length * 2; 194 195 CharArray[] tmp = new CharArray[newCapacity]; 196 System.arraycopy(_localNames, 0, tmp, 0, _length); 197 _localNames = tmp; 198 199 tmp = new CharArray[newCapacity]; 200 System.arraycopy(_prefixes, 0, tmp, 0, _length); 201 _prefixes = tmp; 202 203 tmp = new CharArray[newCapacity]; 204 System.arraycopy(_qNames, 0, tmp, 0, _length); 205 _qNames = tmp; 206 207 tmp = new CharArray[newCapacity]; 208 System.arraycopy(_values, 0, tmp, 0, _length); 209 _values = tmp; 210 } 211 }); 212 } 213 214 } | Popular Tags |