KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > stream > AttributesImpl


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 /**
11  * This class provides the implementation of the {@link Attributes}
12  * interface for the StAX parser.
13  *
14  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
15  */

16 final class AttributesImpl implements Attributes, Reusable {
17
18     /**
19      * Holds the local names.
20      */

21     private CharArray[] _localNames = new CharArray[16];
22
23     /**
24      * Holds the prefixes.
25      */

26     private CharArray[] _prefixes = new CharArray[16];
27
28     /**
29      * Holds the qualified names.
30      */

31     private CharArray[] _qNames = new CharArray[16];
32
33     /**
34      * Holds the values.
35      */

36     private CharArray[] _values = new CharArray[16];
37
38     /**
39      * Holds the namespace stack.
40      */

41     private final NamespacesImpl _namespaces;
42
43     /**
44      * Holds the current number of attributes.
45      */

46     private int _length;
47
48     ////////////////////////////////////////////////////////////////////
49
// Constructors.
50
////////////////////////////////////////////////////////////////////
51

52     /**
53      * Creates a list of attribute using the specified namespace stack.
54      */

55     public AttributesImpl(NamespacesImpl namespaces) {
56         _namespaces = namespaces;
57     }
58
59     // Implements Attributes.
60
public int getLength() {
61         return _length;
62     }
63
64     // Implements Attributes.
65
public CharArray getURI(int index) {
66         return (index >= 0 && index < _length) ? _namespaces
67                 .getNamespaceURI(_prefixes[index]) : null;
68     }
69
70     // Implements Attributes.
71
public CharArray getLocalName(int index) {
72         return (index >= 0 && index < _length) ? _localNames[index] : null;
73     }
74
75     // Implements Attributes.
76
public CharArray getPrefix(int index) {
77         return (index >= 0 && index < _length) ? _prefixes[index] : null;
78     }
79
80     // Implements Attributes.
81
public CharArray getQName(int index) {
82         return (index >= 0 && index < _length) ? _qNames[index] : null;
83     }
84
85     // Implements Attributes.
86
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     // Implements Attributes.
93
public CharArray getValue(int index) {
94         return (index >= 0 && index < _length) ? _values[index] : null;
95     }
96
97     // Implements Attributes.
98
public int getIndex(CharSequence JavaDoc uri, CharSequence JavaDoc localName) {
99         if (uri == null)
100             throw new IllegalArgumentException JavaDoc(
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     // Implements Attributes.
113
public int getIndex(CharSequence JavaDoc 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     // Implements Attributes.
122
public CharArray getType(CharSequence JavaDoc uri, CharSequence JavaDoc localName) {
123         final int index = getIndex(uri, localName);
124         return (index >= 0) ? CDATA : null;
125     }
126
127     // Implements Attributes.
128
public CharArray getType(CharSequence JavaDoc qName) {
129         final int index = getIndex(qName);
130         return (index >= 0) ? CDATA : null;
131     }
132
133     // Implements Attributes.
134
public CharArray getValue(CharSequence JavaDoc uri, CharSequence JavaDoc localName) {
135         final int index = getIndex(uri, localName);
136         return (index >= 0) ? _values[index] : null;
137     }
138
139     // Implements Attributes.
140
public CharArray getValue(CharSequence JavaDoc qName) {
141         final int index = getIndex(qName);
142         return (index >= 0) ? _values[index] : null;
143     }
144
145     /**
146      * Clear the attribute list for reuse.
147      */

148     public void reset() {
149         _length = 0;
150     }
151
152     /**
153      * Adds an attribute to the end of the attribute list.
154      *
155      * @param localName the local name.
156      * @param prefix the prefix or <code>null</code> if none.
157      * @param qName the qualified (prefixed) name.
158      * @param value the attribute value.
159      */

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     /**
172      * Returns the string representation of these attributes.
173      *
174      * @return this attributes textual representation.
175      */

176     public String JavaDoc 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 JavaDoc() {
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