001: package javolution.xml.stream;
002:
003: import j2me.lang.CharSequence;
004: import j2mex.realtime.MemoryArea;
005: import javolution.lang.Reusable;
006: import javolution.text.CharArray;
007: import javolution.text.Text;
008: import javolution.xml.sax.Attributes;
009:
010: /**
011: * This class provides the implementation of the {@link Attributes}
012: * interface for the StAX parser.
013: *
014: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
015: */
016: final class AttributesImpl implements Attributes, Reusable {
017:
018: /**
019: * Holds the local names.
020: */
021: private CharArray[] _localNames = new CharArray[16];
022:
023: /**
024: * Holds the prefixes.
025: */
026: private CharArray[] _prefixes = new CharArray[16];
027:
028: /**
029: * Holds the qualified names.
030: */
031: private CharArray[] _qNames = new CharArray[16];
032:
033: /**
034: * Holds the values.
035: */
036: private CharArray[] _values = new CharArray[16];
037:
038: /**
039: * Holds the namespace stack.
040: */
041: private final NamespacesImpl _namespaces;
042:
043: /**
044: * Holds the current number of attributes.
045: */
046: private int _length;
047:
048: ////////////////////////////////////////////////////////////////////
049: // Constructors.
050: ////////////////////////////////////////////////////////////////////
051:
052: /**
053: * Creates a list of attribute using the specified namespace stack.
054: */
055: public AttributesImpl(NamespacesImpl namespaces) {
056: _namespaces = namespaces;
057: }
058:
059: // Implements Attributes.
060: public int getLength() {
061: return _length;
062: }
063:
064: // Implements Attributes.
065: public CharArray getURI(int index) {
066: return (index >= 0 && index < _length) ? _namespaces
067: .getNamespaceURINullAllowed(_prefixes[index]) : null;
068: }
069:
070: // Implements Attributes.
071: public CharArray getLocalName(int index) {
072: return (index >= 0 && index < _length) ? _localNames[index]
073: : null;
074: }
075:
076: // Implements Attributes.
077: public CharArray getPrefix(int index) {
078: return (index >= 0 && index < _length) ? _prefixes[index]
079: : null;
080: }
081:
082: // Implements Attributes.
083: public CharArray getQName(int index) {
084: return (index >= 0 && index < _length) ? _qNames[index] : null;
085: }
086:
087: // Implements Attributes.
088: public CharArray getType(int index) {
089: return (index >= 0 && index < _length) ? CDATA : null;
090: }
091:
092: private static final CharArray CDATA = new CharArray("CDATA");
093:
094: // Implements Attributes.
095: public CharArray getValue(int index) {
096: return (index >= 0 && index < _length) ? _values[index] : null;
097: }
098:
099: // Implements Attributes.
100: public int getIndex(CharSequence uri, CharSequence localName) {
101: if (uri == null)
102: throw new IllegalArgumentException(
103: "null namespace URI is not allowed");
104: for (int i = 0; i < _length; i++) {
105: if (_localNames[i].equals(localName)) {
106: CharArray ns = _namespaces
107: .getNamespaceURINullAllowed(_prefixes[i]);
108: if ((ns != null) && ns.equals(uri))
109: return i;
110: }
111: }
112: return -1;
113: }
114:
115: // Implements Attributes.
116: public int getIndex(CharSequence qName) {
117: for (int i = 0; i < _length; i++) {
118: if (_qNames[i].equals(qName))
119: return i;
120: }
121: return -1;
122: }
123:
124: // Implements Attributes.
125: public CharArray getType(CharSequence uri, CharSequence localName) {
126: final int index = getIndex(uri, localName);
127: return (index >= 0) ? CDATA : null;
128: }
129:
130: // Implements Attributes.
131: public CharArray getType(CharSequence qName) {
132: final int index = getIndex(qName);
133: return (index >= 0) ? CDATA : null;
134: }
135:
136: // Implements Attributes.
137: public CharArray getValue(CharSequence uri, CharSequence localName) {
138: final int index = getIndex(uri, localName);
139: return (index >= 0) ? _values[index] : null;
140: }
141:
142: // Implements Attributes.
143: public CharArray getValue(CharSequence qName) {
144: final int index = getIndex(qName);
145: return (index >= 0) ? _values[index] : null;
146: }
147:
148: /**
149: * Clear the attribute list for reuse.
150: */
151: public void reset() {
152: _length = 0;
153: }
154:
155: /**
156: * Adds an attribute to the end of the attribute list.
157: *
158: * @param localName the local name.
159: * @param prefix the prefix or <code>null</code> if none.
160: * @param qName the qualified (prefixed) name.
161: * @param value the attribute value.
162: */
163: public void addAttribute(CharArray localName, CharArray prefix,
164: CharArray qName, CharArray value) {
165: if (_length >= _localNames.length) {
166: increaseCapacity();
167: }
168: _localNames[_length] = localName;
169: _prefixes[_length] = prefix;
170: _qNames[_length] = qName;
171: _values[_length++] = value;
172: }
173:
174: /**
175: * Returns the string representation of these attributes.
176: *
177: * @return this attributes textual representation.
178: */
179: public String toString() {
180: Text text = Text.valueOf('[');
181: final Text equ = Text.valueOf('=');
182: final Text sep = Text.valueOf(", ");
183: for (int i = 0; i < _length;) {
184: text = text.concat(Text.valueOf(_qNames[i]).concat(equ)
185: .concat(Text.valueOf(_values[i])));
186: if (++i != _length) {
187: text = text.concat(sep);
188: }
189: }
190: return text.concat(Text.valueOf(']')).toString();
191: }
192:
193: private void increaseCapacity() {
194: MemoryArea.getMemoryArea(this ).executeInArea(new Runnable() {
195: public void run() {
196: final int newCapacity = _length * 2;
197:
198: CharArray[] tmp = new CharArray[newCapacity];
199: System.arraycopy(_localNames, 0, tmp, 0, _length);
200: _localNames = tmp;
201:
202: tmp = new CharArray[newCapacity];
203: System.arraycopy(_prefixes, 0, tmp, 0, _length);
204: _prefixes = tmp;
205:
206: tmp = new CharArray[newCapacity];
207: System.arraycopy(_qNames, 0, tmp, 0, _length);
208: _qNames = tmp;
209:
210: tmp = new CharArray[newCapacity];
211: System.arraycopy(_values, 0, tmp, 0, _length);
212: _values = tmp;
213: }
214: });
215: }
216:
217: }
|