Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2012, 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 | package com.sun.xml.internal.stream.buffer; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import org.xml.sax.Attributes; |
aoqi@0 | 29 | |
aoqi@0 | 30 | /** |
aoqi@0 | 31 | * Class for holding attributes. |
aoqi@0 | 32 | * |
aoqi@0 | 33 | * Since it implements {@link Attributes}, this class follows the SAX convention |
aoqi@0 | 34 | * of using "" instead of null. |
aoqi@0 | 35 | */ |
aoqi@0 | 36 | @SuppressWarnings({"PointlessArithmeticExpression"}) |
aoqi@0 | 37 | public final class AttributesHolder implements Attributes { |
aoqi@0 | 38 | private static final int DEFAULT_CAPACITY = 8; |
aoqi@0 | 39 | private static final int ITEM_SIZE = 1 << 3; |
aoqi@0 | 40 | |
aoqi@0 | 41 | private static final int PREFIX = 0; |
aoqi@0 | 42 | private static final int URI = 1; |
aoqi@0 | 43 | private static final int LOCAL_NAME = 2; |
aoqi@0 | 44 | private static final int QNAME = 3; |
aoqi@0 | 45 | private static final int TYPE = 4; |
aoqi@0 | 46 | private static final int VALUE = 5; |
aoqi@0 | 47 | |
aoqi@0 | 48 | private int _attributeCount; |
aoqi@0 | 49 | |
aoqi@0 | 50 | private String[] _strings; |
aoqi@0 | 51 | |
aoqi@0 | 52 | public AttributesHolder() { |
aoqi@0 | 53 | _strings = new String[DEFAULT_CAPACITY * ITEM_SIZE]; |
aoqi@0 | 54 | } |
aoqi@0 | 55 | |
aoqi@0 | 56 | public final int getLength() { |
aoqi@0 | 57 | return _attributeCount; |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | public final String getPrefix(int index) { |
aoqi@0 | 61 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 62 | _strings[(index << 3) + PREFIX] : null; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | public final String getLocalName(int index) { |
aoqi@0 | 66 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 67 | _strings[(index << 3) + LOCAL_NAME] : null; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | public final String getQName(int index) { |
aoqi@0 | 71 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 72 | _strings[(index << 3) + QNAME] : null; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public final String getType(int index) { |
aoqi@0 | 76 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 77 | _strings[(index << 3) + TYPE] : null; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | public final String getURI(int index) { |
aoqi@0 | 81 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 82 | _strings[(index << 3) + URI] : null; |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | public final String getValue(int index) { |
aoqi@0 | 86 | return (index >= 0 && index < _attributeCount) ? |
aoqi@0 | 87 | _strings[(index << 3) + VALUE] : null; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | public final int getIndex(String qName) { |
aoqi@0 | 91 | for (int i = 0; i < _attributeCount; i++) { |
aoqi@0 | 92 | if (qName.equals(_strings[(i << 3) + QNAME])) { |
aoqi@0 | 93 | return i; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | return -1; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | public final String getType(String qName) { |
aoqi@0 | 100 | final int i = (getIndex(qName) << 3) + TYPE; |
aoqi@0 | 101 | return (i >= 0) ? _strings[i] : null; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | public final String getValue(String qName) { |
aoqi@0 | 105 | final int i = (getIndex(qName) << 3) + VALUE; |
aoqi@0 | 106 | return (i >= 0) ? _strings[i] : null; |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | public final int getIndex(String uri, String localName) { |
aoqi@0 | 110 | for (int i = 0; i < _attributeCount; i++) { |
aoqi@0 | 111 | if (localName.equals(_strings[(i << 3) + LOCAL_NAME]) && |
aoqi@0 | 112 | uri.equals(_strings[(i << 3) + URI])) { |
aoqi@0 | 113 | return i; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | } |
aoqi@0 | 116 | return -1; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | public final String getType(String uri, String localName) { |
aoqi@0 | 120 | final int i = (getIndex(uri, localName) << 3) + TYPE; |
aoqi@0 | 121 | return (i >= 0) ? _strings[i] : null; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | public final String getValue(String uri, String localName) { |
aoqi@0 | 125 | final int i = (getIndex(uri, localName) << 3) + VALUE; |
aoqi@0 | 126 | return (i >= 0) ? _strings[i] : null; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | public final void clear() { |
aoqi@0 | 130 | if (_attributeCount > 0) { |
aoqi@0 | 131 | for (int i = 0; i < _attributeCount; i++) { |
aoqi@0 | 132 | _strings[(i << 3) + VALUE] = null; |
aoqi@0 | 133 | } |
aoqi@0 | 134 | _attributeCount = 0; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Add an attribute using a qualified name that contains the |
aoqi@0 | 141 | * prefix and local name. |
aoqi@0 | 142 | * |
aoqi@0 | 143 | * @param uri |
aoqi@0 | 144 | * This can be empty but not null, just like everywhere else in SAX. |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | public final void addAttributeWithQName(String uri, String localName, String qName, String type, String value) { |
aoqi@0 | 147 | final int i = _attributeCount << 3; |
aoqi@0 | 148 | if (i == _strings.length) { |
aoqi@0 | 149 | resize(i); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | _strings[i + PREFIX] = null; |
aoqi@0 | 153 | _strings[i + URI] = uri; |
aoqi@0 | 154 | _strings[i + LOCAL_NAME] = localName; |
aoqi@0 | 155 | _strings[i + QNAME] = qName; |
aoqi@0 | 156 | _strings[i + TYPE] = type; |
aoqi@0 | 157 | _strings[i + VALUE] = value; |
aoqi@0 | 158 | |
aoqi@0 | 159 | _attributeCount++; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Add an attribute using a prefix. |
aoqi@0 | 164 | * |
aoqi@0 | 165 | * @param prefix |
aoqi@0 | 166 | * This can be empty but not null, just like everywhere else in SAX. |
aoqi@0 | 167 | * @param uri |
aoqi@0 | 168 | * This can be empty but not null, just like everywhere else in SAX. |
aoqi@0 | 169 | */ |
aoqi@0 | 170 | public final void addAttributeWithPrefix(String prefix, String uri, String localName, String type, String value) { |
aoqi@0 | 171 | final int i = _attributeCount << 3; |
aoqi@0 | 172 | if (i == _strings.length) { |
aoqi@0 | 173 | resize(i); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | _strings[i + PREFIX] = prefix; |
aoqi@0 | 177 | _strings[i + URI] = uri; |
aoqi@0 | 178 | _strings[i + LOCAL_NAME] = localName; |
aoqi@0 | 179 | _strings[i + QNAME] = null; |
aoqi@0 | 180 | _strings[i + TYPE] = type; |
aoqi@0 | 181 | _strings[i + VALUE] = value; |
aoqi@0 | 182 | |
aoqi@0 | 183 | _attributeCount++; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | |
aoqi@0 | 186 | private void resize(int length) { |
aoqi@0 | 187 | final int newLength = length * 2; |
aoqi@0 | 188 | final String[] strings = new String[newLength]; |
aoqi@0 | 189 | System.arraycopy(_strings, 0, strings, 0, length); |
aoqi@0 | 190 | _strings = strings; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | } |