aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.stream.buffer; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: aoqi@0: /** aoqi@0: * Class for holding attributes. aoqi@0: * aoqi@0: * Since it implements {@link Attributes}, this class follows the SAX convention aoqi@0: * of using "" instead of null. aoqi@0: */ aoqi@0: @SuppressWarnings({"PointlessArithmeticExpression"}) aoqi@0: public final class AttributesHolder implements Attributes { aoqi@0: private static final int DEFAULT_CAPACITY = 8; aoqi@0: private static final int ITEM_SIZE = 1 << 3; aoqi@0: aoqi@0: private static final int PREFIX = 0; aoqi@0: private static final int URI = 1; aoqi@0: private static final int LOCAL_NAME = 2; aoqi@0: private static final int QNAME = 3; aoqi@0: private static final int TYPE = 4; aoqi@0: private static final int VALUE = 5; aoqi@0: aoqi@0: private int _attributeCount; aoqi@0: aoqi@0: private String[] _strings; aoqi@0: aoqi@0: public AttributesHolder() { aoqi@0: _strings = new String[DEFAULT_CAPACITY * ITEM_SIZE]; aoqi@0: } aoqi@0: aoqi@0: public final int getLength() { aoqi@0: return _attributeCount; aoqi@0: } aoqi@0: aoqi@0: public final String getPrefix(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + PREFIX] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getLocalName(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + LOCAL_NAME] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getQName(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + QNAME] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getType(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + TYPE] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getURI(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + URI] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getValue(int index) { aoqi@0: return (index >= 0 && index < _attributeCount) ? aoqi@0: _strings[(index << 3) + VALUE] : null; aoqi@0: } aoqi@0: aoqi@0: public final int getIndex(String qName) { aoqi@0: for (int i = 0; i < _attributeCount; i++) { aoqi@0: if (qName.equals(_strings[(i << 3) + QNAME])) { aoqi@0: return i; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: public final String getType(String qName) { aoqi@0: final int i = (getIndex(qName) << 3) + TYPE; aoqi@0: return (i >= 0) ? _strings[i] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getValue(String qName) { aoqi@0: final int i = (getIndex(qName) << 3) + VALUE; aoqi@0: return (i >= 0) ? _strings[i] : null; aoqi@0: } aoqi@0: aoqi@0: public final int getIndex(String uri, String localName) { aoqi@0: for (int i = 0; i < _attributeCount; i++) { aoqi@0: if (localName.equals(_strings[(i << 3) + LOCAL_NAME]) && aoqi@0: uri.equals(_strings[(i << 3) + URI])) { aoqi@0: return i; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: public final String getType(String uri, String localName) { aoqi@0: final int i = (getIndex(uri, localName) << 3) + TYPE; aoqi@0: return (i >= 0) ? _strings[i] : null; aoqi@0: } aoqi@0: aoqi@0: public final String getValue(String uri, String localName) { aoqi@0: final int i = (getIndex(uri, localName) << 3) + VALUE; aoqi@0: return (i >= 0) ? _strings[i] : null; aoqi@0: } aoqi@0: aoqi@0: public final void clear() { aoqi@0: if (_attributeCount > 0) { aoqi@0: for (int i = 0; i < _attributeCount; i++) { aoqi@0: _strings[(i << 3) + VALUE] = null; aoqi@0: } aoqi@0: _attributeCount = 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Add an attribute using a qualified name that contains the aoqi@0: * prefix and local name. aoqi@0: * aoqi@0: * @param uri aoqi@0: * This can be empty but not null, just like everywhere else in SAX. aoqi@0: */ aoqi@0: public final void addAttributeWithQName(String uri, String localName, String qName, String type, String value) { aoqi@0: final int i = _attributeCount << 3; aoqi@0: if (i == _strings.length) { aoqi@0: resize(i); aoqi@0: } aoqi@0: aoqi@0: _strings[i + PREFIX] = null; aoqi@0: _strings[i + URI] = uri; aoqi@0: _strings[i + LOCAL_NAME] = localName; aoqi@0: _strings[i + QNAME] = qName; aoqi@0: _strings[i + TYPE] = type; aoqi@0: _strings[i + VALUE] = value; aoqi@0: aoqi@0: _attributeCount++; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Add an attribute using a prefix. aoqi@0: * aoqi@0: * @param prefix aoqi@0: * This can be empty but not null, just like everywhere else in SAX. aoqi@0: * @param uri aoqi@0: * This can be empty but not null, just like everywhere else in SAX. aoqi@0: */ aoqi@0: public final void addAttributeWithPrefix(String prefix, String uri, String localName, String type, String value) { aoqi@0: final int i = _attributeCount << 3; aoqi@0: if (i == _strings.length) { aoqi@0: resize(i); aoqi@0: } aoqi@0: aoqi@0: _strings[i + PREFIX] = prefix; aoqi@0: _strings[i + URI] = uri; aoqi@0: _strings[i + LOCAL_NAME] = localName; aoqi@0: _strings[i + QNAME] = null; aoqi@0: _strings[i + TYPE] = type; aoqi@0: _strings[i + VALUE] = value; aoqi@0: aoqi@0: _attributeCount++; aoqi@0: } aoqi@0: aoqi@0: private void resize(int length) { aoqi@0: final int newLength = length * 2; aoqi@0: final String[] strings = new String[newLength]; aoqi@0: System.arraycopy(_strings, 0, strings, 0, length); aoqi@0: _strings = strings; aoqi@0: } aoqi@0: aoqi@0: }