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. package com.sun.org.apache.xalan.internal.xsltc.runtime;
  20. import java.util.Vector;
  21. /**
  22. * @author Morten Jorgensen
  23. */
  24. public class AttributeList implements org.xml.sax.Attributes {
  25. private final static String EMPTYSTRING = "";
  26. private final static String CDATASTRING = "CDATA";
  27. private Hashtable _attributes;
  28. private Vector _names;
  29. private Vector _qnames;
  30. private Vector _values;
  31. private Vector _uris;
  32. private int _length;
  33. /**
  34. * AttributeList constructor
  35. */
  36. public AttributeList() {
  37. /*
  38. _attributes = new Hashtable();
  39. _names = new Vector();
  40. _values = new Vector();
  41. _qnames = new Vector();
  42. _uris = new Vector();
  43. */
  44. _length = 0;
  45. }
  46. /**
  47. * Attributes clone constructor
  48. */
  49. public AttributeList(org.xml.sax.Attributes attributes) {
  50. this();
  51. if (attributes != null) {
  52. final int count = attributes.getLength();
  53. for (int i = 0; i < count; i++) {
  54. add(attributes.getQName(i),attributes.getValue(i));
  55. }
  56. }
  57. }
  58. /**
  59. * Allocate memory for the AttributeList
  60. * %OPT% Use on-demand allocation for the internal vectors. The memory
  61. * is only allocated when there is an attribute. This reduces the cost
  62. * of creating many small RTFs.
  63. */
  64. private void alloc() {
  65. _attributes = new Hashtable();
  66. _names = new Vector();
  67. _values = new Vector();
  68. _qnames = new Vector();
  69. _uris = new Vector();
  70. }
  71. /**
  72. * SAX2: Return the number of attributes in the list.
  73. */
  74. public int getLength() {
  75. return(_length);
  76. }
  77. /**
  78. * SAX2: Look up an attribute's Namespace URI by index.
  79. */
  80. public String getURI(int index) {
  81. if (index < _length)
  82. return((String)_uris.elementAt(index));
  83. else
  84. return(null);
  85. }
  86. /**
  87. * SAX2: Look up an attribute's local name by index.
  88. */
  89. public String getLocalName(int index) {
  90. if (index < _length)
  91. return((String)_names.elementAt(index));
  92. else
  93. return(null);
  94. }
  95. /**
  96. * Return the name of an attribute in this list (by position).
  97. */
  98. public String getQName(int pos) {
  99. if (pos < _length)
  100. return((String)_qnames.elementAt(pos));
  101. else
  102. return(null);
  103. }
  104. /**
  105. * SAX2: Look up an attribute's type by index.
  106. */
  107. public String getType(int index) {
  108. return(CDATASTRING);
  109. }
  110. /**
  111. * SAX2: Look up the index of an attribute by Namespace name.
  112. */
  113. public int getIndex(String namespaceURI, String localPart) {
  114. return(-1);
  115. }
  116. /**
  117. * SAX2: Look up the index of an attribute by XML 1.0 qualified name.
  118. */
  119. public int getIndex(String qname) {
  120. return(-1);
  121. }
  122. /**
  123. * SAX2: Look up an attribute's type by Namespace name.
  124. */
  125. public String getType(String uri, String localName) {
  126. return(CDATASTRING);
  127. }
  128. /**
  129. * SAX2: Look up an attribute's type by qname.
  130. */
  131. public String getType(String qname) {
  132. return(CDATASTRING);
  133. }
  134. /**
  135. * SAX2: Look up an attribute's value by index.
  136. */
  137. public String getValue(int pos) {
  138. if (pos < _length)
  139. return((String)_values.elementAt(pos));
  140. else
  141. return(null);
  142. }
  143. /**
  144. * SAX2: Look up an attribute's value by qname.
  145. */
  146. public String getValue(String qname) {
  147. if (_attributes != null) {
  148. final Integer obj = (Integer)_attributes.get(qname);
  149. if (obj == null) return null;
  150. return(getValue(obj.intValue()));
  151. }
  152. else
  153. return null;
  154. }
  155. /**
  156. * SAX2: Look up an attribute's value by Namespace name - SLOW!
  157. */
  158. public String getValue(String uri, String localName) {
  159. return(getValue(uri+':'+localName));
  160. }
  161. /**
  162. * Adds an attribute to the list
  163. */
  164. public void add(String qname, String value) {
  165. // Initialize the internal vectors at the first usage.
  166. if (_attributes == null)
  167. alloc();
  168. // Stuff the QName into the names vector & hashtable
  169. Integer obj = (Integer)_attributes.get(qname);
  170. if (obj == null) {
  171. _attributes.put(qname, obj = new Integer(_length++));
  172. _qnames.addElement(qname);
  173. _values.addElement(value);
  174. int col = qname.lastIndexOf(':');
  175. if (col > -1) {
  176. _uris.addElement(qname.substring(0,col));
  177. _names.addElement(qname.substring(col+1));
  178. }
  179. else {
  180. _uris.addElement(EMPTYSTRING);
  181. _names.addElement(qname);
  182. }
  183. }
  184. else {
  185. final int index = obj.intValue();
  186. _values.set(index, value);
  187. }
  188. }
  189. /**
  190. * Clears the attribute list
  191. */
  192. public void clear() {
  193. _length = 0;
  194. if (_attributes != null) {
  195. _attributes.clear();
  196. _names.removeAllElements();
  197. _values.removeAllElements();
  198. _qnames.removeAllElements();
  199. _uris.removeAllElements();
  200. }
  201. }
  202. }