001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: AttributeList.java,v 1.8 2004/02/16 22:55:54 minchau Exp $
018: */
019:
020: package org.apache.xalan.xsltc.runtime;
021:
022: import java.util.Vector;
023:
024: /**
025: * @author Morten Jorgensen
026: */
027: public class AttributeList implements org.xml.sax.Attributes {
028:
029: private final static String EMPTYSTRING = "";
030: private final static String CDATASTRING = "CDATA";
031:
032: private Hashtable _attributes;
033: private Vector _names;
034: private Vector _qnames;
035: private Vector _values;
036: private Vector _uris;
037: private int _length;
038:
039: /**
040: * AttributeList constructor
041: */
042: public AttributeList() {
043: /*
044: _attributes = new Hashtable();
045: _names = new Vector();
046: _values = new Vector();
047: _qnames = new Vector();
048: _uris = new Vector();
049: */
050: _length = 0;
051: }
052:
053: /**
054: * Attributes clone constructor
055: */
056: public AttributeList(org.xml.sax.Attributes attributes) {
057: this ();
058: if (attributes != null) {
059: final int count = attributes.getLength();
060: for (int i = 0; i < count; i++) {
061: add(attributes.getQName(i), attributes.getValue(i));
062: }
063: }
064: }
065:
066: /**
067: * Allocate memory for the AttributeList
068: * %OPT% Use on-demand allocation for the internal vectors. The memory
069: * is only allocated when there is an attribute. This reduces the cost
070: * of creating many small RTFs.
071: */
072: private void alloc() {
073: _attributes = new Hashtable();
074: _names = new Vector();
075: _values = new Vector();
076: _qnames = new Vector();
077: _uris = new Vector();
078: }
079:
080: /**
081: * SAX2: Return the number of attributes in the list.
082: */
083: public int getLength() {
084: return (_length);
085: }
086:
087: /**
088: * SAX2: Look up an attribute's Namespace URI by index.
089: */
090: public String getURI(int index) {
091: if (index < _length)
092: return ((String) _uris.elementAt(index));
093: else
094: return (null);
095: }
096:
097: /**
098: * SAX2: Look up an attribute's local name by index.
099: */
100: public String getLocalName(int index) {
101: if (index < _length)
102: return ((String) _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 getQName(int pos) {
111: if (pos < _length)
112: return ((String) _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 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 namespaceURI, String 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 qname) {
135: return (-1);
136: }
137:
138: /**
139: * SAX2: Look up an attribute's type by Namespace name.
140: */
141: public String getType(String uri, String localName) {
142: return (CDATASTRING);
143: }
144:
145: /**
146: * SAX2: Look up an attribute's type by qname.
147: */
148: public String getType(String qname) {
149: return (CDATASTRING);
150: }
151:
152: /**
153: * SAX2: Look up an attribute's value by index.
154: */
155: public String getValue(int pos) {
156: if (pos < _length)
157: return ((String) _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 getValue(String qname) {
166: if (_attributes != null) {
167: final Integer obj = (Integer) _attributes.get(qname);
168: if (obj == null)
169: return null;
170: return (getValue(obj.intValue()));
171: } else
172: return null;
173: }
174:
175: /**
176: * SAX2: Look up an attribute's value by Namespace name - SLOW!
177: */
178: public String getValue(String uri, String localName) {
179: return (getValue(uri + ':' + localName));
180: }
181:
182: /**
183: * Adds an attribute to the list
184: */
185: public void add(String qname, String 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 obj = (Integer) _attributes.get(qname);
192: if (obj == null) {
193: _attributes.put(qname, obj = new Integer(_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: } else {
201: _uris.addElement(EMPTYSTRING);
202: _names.addElement(qname);
203: }
204: } else {
205: final int index = obj.intValue();
206: _values.set(index, value);
207: }
208: }
209:
210: /**
211: * Clears the attribute list
212: */
213: public void clear() {
214: _length = 0;
215: if (_attributes != null) {
216: _attributes.clear();
217: _names.removeAllElements();
218: _values.removeAllElements();
219: _qnames.removeAllElements();
220: _uris.removeAllElements();
221: }
222: }
223:
224: }
|