1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/gen/config/AttributesImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,626 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +// AttributesImpl.java - default implementation of Attributes. 1.30 +// Written by David Megginson, sax@megginson.com 1.31 +// NO WARRANTY! This class is in the public domain. 1.32 + 1.33 +// $Id: AttributesImpl.java,v 1.4 2002/09/29 02:55:48 okajima Exp $ 1.34 + 1.35 +//fixed bug at removeAttribute!! by Daisuke OKAJIMA 2002.4.21 1.36 + 1.37 +package com.sun.tools.internal.jxc.gen.config; 1.38 + 1.39 +import org.xml.sax.Attributes; 1.40 + 1.41 + 1.42 +/** 1.43 + * Default implementation of the Attributes interface. 1.44 + * 1.45 + * <blockquote> 1.46 + * <em>This module, both source code and documentation, is in the 1.47 + * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> 1.48 + * </blockquote> 1.49 + * 1.50 + * <p>This class provides a default implementation of the SAX2 1.51 + * {@link org.xml.sax.Attributes Attributes} interface, with the 1.52 + * addition of manipulators so that the list can be modified or 1.53 + * reused.</p> 1.54 + * 1.55 + * <p>There are two typical uses of this class:</p> 1.56 + * 1.57 + * <ol> 1.58 + * <li>to take a persistent snapshot of an Attributes object 1.59 + * in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li> 1.60 + * <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li> 1.61 + * </ol> 1.62 + * 1.63 + * <p>This class replaces the now-deprecated SAX1 {@link 1.64 + * org.xml.sax.helpers.AttributeListImpl AttributeListImpl} 1.65 + * class; in addition to supporting the updated Attributes 1.66 + * interface rather than the deprecated {@link org.xml.sax.AttributeList 1.67 + * AttributeList} interface, it also includes a much more efficient 1.68 + * implementation using a single array rather than a set of Vectors.</p> 1.69 + * 1.70 + * @since SAX 2.0 1.71 + * @author David Megginson, 1.72 + * <a href="mailto:sax@megginson.com">sax@megginson.com</a> 1.73 + * @version 2.0 1.74 + */ 1.75 +public class AttributesImpl implements Attributes 1.76 +{ 1.77 + 1.78 + 1.79 + //////////////////////////////////////////////////////////////////// 1.80 + // Constructors. 1.81 + //////////////////////////////////////////////////////////////////// 1.82 + 1.83 + 1.84 + /** 1.85 + * Construct a new, empty AttributesImpl object. 1.86 + */ 1.87 + public AttributesImpl () 1.88 + { 1.89 + length = 0; 1.90 + data = null; 1.91 + } 1.92 + 1.93 + 1.94 + /** 1.95 + * Copy an existing Attributes object. 1.96 + * 1.97 + * <p>This constructor is especially useful inside a 1.98 + * {@link org.xml.sax.ContentHandler#startElement startElement} event.</p> 1.99 + * 1.100 + * @param atts The existing Attributes object. 1.101 + */ 1.102 + public AttributesImpl (Attributes atts) 1.103 + { 1.104 + setAttributes(atts); 1.105 + } 1.106 + 1.107 + 1.108 + 1.109 + //////////////////////////////////////////////////////////////////// 1.110 + // Implementation of org.xml.sax.Attributes. 1.111 + //////////////////////////////////////////////////////////////////// 1.112 + 1.113 + 1.114 + /** 1.115 + * Return the number of attributes in the list. 1.116 + * 1.117 + * @return The number of attributes in the list. 1.118 + * @see org.xml.sax.Attributes#getLength 1.119 + */ 1.120 + public int getLength () 1.121 + { 1.122 + return length; 1.123 + } 1.124 + 1.125 + 1.126 + /** 1.127 + * Return an attribute's Namespace URI. 1.128 + * 1.129 + * @param index The attribute's index (zero-based). 1.130 + * @return The Namespace URI, the empty string if none is 1.131 + * available, or null if the index is out of range. 1.132 + * @see org.xml.sax.Attributes#getURI 1.133 + */ 1.134 + public String getURI (int index) 1.135 + { 1.136 + if (index >= 0 && index < length) { 1.137 + return data[index*5]; 1.138 + } else { 1.139 + return null; 1.140 + } 1.141 + } 1.142 + 1.143 + 1.144 + /** 1.145 + * Return an attribute's local name. 1.146 + * 1.147 + * @param index The attribute's index (zero-based). 1.148 + * @return The attribute's local name, the empty string if 1.149 + * none is available, or null if the index if out of range. 1.150 + * @see org.xml.sax.Attributes#getLocalName 1.151 + */ 1.152 + public String getLocalName (int index) 1.153 + { 1.154 + if (index >= 0 && index < length) { 1.155 + return data[index*5+1]; 1.156 + } else { 1.157 + return null; 1.158 + } 1.159 + } 1.160 + 1.161 + 1.162 + /** 1.163 + * Return an attribute's qualified (prefixed) name. 1.164 + * 1.165 + * @param index The attribute's index (zero-based). 1.166 + * @return The attribute's qualified name, the empty string if 1.167 + * none is available, or null if the index is out of bounds. 1.168 + * @see org.xml.sax.Attributes#getQName 1.169 + */ 1.170 + public String getQName (int index) 1.171 + { 1.172 + if (index >= 0 && index < length) { 1.173 + return data[index*5+2]; 1.174 + } else { 1.175 + return null; 1.176 + } 1.177 + } 1.178 + 1.179 + 1.180 + /** 1.181 + * Return an attribute's type by index. 1.182 + * 1.183 + * @param index The attribute's index (zero-based). 1.184 + * @return The attribute's type, "CDATA" if the type is unknown, or null 1.185 + * if the index is out of bounds. 1.186 + * @see org.xml.sax.Attributes#getType(int) 1.187 + */ 1.188 + public String getType (int index) 1.189 + { 1.190 + if (index >= 0 && index < length) { 1.191 + return data[index*5+3]; 1.192 + } else { 1.193 + return null; 1.194 + } 1.195 + } 1.196 + 1.197 + 1.198 + /** 1.199 + * Return an attribute's value by index. 1.200 + * 1.201 + * @param index The attribute's index (zero-based). 1.202 + * @return The attribute's value or null if the index is out of bounds. 1.203 + * @see org.xml.sax.Attributes#getValue(int) 1.204 + */ 1.205 + public String getValue (int index) 1.206 + { 1.207 + if (index >= 0 && index < length) { 1.208 + return data[index*5+4]; 1.209 + } else { 1.210 + return null; 1.211 + } 1.212 + } 1.213 + 1.214 + 1.215 + /** 1.216 + * Look up an attribute's index by Namespace name. 1.217 + * 1.218 + * <p>In many cases, it will be more efficient to look up the name once and 1.219 + * use the index query methods rather than using the name query methods 1.220 + * repeatedly.</p> 1.221 + * 1.222 + * @param uri The attribute's Namespace URI, or the empty 1.223 + * string if none is available. 1.224 + * @param localName The attribute's local name. 1.225 + * @return The attribute's index, or -1 if none matches. 1.226 + * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String) 1.227 + */ 1.228 + public int getIndex (String uri, String localName) 1.229 + { 1.230 + int max = length * 5; 1.231 + for (int i = 0; i < max; i += 5) { 1.232 + if (data[i].equals(uri) && data[i+1].equals(localName)) { 1.233 + return i / 5; 1.234 + } 1.235 + } 1.236 + return -1; 1.237 + } 1.238 + 1.239 + 1.240 + /** 1.241 + * Look up an attribute's index by qualified (prefixed) name. 1.242 + * 1.243 + * @param qName The qualified name. 1.244 + * @return The attribute's index, or -1 if none matches. 1.245 + * @see org.xml.sax.Attributes#getIndex(java.lang.String) 1.246 + */ 1.247 + public int getIndex (String qName) 1.248 + { 1.249 + int max = length * 5; 1.250 + for (int i = 0; i < max; i += 5) { 1.251 + if (data[i+2].equals(qName)) { 1.252 + return i / 5; 1.253 + } 1.254 + } 1.255 + return -1; 1.256 + } 1.257 + 1.258 + 1.259 + /** 1.260 + * Look up an attribute's type by Namespace-qualified name. 1.261 + * 1.262 + * @param uri The Namespace URI, or the empty string for a name 1.263 + * with no explicit Namespace URI. 1.264 + * @param localName The local name. 1.265 + * @return The attribute's type, or null if there is no 1.266 + * matching attribute. 1.267 + * @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String) 1.268 + */ 1.269 + public String getType (String uri, String localName) 1.270 + { 1.271 + int max = length * 5; 1.272 + for (int i = 0; i < max; i += 5) { 1.273 + if (data[i].equals(uri) && data[i+1].equals(localName)) { 1.274 + return data[i+3]; 1.275 + } 1.276 + } 1.277 + return null; 1.278 + } 1.279 + 1.280 + 1.281 + /** 1.282 + * Look up an attribute's type by qualified (prefixed) name. 1.283 + * 1.284 + * @param qName The qualified name. 1.285 + * @return The attribute's type, or null if there is no 1.286 + * matching attribute. 1.287 + * @see org.xml.sax.Attributes#getType(java.lang.String) 1.288 + */ 1.289 + public String getType (String qName) 1.290 + { 1.291 + int max = length * 5; 1.292 + for (int i = 0; i < max; i += 5) { 1.293 + if (data[i+2].equals(qName)) { 1.294 + return data[i+3]; 1.295 + } 1.296 + } 1.297 + return null; 1.298 + } 1.299 + 1.300 + 1.301 + /** 1.302 + * Look up an attribute's value by Namespace-qualified name. 1.303 + * 1.304 + * @param uri The Namespace URI, or the empty string for a name 1.305 + * with no explicit Namespace URI. 1.306 + * @param localName The local name. 1.307 + * @return The attribute's value, or null if there is no 1.308 + * matching attribute. 1.309 + * @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String) 1.310 + */ 1.311 + public String getValue (String uri, String localName) 1.312 + { 1.313 + int max = length * 5; 1.314 + for (int i = 0; i < max; i += 5) { 1.315 + if (data[i].equals(uri) && data[i+1].equals(localName)) { 1.316 + return data[i+4]; 1.317 + } 1.318 + } 1.319 + return null; 1.320 + } 1.321 + 1.322 + 1.323 + /** 1.324 + * Look up an attribute's value by qualified (prefixed) name. 1.325 + * 1.326 + * @param qName The qualified name. 1.327 + * @return The attribute's value, or null if there is no 1.328 + * matching attribute. 1.329 + * @see org.xml.sax.Attributes#getValue(java.lang.String) 1.330 + */ 1.331 + public String getValue (String qName) 1.332 + { 1.333 + int max = length * 5; 1.334 + for (int i = 0; i < max; i += 5) { 1.335 + if (data[i+2].equals(qName)) { 1.336 + return data[i+4]; 1.337 + } 1.338 + } 1.339 + return null; 1.340 + } 1.341 + 1.342 + 1.343 + 1.344 + //////////////////////////////////////////////////////////////////// 1.345 + // Manipulators. 1.346 + //////////////////////////////////////////////////////////////////// 1.347 + 1.348 + 1.349 + /** 1.350 + * Clear the attribute list for reuse. 1.351 + * 1.352 + * <p>Note that no memory is actually freed by this call: 1.353 + * the current arrays are kept so that they can be 1.354 + * reused.</p> 1.355 + */ 1.356 + public void clear () 1.357 + { 1.358 + length = 0; 1.359 + } 1.360 + 1.361 + 1.362 + /** 1.363 + * Copy an entire Attributes object. 1.364 + * 1.365 + * <p>It may be more efficient to reuse an existing object 1.366 + * rather than constantly allocating new ones.</p> 1.367 + * 1.368 + * @param atts The attributes to copy. 1.369 + */ 1.370 + public void setAttributes (Attributes atts) 1.371 + { 1.372 + clear(); 1.373 + length = atts.getLength(); 1.374 + data = new String[length*5]; 1.375 + for (int i = 0; i < length; i++) { 1.376 + data[i*5] = atts.getURI(i); 1.377 + data[i*5+1] = atts.getLocalName(i); 1.378 + data[i*5+2] = atts.getQName(i); 1.379 + data[i*5+3] = atts.getType(i); 1.380 + data[i*5+4] = atts.getValue(i); 1.381 + } 1.382 + } 1.383 + 1.384 + 1.385 + /** 1.386 + * Add an attribute to the end of the list. 1.387 + * 1.388 + * <p>For the sake of speed, this method does no checking 1.389 + * to see if the attribute is already in the list: that is 1.390 + * the responsibility of the application.</p> 1.391 + * 1.392 + * @param uri The Namespace URI, or the empty string if 1.393 + * none is available or Namespace processing is not 1.394 + * being performed. 1.395 + * @param localName The local name, or the empty string if 1.396 + * Namespace processing is not being performed. 1.397 + * @param qName The qualified (prefixed) name, or the empty string 1.398 + * if qualified names are not available. 1.399 + * @param type The attribute type as a string. 1.400 + * @param value The attribute value. 1.401 + */ 1.402 + public void addAttribute (String uri, String localName, String qName, 1.403 + String type, String value) 1.404 + { 1.405 + ensureCapacity(length+1); 1.406 + data[length*5] = uri; 1.407 + data[length*5+1] = localName; 1.408 + data[length*5+2] = qName; 1.409 + data[length*5+3] = type; 1.410 + data[length*5+4] = value; 1.411 + length++; 1.412 + } 1.413 + 1.414 + 1.415 + /** 1.416 + * Set an attribute in the list. 1.417 + * 1.418 + * <p>For the sake of speed, this method does no checking 1.419 + * for name conflicts or well-formedness: such checks are the 1.420 + * responsibility of the application.</p> 1.421 + * 1.422 + * @param index The index of the attribute (zero-based). 1.423 + * @param uri The Namespace URI, or the empty string if 1.424 + * none is available or Namespace processing is not 1.425 + * being performed. 1.426 + * @param localName The local name, or the empty string if 1.427 + * Namespace processing is not being performed. 1.428 + * @param qName The qualified name, or the empty string 1.429 + * if qualified names are not available. 1.430 + * @param type The attribute type as a string. 1.431 + * @param value The attribute value. 1.432 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.433 + * supplied index does not point to an attribute 1.434 + * in the list. 1.435 + */ 1.436 + public void setAttribute (int index, String uri, String localName, 1.437 + String qName, String type, String value) 1.438 + { 1.439 + if (index >= 0 && index < length) { 1.440 + data[index*5] = uri; 1.441 + data[index*5+1] = localName; 1.442 + data[index*5+2] = qName; 1.443 + data[index*5+3] = type; 1.444 + data[index*5+4] = value; 1.445 + } else { 1.446 + badIndex(index); 1.447 + } 1.448 + } 1.449 + 1.450 + 1.451 + /** 1.452 + * Remove an attribute from the list. 1.453 + * 1.454 + * @param index The index of the attribute (zero-based). 1.455 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.456 + * supplied index does not point to an attribute 1.457 + * in the list. 1.458 + */ 1.459 + public void removeAttribute (int index) 1.460 + { 1.461 + if (index >= 0 && index < length) { 1.462 + if (index < length - 1) { 1.463 + System.arraycopy(data, (index+1)*5, data, index*5, 1.464 + (length-index-1)*5); 1.465 + } 1.466 + length--; 1.467 + } else { 1.468 + badIndex(index); 1.469 + } 1.470 + } 1.471 + 1.472 + 1.473 + /** 1.474 + * Set the Namespace URI of a specific attribute. 1.475 + * 1.476 + * @param index The index of the attribute (zero-based). 1.477 + * @param uri The attribute's Namespace URI, or the empty 1.478 + * string for none. 1.479 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.480 + * supplied index does not point to an attribute 1.481 + * in the list. 1.482 + */ 1.483 + public void setURI (int index, String uri) 1.484 + { 1.485 + if (index >= 0 && index < length) { 1.486 + data[index*5] = uri; 1.487 + } else { 1.488 + badIndex(index); 1.489 + } 1.490 + } 1.491 + 1.492 + 1.493 + /** 1.494 + * Set the local name of a specific attribute. 1.495 + * 1.496 + * @param index The index of the attribute (zero-based). 1.497 + * @param localName The attribute's local name, or the empty 1.498 + * string for none. 1.499 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.500 + * supplied index does not point to an attribute 1.501 + * in the list. 1.502 + */ 1.503 + public void setLocalName (int index, String localName) 1.504 + { 1.505 + if (index >= 0 && index < length) { 1.506 + data[index*5+1] = localName; 1.507 + } else { 1.508 + badIndex(index); 1.509 + } 1.510 + } 1.511 + 1.512 + 1.513 + /** 1.514 + * Set the qualified name of a specific attribute. 1.515 + * 1.516 + * @param index The index of the attribute (zero-based). 1.517 + * @param qName The attribute's qualified name, or the empty 1.518 + * string for none. 1.519 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.520 + * supplied index does not point to an attribute 1.521 + * in the list. 1.522 + */ 1.523 + public void setQName (int index, String qName) 1.524 + { 1.525 + if (index >= 0 && index < length) { 1.526 + data[index*5+2] = qName; 1.527 + } else { 1.528 + badIndex(index); 1.529 + } 1.530 + } 1.531 + 1.532 + 1.533 + /** 1.534 + * Set the type of a specific attribute. 1.535 + * 1.536 + * @param index The index of the attribute (zero-based). 1.537 + * @param type The attribute's type. 1.538 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.539 + * supplied index does not point to an attribute 1.540 + * in the list. 1.541 + */ 1.542 + public void setType (int index, String type) 1.543 + { 1.544 + if (index >= 0 && index < length) { 1.545 + data[index*5+3] = type; 1.546 + } else { 1.547 + badIndex(index); 1.548 + } 1.549 + } 1.550 + 1.551 + 1.552 + /** 1.553 + * Set the value of a specific attribute. 1.554 + * 1.555 + * @param index The index of the attribute (zero-based). 1.556 + * @param value The attribute's value. 1.557 + * @exception java.lang.ArrayIndexOutOfBoundsException When the 1.558 + * supplied index does not point to an attribute 1.559 + * in the list. 1.560 + */ 1.561 + public void setValue (int index, String value) 1.562 + { 1.563 + if (index >= 0 && index < length) { 1.564 + data[index*5+4] = value; 1.565 + } else { 1.566 + badIndex(index); 1.567 + } 1.568 + } 1.569 + 1.570 + 1.571 + 1.572 + //////////////////////////////////////////////////////////////////// 1.573 + // Internal methods. 1.574 + //////////////////////////////////////////////////////////////////// 1.575 + 1.576 + 1.577 + /** 1.578 + * Ensure the internal array's capacity. 1.579 + * 1.580 + * @param n The minimum number of attributes that the array must 1.581 + * be able to hold. 1.582 + */ 1.583 + private void ensureCapacity (int n) 1.584 + { 1.585 + if (n > 0 && (data == null || data.length==0)) { 1.586 + data = new String[25]; 1.587 + } 1.588 + 1.589 + int max = data.length; 1.590 + if (max >= n * 5) { 1.591 + return; 1.592 + } 1.593 + 1.594 + 1.595 + while (max < n * 5) { 1.596 + max *= 2; 1.597 + } 1.598 + String newData[] = new String[max]; 1.599 + System.arraycopy(data, 0, newData, 0, length*5); 1.600 + data = newData; 1.601 + } 1.602 + 1.603 + 1.604 + /** 1.605 + * Report a bad array index in a manipulator. 1.606 + * 1.607 + * @param index The index to report. 1.608 + * @exception java.lang.ArrayIndexOutOfBoundsException Always. 1.609 + */ 1.610 + private void badIndex (int index) 1.611 + throws ArrayIndexOutOfBoundsException 1.612 + { 1.613 + String msg = 1.614 + "Attempt to modify attribute at illegal index: " + index; 1.615 + throw new ArrayIndexOutOfBoundsException(msg); 1.616 + } 1.617 + 1.618 + 1.619 + 1.620 + //////////////////////////////////////////////////////////////////// 1.621 + // Internal state. 1.622 + //////////////////////////////////////////////////////////////////// 1.623 + 1.624 + int length; 1.625 + String data []; 1.626 + 1.627 +} 1.628 + 1.629 +// end of AttributesImpl.java