KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > sax > AttributesHolder


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.sax;
41
42 import com.sun.xml.fastinfoset.EncodingConstants;
43 import com.sun.xml.fastinfoset.QualifiedName;
44 import com.sun.xml.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory;
45 import java.io.IOException JavaDoc;
46 import java.util.Map JavaDoc;
47 import org.jvnet.fastinfoset.EncodingAlgorithm;
48 import org.jvnet.fastinfoset.EncodingAlgorithmException;
49 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
50 import org.jvnet.fastinfoset.FastInfosetException;
51
52 import org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
53 import com.sun.xml.fastinfoset.CommonResourceBundle;
54
55 public class AttributesHolder implements EncodingAlgorithmAttributes {
56     private static final int DEFAULT_CAPACITY = 8;
57
58     private Map JavaDoc _registeredEncodingAlgorithms;
59     
60     private int _attributeCount;
61     
62     private QualifiedName[] _names;
63     private String JavaDoc[] _values;
64     
65     private String JavaDoc[] _algorithmURIs;
66     private int[] _algorithmIds;
67     private Object JavaDoc[] _algorithmData;
68     
69     public AttributesHolder() {
70         _names = new QualifiedName[DEFAULT_CAPACITY];
71         _values = new String JavaDoc[DEFAULT_CAPACITY];
72         
73         _algorithmURIs = new String JavaDoc[DEFAULT_CAPACITY];
74         _algorithmIds = new int[DEFAULT_CAPACITY];
75         _algorithmData = new Object JavaDoc[DEFAULT_CAPACITY];
76     }
77
78     public AttributesHolder(Map JavaDoc registeredEncodingAlgorithms) {
79         this();
80         _registeredEncodingAlgorithms = registeredEncodingAlgorithms;
81     }
82     
83     // org.xml.sax.Attributes
84

85     public final int getLength() {
86         return _attributeCount;
87     }
88
89     public final String JavaDoc getLocalName(int index) {
90         return _names[index].localName;
91     }
92
93     public final String JavaDoc getQName(int index) {
94         return _names[index].getQNameString();
95     }
96
97     public final String JavaDoc getType(int index) {
98         return "CDATA";
99     }
100
101     public final String JavaDoc getURI(int index) {
102         return _names[index].namespaceName;
103     }
104
105     public final String JavaDoc getValue(int index) {
106         final String JavaDoc value = _values[index];
107         if (value != null) {
108             return value;
109         }
110         
111         if (_algorithmData[index] == null || _registeredEncodingAlgorithms == null) {
112             return null;
113         }
114                 
115         try {
116             return _values[index] = convertEncodingAlgorithmDataToString(
117                     _algorithmIds[index],
118                     _algorithmURIs[index],
119                     _algorithmData[index]).toString();
120         } catch (IOException JavaDoc e) {
121             return null;
122         } catch (FastInfosetException e) {
123             return null;
124         }
125     }
126
127     public final int getIndex(String JavaDoc qName) {
128         int i = qName.indexOf(':');
129         String JavaDoc prefix = "";
130         String JavaDoc localName = qName;
131         if (i >= 0) {
132             prefix = qName.substring(0, i);
133             localName = qName.substring(i + 1);
134         }
135         
136         for (i = 0; i < _attributeCount; i++) {
137             QualifiedName name = _names[i];
138             if (localName.equals(name.localName) &&
139                 prefix.equals(name.prefix)) {
140                 return i;
141             }
142         }
143         return -1;
144     }
145
146     public final String JavaDoc getType(String JavaDoc qName) {
147         int index = getIndex(qName);
148         if (index >= 0) {
149             return "CDATA";
150         } else {
151             return null;
152         }
153     }
154
155     public final String JavaDoc getValue(String JavaDoc qName) {
156         int index = getIndex(qName);
157         if (index >= 0) {
158             return _values[index];
159         } else {
160             return null;
161         }
162     }
163
164     public final int getIndex(String JavaDoc uri, String JavaDoc localName) {
165         for (int i = 0; i < _attributeCount; i++) {
166             QualifiedName name = _names[i];
167             if (localName.equals(name.localName) &&
168                 uri.equals(name.namespaceName)) {
169                 return i;
170             }
171         }
172         return -1;
173     }
174
175     public final String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
176         int index = getIndex(uri, localName);
177         if (index >= 0) {
178             return "CDATA";
179         } else {
180             return null;
181         }
182     }
183
184     public final String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
185         int index = getIndex(uri, localName);
186         if (index >= 0) {
187             return _values[index];
188         } else {
189             return null;
190         }
191     }
192
193     public final void clear() {
194         for (int i = 0; i < _attributeCount; i++) {
195             _values[i] = null;
196             _algorithmData[i] = null;
197         }
198         _attributeCount = 0;
199     }
200
201     // EncodingAlgorithmAttributes
202

203     public final String JavaDoc getAlgorithmURI(int index) {
204         return _algorithmURIs[index];
205     }
206  
207     public final int getAlgorithmIndex(int index) {
208         return _algorithmIds[index];
209     }
210     
211     public final Object JavaDoc getAlgorithmData(int index) {
212         return _algorithmData[index];
213     }
214
215     
216     // -----
217

218     public final void addAttribute(QualifiedName name, String JavaDoc value) {
219         if (_attributeCount == _names.length) {
220             resize();
221         }
222         _names[_attributeCount] = name;
223         _values[_attributeCount++] = value;
224     }
225
226     public final void addAttributeWithAlgorithmData(QualifiedName name, String JavaDoc URI, int id, Object JavaDoc data) {
227         if (_attributeCount == _names.length) {
228             resize();
229         }
230         _names[_attributeCount] = name;
231         _values[_attributeCount] = null;
232
233         _algorithmURIs[_attributeCount] = URI;
234         _algorithmIds[_attributeCount] = id;
235         _algorithmData[_attributeCount++] = data;
236     }
237
238     public final QualifiedName getQualifiedName(int index) {
239         return _names[index];
240     }
241     
242     public final String JavaDoc getPrefix(int index) {
243         return _names[index].prefix;
244     }
245         
246     private final void resize() {
247         final int newLength = _attributeCount * 3 / 2 + 1;
248
249         QualifiedName[] names = new QualifiedName[newLength];
250         String JavaDoc[] values = new String JavaDoc[newLength];
251
252         String JavaDoc[] algorithmURIs = new String JavaDoc[newLength];
253         int[] algorithmIds = new int[newLength];
254         Object JavaDoc[] algorithmData = new Object JavaDoc[newLength];
255
256         System.arraycopy(_names, 0, names, 0, _attributeCount);
257         System.arraycopy(_values, 0, values, 0, _attributeCount);
258
259         System.arraycopy(_algorithmURIs, 0, algorithmURIs, 0, _attributeCount);
260         System.arraycopy(_algorithmIds, 0, algorithmIds, 0, _attributeCount);
261         System.arraycopy(_algorithmData, 0, algorithmData, 0, _attributeCount);
262
263         _names = names;
264         _values = values;
265
266         _algorithmURIs = algorithmURIs;
267         _algorithmIds = algorithmIds;
268         _algorithmData = algorithmData;
269     }
270     
271     private final StringBuffer JavaDoc convertEncodingAlgorithmDataToString(int identifier, String JavaDoc URI, Object JavaDoc data) throws FastInfosetException, IOException JavaDoc {
272         EncodingAlgorithm ea = null;
273         if (identifier < EncodingConstants.ENCODING_ALGORITHM_BUILTIN_END) {
274             ea = BuiltInEncodingAlgorithmFactory.table[identifier];
275         } else if (identifier == EncodingAlgorithmIndexes.CDATA) {
276             throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.CDATAAlgorithmNotSupported"));
277         } else if (identifier >= EncodingConstants.ENCODING_ALGORITHM_APPLICATION_START) {
278             if (URI == null) {
279                 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.URINotPresent") + identifier);
280             }
281             
282             ea = (EncodingAlgorithm)_registeredEncodingAlgorithms.get(URI);
283             if (ea == null) {
284                 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.algorithmNotRegistered") + URI);
285             }
286         } else {
287             // Reserved built-in algorithms for future use
288
// TODO should use sax property to decide if event will be
289
// reported, allows for support through handler if required.
290
throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.identifiers10to31Reserved"));
291         }
292
293         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
294         ea.convertToCharacters(data, sb);
295         return sb;
296     }
297 }
298
Popular Tags