001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml2;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.xml.QName;
033:
034: import org.xml.sax.Attributes;
035:
036: /**
037: * Implements the SAX Attributes class.
038: */
039: class AttributesImpl implements Attributes {
040: private InternQName[] _names = new InternQName[32];
041: private String[] _values = new String[32];
042: private int _size;
043:
044: /**
045: * Clears the attributes.
046: */
047: void clear() {
048: _size = 0;
049: }
050:
051: /**
052: * Adds a new attribute name and value.
053: */
054: void add(InternQName name, String value) {
055: if (_size == _names.length) {
056: InternQName[] newNames = new InternQName[2 * _names.length];
057: String[] newValues = new String[2 * _names.length];
058: System.arraycopy(_names, 0, newNames, 0, _names.length);
059: System.arraycopy(_values, 0, newValues, 0, _names.length);
060: _names = newNames;
061: _values = newValues;
062: }
063:
064: _names[_size] = name;
065: _values[_size] = value;
066: _size++;
067: }
068:
069: /**
070: * Returns the number of attributes.
071: */
072: public int getLength() {
073: return _size;
074: }
075:
076: /**
077: * Returns the indexed name.
078: */
079: /*
080: public QName getName(int i)
081: {
082: return _names[i];
083: }
084: */
085:
086: /**
087: * Returns the name.
088: */
089: public String getQName(int i) {
090: return _names[i].getName();
091: }
092:
093: /**
094: * Returns the namespace URI.
095: */
096: public String getURI(int i) {
097: throw new UnsupportedOperationException();
098: //return _names[i].getNamespaceURI();
099: }
100:
101: /**
102: * Returns the local name.
103: */
104: public String getLocalName(int i) {
105: return _names[i].getLocalName();
106: }
107:
108: /**
109: * Returns the value.
110: */
111: public String getValue(int i) {
112: return _values[i];
113: }
114:
115: /**
116: * Returns the value with the given name.
117: */
118: public String getValue(String qName) {
119: for (int i = _size - 1; i >= 0; i--) {
120: if (qName.equals(_names[i].getName()))
121: return _values[i];
122: }
123:
124: return null;
125: }
126:
127: /**
128: * Returns the value for hte uri and local name.
129: */
130: public String getValue(String uri, String localName) {
131: for (int i = _size - 1; i >= 0; i--) {
132: if (uri.equals(_names[i].getNamespaceURI())
133: && localName.equals(_names[i].getLocalName()))
134: return _values[i];
135: }
136:
137: return null;
138: }
139:
140: /**
141: * Returns the index of the given name.
142: */
143: public int getIndex(String qName) {
144: for (int i = _size - 1; i >= 0; i--) {
145: if (qName.equals(_names[i].getName()))
146: return i;
147: }
148:
149: return -1;
150: }
151:
152: /**
153: * Returns the index of the matching name.
154: */
155: public int getIndex(String uri, String localName) {
156: for (int i = _size - 1; i >= 0; i--) {
157: if (uri.equals(_names[i].getNamespaceURI())
158: && localName.equals(_names[i].getLocalName()))
159: return i;
160: }
161:
162: return -1;
163: }
164:
165: /**
166: * Returns the set type.
167: */
168: public String getType(int i) {
169: return "CDATA";
170: }
171:
172: /**
173: * Returns the set type.
174: */
175: public String getType(String uri, String localName) {
176: return "CDATA";
177: }
178:
179: /**
180: * Returns the set type.
181: */
182: public String getType(String qName) {
183: return "CDATA";
184: }
185:
186: /**
187: * Returns a printable version.
188: */
189: public String toString() {
190: CharBuffer cb = new CharBuffer();
191: cb.append("AttributesImpl[");
192: for (int i = 0; i < _size; i++) {
193: cb.append(" ");
194: cb.append(_names[i]);
195: cb.append("=\"");
196: cb.append(_values[i]);
197: cb.append("\"");
198: }
199: cb.append("]");
200:
201: return cb.close();
202: }
203: }
|