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:
033: import org.xml.sax.Attributes;
034: import javax.xml.namespace.QName;
035:
036: class QAttributes implements Attributes {
037: QName[] names = new QName[32];
038: String[] values = new String[32];
039: int size;
040:
041: void clear() {
042: size = 0;
043: }
044:
045: void add(QName name, String value) {
046: if (size == names.length) {
047: QName[] newNames = new QName[2 * names.length];
048: String[] newValues = new String[2 * names.length];
049: System.arraycopy(names, 0, newNames, 0, names.length);
050: System.arraycopy(values, 0, newValues, 0, names.length);
051: names = newNames;
052: values = newValues;
053: }
054:
055: names[size] = name;
056: values[size] = value;
057: size++;
058: }
059:
060: public int getLength() {
061: return size;
062: }
063:
064: public QName getName(int i) {
065: return names[i];
066: }
067:
068: public String getQName(int i) {
069: String prefix = names[i].getPrefix();
070:
071: if (prefix != null && prefix.length() > 0)
072: return prefix + ":" + names[i].getLocalPart();
073: else
074: return names[i].getLocalPart();
075: }
076:
077: public String getURI(int i) {
078: String uri = names[i].getNamespaceURI();
079:
080: if (uri != null)
081: return uri;
082: else
083: return "";
084: }
085:
086: public String getLocalName(int i) {
087: String name = names[i].getLocalPart();
088:
089: if (name != null)
090: return name;
091: else
092: return "";
093: }
094:
095: public String getValue(int i) {
096: return values[i];
097: }
098:
099: public String getValue(String qName) {
100: for (int i = 0; i < size; i++) {
101: if (qName.equals(names[i].getLocalPart()))
102: return values[i];
103: }
104:
105: return null;
106: }
107:
108: public String getValue(String uri, String localName) {
109: for (int i = 0; i < size; i++) {
110: String testURI = names[i].getNamespaceURI();
111:
112: if (testURI == null)
113: testURI = "";
114:
115: if (uri.equals(testURI)
116: && localName.equals(names[i].getLocalPart()))
117: return values[i];
118: }
119:
120: return null;
121: }
122:
123: public int getIndex(String qName) {
124: for (int i = 0; i < size; i++) {
125: if (qName.equals(names[i].getLocalPart()))
126: return i;
127: }
128:
129: return -1;
130: }
131:
132: public int getIndex(String uri, String localName) {
133: for (int i = 0; i < size; i++) {
134: if (uri.equals(names[i].getNamespaceURI())
135: && localName.equals(names[i].getLocalPart()))
136: return i;
137: }
138:
139: return -1;
140: }
141:
142: public String getType(int i) {
143: return "CDATA";
144: }
145:
146: public String getType(String uri, String localName) {
147: return "CDATA";
148: }
149:
150: public String getType(String qName) {
151: return "CDATA";
152: }
153:
154: public String toString() {
155: StringBuilder cb = new StringBuilder();
156: cb.append("QAttributes[");
157: for (int i = 0; i < size; i++) {
158: cb.append(" ");
159: cb.append(names[i]);
160: cb.append("=\"");
161: cb.append(values[i]);
162: cb.append("\"");
163: }
164: cb.append("]");
165:
166: return cb.toString();
167: }
168: }
|