001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.stream.buffer;
021:
022: import org.xml.sax.Attributes;
023:
024: /**
025: * Class for holding attributes.
026: *
027: * Since it implements {@link Attributes}, this class follows the SAX convention
028: * of using "" instead of null.
029: */
030: @SuppressWarnings({"PointlessArithmeticExpression"})
031: public final class AttributesHolder implements Attributes {
032: protected static final int DEFAULT_CAPACITY = 8;
033: protected static final int ITEM_SIZE = 1 << 3;
034:
035: protected static final int PREFIX = 0;
036: protected static final int URI = 1;
037: protected static final int LOCAL_NAME = 2;
038: protected static final int QNAME = 3;
039: protected static final int TYPE = 4;
040: protected static final int VALUE = 5;
041:
042: protected int _attributeCount;
043:
044: protected String[] _strings;
045:
046: public AttributesHolder() {
047: _strings = new String[DEFAULT_CAPACITY * ITEM_SIZE];
048: }
049:
050: public final int getLength() {
051: return _attributeCount;
052: }
053:
054: public final String getPrefix(int index) {
055: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
056: + PREFIX]
057: : null;
058: }
059:
060: public final String getLocalName(int index) {
061: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
062: + LOCAL_NAME]
063: : null;
064: }
065:
066: public final String getQName(int index) {
067: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
068: + QNAME]
069: : null;
070: }
071:
072: public final String getType(int index) {
073: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
074: + TYPE]
075: : null;
076: }
077:
078: public final String getURI(int index) {
079: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
080: + URI]
081: : null;
082: }
083:
084: public final String getValue(int index) {
085: return (index >= 0 && index < _attributeCount) ? _strings[(index << 3)
086: + VALUE]
087: : null;
088: }
089:
090: public final int getIndex(String qName) {
091: for (int i = 0; i < _attributeCount; i++) {
092: if (qName.equals(_strings[(i << 3) + QNAME])) {
093: return i;
094: }
095: }
096: return -1;
097: }
098:
099: public final String getType(String qName) {
100: final int i = (getIndex(qName) << 3) + TYPE;
101: return (i >= 0) ? _strings[i] : null;
102: }
103:
104: public final String getValue(String qName) {
105: final int i = (getIndex(qName) << 3) + VALUE;
106: return (i >= 0) ? _strings[i] : null;
107: }
108:
109: public final int getIndex(String uri, String localName) {
110: for (int i = 0; i < _attributeCount; i++) {
111: if (localName.equals(_strings[(i << 3) + LOCAL_NAME])
112: && uri.equals(_strings[(i << 3) + URI])) {
113: return i;
114: }
115: }
116: return -1;
117: }
118:
119: public final String getType(String uri, String localName) {
120: final int i = (getIndex(uri, localName) << 3) + TYPE;
121: return (i >= 0) ? _strings[i] : null;
122: }
123:
124: public final String getValue(String uri, String localName) {
125: final int i = (getIndex(uri, localName) << 3) + VALUE;
126: return (i >= 0) ? _strings[i] : null;
127: }
128:
129: public final void clear() {
130: if (_attributeCount > 0) {
131: for (int i = 0; i < _attributeCount; i++) {
132: _strings[(i << 3) + VALUE] = null;
133: }
134: _attributeCount = 0;
135: }
136: }
137:
138: /**
139: * Add an attribute using a qualified name that contains the
140: * prefix and local name.
141: *
142: * @param uri
143: * This can be empty but not null, just like everywhere else in SAX.
144: */
145: public final void addAttributeWithQName(String uri,
146: String localName, String qName, String type, String value) {
147: final int i = _attributeCount << 3;
148: if (i == _strings.length) {
149: resize(i);
150: }
151:
152: _strings[i + PREFIX] = null;
153: _strings[i + URI] = uri;
154: _strings[i + LOCAL_NAME] = localName;
155: _strings[i + QNAME] = qName;
156: _strings[i + TYPE] = type;
157: _strings[i + VALUE] = value;
158:
159: _attributeCount++;
160: }
161:
162: /**
163: * Add an attribute using a prefix.
164: *
165: * @param prefix
166: * This can be empty but not null, just like everywhere else in SAX.
167: * @param uri
168: * This can be empty but not null, just like everywhere else in SAX.
169: */
170: public final void addAttributeWithPrefix(String prefix, String uri,
171: String localName, String type, String value) {
172: final int i = _attributeCount << 3;
173: if (i == _strings.length) {
174: resize(i);
175: }
176:
177: _strings[i + PREFIX] = prefix;
178: _strings[i + URI] = uri;
179: _strings[i + LOCAL_NAME] = localName;
180: _strings[i + QNAME] = null;
181: _strings[i + TYPE] = type;
182: _strings[i + VALUE] = value;
183:
184: _attributeCount++;
185: }
186:
187: private void resize(int length) {
188: final int newLength = length * 2;
189: final String[] strings = new String[newLength];
190: System.arraycopy(_strings, 0, strings, 0, length);
191: _strings = strings;
192: }
193:
194: }
|