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