Quadcap Embeddable Server

com/quadcap/text/sax/AttributeList.java

Go to the documentation of this file.
00001 package com.quadcap.text.sax; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 /** 00042 * AttributeList implementation; uses arrays of Strings internally. 00043 * 00044 * @author Stan Bailes 00045 */ 00046 public class AttributeList implements org.xml.sax.AttributeList { 00047 int length = 0; 00048 String[] name = new String[8]; 00049 String[] value = new String[8]; 00050 String[] type = new String[8]; 00051 00052 /** 00053 * Return the number of attributes in this list. 00054 * 00055 * <p>The SAX parser may provide attributes in any 00056 * arbitrary order, regardless of the order in which they were 00057 * declared or specified. The number of attributes may be 00058 * zero.</p> 00059 * 00060 * @return The number of attributes in the list. 00061 */ 00062 public final int getLength() { 00063 return length; 00064 } 00065 00066 00067 /** 00068 * Return the name of an attribute in this list (by position). 00069 * 00070 * <p>The names must be unique: the SAX parser shall not include the 00071 * same attribute twice. Attributes without values (those declared 00072 * #IMPLIED without a value specified in the start tag) will be 00073 * omitted from the list.</p> 00074 * 00075 * <p>If the attribute name has a namespace prefix, the prefix 00076 * will still be attached.</p> 00077 * 00078 * @param i The index of the attribute in the list (starting at 0). 00079 * @return The name of the indexed attribute, or null 00080 * if the index is out of range. 00081 * @see #getLength 00082 */ 00083 public final String getName(int i) { 00084 try { 00085 return name[i]; 00086 } catch (ArrayIndexOutOfBoundsException e) { 00087 return null; 00088 } 00089 } 00090 00091 00092 /** 00093 * Return the type of an attribute in the list (by position). 00094 * 00095 * <p>The attribute type is one of the strings "CDATA", "ID", 00096 * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", 00097 * or "NOTATION" (always in upper case).</p> 00098 * 00099 * <p>If the parser has not read a declaration for the attribute, 00100 * or if the parser does not report attribute types, then it must 00101 * return the value "CDATA" as stated in the XML 1.0 Recommentation 00102 * (clause 3.3.3, "Attribute-Value Normalization").</p> 00103 * 00104 * <p>For an enumerated attribute that is not a notation, the 00105 * parser will report the type as "NMTOKEN".</p> 00106 * 00107 * @param i The index of the attribute in the list (starting at 0). 00108 * @return The attribute type as a string, or 00109 * null if the index is out of range. 00110 * @see #getLength 00111 * @see #getType(java.lang.String) 00112 */ 00113 public final String getType(int i) { 00114 try { 00115 return type[i]; 00116 } catch (ArrayIndexOutOfBoundsException e) { 00117 return null; 00118 } 00119 } 00120 00121 /** 00122 * Return the value of an attribute in the list (by position). 00123 * 00124 * <p>If the attribute value is a list of tokens (IDREFS, 00125 * ENTITIES, or NMTOKENS), the tokens will be concatenated 00126 * into a single string separated by whitespace.</p> 00127 * 00128 * @param i The index of the attribute in the list (starting at 0). 00129 * @return The attribute value as a string, or 00130 * null if the index is out of range. 00131 * @see #getLength 00132 * @see #getValue(java.lang.String) 00133 */ 00134 public final String getValue (int i) { 00135 try { 00136 return value[i]; 00137 } catch (ArrayIndexOutOfBoundsException e) { 00138 return null; 00139 } 00140 } 00141 00142 00143 00144 /** 00145 * Return the type of an attribute in the list (by name). 00146 * 00147 * <p>The return value is the same as the return value for 00148 * getType(int).</p> 00149 * 00150 * <p>If the attribute name has a namespace prefix in the document, 00151 * the application must include the prefix here.</p> 00152 * 00153 * @param name The name of the attribute. 00154 * @return The attribute type as a string, or null if no 00155 * such attribute exists. 00156 * @see #getType(int) 00157 */ 00158 public final String getType(String name) { 00159 return getType(getAttribute(name)); 00160 } 00161 00162 /** 00163 * Return the value of an attribute in the list (by name). 00164 * 00165 * <p>The return value is the same as the return value for 00166 * getValue(int).</p> 00167 * 00168 * <p>If the attribute name has a namespace prefix in the document, 00169 * the application must include the prefix here.</p> 00170 * 00171 * @param i The index of the attribute in the list. 00172 * @return The attribute value as a string, or null if 00173 * no such attribute exists. 00174 * @see #getValue(int) 00175 */ 00176 public final String getValue(String n) { 00177 return getValue(getAttribute(n)); 00178 } 00179 00180 final int getAttribute(String n) { 00181 for (int i = 0; i < length; i++) { 00182 if (name[i].equals(n)) return i; 00183 } 00184 return -1; 00185 } 00186 00187 final String[] resize(String[] v, int len) { 00188 String[] n = new String[len]; 00189 System.arraycopy(v, 0, n, 0, v.length); 00190 return n; 00191 } 00192 00193 final void addAttribute(String n, String t, String v) { 00194 if (length >= name.length) { 00195 name = resize(name, length + (length >> 2) + 4); 00196 type = resize(type, length + (length >> 2) + 4); 00197 value = resize(value, length + (length >> 2) + 4); 00198 } 00199 name[length] = n; 00200 type[length] = t; 00201 value[length] = v; 00202 length++; 00203 } 00204 00205 final void clear() { 00206 length = 0; 00207 } 00208 00209 public static AttributeList copy(org.xml.sax.AttributeList a) { 00210 AttributeList c = new AttributeList(); 00211 for (int i = 0; i < a.getLength(); i++) { 00212 c.addAttribute(a.getName(i), a.getType(i), a.getValue(i)); 00213 } 00214 return c; 00215 } 00216 00217 public static String toString(org.xml.sax.AttributeList attributes) { 00218 StringBuffer sb = new StringBuffer(); 00219 if (attributes != null) { 00220 for (int i = 0; i < attributes.getLength(); i++) { 00221 sb.append(' '); 00222 sb.append(attributes.getName(i)); 00223 sb.append("=\""); 00224 sb.append(attributes.getValue(i)); 00225 sb.append("\""); 00226 } 00227 } 00228 return sb.toString(); 00229 } 00230 00231 public String toString() { 00232 return toString(this); 00233 } 00234 }