KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > runtime > AttributeList


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: AttributeList.java,v 1.8 2004/02/16 22:55:54 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.runtime;
21
22 import java.util.Vector JavaDoc;
23
24 /**
25  * @author Morten Jorgensen
26  */

27 public class AttributeList implements org.xml.sax.Attributes JavaDoc {
28
29     private final static String JavaDoc EMPTYSTRING = "";
30     private final static String JavaDoc CDATASTRING = "CDATA";
31
32     private Hashtable _attributes;
33     private Vector JavaDoc _names;
34     private Vector JavaDoc _qnames;
35     private Vector JavaDoc _values;
36     private Vector JavaDoc _uris;
37     private int _length;
38
39     /**
40      * AttributeList constructor
41      */

42     public AttributeList() {
43     /*
44     _attributes = new Hashtable();
45     _names = new Vector();
46     _values = new Vector();
47     _qnames = new Vector();
48     _uris = new Vector();
49     */

50     _length = 0;
51     }
52
53     /**
54      * Attributes clone constructor
55      */

56     public AttributeList(org.xml.sax.Attributes JavaDoc attributes) {
57     this();
58     if (attributes != null) {
59         final int count = attributes.getLength();
60         for (int i = 0; i < count; i++) {
61         add(attributes.getQName(i),attributes.getValue(i));
62         }
63     }
64     }
65     
66     /**
67      * Allocate memory for the AttributeList
68      * %OPT% Use on-demand allocation for the internal vectors. The memory
69      * is only allocated when there is an attribute. This reduces the cost
70      * of creating many small RTFs.
71      */

72     private void alloc() {
73     _attributes = new Hashtable();
74     _names = new Vector JavaDoc();
75     _values = new Vector JavaDoc();
76     _qnames = new Vector JavaDoc();
77     _uris = new Vector JavaDoc();
78     }
79
80     /**
81      * SAX2: Return the number of attributes in the list.
82      */

83     public int getLength() {
84     return(_length);
85     }
86
87     /**
88      * SAX2: Look up an attribute's Namespace URI by index.
89      */

90     public String JavaDoc getURI(int index) {
91     if (index < _length)
92         return((String JavaDoc)_uris.elementAt(index));
93     else
94         return(null);
95     }
96
97     /**
98      * SAX2: Look up an attribute's local name by index.
99      */

100     public String JavaDoc getLocalName(int index) {
101     if (index < _length)
102         return((String JavaDoc)_names.elementAt(index));
103     else
104         return(null);
105     }
106
107     /**
108      * Return the name of an attribute in this list (by position).
109      */

110     public String JavaDoc getQName(int pos) {
111     if (pos < _length)
112         return((String JavaDoc)_qnames.elementAt(pos));
113     else
114         return(null);
115     }
116
117     /**
118      * SAX2: Look up an attribute's type by index.
119      */

120     public String JavaDoc getType(int index) {
121     return(CDATASTRING);
122     }
123
124     /**
125      * SAX2: Look up the index of an attribute by Namespace name.
126      */

127     public int getIndex(String JavaDoc namespaceURI, String JavaDoc localPart) {
128     return(-1);
129     }
130
131     /**
132      * SAX2: Look up the index of an attribute by XML 1.0 qualified name.
133      */

134     public int getIndex(String JavaDoc qname) {
135     return(-1);
136     }
137
138     /**
139      * SAX2: Look up an attribute's type by Namespace name.
140      */

141     public String JavaDoc getType(String JavaDoc uri, String JavaDoc localName) {
142     return(CDATASTRING);
143     }
144
145     /**
146      * SAX2: Look up an attribute's type by qname.
147      */

148     public String JavaDoc getType(String JavaDoc qname) {
149     return(CDATASTRING);
150     }
151
152     /**
153      * SAX2: Look up an attribute's value by index.
154      */

155     public String JavaDoc getValue(int pos) {
156     if (pos < _length)
157         return((String JavaDoc)_values.elementAt(pos));
158     else
159         return(null);
160     }
161
162     /**
163      * SAX2: Look up an attribute's value by qname.
164      */

165     public String JavaDoc getValue(String JavaDoc qname) {
166     if (_attributes != null) {
167         final Integer JavaDoc obj = (Integer JavaDoc)_attributes.get(qname);
168         if (obj == null) return null;
169         return(getValue(obj.intValue()));
170     }
171     else
172         return null;
173     }
174
175     /**
176      * SAX2: Look up an attribute's value by Namespace name - SLOW!
177      */

178     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localName) {
179     return(getValue(uri+':'+localName));
180     }
181
182     /**
183      * Adds an attribute to the list
184      */

185     public void add(String JavaDoc qname, String JavaDoc value) {
186     // Initialize the internal vectors at the first usage.
187
if (_attributes == null)
188         alloc();
189     
190     // Stuff the QName into the names vector & hashtable
191
Integer JavaDoc obj = (Integer JavaDoc)_attributes.get(qname);
192     if (obj == null) {
193         _attributes.put(qname, obj = new Integer JavaDoc(_length++));
194         _qnames.addElement(qname);
195         _values.addElement(value);
196         int col = qname.lastIndexOf(':');
197         if (col > -1) {
198         _uris.addElement(qname.substring(0,col));
199         _names.addElement(qname.substring(col+1));
200         }
201         else {
202         _uris.addElement(EMPTYSTRING);
203         _names.addElement(qname);
204         }
205     }
206     else {
207         final int index = obj.intValue();
208         _values.set(index, value);
209     }
210     }
211
212     /**
213      * Clears the attribute list
214      */

215     public void clear() {
216     _length = 0;
217     if (_attributes != null) {
218         _attributes.clear();
219         _names.removeAllElements();
220         _values.removeAllElements();
221         _qnames.removeAllElements();
222         _uris.removeAllElements();
223     }
224     }
225     
226 }
227
Popular Tags