Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | // AttributesImpl.java - default implementation of Attributes. |
aoqi@0 | 27 | // http://www.saxproject.org |
aoqi@0 | 28 | // Written by David Megginson |
aoqi@0 | 29 | // NO WARRANTY! This class is in the public domain. |
aoqi@0 | 30 | package com.sun.xml.internal.bind.util; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import org.xml.sax.Attributes; |
aoqi@0 | 33 | |
aoqi@0 | 34 | |
aoqi@0 | 35 | /** |
aoqi@0 | 36 | * Default implementation of the Attributes interface. |
aoqi@0 | 37 | * |
aoqi@0 | 38 | * <blockquote> |
aoqi@0 | 39 | * <em>This module, both source code and documentation, is in the |
aoqi@0 | 40 | * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> |
aoqi@0 | 41 | * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> |
aoqi@0 | 42 | * for further information. |
aoqi@0 | 43 | * </blockquote> |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * <p>This class provides a default implementation of the SAX2 |
aoqi@0 | 46 | * {@link org.xml.sax.Attributes Attributes} interface, with the |
aoqi@0 | 47 | * addition of manipulators so that the list can be modified or |
aoqi@0 | 48 | * reused.</p> |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * <p>There are two typical uses of this class:</p> |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * <ol> |
aoqi@0 | 53 | * <li>to take a persistent snapshot of an Attributes object |
aoqi@0 | 54 | * in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li> |
aoqi@0 | 55 | * <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li> |
aoqi@0 | 56 | * </ol> |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * <p>This class replaces the now-deprecated SAX1 {@link |
aoqi@0 | 59 | * org.xml.sax.helpers.AttributeListImpl AttributeListImpl} |
aoqi@0 | 60 | * class; in addition to supporting the updated Attributes |
aoqi@0 | 61 | * interface rather than the deprecated {@link org.xml.sax.AttributeList |
aoqi@0 | 62 | * AttributeList} interface, it also includes a much more efficient |
aoqi@0 | 63 | * implementation using a single array rather than a set of Vectors.</p> |
aoqi@0 | 64 | * |
aoqi@0 | 65 | * @since JAXB1.0 |
aoqi@0 | 66 | * @since SAX 2.0 |
aoqi@0 | 67 | * @author David Megginson |
aoqi@0 | 68 | * @version 2.0.1 (sax2r2) |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | public class AttributesImpl implements Attributes |
aoqi@0 | 71 | { |
aoqi@0 | 72 | |
aoqi@0 | 73 | |
aoqi@0 | 74 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 75 | // Constructors. |
aoqi@0 | 76 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 77 | |
aoqi@0 | 78 | |
aoqi@0 | 79 | /** |
aoqi@0 | 80 | * Construct a new, empty AttributesImpl object. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public AttributesImpl () |
aoqi@0 | 83 | { |
aoqi@0 | 84 | length = 0; |
aoqi@0 | 85 | data = null; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Copy an existing Attributes object. |
aoqi@0 | 91 | * |
aoqi@0 | 92 | * <p>This constructor is especially useful inside a |
aoqi@0 | 93 | * {@link org.xml.sax.ContentHandler#startElement startElement} event.</p> |
aoqi@0 | 94 | * |
aoqi@0 | 95 | * @param atts The existing Attributes object. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | public AttributesImpl (Attributes atts) |
aoqi@0 | 98 | { |
aoqi@0 | 99 | setAttributes(atts); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | |
aoqi@0 | 103 | |
aoqi@0 | 104 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 105 | // Implementation of org.xml.sax.Attributes. |
aoqi@0 | 106 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 107 | |
aoqi@0 | 108 | |
aoqi@0 | 109 | /** |
aoqi@0 | 110 | * Return the number of attributes in the list. |
aoqi@0 | 111 | * |
aoqi@0 | 112 | * @return The number of attributes in the list. |
aoqi@0 | 113 | * @see org.xml.sax.Attributes#getLength() |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | public int getLength () |
aoqi@0 | 116 | { |
aoqi@0 | 117 | return length; |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Return an attribute's Namespace URI. |
aoqi@0 | 123 | * |
aoqi@0 | 124 | * @param index The attribute's index (zero-based). |
aoqi@0 | 125 | * @return The Namespace URI, the empty string if none is |
aoqi@0 | 126 | * available, or null if the index is out of range. |
aoqi@0 | 127 | * @see org.xml.sax.Attributes#getURI(int) |
aoqi@0 | 128 | */ |
aoqi@0 | 129 | public String getURI (int index) |
aoqi@0 | 130 | { |
aoqi@0 | 131 | if (index >= 0 && index < length) { |
aoqi@0 | 132 | return data[index*5]; |
aoqi@0 | 133 | } else { |
aoqi@0 | 134 | return null; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Return an attribute's local name. |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * @param index The attribute's index (zero-based). |
aoqi@0 | 143 | * @return The attribute's local name, the empty string if |
aoqi@0 | 144 | * none is available, or null if the index if out of range. |
aoqi@0 | 145 | * @see org.xml.sax.Attributes#getLocalName(int) |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | public String getLocalName (int index) |
aoqi@0 | 148 | { |
aoqi@0 | 149 | if (index >= 0 && index < length) { |
aoqi@0 | 150 | return data[index*5+1]; |
aoqi@0 | 151 | } else { |
aoqi@0 | 152 | return null; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | |
aoqi@0 | 157 | /** |
aoqi@0 | 158 | * Return an attribute's qualified (prefixed) name. |
aoqi@0 | 159 | * |
aoqi@0 | 160 | * @param index The attribute's index (zero-based). |
aoqi@0 | 161 | * @return The attribute's qualified name, the empty string if |
aoqi@0 | 162 | * none is available, or null if the index is out of bounds. |
aoqi@0 | 163 | * @see org.xml.sax.Attributes#getQName(int) |
aoqi@0 | 164 | */ |
aoqi@0 | 165 | public String getQName (int index) |
aoqi@0 | 166 | { |
aoqi@0 | 167 | if (index >= 0 && index < length) { |
aoqi@0 | 168 | return data[index*5+2]; |
aoqi@0 | 169 | } else { |
aoqi@0 | 170 | return null; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * Return an attribute's type by index. |
aoqi@0 | 177 | * |
aoqi@0 | 178 | * @param index The attribute's index (zero-based). |
aoqi@0 | 179 | * @return The attribute's type, "CDATA" if the type is unknown, or null |
aoqi@0 | 180 | * if the index is out of bounds. |
aoqi@0 | 181 | * @see org.xml.sax.Attributes#getType(int) |
aoqi@0 | 182 | */ |
aoqi@0 | 183 | public String getType (int index) |
aoqi@0 | 184 | { |
aoqi@0 | 185 | if (index >= 0 && index < length) { |
aoqi@0 | 186 | return data[index*5+3]; |
aoqi@0 | 187 | } else { |
aoqi@0 | 188 | return null; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | |
aoqi@0 | 193 | /** |
aoqi@0 | 194 | * Return an attribute's value by index. |
aoqi@0 | 195 | * |
aoqi@0 | 196 | * @param index The attribute's index (zero-based). |
aoqi@0 | 197 | * @return The attribute's value or null if the index is out of bounds. |
aoqi@0 | 198 | * @see org.xml.sax.Attributes#getValue(int) |
aoqi@0 | 199 | */ |
aoqi@0 | 200 | public String getValue (int index) |
aoqi@0 | 201 | { |
aoqi@0 | 202 | if (index >= 0 && index < length) { |
aoqi@0 | 203 | return data[index*5+4]; |
aoqi@0 | 204 | } else { |
aoqi@0 | 205 | return null; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | |
aoqi@0 | 210 | /** |
aoqi@0 | 211 | * Look up an attribute's index by Namespace name. |
aoqi@0 | 212 | * |
aoqi@0 | 213 | * <p>In many cases, it will be more efficient to look up the name once and |
aoqi@0 | 214 | * use the index query methods rather than using the name query methods |
aoqi@0 | 215 | * repeatedly.</p> |
aoqi@0 | 216 | * |
aoqi@0 | 217 | * @param uri The attribute's Namespace URI, or the empty |
aoqi@0 | 218 | * string if none is available. |
aoqi@0 | 219 | * @param localName The attribute's local name. |
aoqi@0 | 220 | * @return The attribute's index, or -1 if none matches. |
aoqi@0 | 221 | * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String) |
aoqi@0 | 222 | */ |
aoqi@0 | 223 | public int getIndex (String uri, String localName) |
aoqi@0 | 224 | { |
aoqi@0 | 225 | int max = length * 5; |
aoqi@0 | 226 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 227 | if (data[i].equals(uri) && data[i+1].equals(localName)) { |
aoqi@0 | 228 | return i / 5; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | } |
aoqi@0 | 231 | return -1; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | /** |
aoqi@0 | 235 | * Can be used if parameters are interned. |
aoqi@0 | 236 | */ |
aoqi@0 | 237 | public int getIndexFast(String uri, String localName) { |
aoqi@0 | 238 | for (int i = (length-1)*5; i>=0; i-=5) { |
aoqi@0 | 239 | // local names tend to be different, so test it first |
aoqi@0 | 240 | if (data[i + 1] == localName && data[i] == uri ) { |
aoqi@0 | 241 | return i / 5; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | } |
aoqi@0 | 244 | return -1; |
aoqi@0 | 245 | } |
aoqi@0 | 246 | |
aoqi@0 | 247 | |
aoqi@0 | 248 | /** |
aoqi@0 | 249 | * Look up an attribute's index by qualified (prefixed) name. |
aoqi@0 | 250 | * |
aoqi@0 | 251 | * @param qName The qualified name. |
aoqi@0 | 252 | * @return The attribute's index, or -1 if none matches. |
aoqi@0 | 253 | * @see org.xml.sax.Attributes#getIndex(java.lang.String) |
aoqi@0 | 254 | */ |
aoqi@0 | 255 | public int getIndex (String qName) |
aoqi@0 | 256 | { |
aoqi@0 | 257 | int max = length * 5; |
aoqi@0 | 258 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 259 | if (data[i+2].equals(qName)) { |
aoqi@0 | 260 | return i / 5; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |
aoqi@0 | 263 | return -1; |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | |
aoqi@0 | 267 | /** |
aoqi@0 | 268 | * Look up an attribute's type by Namespace-qualified name. |
aoqi@0 | 269 | * |
aoqi@0 | 270 | * @param uri The Namespace URI, or the empty string for a name |
aoqi@0 | 271 | * with no explicit Namespace URI. |
aoqi@0 | 272 | * @param localName The local name. |
aoqi@0 | 273 | * @return The attribute's type, or null if there is no |
aoqi@0 | 274 | * matching attribute. |
aoqi@0 | 275 | * @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String) |
aoqi@0 | 276 | */ |
aoqi@0 | 277 | public String getType (String uri, String localName) |
aoqi@0 | 278 | { |
aoqi@0 | 279 | int max = length * 5; |
aoqi@0 | 280 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 281 | if (data[i].equals(uri) && data[i+1].equals(localName)) { |
aoqi@0 | 282 | return data[i+3]; |
aoqi@0 | 283 | } |
aoqi@0 | 284 | } |
aoqi@0 | 285 | return null; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | |
aoqi@0 | 288 | |
aoqi@0 | 289 | /** |
aoqi@0 | 290 | * Look up an attribute's type by qualified (prefixed) name. |
aoqi@0 | 291 | * |
aoqi@0 | 292 | * @param qName The qualified name. |
aoqi@0 | 293 | * @return The attribute's type, or null if there is no |
aoqi@0 | 294 | * matching attribute. |
aoqi@0 | 295 | * @see org.xml.sax.Attributes#getType(java.lang.String) |
aoqi@0 | 296 | */ |
aoqi@0 | 297 | public String getType (String qName) |
aoqi@0 | 298 | { |
aoqi@0 | 299 | int max = length * 5; |
aoqi@0 | 300 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 301 | if (data[i+2].equals(qName)) { |
aoqi@0 | 302 | return data[i+3]; |
aoqi@0 | 303 | } |
aoqi@0 | 304 | } |
aoqi@0 | 305 | return null; |
aoqi@0 | 306 | } |
aoqi@0 | 307 | |
aoqi@0 | 308 | |
aoqi@0 | 309 | /** |
aoqi@0 | 310 | * Look up an attribute's value by Namespace-qualified name. |
aoqi@0 | 311 | * |
aoqi@0 | 312 | * @param uri The Namespace URI, or the empty string for a name |
aoqi@0 | 313 | * with no explicit Namespace URI. |
aoqi@0 | 314 | * @param localName The local name. |
aoqi@0 | 315 | * @return The attribute's value, or null if there is no |
aoqi@0 | 316 | * matching attribute. |
aoqi@0 | 317 | * @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String) |
aoqi@0 | 318 | */ |
aoqi@0 | 319 | public String getValue (String uri, String localName) |
aoqi@0 | 320 | { |
aoqi@0 | 321 | int max = length * 5; |
aoqi@0 | 322 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 323 | if (data[i].equals(uri) && data[i+1].equals(localName)) { |
aoqi@0 | 324 | return data[i+4]; |
aoqi@0 | 325 | } |
aoqi@0 | 326 | } |
aoqi@0 | 327 | return null; |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | |
aoqi@0 | 331 | /** |
aoqi@0 | 332 | * Look up an attribute's value by qualified (prefixed) name. |
aoqi@0 | 333 | * |
aoqi@0 | 334 | * @param qName The qualified name. |
aoqi@0 | 335 | * @return The attribute's value, or null if there is no |
aoqi@0 | 336 | * matching attribute. |
aoqi@0 | 337 | * @see org.xml.sax.Attributes#getValue(java.lang.String) |
aoqi@0 | 338 | */ |
aoqi@0 | 339 | public String getValue (String qName) |
aoqi@0 | 340 | { |
aoqi@0 | 341 | int max = length * 5; |
aoqi@0 | 342 | for (int i = 0; i < max; i += 5) { |
aoqi@0 | 343 | if (data[i+2].equals(qName)) { |
aoqi@0 | 344 | return data[i+4]; |
aoqi@0 | 345 | } |
aoqi@0 | 346 | } |
aoqi@0 | 347 | return null; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | |
aoqi@0 | 350 | |
aoqi@0 | 351 | |
aoqi@0 | 352 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 353 | // Manipulators. |
aoqi@0 | 354 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 355 | |
aoqi@0 | 356 | |
aoqi@0 | 357 | /** |
aoqi@0 | 358 | * Clear the attribute list for reuse. |
aoqi@0 | 359 | * |
aoqi@0 | 360 | * <p>Note that little memory is freed by this call: |
aoqi@0 | 361 | * the current array is kept so it can be |
aoqi@0 | 362 | * reused.</p> |
aoqi@0 | 363 | */ |
aoqi@0 | 364 | public void clear () |
aoqi@0 | 365 | { |
aoqi@0 | 366 | if (data != null) { |
aoqi@0 | 367 | for (int i = 0; i < (length * 5); i++) |
aoqi@0 | 368 | data [i] = null; |
aoqi@0 | 369 | } |
aoqi@0 | 370 | length = 0; |
aoqi@0 | 371 | } |
aoqi@0 | 372 | |
aoqi@0 | 373 | |
aoqi@0 | 374 | /** |
aoqi@0 | 375 | * Copy an entire Attributes object. |
aoqi@0 | 376 | * |
aoqi@0 | 377 | * <p>It may be more efficient to reuse an existing object |
aoqi@0 | 378 | * rather than constantly allocating new ones.</p> |
aoqi@0 | 379 | * |
aoqi@0 | 380 | * @param atts The attributes to copy. |
aoqi@0 | 381 | */ |
aoqi@0 | 382 | public void setAttributes (Attributes atts) |
aoqi@0 | 383 | { |
aoqi@0 | 384 | clear(); |
aoqi@0 | 385 | length = atts.getLength(); |
aoqi@0 | 386 | if (length > 0) { |
aoqi@0 | 387 | data = new String[length*5]; |
aoqi@0 | 388 | for (int i = 0; i < length; i++) { |
aoqi@0 | 389 | data[i*5] = atts.getURI(i); |
aoqi@0 | 390 | data[i*5+1] = atts.getLocalName(i); |
aoqi@0 | 391 | data[i*5+2] = atts.getQName(i); |
aoqi@0 | 392 | data[i*5+3] = atts.getType(i); |
aoqi@0 | 393 | data[i*5+4] = atts.getValue(i); |
aoqi@0 | 394 | } |
aoqi@0 | 395 | } |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | |
aoqi@0 | 399 | /** |
aoqi@0 | 400 | * Add an attribute to the end of the list. |
aoqi@0 | 401 | * |
aoqi@0 | 402 | * <p>For the sake of speed, this method does no checking |
aoqi@0 | 403 | * to see if the attribute is already in the list: that is |
aoqi@0 | 404 | * the responsibility of the application.</p> |
aoqi@0 | 405 | * |
aoqi@0 | 406 | * @param uri The Namespace URI, or the empty string if |
aoqi@0 | 407 | * none is available or Namespace processing is not |
aoqi@0 | 408 | * being performed. |
aoqi@0 | 409 | * @param localName The local name, or the empty string if |
aoqi@0 | 410 | * Namespace processing is not being performed. |
aoqi@0 | 411 | * @param qName The qualified (prefixed) name, or the empty string |
aoqi@0 | 412 | * if qualified names are not available. |
aoqi@0 | 413 | * @param type The attribute type as a string. |
aoqi@0 | 414 | * @param value The attribute value. |
aoqi@0 | 415 | */ |
aoqi@0 | 416 | public void addAttribute (String uri, String localName, String qName, |
aoqi@0 | 417 | String type, String value) |
aoqi@0 | 418 | { |
aoqi@0 | 419 | ensureCapacity(length+1); |
aoqi@0 | 420 | data[length*5] = uri; |
aoqi@0 | 421 | data[length*5+1] = localName; |
aoqi@0 | 422 | data[length*5+2] = qName; |
aoqi@0 | 423 | data[length*5+3] = type; |
aoqi@0 | 424 | data[length*5+4] = value; |
aoqi@0 | 425 | length++; |
aoqi@0 | 426 | } |
aoqi@0 | 427 | |
aoqi@0 | 428 | |
aoqi@0 | 429 | /** |
aoqi@0 | 430 | * Set an attribute in the list. |
aoqi@0 | 431 | * |
aoqi@0 | 432 | * <p>For the sake of speed, this method does no checking |
aoqi@0 | 433 | * for name conflicts or well-formedness: such checks are the |
aoqi@0 | 434 | * responsibility of the application.</p> |
aoqi@0 | 435 | * |
aoqi@0 | 436 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 437 | * @param uri The Namespace URI, or the empty string if |
aoqi@0 | 438 | * none is available or Namespace processing is not |
aoqi@0 | 439 | * being performed. |
aoqi@0 | 440 | * @param localName The local name, or the empty string if |
aoqi@0 | 441 | * Namespace processing is not being performed. |
aoqi@0 | 442 | * @param qName The qualified name, or the empty string |
aoqi@0 | 443 | * if qualified names are not available. |
aoqi@0 | 444 | * @param type The attribute type as a string. |
aoqi@0 | 445 | * @param value The attribute value. |
aoqi@0 | 446 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 447 | * supplied index does not point to an attribute |
aoqi@0 | 448 | * in the list. |
aoqi@0 | 449 | */ |
aoqi@0 | 450 | public void setAttribute (int index, String uri, String localName, |
aoqi@0 | 451 | String qName, String type, String value) |
aoqi@0 | 452 | { |
aoqi@0 | 453 | if (index >= 0 && index < length) { |
aoqi@0 | 454 | data[index*5] = uri; |
aoqi@0 | 455 | data[index*5+1] = localName; |
aoqi@0 | 456 | data[index*5+2] = qName; |
aoqi@0 | 457 | data[index*5+3] = type; |
aoqi@0 | 458 | data[index*5+4] = value; |
aoqi@0 | 459 | } else { |
aoqi@0 | 460 | badIndex(index); |
aoqi@0 | 461 | } |
aoqi@0 | 462 | } |
aoqi@0 | 463 | |
aoqi@0 | 464 | |
aoqi@0 | 465 | /** |
aoqi@0 | 466 | * Remove an attribute from the list. |
aoqi@0 | 467 | * |
aoqi@0 | 468 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 469 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 470 | * supplied index does not point to an attribute |
aoqi@0 | 471 | * in the list. |
aoqi@0 | 472 | */ |
aoqi@0 | 473 | public void removeAttribute (int index) |
aoqi@0 | 474 | { |
aoqi@0 | 475 | if (index >= 0 && index < length) { |
aoqi@0 | 476 | if (index < length - 1) { |
aoqi@0 | 477 | System.arraycopy(data, (index+1)*5, data, index*5, |
aoqi@0 | 478 | (length-index-1)*5); |
aoqi@0 | 479 | } |
aoqi@0 | 480 | index = (length - 1) * 5; |
aoqi@0 | 481 | data [index++] = null; |
aoqi@0 | 482 | data [index++] = null; |
aoqi@0 | 483 | data [index++] = null; |
aoqi@0 | 484 | data [index++] = null; |
aoqi@0 | 485 | data [index] = null; |
aoqi@0 | 486 | length--; |
aoqi@0 | 487 | } else { |
aoqi@0 | 488 | badIndex(index); |
aoqi@0 | 489 | } |
aoqi@0 | 490 | } |
aoqi@0 | 491 | |
aoqi@0 | 492 | |
aoqi@0 | 493 | /** |
aoqi@0 | 494 | * Set the Namespace URI of a specific attribute. |
aoqi@0 | 495 | * |
aoqi@0 | 496 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 497 | * @param uri The attribute's Namespace URI, or the empty |
aoqi@0 | 498 | * string for none. |
aoqi@0 | 499 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 500 | * supplied index does not point to an attribute |
aoqi@0 | 501 | * in the list. |
aoqi@0 | 502 | */ |
aoqi@0 | 503 | public void setURI (int index, String uri) |
aoqi@0 | 504 | { |
aoqi@0 | 505 | if (index >= 0 && index < length) { |
aoqi@0 | 506 | data[index*5] = uri; |
aoqi@0 | 507 | } else { |
aoqi@0 | 508 | badIndex(index); |
aoqi@0 | 509 | } |
aoqi@0 | 510 | } |
aoqi@0 | 511 | |
aoqi@0 | 512 | |
aoqi@0 | 513 | /** |
aoqi@0 | 514 | * Set the local name of a specific attribute. |
aoqi@0 | 515 | * |
aoqi@0 | 516 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 517 | * @param localName The attribute's local name, or the empty |
aoqi@0 | 518 | * string for none. |
aoqi@0 | 519 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 520 | * supplied index does not point to an attribute |
aoqi@0 | 521 | * in the list. |
aoqi@0 | 522 | */ |
aoqi@0 | 523 | public void setLocalName (int index, String localName) |
aoqi@0 | 524 | { |
aoqi@0 | 525 | if (index >= 0 && index < length) { |
aoqi@0 | 526 | data[index*5+1] = localName; |
aoqi@0 | 527 | } else { |
aoqi@0 | 528 | badIndex(index); |
aoqi@0 | 529 | } |
aoqi@0 | 530 | } |
aoqi@0 | 531 | |
aoqi@0 | 532 | |
aoqi@0 | 533 | /** |
aoqi@0 | 534 | * Set the qualified name of a specific attribute. |
aoqi@0 | 535 | * |
aoqi@0 | 536 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 537 | * @param qName The attribute's qualified name, or the empty |
aoqi@0 | 538 | * string for none. |
aoqi@0 | 539 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 540 | * supplied index does not point to an attribute |
aoqi@0 | 541 | * in the list. |
aoqi@0 | 542 | */ |
aoqi@0 | 543 | public void setQName (int index, String qName) |
aoqi@0 | 544 | { |
aoqi@0 | 545 | if (index >= 0 && index < length) { |
aoqi@0 | 546 | data[index*5+2] = qName; |
aoqi@0 | 547 | } else { |
aoqi@0 | 548 | badIndex(index); |
aoqi@0 | 549 | } |
aoqi@0 | 550 | } |
aoqi@0 | 551 | |
aoqi@0 | 552 | |
aoqi@0 | 553 | /** |
aoqi@0 | 554 | * Set the type of a specific attribute. |
aoqi@0 | 555 | * |
aoqi@0 | 556 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 557 | * @param type The attribute's type. |
aoqi@0 | 558 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 559 | * supplied index does not point to an attribute |
aoqi@0 | 560 | * in the list. |
aoqi@0 | 561 | */ |
aoqi@0 | 562 | public void setType (int index, String type) |
aoqi@0 | 563 | { |
aoqi@0 | 564 | if (index >= 0 && index < length) { |
aoqi@0 | 565 | data[index*5+3] = type; |
aoqi@0 | 566 | } else { |
aoqi@0 | 567 | badIndex(index); |
aoqi@0 | 568 | } |
aoqi@0 | 569 | } |
aoqi@0 | 570 | |
aoqi@0 | 571 | |
aoqi@0 | 572 | /** |
aoqi@0 | 573 | * Set the value of a specific attribute. |
aoqi@0 | 574 | * |
aoqi@0 | 575 | * @param index The index of the attribute (zero-based). |
aoqi@0 | 576 | * @param value The attribute's value. |
aoqi@0 | 577 | * @exception java.lang.ArrayIndexOutOfBoundsException When the |
aoqi@0 | 578 | * supplied index does not point to an attribute |
aoqi@0 | 579 | * in the list. |
aoqi@0 | 580 | */ |
aoqi@0 | 581 | public void setValue (int index, String value) |
aoqi@0 | 582 | { |
aoqi@0 | 583 | if (index >= 0 && index < length) { |
aoqi@0 | 584 | data[index*5+4] = value; |
aoqi@0 | 585 | } else { |
aoqi@0 | 586 | badIndex(index); |
aoqi@0 | 587 | } |
aoqi@0 | 588 | } |
aoqi@0 | 589 | |
aoqi@0 | 590 | |
aoqi@0 | 591 | |
aoqi@0 | 592 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 593 | // Internal methods. |
aoqi@0 | 594 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 595 | |
aoqi@0 | 596 | |
aoqi@0 | 597 | /** |
aoqi@0 | 598 | * Ensure the internal array's capacity. |
aoqi@0 | 599 | * |
aoqi@0 | 600 | * @param n The minimum number of attributes that the array must |
aoqi@0 | 601 | * be able to hold. |
aoqi@0 | 602 | */ |
aoqi@0 | 603 | private void ensureCapacity (int n) { |
aoqi@0 | 604 | if (n <= 0) { |
aoqi@0 | 605 | return; |
aoqi@0 | 606 | } |
aoqi@0 | 607 | int max; |
aoqi@0 | 608 | if (data == null || data.length == 0) { |
aoqi@0 | 609 | max = 25; |
aoqi@0 | 610 | } |
aoqi@0 | 611 | else if (data.length >= n * 5) { |
aoqi@0 | 612 | return; |
aoqi@0 | 613 | } |
aoqi@0 | 614 | else { |
aoqi@0 | 615 | max = data.length; |
aoqi@0 | 616 | } |
aoqi@0 | 617 | while (max < n * 5) { |
aoqi@0 | 618 | max *= 2; |
aoqi@0 | 619 | } |
aoqi@0 | 620 | |
aoqi@0 | 621 | String newData[] = new String[max]; |
aoqi@0 | 622 | if (length > 0) { |
aoqi@0 | 623 | System.arraycopy(data, 0, newData, 0, length*5); |
aoqi@0 | 624 | } |
aoqi@0 | 625 | data = newData; |
aoqi@0 | 626 | } |
aoqi@0 | 627 | |
aoqi@0 | 628 | |
aoqi@0 | 629 | /** |
aoqi@0 | 630 | * Report a bad array index in a manipulator. |
aoqi@0 | 631 | * |
aoqi@0 | 632 | * @param index The index to report. |
aoqi@0 | 633 | * @exception java.lang.ArrayIndexOutOfBoundsException Always. |
aoqi@0 | 634 | */ |
aoqi@0 | 635 | private void badIndex (int index) |
aoqi@0 | 636 | throws ArrayIndexOutOfBoundsException |
aoqi@0 | 637 | { |
aoqi@0 | 638 | String msg = |
aoqi@0 | 639 | "Attempt to modify attribute at illegal index: " + index; |
aoqi@0 | 640 | throw new ArrayIndexOutOfBoundsException(msg); |
aoqi@0 | 641 | } |
aoqi@0 | 642 | |
aoqi@0 | 643 | |
aoqi@0 | 644 | |
aoqi@0 | 645 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 646 | // Internal state. |
aoqi@0 | 647 | //////////////////////////////////////////////////////////////////// |
aoqi@0 | 648 | |
aoqi@0 | 649 | int length; |
aoqi@0 | 650 | String data []; |
aoqi@0 | 651 | |
aoqi@0 | 652 | } |
aoqi@0 | 653 | |
aoqi@0 | 654 | // end of AttributesImpl.java |